Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bachmanity1 committed Oct 2, 2024
1 parent 57a0ee2 commit 962cea7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,17 @@ private String getDiff(Map<String, Object> prunedActualMap, Map<String, Object>
}

@SuppressWarnings("unchecked")
private Map<String, Object> sortMap(Map<String, Object> map) {
Map<String, Object> sortMap(Map<String, Object> map) {
List<String> sortedKeys = new ArrayList<>(map.keySet());
Collections.sort(sortedKeys);

Map<String, Object> sortedMap = new LinkedHashMap<>();
for (String key : sortedKeys) {
Object value = map.get(key);
if (value instanceof Map) {
Map<String, Object> nestedMap = (Map<String, Object>) value;
sortedMap.put(key, sortMap(nestedMap));
sortedMap.put(key, sortMap((Map<String, Object>) value));
} else if (value instanceof List) {
sortedMap.put(key, sortList((List<?>) value));
sortedMap.put(key, sortListItems((List<Object>) value));
} else {
sortedMap.put(key, value);
}
Expand All @@ -156,14 +155,13 @@ private Map<String, Object> sortMap(Map<String, Object> map) {
}

@SuppressWarnings("unchecked")
private List<?> sortList(List<?> list) {
List<Object> sortListItems(List<Object> list) {
List<Object> sortedList = new ArrayList<>();
for (Object item : list) {
if (item instanceof Map) {
Map<String, Object> mapItem = (Map<String, Object>) item;
sortedList.add(sortMap(mapItem));
sortedList.add(sortMap((Map<String, Object>) item));
} else if (item instanceof List) {
sortedList.add(sortList((List<?>) item));
sortedList.add(sortListItems((List<Object>) item));
} else {
sortedList.add(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.javaoperatorsdk.operator.processing.dependent.kubernetes;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -121,4 +124,47 @@ private <R> R loadResource(String fileName, Class<R> clazz) {
fileName);
}

@Test
@SuppressWarnings("unchecked")
void sortListItemsTest() {
Map<String, Object> nestedMap1 = new HashMap<>();
nestedMap1.put("z", 26);
nestedMap1.put("y", 25);

Map<String, Object> nestedMap2 = new HashMap<>();
nestedMap2.put("b", 26);
nestedMap2.put("c", 25);
nestedMap2.put("a", 24);

List<Object> unsortedListItems = Arrays.asList(1, nestedMap1, nestedMap2);
List<Object> sortedListItems = matcher.sortListItems(unsortedListItems);

assertThat(sortedListItems).element(0).isEqualTo(1);

Map<String, Object> sortedNestedMap1 = (Map<String, Object>) sortedListItems.get(1);
assertThat(sortedNestedMap1.keySet()).containsExactly("y", "z");

Map<String, Object> sortedNestedMap2 = (Map<String, Object>) sortedListItems.get(2);
assertThat(sortedNestedMap2.keySet()).containsExactly("a", "b", "c");
}

@Test
@SuppressWarnings("unchecked")
void testSortMapWithNestedMap() {
Map<String, Object> nestedMap = new HashMap<>();
nestedMap.put("z", 26);
nestedMap.put("y", 25);

Map<String, Object> unsortedMap = new HashMap<>();
unsortedMap.put("b", nestedMap);
unsortedMap.put("a", 1);
unsortedMap.put("c", 2);

Map<String, Object> sortedMap = matcher.sortMap(unsortedMap);

assertThat(sortedMap.keySet()).containsExactly("a", "b", "c");

Map<String, Object> sortedNestedMap = (Map<String, Object>) sortedMap.get("b");
assertThat(sortedNestedMap.keySet()).containsExactly("y", "z");
}
}

0 comments on commit 962cea7

Please sign in to comment.