X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2FAdderBusinessLogicTest.java;h=63447e36173021176f906cdb1b7255231930638b;hb=0cd07d4498de934aefece33e20eee0df684e62e5;hp=8ae172876879bbeb3ace54abc01b9dec39848143;hpb=f4848341ef2fee62e3c62c63deed277a4f1a2d8a;p=demos%2Fkafka%2Ftraining diff --git a/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java b/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java index 8ae1728..63447e3 100644 --- a/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java +++ b/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.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.*; @@ -10,7 +18,7 @@ public class AdderBusinessLogicTest { @Test @DisplayName("A new sum can be started, if it does not exist") - public void testCanStartSumIfNotExists() + public void testStartSumPossibleIfNotExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); assertThatNoException().isThrownBy(() -> adder.startSum("foo")); @@ -27,7 +35,7 @@ public class AdderBusinessLogicTest @Test @DisplayName("An empty Optional should be returned, for a non-existing sum") - public void testEmptyOptionalForNonExistingSum() + public void testGetSumReturnsEmptyOptionalForNonExistingSum() { AdderBusinessLogic adder = new AdderBusinessLogic(); assertThat(adder.getSum("foo")).isEmpty(); @@ -35,7 +43,7 @@ public class AdderBusinessLogicTest @Test @DisplayName("A non-empty Optional should be returned, for an existing sum") - public void testNonEmptyOptionalForExistingSum() + public void testGetSumReturnsNonEmptyOptionalForExistingSum() { AdderBusinessLogic adder = new AdderBusinessLogic(); adder.startSum("foo"); @@ -44,13 +52,23 @@ public class AdderBusinessLogicTest @Test @DisplayName("A sum can be ended, if it does exist") - public void testCanEndIfSumExists() + public void testEndSumPossibleIfSumExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); adder.startSum("foo"); assertThatNoException().isThrownBy(() -> adder.endSum("foo")); } + @Test + @DisplayName("An existing sum is removed, if ended") + public void testEndSumRemovesSumIfSumExists() + { + AdderBusinessLogic adder = new AdderBusinessLogic(); + adder.startSum("foo"); + adder.endSum("foo"); + assertThat(adder.getSum("foo")).isEmpty(); + } + @Test @DisplayName("An existing Sum returns a non-null value, if ended") public void testEndSumReturnsNonNullValueIfSumExists() @@ -76,4 +94,60 @@ public class AdderBusinessLogicTest AdderBusinessLogic adder = new AdderBusinessLogic(); assertThatIllegalStateException().isThrownBy(() -> adder.endSum("foo")); } + + @Test + @DisplayName("Adding to a non-existent sum causes an IllegalStateException") + public void testAddToSumCausesExceptionIfNotExists() + { + 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") + @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE }) + public void testAddSumWithNonPositiveValueToExistingSumCausesException(int value) + { + AdderBusinessLogic adder = new AdderBusinessLogic(); + adder.startSum("foo"); + assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value)); + } + + @ParameterizedTest(name = "{index}: Adding {0}") + @DisplayName("Can add a positive value to an existing sum") + @ValueSource(ints = { 1, 3, 6, 66, 7, 9 }) + public void testAddSumWithPositiveValuePossibleIfSumExists(int value) + { + AdderBusinessLogic adder = new AdderBusinessLogic(); + adder.startSum("foo"); + assertThatNoException().isThrownBy(() -> adder.addToSum("foo", value)); + } + + @ParameterizedTest(name = "{index}: Summing up {0}") + @DisplayName("Adds up numbers correctly") + @MethodSource("numbersProvider") + public void testAddSumAddsUpNumbersCorrectlyIfSumExists(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); + } + + 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())); + } }