1 package de.juplo.kafka;
3 import org.junit.jupiter.api.DisplayName;
4 import org.junit.jupiter.api.Test;
5 import org.junit.jupiter.params.ParameterizedTest;
6 import org.junit.jupiter.params.provider.Arguments;
7 import org.junit.jupiter.params.provider.MethodSource;
8 import org.junit.jupiter.params.provider.ValueSource;
10 import java.util.Arrays;
11 import java.util.stream.IntStream;
12 import java.util.stream.Stream;
14 import static org.assertj.core.api.Assertions.*;
17 public class AdderBusinessLogicTest
20 @DisplayName("A new sum can be started, if it does not exist")
21 public void testStartSumPossibleIfNotExists()
23 AdderBusinessLogic adder = new AdderBusinessLogic();
24 assertThatNoException().isThrownBy(() -> adder.startSum("foo"));
28 @DisplayName("Starting an already existing sum again, causes an IllegalStateException")
29 public void testStartSumCausesExceptionIfExists()
31 AdderBusinessLogic adder = new AdderBusinessLogic();
32 adder.startSum("foo");
33 assertThatIllegalStateException().isThrownBy(() -> adder.startSum("foo"));
37 @DisplayName("An empty Optional should be returned, for a non-existing sum")
38 public void testGetSumReturnsEmptyOptionalForNonExistingSum()
40 AdderBusinessLogic adder = new AdderBusinessLogic();
41 assertThat(adder.getSum("foo")).isEmpty();
45 @DisplayName("A non-empty Optional should be returned, for an existing sum")
46 public void testGetSumReturnsNonEmptyOptionalForExistingSum()
48 AdderBusinessLogic adder = new AdderBusinessLogic();
49 adder.startSum("foo");
50 assertThat(adder.getSum("foo")).isNotEmpty();
54 @DisplayName("A sum can be ended, if it does exist")
55 public void testEndSumPossibleIfSumExists()
57 AdderBusinessLogic adder = new AdderBusinessLogic();
58 adder.startSum("foo");
59 assertThatNoException().isThrownBy(() -> adder.endSum("foo"));
63 @DisplayName("An existing sum is removed, if ended")
64 public void testEndSumRemovesSumIfSumExists()
66 AdderBusinessLogic adder = new AdderBusinessLogic();
67 adder.startSum("foo");
69 assertThat(adder.getSum("foo")).isEmpty();
73 @DisplayName("An existing Sum returns a non-null value, if ended")
74 public void testEndSumReturnsNonNullValueIfSumExists()
76 AdderBusinessLogic adder = new AdderBusinessLogic();
77 adder.startSum("foo");
78 assertThat(adder.endSum("foo")).isNotNull();
82 @DisplayName("An existing Sum returns a non-negative value, if ended")
83 public void testEndSumReturnsNonNegativeValueIfSumExists()
85 AdderBusinessLogic adder = new AdderBusinessLogic();
86 adder.startSum("foo");
87 assertThat(adder.endSum("foo")).isNotNegative();
91 @DisplayName("Ending a non-existing sum, causes an IllegalStateException")
92 public void testEndSumCausesExceptionIfNotExists()
94 AdderBusinessLogic adder = new AdderBusinessLogic();
95 assertThatIllegalStateException().isThrownBy(() -> adder.endSum("foo"));
99 @DisplayName("Adding to a non-existent sum causes an IllegalStateException")
100 public void testAddToSumCausesExceptionIfNotExists()
102 AdderBusinessLogic adder = new AdderBusinessLogic();
103 assertThatIllegalStateException().isThrownBy(() -> adder.addToSum("foo", 1));
107 @DisplayName("Adding a null-value to an existing sum causes an IllegalArgumentException")
108 public void testAddSumWithNullValueToExistingSumCausesException()
110 AdderBusinessLogic adder = new AdderBusinessLogic();
111 adder.startSum("foo");
112 assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", null));
115 @ParameterizedTest(name = "{index}: Adding {0}")
116 @DisplayName("Adding a non-positive value to an existing sum causes an IllegalArgumentException")
117 @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE })
118 public void testAddSumWithNonPositiveValueToExistingSumCausesException(int value)
120 AdderBusinessLogic adder = new AdderBusinessLogic();
121 adder.startSum("foo");
122 assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value));
126 @DisplayName("Can add a positive value to an existing sum")
127 public void testAddSumWithPositiveValuePossibleIfSumExists()
129 AdderBusinessLogic adder = new AdderBusinessLogic();
130 adder.startSum("foo");
131 assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", -1));
134 @ParameterizedTest(name = "{index}: Summing up {0}")
135 @DisplayName("Adds up numbers correctly")
136 @MethodSource("numbersProvider")
137 public void testAddSumAddsUpNumbersCorrectlyIfSumExists(int... numbers)
139 long expectedResult = Arrays.stream(numbers).sum();
140 AdderBusinessLogic adder = new AdderBusinessLogic();
141 adder.startSum("foo");
142 Arrays.stream(numbers).forEach(number -> adder.addToSum("foo", number));
143 assertThat(adder.endSum("foo")).isEqualTo(expectedResult);
146 static Stream<Arguments> numbersProvider() {
148 Arguments.of((Object) IntStream.rangeClosed(1,9).toArray()),
149 Arguments.of((Object) IntStream.rangeClosed(1,19).toArray()),
150 Arguments.of((Object) IntStream.rangeClosed(1,66).toArray()));