ROT: Erwartungen an SumBusinessLogic.addToSum(String, Integer)
[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 testCanStartSumIfNotExists()
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 testEmptyOptionalForNonExistingSum()
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 testNonEmptyOptionalForExistingSum()
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 testCanEndIfSumExists()
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 returns a non-null value, if ended")
64   public void testEndSumReturnsNonNullValueIfSumExists()
65   {
66     AdderBusinessLogic adder = new AdderBusinessLogic();
67     adder.startSum("foo");
68     assertThat(adder.endSum("foo")).isNotNull();
69   }
70
71   @Test
72   @DisplayName("An existing Sum returns a non-negative value, if ended")
73   public void testEndSumReturnsNonNegativeValueIfSumExists()
74   {
75     AdderBusinessLogic adder = new AdderBusinessLogic();
76     adder.startSum("foo");
77     assertThat(adder.endSum("foo")).isNotNegative();
78   }
79
80   @Test
81   @DisplayName("Ending a non-existing sum, causes an IllegalStateException")
82   public void testEndSumCausesExceptionIfNotExists()
83   {
84     AdderBusinessLogic adder = new AdderBusinessLogic();
85     assertThatIllegalStateException().isThrownBy(() -> adder.endSum("foo"));
86   }
87
88   @Test
89   @DisplayName("Adding to a non-existent sum causes an IllegalStateException")
90   public void testAddToSumCausesExceptionIfNotExists()
91   {
92     AdderBusinessLogic adder = new AdderBusinessLogic();
93     assertThatIllegalStateException().isThrownBy(() -> adder.addToSum("foo", 1));
94   }
95
96   @Test
97   @DisplayName("Adding a null-value to an existing sum causes an IllegalArgumentException")
98   public void testAddNullToExistingSumCausesException()
99   {
100     AdderBusinessLogic adder = new AdderBusinessLogic();
101     adder.startSum("foo");
102     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", null));
103   }
104
105   @ParameterizedTest(name = "{index}: Adding {0}")
106   @DisplayName("Adding a non-positive value to an existing sum causes an IllegalArgumentException")
107   @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE })
108   public void testAddingNonPositiveValueToExistingSumCausesException(int value)
109   {
110     AdderBusinessLogic adder = new AdderBusinessLogic();
111     adder.startSum("foo");
112     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value));
113   }
114
115   @Test
116   @DisplayName("Can add a positive value to an existing sum")
117   public void testCanAddPositiveValueToExistingSum()
118   {
119     AdderBusinessLogic adder = new AdderBusinessLogic();
120     adder.startSum("foo");
121     assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", -1));
122   }
123
124   @ParameterizedTest(name = "{index}: Summing up {0}")
125   @DisplayName("Adds up numbers correctly")
126   @MethodSource("numbersProvider")
127   public void testCanAddPositiveValueToExistingSum(int... numbers)
128   {
129     long expectedResult = Arrays.stream(numbers).sum();
130     AdderBusinessLogic adder = new AdderBusinessLogic();
131     adder.startSum("foo");
132     Arrays.stream(numbers).forEach(number -> adder.addToSum("foo", number));
133     assertThat(adder.endSum("foo")).isEqualTo(expectedResult);
134   }
135
136   static Stream<Arguments> numbersProvider() {
137     return Stream.of(
138         Arguments.of((Object) IntStream.rangeClosed(1,9).toArray()),
139         Arguments.of((Object) IntStream.rangeClosed(1,19).toArray()),
140         Arguments.of((Object) IntStream.rangeClosed(1,66).toArray()));
141   }
142 }