Skip to content

Commit 01f24d2

Browse files
FINERACT-2534: Add unit tests for StreamUtil utility class
Add unit tests for: - foldLeft collector functionality - mergeMapsOfLists collector functionality Include edge case tests for empty streams and duplicate map keys. Improves reliability and test coverage for StreamUtil.
1 parent 83021ce commit 01f24d2

1 file changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.fineract.util;
20+
21+
import java.util.List;
22+
import java.util.Map;
23+
import java.util.stream.Stream;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.Test;
26+
27+
class StreamUtilTest {
28+
29+
@Test
30+
void foldLeft() {
31+
List<Integer> numbers = List.of(1, 2, 3, 4);
32+
33+
Integer result = numbers.stream().collect(StreamUtil.foldLeft(0, Integer::sum));
34+
35+
Assertions.assertEquals(10, result);
36+
}
37+
38+
@Test
39+
void foldLeftWithEmptyStream() {
40+
List<Integer> numbers = List.of();
41+
42+
Integer result = numbers.stream().collect(StreamUtil.foldLeft(0, Integer::sum));
43+
44+
Assertions.assertEquals(0, result);
45+
}
46+
47+
@Test
48+
void mergeMapsOfLists() {
49+
Map<String, List<Integer>> map1 = Map.of("a", List.of(1, 2));
50+
Map<String, List<Integer>> map2 = Map.of("a", List.of(3, 4));
51+
52+
Map<String, List<Integer>> merged = Stream.of(map1, map2).collect(StreamUtil.mergeMapsOfLists());
53+
54+
Assertions.assertEquals(List.of(1, 2, 3, 4), merged.get("a"));
55+
}
56+
57+
@Test
58+
void mergeMapsOfListsWithEmptyStream() {
59+
Map<String, List<Integer>> merged = Stream.<Map<String, List<Integer>>>of().collect(StreamUtil.mergeMapsOfLists());
60+
61+
Assertions.assertTrue(merged.isEmpty());
62+
}
63+
64+
@Test
65+
void mergeMapsOfListsWhenSameKeyAppearsInMultipleMaps() {
66+
Map<String, List<Integer>> map1 = Map.of("a", List.of(1, 2));
67+
Map<String, List<Integer>> map2 = Map.of("a", List.of(3, 4));
68+
69+
Map<String, List<Integer>> merged = Stream.of(map1, map2).collect(StreamUtil.mergeMapsOfLists());
70+
71+
Assertions.assertEquals(List.of(1, 2, 3, 4), merged.get("a"));
72+
}
73+
}

0 commit comments

Comments
 (0)