From 83687565029b7fa3b8707f172b5e53b58fc5bcba Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 13 Aug 2022 16:02:25 +0200 Subject: [PATCH] ROT: Erwartungen an SumBusinessLogic.addToSum(String, Integer) --- .../java/de/juplo/kafka/SumBusinessLogic.java | 4 ++ .../de/juplo/kafka/SumBusinessLogicTest.java | 63 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/src/main/java/de/juplo/kafka/SumBusinessLogic.java b/src/main/java/de/juplo/kafka/SumBusinessLogic.java index 38485d2..f7bf09f 100644 --- a/src/main/java/de/juplo/kafka/SumBusinessLogic.java +++ b/src/main/java/de/juplo/kafka/SumBusinessLogic.java @@ -34,6 +34,10 @@ public class SumBusinessLogic return Optional.ofNullable(state.get(user)); } + public synchronized void addToSum(String user, Integer value) + { + } + public synchronized Long endSum(String user) { if (!state.containsKey(user)) diff --git a/src/test/java/de/juplo/kafka/SumBusinessLogicTest.java b/src/test/java/de/juplo/kafka/SumBusinessLogicTest.java index e65ee83..a0e5e6e 100644 --- a/src/test/java/de/juplo/kafka/SumBusinessLogicTest.java +++ b/src/test/java/de/juplo/kafka/SumBusinessLogicTest.java @@ -2,6 +2,14 @@ package de.juplo.kafka; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; + +import java.util.Arrays; +import java.util.stream.IntStream; +import java.util.stream.Stream; import static org.assertj.core.api.Assertions.*; @@ -76,4 +84,59 @@ public class SumBusinessLogicTest SumBusinessLogic sumBusinessLogic = new SumBusinessLogic(); assertThatIllegalStateException().isThrownBy(() -> sumBusinessLogic.endSum("foo")); } + + @Test + @DisplayName("Adding to a non-existent sum causes an IllegalStateException") + public void testAddToSumCausesExceptionIfNotExists() + { + SumBusinessLogic sumBusinessLogic = new SumBusinessLogic(); + assertThatIllegalStateException().isThrownBy(() -> sumBusinessLogic.addToSum("foo", 1)); + } + + @Test + @DisplayName("Adding a null-value to an existing sum causes an IllegalArgumentException") + public void testAddNullToExistingSumCausesException() + { + SumBusinessLogic sumBusinessLogic = new SumBusinessLogic(); + sumBusinessLogic.startSum("foo"); + assertThatIllegalArgumentException().isThrownBy(() -> sumBusinessLogic.addToSum("foo", null)); + } + + @ParameterizedTest(name = "{index}: Adding {0}") + @DisplayName("Adding a non-positive value to an existing sum causes an IllegalArgumentException") + @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE }) + public void testAddingNonPositiveValueToExistingSumCausesException(int value) + { + SumBusinessLogic sumBusinessLogic = new SumBusinessLogic(); + sumBusinessLogic.startSum("foo"); + assertThatIllegalArgumentException().isThrownBy(() -> sumBusinessLogic.addToSum("foo", value)); + } + + @Test + @DisplayName("Can add a positive value to an existing sum") + public void testCanAddPositiveValueToExistingSum() + { + SumBusinessLogic sumBusinessLogic = new SumBusinessLogic(); + sumBusinessLogic.startSum("foo"); + assertThatIllegalArgumentException().isThrownBy(() -> sumBusinessLogic.addToSum("foo", -1)); + } + + @ParameterizedTest(name = "{index}: Summing up {0}") + @DisplayName("Adds up numbers correctly") + @MethodSource("numbersProvider") + public void testCanAddPositiveValueToExistingSum(int... numbers) + { + long expectedResult = Arrays.stream(numbers).sum(); + SumBusinessLogic sumBusinessLogic = new SumBusinessLogic(); + sumBusinessLogic.startSum("foo"); + Arrays.stream(numbers).forEach(number -> sumBusinessLogic.addToSum("foo", number)); + assertThat(sumBusinessLogic.endSum("foo")).isEqualTo(expectedResult); + } + + static Stream numbersProvider() { + return Stream.of( + Arguments.of((Object) IntStream.rangeClosed(1,9).toArray()), + Arguments.of((Object) IntStream.rangeClosed(1,19).toArray()), + Arguments.of((Object) IntStream.rangeClosed(1,66).toArray())); + } } -- 2.20.1