Skip to content

Commit 4de73a7

Browse files
committed
add tests
1 parent d441b3c commit 4de73a7

1 file changed

Lines changed: 266 additions & 0 deletions

File tree

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,266 @@
1+
package it.aboutbits.springboot.toolbox.util;
2+
3+
import org.junit.jupiter.api.DisplayName;
4+
import org.junit.jupiter.api.Nested;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.data.util.Streamable;
7+
8+
import java.util.Arrays;
9+
import java.util.Collections;
10+
import java.util.List;
11+
import java.util.stream.Stream;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
class FilterUtilTest {
17+
@Nested
18+
class FilterToSet {
19+
@Test
20+
@DisplayName("Should filter Collection to Set using predicate")
21+
void shouldFilterCollectionToSetUsingPredicate() {
22+
// given
23+
var numbers = Arrays.asList(1, 2, 3, 4, 5);
24+
25+
// when
26+
var result = FilterUtil.filterToSet(numbers, n -> n > 2);
27+
28+
// then
29+
assertThat(result).containsExactlyInAnyOrder(3, 4, 5);
30+
}
31+
32+
@Test
33+
@DisplayName("Should handle empty Collection when filtering to Set")
34+
void shouldHandleEmptyCollectionWhenFilteringToSet() {
35+
// given
36+
var emptyList = Collections.<Integer>emptyList();
37+
38+
// when
39+
var result = FilterUtil.filterToSet(emptyList, n -> n > 2);
40+
41+
// then
42+
assertTrue(result.isEmpty());
43+
}
44+
45+
@Test
46+
@DisplayName("Should remove duplicates when filtering Collection to Set")
47+
void shouldRemoveDuplicatesWhenFilteringCollectionToSet() {
48+
// given
49+
var numbersWithDuplicates = Arrays.asList(1, 2, 2, 3, 3, 3);
50+
51+
// when
52+
var result = FilterUtil.filterToSet(numbersWithDuplicates, n -> n >= 2);
53+
54+
// then
55+
assertThat(result).containsExactlyInAnyOrder(2, 3);
56+
assertThat(result).hasSize(2);
57+
}
58+
59+
@Test
60+
@DisplayName("Should filter Streamable to Set using predicate")
61+
void shouldFilterStreamableToSetUsingPredicate() {
62+
// given
63+
var numbers = Streamable.of(1, 2, 3, 4, 5);
64+
65+
// when
66+
var result = FilterUtil.filterToSet(numbers, n -> n % 2 == 0);
67+
68+
// then
69+
assertThat(result).containsExactlyInAnyOrder(2, 4);
70+
}
71+
72+
@Test
73+
@DisplayName("Should handle empty Streamable when filtering to Set")
74+
void shouldHandleEmptyStreamableWhenFilteringToSet() {
75+
// given
76+
var emptyStreamable = Streamable.<Integer>empty();
77+
78+
// when
79+
var result = FilterUtil.filterToSet(emptyStreamable, n -> n > 2);
80+
81+
// then
82+
assertTrue(result.isEmpty());
83+
}
84+
85+
@Test
86+
@DisplayName("Should remove duplicates when filtering Stream to Set")
87+
void shouldRemoveDuplicatesWhenFilteringStreamToSet() {
88+
// given
89+
var numbersWithDuplicates = Stream.of(1, 2, 2, 3, 3, 3);
90+
91+
// when
92+
var result = FilterUtil.filterToSet(numbersWithDuplicates, n -> n >= 2);
93+
94+
// then
95+
assertThat(result).containsExactlyInAnyOrder(2, 3);
96+
assertThat(result).hasSize(2);
97+
}
98+
}
99+
100+
@Nested
101+
class FilterToList {
102+
@Test
103+
@DisplayName("Should filter Collection to List using predicate")
104+
void shouldFilterCollectionToListUsingPredicate() {
105+
// given
106+
var numbers = Arrays.asList(1, 2, 3, 4, 5);
107+
108+
// when
109+
var result = FilterUtil.filterToList(numbers, n -> n % 2 == 1);
110+
111+
// then
112+
assertThat(result).containsExactly(1, 3, 5);
113+
}
114+
115+
@Test
116+
@DisplayName("Should handle empty Collection when filtering to List")
117+
void shouldHandleEmptyCollectionWhenFilteringToList() {
118+
// given
119+
var emptyList = Collections.<Integer>emptyList();
120+
121+
// when
122+
var result = FilterUtil.filterToList(emptyList, n -> n > 2);
123+
124+
// then
125+
assertTrue(result.isEmpty());
126+
}
127+
128+
@Test
129+
@DisplayName("Should preserve duplicates when filtering Collection to List")
130+
void shouldPreserveDuplicatesWhenFilteringCollectionToList() {
131+
// given
132+
var numbersWithDuplicates = Arrays.asList(1, 2, 2, 3, 3, 3);
133+
134+
// when
135+
var result = FilterUtil.filterToList(numbersWithDuplicates, n -> n >= 2);
136+
137+
// then
138+
assertThat(result).containsExactly(2, 2, 3, 3, 3);
139+
assertThat(result).hasSize(5);
140+
}
141+
142+
@Test
143+
@DisplayName("Should filter Streamable to List using predicate")
144+
void shouldFilterStreamableToListUsingPredicate() {
145+
// given
146+
var numbers = Streamable.of(1, 2, 3, 4, 5);
147+
148+
// when
149+
var result = FilterUtil.filterToList(numbers, n -> n < 4);
150+
151+
// then
152+
assertThat(result).containsExactly(1, 2, 3);
153+
}
154+
155+
@Test
156+
@DisplayName("Should handle empty Streamable when filtering to List")
157+
void shouldHandleEmptyStreamableWhenFilteringToList() {
158+
// given
159+
var emptyStreamable = Streamable.<Integer>empty();
160+
161+
// when
162+
var result = FilterUtil.filterToList(emptyStreamable, n -> n > 2);
163+
164+
// then
165+
assertTrue(result.isEmpty());
166+
}
167+
168+
@Test
169+
@DisplayName("Should preserve duplicates when filtering Stream to List")
170+
void shouldPreserveDuplicatesWhenFilteringStreamToList() {
171+
// given
172+
var numbersWithDuplicates = Stream.of(1, 2, 2, 3, 3, 3);
173+
174+
// when
175+
var result = FilterUtil.filterToList(numbersWithDuplicates, n -> n >= 2);
176+
177+
// then
178+
assertThat(result).containsExactly(2, 2, 3, 3, 3);
179+
assertThat(result).hasSize(5);
180+
}
181+
}
182+
183+
@Nested
184+
class FilterToStream {
185+
@Test
186+
@DisplayName("Should filter Collection to Stream using predicate")
187+
void shouldFilterCollectionToStreamUsingPredicate() {
188+
// given
189+
var numbers = Arrays.asList(1, 2, 3, 4, 5);
190+
191+
// when
192+
var resultStream = FilterUtil.filterToStream(numbers, n -> n >= 4);
193+
194+
// then
195+
var result = resultStream.toList();
196+
assertThat(result).containsExactly(4, 5);
197+
}
198+
199+
@Test
200+
@DisplayName("Should handle empty Collection when filtering to Stream")
201+
void shouldHandleEmptyCollectionWhenFilteringToStream() {
202+
// given
203+
var emptyList = Collections.<Integer>emptyList();
204+
205+
// when
206+
var resultStream = FilterUtil.filterToStream(emptyList, n -> n > 2);
207+
208+
// then
209+
assertTrue(resultStream.toList().isEmpty());
210+
}
211+
212+
@Test
213+
@DisplayName("Should filter Streamable to Stream using predicate")
214+
void shouldFilterStreamableToStreamUsingPredicate() {
215+
// given
216+
var numbers = Streamable.of(1, 2, 3, 4, 5);
217+
218+
// when
219+
var resultStream = FilterUtil.filterToStream(numbers, n -> n <= 2);
220+
221+
// then
222+
var result = resultStream.toList();
223+
assertThat(result).containsExactly(1, 2);
224+
}
225+
226+
@Test
227+
@DisplayName("Should handle empty Streamable when filtering to Stream")
228+
void shouldHandleEmptyStreamableWhenFilteringToStream() {
229+
// given
230+
var emptyStreamable = Streamable.<Integer>empty();
231+
232+
// when
233+
var resultStream = FilterUtil.filterToStream(emptyStreamable, n -> n > 2);
234+
235+
// then
236+
assertTrue(resultStream.toList().isEmpty());
237+
}
238+
239+
@Test
240+
@DisplayName("Should filter Stream to Stream using predicate")
241+
void shouldFilterStreamToStreamUsingPredicate() {
242+
// given
243+
var numbers = Stream.of(1, 2, 3, 4, 5);
244+
245+
// when
246+
var resultStream = FilterUtil.filterToStream(numbers, n -> n % 2 == 0);
247+
248+
// then
249+
var result = resultStream.toList();
250+
assertThat(result).containsExactly(2, 4);
251+
}
252+
253+
@Test
254+
@DisplayName("Should handle empty Stream when filtering to Stream")
255+
void shouldHandleEmptyStreamWhenFilteringToStream() {
256+
// given
257+
var emptyStream = Stream.<Integer>empty();
258+
259+
// when
260+
var resultStream = FilterUtil.filterToStream(emptyStream, n -> n > 2);
261+
262+
// then
263+
assertTrue(resultStream.toList().isEmpty());
264+
}
265+
}
266+
}

0 commit comments

Comments
 (0)