ROT: Verbesserungen aus 'deserialization' in 'sumup-adder' gemerged
[demos/kafka/training] / src / test / java / de / juplo / kafka / AdderBusinessLogicTest.java
1 package de.juplo.kafka;
2
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;
9
10 import java.util.Arrays;
11 import java.util.stream.IntStream;
12 import java.util.stream.Stream;
13
14 import static org.assertj.core.api.Assertions.*;
15
16
17 public class AdderBusinessLogicTest
18 {
19   @Test
20   @DisplayName("A new sum can be started, if it does not exist")
21   public void testStartSumPossibleIfNotExists()
22   {
23     AdderBusinessLogic adder = new AdderBusinessLogic();
24     assertThatNoException().isThrownBy(() -> adder.startSum("foo"));
25   }
26
27   @Test
28   @DisplayName("Starting an already existing sum again, causes an IllegalStateException")
29   public void testStartSumCausesExceptionIfExists()
30   {
31     AdderBusinessLogic adder = new AdderBusinessLogic();
32     adder.startSum("foo");
33     assertThatIllegalStateException().isThrownBy(() -> adder.startSum("foo"));
34   }
35
36   @Test
37   @DisplayName("An empty Optional should be returned, for a non-existing sum")
38   public void testGetSumReturnsEmptyOptionalForNonExistingSum()
39   {
40     AdderBusinessLogic adder = new AdderBusinessLogic();
41     assertThat(adder.getSum("foo")).isEmpty();
42   }
43
44   @Test
45   @DisplayName("A non-empty Optional should be returned, for an existing sum")
46   public void testGetSumReturnsNonEmptyOptionalForExistingSum()
47   {
48     AdderBusinessLogic adder = new AdderBusinessLogic();
49     adder.startSum("foo");
50     assertThat(adder.getSum("foo")).isNotEmpty();
51   }
52
53   @Test
54   @DisplayName("A sum can be ended, if it does exist")
55   public void testEndSumPossibleIfSumExists()
56   {
57     AdderBusinessLogic adder = new AdderBusinessLogic();
58     adder.startSum("foo");
59     assertThatNoException().isThrownBy(() -> adder.endSum("foo"));
60   }
61
62   @Test
63   @DisplayName("An existing sum is removed, if ended")
64   public void testEndSumRemovesSumIfSumExists()
65   {
66     AdderBusinessLogic adder = new AdderBusinessLogic();
67     adder.startSum("foo");
68     adder.endSum("foo");
69     assertThat(adder.getSum("foo")).isEmpty();
70   }
71
72   @Test
73   @DisplayName("An existing Sum returns a non-null value, if ended")
74   public void testEndSumReturnsNonNullValueIfSumExists()
75   {
76     AdderBusinessLogic adder = new AdderBusinessLogic();
77     adder.startSum("foo");
78     assertThat(adder.endSum("foo")).isNotNull();
79   }
80
81   @Test
82   @DisplayName("An existing Sum returns a non-negative value, if ended")
83   public void testEndSumReturnsNonNegativeValueIfSumExists()
84   {
85     AdderBusinessLogic adder = new AdderBusinessLogic();
86     adder.startSum("foo");
87     assertThat(adder.endSum("foo")).isNotNegative();
88   }
89
90   @Test
91   @DisplayName("Ending a non-existing sum, causes an IllegalStateException")
92   public void testEndSumCausesExceptionIfNotExists()
93   {
94     AdderBusinessLogic adder = new AdderBusinessLogic();
95     assertThatIllegalStateException().isThrownBy(() -> adder.endSum("foo"));
96   }
97
98   @Test
99   @DisplayName("Adding to a non-existent sum causes an IllegalStateException")
100   public void testAddToSumCausesExceptionIfNotExists()
101   {
102     AdderBusinessLogic adder = new AdderBusinessLogic();
103     assertThatIllegalStateException().isThrownBy(() -> adder.addToSum("foo", 1));
104   }
105
106   @Test
107   @DisplayName("Adding a null-value to an existing sum causes an IllegalArgumentException")
108   public void testAddSumWithNullValueToExistingSumCausesException()
109   {
110     AdderBusinessLogic adder = new AdderBusinessLogic();
111     adder.startSum("foo");
112     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", null));
113   }
114
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)
119   {
120     AdderBusinessLogic adder = new AdderBusinessLogic();
121     adder.startSum("foo");
122     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value));
123   }
124
125   @Test
126   @DisplayName("Can add a positive value to an existing sum")
127   public void testAddSumWithPositiveValuePossibleIfSumExists()
128   {
129     AdderBusinessLogic adder = new AdderBusinessLogic();
130     adder.startSum("foo");
131     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", -1));
132   }
133
134   @ParameterizedTest(name = "{index}: Summing up {0}")
135   @DisplayName("Adds up numbers correctly")
136   @MethodSource("numbersProvider")
137   public void testAddSumAddsUpNumbersCorrectlyIfSumExists(int... numbers)
138   {
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);
144   }
145
146   static Stream<Arguments> numbersProvider() {
147     return Stream.of(
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()));
151   }
152 }