X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Fkafka%2FAdderBusinessLogicTest.java;h=8e4926315e95d05eefd2dcd90bf1ab783db98e52;hb=595eab489c638b07072f6ec7e3c6f52000295931;hp=4c0ed4bd14f35bc2cb5cf4ff7b874eee36958c6e;hpb=7acccf19ab0f7b11aa5fcd70867ae3ea80de2dcb;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 4c0ed4b..8e49263 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.*; @@ -9,36 +17,101 @@ 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 testCanStartSumIfNotExists() + @DisplayName("An empty Optional should be returned, for a non-existing sum") + public void testGetSumReturnsEmptyOptionalForNonExistingSum() { AdderBusinessLogic adder = new AdderBusinessLogic(); - assertThatNoException().isThrownBy(() -> adder.startSum("foo")); + assertThat(adder.getSum("foo")).isEmpty(); } @Test - @DisplayName("Starting an already existing sum again, causes an IllegalStateException") - public void testStartSumCausesExceptionIfExists() + @DisplayName("A non-empty Optional should be returned, for an existing sum") + public void testGetSumReturnsNonEmptyOptionalForExistingSum() { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); - assertThatIllegalStateException().isThrownBy(() -> adder.startSum("foo")); + adder.addToSum("foo", 6); + assertThat(adder.getSum("foo")).isNotEmpty(); } @Test - @DisplayName("An empty Optional should be returned, for a non-existing sum") - public void testEmptyOptionalForNonExistingSum() + @DisplayName("A sum can be calculated, if it does exist") + public void testCalculatePossibleIfSumExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); + adder.addToSum("foo", 6); + assertThatNoException().isThrownBy(() -> adder.calculate("foo")); + } + + @Test + @DisplayName("An existing sum is removed, if ended") + public void testCalculateRemovesSumIfSumExists() + { + AdderBusinessLogic adder = new AdderBusinessLogic(); + adder.addToSum("foo", 6); + adder.calculate("foo"); assertThat(adder.getSum("foo")).isEmpty(); } @Test - @DisplayName("A non-empty Optional should be returned, for an existing sum") - public void testNonEmptyOptionalForExistingSum() + @DisplayName("An existing sum returns a non-null value, if calculated") + public void testCalculateReturnsNonNullValueIfSumExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); - assertThat(adder.getSum("foo")).isNotEmpty(); + adder.addToSum("foo", 6); + assertThat(adder.calculate("foo")).isNotNull(); + } + + @Test + @DisplayName("Ending a non-existing sum, causes an IllegalStateException") + public void testCalculateCausesExceptionIfNotExists() + { + AdderBusinessLogic adder = new AdderBusinessLogic(); + assertThatIllegalStateException().isThrownBy(() -> adder.calculate("foo")); + } + + @Test + @DisplayName("Adding a null-value to a sum causes an IllegalArgumentException") + public void testAddToSumWithNullValueCausesException() + { + AdderBusinessLogic adder = new AdderBusinessLogic(); + assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", null)); + } + + @ParameterizedTest(name = "{index}: Adding {0}") + @DisplayName("Adding a non-positive value to a sum causes an IllegalArgumentException") + @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE }) + public void testAddToSumWithNonPositiveValueCausesException(int value) + { + AdderBusinessLogic adder = new AdderBusinessLogic(); + assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value)); + } + + @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(); + assertThatNoException().isThrownBy(() -> adder.addToSum("foo", value)); + } + + @ParameterizedTest(name = "{index}: Summing up {0}") + @DisplayName("Adds up numbers correctly") + @MethodSource("numbersProvider") + public void testAddToSumAddsUpNumbersCorrectlyIfSumExists(int... numbers) + { + long expectedResult = Arrays.stream(numbers).sum(); + AdderBusinessLogic adder = new AdderBusinessLogic(); + Arrays.stream(numbers).forEach(number -> adder.addToSum("foo", number)); + AdderResult result = adder.calculate("foo"); + assertThat(result.number).isEqualTo(numbers[numbers.length-1]); + assertThat(result.sum).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())); } }