Skip to content

Commit b7d0c5e

Browse files
committed
add tests
1 parent d441b3c commit b7d0c5e

1 file changed

Lines changed: 265 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)