Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 05cf78a

Browse files
SORMAS-Foundation#3616 Refactoring of ExternalVisitsResourceTest
- read json directly - readability - check enum names one by none
1 parent 304ea54 commit 05cf78a

1 file changed

Lines changed: 69 additions & 57 deletions

File tree

sormas-rest/src/test/java/de/symeda/sormas/rest/ExternalVisitsResourceTest.java

Lines changed: 69 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import static org.junit.Assert.assertEquals;
44

55
import java.io.IOException;
6+
import java.io.Reader;
67
import java.nio.charset.StandardCharsets;
78
import java.nio.file.Files;
9+
import java.nio.file.Path;
810
import java.nio.file.Paths;
911
import java.util.ArrayList;
1012
import java.util.Arrays;
13+
import java.util.Collections;
1114
import java.util.List;
1215
import java.util.Map;
13-
import java.util.stream.Stream;
1416

1517
import org.junit.Test;
1618

@@ -27,26 +29,17 @@ public class ExternalVisitsResourceTest {
2729
* https://gitter.im/SORMAS-Project!
2830
*/
2931
public void testIfRelevantSwaggerDocumentationIsUnchanged() throws IOException {
30-
ObjectMapper objectMapper = new ObjectMapper();
3132

3233
//load released and new swagger docu information
33-
String releasedSwaggerDocuPath = System.getProperty("user.dir");
34-
releasedSwaggerDocuPath = releasedSwaggerDocuPath + "/src/test/resources/swagger.json";
35-
String releasedSwaggerDocu = fileToString(releasedSwaggerDocuPath);
36-
Map<String, Object> releasedSwaggerDocuMap = objectMapper.readValue(releasedSwaggerDocu, new TypeReference<Map<String, Object>>() {
37-
});
34+
Map<String, Object> releasedSwaggerDocuMap = loadJson("./src/test/resources/swagger.json");
3835

39-
String newSwaggerDocuPath = System.getProperty("user.dir");
40-
newSwaggerDocuPath = newSwaggerDocuPath + "/target/test-classes/swagger.json";
41-
String newSwaggerDocu = fileToString(newSwaggerDocuPath);
42-
Map<String, Object> newSwaggerDocuMap = objectMapper.readValue(newSwaggerDocu, new TypeReference<Map<String, Object>>() {
43-
});
36+
Map<String, Object> newSwaggerDocuMap = loadJson("./target/test-classes/swagger.json");
4437

4538
// Check whether path information is equal in new and released swagger docu
46-
ArrayList releasedControllerList = new ArrayList();
39+
ArrayList<Object> releasedControllerList = new ArrayList<>();
4740
extractPathsOfController(releasedSwaggerDocuMap, "External Visits Controller", releasedControllerList);
4841

49-
ArrayList newControllerList = new ArrayList();
42+
ArrayList<Object> newControllerList = new ArrayList<>();
5043
extractPathsOfController(newSwaggerDocuMap, "External Visits Controller", newControllerList);
5144

5245
assertEquals(releasedControllerList, newControllerList);
@@ -63,19 +56,20 @@ public void testIfRelevantSwaggerDocumentationIsUnchanged() throws IOException {
6356
"SymptomState",
6457
"YesNoUnknown",
6558
"TemperatureSource");
66-
ArrayList releasedDetailList = new ArrayList();
67-
ArrayList newDetailList = new ArrayList();
6859

6960
for (String name : enumNames) {
61+
ArrayList<Object> releasedDetailList = new ArrayList<>();
62+
ArrayList<Object> newDetailList = new ArrayList<>();
7063
extractDetail(releasedSwaggerDocuMap, name, releasedDetailList);
7164
extractDetail(newSwaggerDocuMap, name, newDetailList);
65+
66+
assertEquals("", releasedDetailList, newDetailList);
7267
}
73-
assertEquals(releasedDetailList, newDetailList);
7468
}
7569

7670
/**
7771
*
78-
* @param topLevelMap
72+
* @param level1
7973
* Nested Map from which to extract the information. It's supposed to be a mapped swagger.json
8074
* @param controller
8175
* The name of the controller, e.g. External Visits Controller
@@ -84,31 +78,41 @@ public void testIfRelevantSwaggerDocumentationIsUnchanged() throws IOException {
8478
* @return Documentation about any path found for the specified controller (e.g. /visits-external/person/{personUuid} for the External
8579
* Visits Controller). This includes parameter names for that path, but not information about related enums.
8680
*/
87-
private static ArrayList extractPathsOfController(Map topLevelMap, String controller, ArrayList list) {
88-
for (Object firstLayerKey : topLevelMap.keySet()) {
89-
Object firstLayerValue = topLevelMap.get(firstLayerKey);
90-
if (firstLayerValue instanceof Map) {
91-
Map<String, Object> secondLayerMap = (Map<String, Object>) topLevelMap.get(firstLayerKey);
92-
for (Object secondLayerKey : secondLayerMap.keySet()) {
93-
Object secondLayerValue = secondLayerMap.get(secondLayerKey);
94-
if (secondLayerValue instanceof Map) {
95-
Map<String, Object> thirdLayerMap = (Map<String, Object>) secondLayerMap.get(secondLayerKey);
96-
for (Object thirdLayerKey : thirdLayerMap.keySet()) {
97-
// tags are always represented in the third layer and as ArrayLists
98-
if ("tags".equals(thirdLayerKey) && thirdLayerMap.get(thirdLayerKey) instanceof ArrayList) {
99-
ArrayList tags = (ArrayList) thirdLayerMap.get(thirdLayerKey);
100-
if (tags.contains(controller)) {
101-
list.add(firstLayerKey);
102-
list.add(topLevelMap.get(firstLayerKey));
103-
}
104-
}
105-
}
106-
}
81+
private static void extractPathsOfController(Map<String, Object> level1, String controller, ArrayList<Object> list) {
82+
level1.entrySet().forEach(e1 -> {
83+
String key1 = e1.getKey();
84+
Object value1 = e1.getValue();
85+
if (isInnerNode(value1)) {
86+
Map<String, Object> level2 = innerNode(value1);
87+
if (hasTag(level2, controller)) {
88+
list.add(key1);
89+
list.add(value1);
10790
}
108-
extractPathsOfController((Map) firstLayerValue, controller, list);
91+
extractPathsOfController(level2, controller, list);
10992
}
93+
});
94+
}
95+
96+
private static boolean hasTag(Map<String, Object> level2, String controller) {
97+
return level2.values()
98+
.stream()
99+
.filter(ExternalVisitsResourceTest::isInnerNode)
100+
.map(ExternalVisitsResourceTest::innerNode)
101+
// tags are always represented in the third layer and as ArrayLists
102+
.map(ExternalVisitsResourceTest::tags)
103+
.filter(t -> t.contains(controller))
104+
.findFirst()
105+
.isPresent();
106+
}
107+
108+
@SuppressWarnings("unchecked")
109+
private static List<Object> tags(Map<String, Object> innerNode) {
110+
Object tags = innerNode.get("tags");
111+
if (tags instanceof List) {
112+
return (List<Object>) tags;
113+
} else {
114+
return Collections.emptyList();
110115
}
111-
return list;
112116
}
113117

114118
/**
@@ -123,29 +127,37 @@ private static ArrayList extractPathsOfController(Map topLevelMap, String contro
123127
* Documentation found about the detail. The Map is searched for a key equal to detailName, an it, plus the according value is
124128
* added to the list.
125129
*/
126-
private static ArrayList extractDetail(Map topLevelMap, String detailName, ArrayList list) {
127-
for (Object firstLayerKey : topLevelMap.keySet()) {
128-
Object firstLayerValue = topLevelMap.get(firstLayerKey);
129-
if (detailName.equals(firstLayerKey)) {
130+
private static void extractDetail(Map<String, Object> level1, String detailName, ArrayList<Object> list) {
131+
level1.entrySet().stream().forEach(e1 -> {
132+
Object value1 = e1.getValue();
133+
if (detailName.equals(e1.getKey())) {
130134
list.add(detailName);
131-
list.add(firstLayerValue);
132-
} else if (firstLayerValue instanceof Map) {
133-
extractDetail((Map) firstLayerValue, detailName, list);
135+
list.add(value1);
136+
} else if (isInnerNode(value1)) {
137+
extractDetail(innerNode(value1), detailName, list);
134138
}
135-
}
136-
return list;
139+
});
137140
}
138141

139-
private static String fileToString(String filePath) {
140-
StringBuilder contentBuilder = new StringBuilder();
142+
private static boolean isInnerNode(Object node) {
143+
return node instanceof Map;
144+
}
141145

142-
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
143-
stream.forEach(s -> contentBuilder.append(s).append("\n"));
144-
} catch (IOException e) {
145-
e.printStackTrace();
146-
}
146+
@SuppressWarnings("unchecked")
147+
private static Map<String, Object> innerNode(Object node) {
148+
return (Map<String, Object>) node;
149+
}
147150

148-
return contentBuilder.toString();
151+
private static Map<String, Object> loadJson(String relativePath) throws IOException {
152+
153+
Path path = Paths.get(System.getProperty("user.dir"), relativePath);
154+
155+
ObjectMapper objectMapper = new ObjectMapper();
156+
157+
try (Reader rd = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
158+
return objectMapper.readValue(rd, new TypeReference<Map<String, Object>>() {
159+
});
160+
}
149161
}
150162

151163
}

0 commit comments

Comments
 (0)