Merge branch 'deserialization' into sumup-adder--ohne--stored-offsets
[demos/kafka/training] / src / test / java / de / juplo / kafka / AdderBusinessLogicTest.java
index 435f036..8e49263 100644 (file)
@@ -16,23 +16,6 @@ import static org.assertj.core.api.Assertions.*;
 
 public class AdderBusinessLogicTest
 {
-  @Test
-  @DisplayName("A new sum can be started, if it does not exist")
-  public void testStartSumPossibleIfNotExists()
-  {
-    AdderBusinessLogic adder = new AdderBusinessLogic();
-    assertThatNoException().isThrownBy(() -> adder.startSum("foo"));
-  }
-
-  @Test
-  @DisplayName("Starting an already existing sum again, causes an IllegalStateException")
-  public void testStartSumCausesExceptionIfExists()
-  {
-    AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
-    assertThatIllegalStateException().isThrownBy(() -> adder.startSum("foo"));
-  }
-
   @Test
   @DisplayName("An empty Optional should be returned, for a non-existing sum")
   public void testGetSumReturnsEmptyOptionalForNonExistingSum()
@@ -46,101 +29,83 @@ public class AdderBusinessLogicTest
   public void testGetSumReturnsNonEmptyOptionalForExistingSum()
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
+    adder.addToSum("foo", 6);
     assertThat(adder.getSum("foo")).isNotEmpty();
   }
 
   @Test
-  @DisplayName("A sum can be ended, if it does exist")
-  public void testEndSumPossibleIfSumExists()
+  @DisplayName("A sum can be calculated, if it does exist")
+  public void testCalculatePossibleIfSumExists()
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
-    assertThatNoException().isThrownBy(() -> adder.endSum("foo"));
+    adder.addToSum("foo", 6);
+    assertThatNoException().isThrownBy(() -> adder.calculate("foo"));
   }
 
   @Test
   @DisplayName("An existing sum is removed, if ended")
-  public void testEndSumRemovesSumIfSumExists()
+  public void testCalculateRemovesSumIfSumExists()
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
-    adder.endSum("foo");
+    adder.addToSum("foo", 6);
+    adder.calculate("foo");
     assertThat(adder.getSum("foo")).isEmpty();
   }
 
   @Test
-  @DisplayName("An existing Sum returns a non-null value, if ended")
-  public void testEndSumReturnsNonNullValueIfSumExists()
-  {
-    AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
-    assertThat(adder.endSum("foo")).isNotNull();
-  }
-
-  @Test
-  @DisplayName("An existing Sum returns a non-negative value, if ended")
-  public void testEndSumReturnsNonNegativeValueIfSumExists()
+  @DisplayName("An existing sum returns a non-null value, if calculated")
+  public void testCalculateReturnsNonNullValueIfSumExists()
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
-    assertThat(adder.endSum("foo")).isNotNegative();
+    adder.addToSum("foo", 6);
+    assertThat(adder.calculate("foo")).isNotNull();
   }
 
   @Test
   @DisplayName("Ending a non-existing sum, causes an IllegalStateException")
-  public void testEndSumCausesExceptionIfNotExists()
+  public void testCalculateCausesExceptionIfNotExists()
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    assertThatIllegalStateException().isThrownBy(() -> adder.endSum("foo"));
+    assertThatIllegalStateException().isThrownBy(() -> adder.calculate("foo"));
   }
 
   @Test
-  @DisplayName("Adding to a non-existent sum causes an IllegalStateException")
-  public void testAddToSumCausesExceptionIfNotExists()
+  @DisplayName("Adding a null-value to a sum causes an IllegalArgumentException")
+  public void testAddToSumWithNullValueCausesException()
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    assertThatIllegalStateException().isThrownBy(() -> adder.addToSum("foo", 1));
-  }
-
-  @Test
-  @DisplayName("Adding a null-value to an existing sum causes an IllegalArgumentException")
-  public void testAddSumWithNullValueToExistingSumCausesException()
-  {
-    AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", null));
   }
 
   @ParameterizedTest(name = "{index}: Adding {0}")
-  @DisplayName("Adding a non-positive value to an existing sum causes an IllegalArgumentException")
+  @DisplayName("Adding a non-positive value to a sum causes an IllegalArgumentException")
   @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE })
-  public void testAddSumWithNonPositiveValueToExistingSumCausesException(int value)
+  public void testAddToSumWithNonPositiveValueCausesException(int value)
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value));
   }
 
-  @Test
-  @DisplayName("Can add a positive value to an existing sum")
-  public void testAddSumWithPositiveValuePossibleIfSumExists()
+  @ParameterizedTest(name = "{index}: Adding {0}")
+  @DisplayName("Can add a positive value to a sum")
+  @ValueSource(ints = { 1, 3, 6, 66, 7, 9 })
+  public void testAddToSumWithPositiveValuePossible(int value)
   {
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
-    assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", -1));
+    assertThatNoException().isThrownBy(() -> adder.addToSum("foo", value));
   }
 
   @ParameterizedTest(name = "{index}: Summing up {0}")
   @DisplayName("Adds up numbers correctly")
   @MethodSource("numbersProvider")
-  public void testAddSumAddsUpNumbersCorrectlyIfSumExists(int... numbers)
+  public void testAddToSumAddsUpNumbersCorrectlyIfSumExists(int... numbers)
   {
     long expectedResult = Arrays.stream(numbers).sum();
     AdderBusinessLogic adder = new AdderBusinessLogic();
-    adder.startSum("foo");
     Arrays.stream(numbers).forEach(number -> adder.addToSum("foo", number));
-    assertThat(adder.endSum("foo")).isEqualTo(expectedResult);
+    AdderResult result = adder.calculate("foo");
+    assertThat(result.number).isEqualTo(numbers[numbers.length-1]);
+    assertThat(result.sum).isEqualTo(expectedResult);
   }
 
   static Stream<Arguments> numbersProvider() {