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

Commit eca4173

Browse files
Merge pull request SORMAS-Foundation#3631 from hzi-braunschweig/bugfix-3616-sormas-rest_cannot_be_deployed_in_Eclipse
Bugfix 3616 sormas rest cannot be deployed in eclipse
2 parents 69aba83 + b56ec71 commit eca4173

1 file changed

Lines changed: 77 additions & 64 deletions

File tree

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package de.symeda.sormas.rest;
22

3-
import com.fasterxml.jackson.core.type.TypeReference;
4-
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import junit.framework.TestCase;
6-
import org.junit.Test;
3+
import static org.junit.Assert.assertEquals;
74

85
import java.io.IOException;
9-
6+
import java.io.Reader;
107
import java.nio.charset.StandardCharsets;
118
import java.nio.file.Files;
9+
import java.nio.file.Path;
1210
import java.nio.file.Paths;
1311
import java.util.ArrayList;
1412
import java.util.Arrays;
13+
import java.util.Collections;
1514
import java.util.List;
1615
import java.util.Map;
17-
import java.util.stream.Stream;
1816

19-
public class ExternalVisitsResourceTest extends TestCase {
17+
import org.junit.Test;
18+
19+
import com.fasterxml.jackson.core.type.TypeReference;
20+
import com.fasterxml.jackson.databind.ObjectMapper;
21+
22+
public class ExternalVisitsResourceTest {
2023

2124
@Test
2225
/*
@@ -26,26 +29,17 @@ public class ExternalVisitsResourceTest extends TestCase {
2629
* https://gitter.im/SORMAS-Project!
2730
*/
2831
public void testIfRelevantSwaggerDocumentationIsUnchanged() throws IOException {
29-
ObjectMapper objectMapper = new ObjectMapper();
3032

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

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

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

48-
ArrayList newControllerList = new ArrayList();
42+
ArrayList<Object> newControllerList = new ArrayList<>();
4943
extractPathsOfController(newSwaggerDocuMap, "External Visits Controller", newControllerList);
5044

5145
assertEquals(releasedControllerList, newControllerList);
@@ -62,19 +56,20 @@ public void testIfRelevantSwaggerDocumentationIsUnchanged() throws IOException {
6256
"SymptomState",
6357
"YesNoUnknown",
6458
"TemperatureSource");
65-
ArrayList releasedDetailList = new ArrayList();
66-
ArrayList newDetailList = new ArrayList();
6759

6860
for (String name : enumNames) {
61+
ArrayList<Object> releasedDetailList = new ArrayList<>();
62+
ArrayList<Object> newDetailList = new ArrayList<>();
6963
extractDetail(releasedSwaggerDocuMap, name, releasedDetailList);
7064
extractDetail(newSwaggerDocuMap, name, newDetailList);
65+
66+
assertEquals("", releasedDetailList, newDetailList);
7167
}
72-
assertEquals(releasedDetailList, newDetailList);
7368
}
7469

7570
/**
7671
*
77-
* @param topLevelMap
72+
* @param level1
7873
* Nested Map from which to extract the information. It's supposed to be a mapped swagger.json
7974
* @param controller
8075
* The name of the controller, e.g. External Visits Controller
@@ -83,36 +78,46 @@ public void testIfRelevantSwaggerDocumentationIsUnchanged() throws IOException {
8378
* @return Documentation about any path found for the specified controller (e.g. /visits-external/person/{personUuid} for the External
8479
* Visits Controller). This includes parameter names for that path, but not information about related enums.
8580
*/
86-
private static ArrayList extractPathsOfController(Map topLevelMap, String controller, ArrayList list) {
87-
for (Object firstLayerKey : topLevelMap.keySet()) {
88-
Object firstLayerValue = topLevelMap.get(firstLayerKey);
89-
if (firstLayerValue instanceof Map) {
90-
Map<String, Object> secondLayerMap = (Map<String, Object>) topLevelMap.get(firstLayerKey);
91-
for (Object secondLayerKey : secondLayerMap.keySet()) {
92-
Object secondLayerValue = secondLayerMap.get(secondLayerKey);
93-
if (secondLayerValue instanceof Map) {
94-
Map<String, Object> thirdLayerMap = (Map<String, Object>) secondLayerMap.get(secondLayerKey);
95-
for (Object thirdLayerKey : thirdLayerMap.keySet()) {
96-
// tags are always represented in the third layer and as ArrayLists
97-
if ("tags".equals(thirdLayerKey) && thirdLayerMap.get(thirdLayerKey) instanceof ArrayList) {
98-
ArrayList tags = (ArrayList) thirdLayerMap.get(thirdLayerKey);
99-
if (tags.contains(controller)) {
100-
list.add(firstLayerKey);
101-
list.add(topLevelMap.get(firstLayerKey));
102-
}
103-
}
104-
}
105-
}
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);
10690
}
107-
extractPathsOfController((Map) firstLayerValue, controller, list);
91+
extractPathsOfController(level2, controller, list);
10892
}
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();
109115
}
110-
return list;
111116
}
112117

113118
/**
114119
*
115-
* @param topLevelMap
120+
* @param level1
116121
* Nested Map from which to extract the information. It's supposed to be a mapped swagger.json
117122
* @param detailName
118123
* The name of the detail, e.g. JournalPersonDto.
@@ -122,29 +127,37 @@ private static ArrayList extractPathsOfController(Map topLevelMap, String contro
122127
* Documentation found about the detail. The Map is searched for a key equal to detailName, an it, plus the according value is
123128
* added to the list.
124129
*/
125-
private static ArrayList extractDetail(Map topLevelMap, String detailName, ArrayList list) {
126-
for (Object firstLayerKey : topLevelMap.keySet()) {
127-
Object firstLayerValue = topLevelMap.get(firstLayerKey);
128-
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())) {
129134
list.add(detailName);
130-
list.add(firstLayerValue);
131-
} else if (firstLayerValue instanceof Map) {
132-
extractDetail((Map) firstLayerValue, detailName, list);
135+
list.add(value1);
136+
} else if (isInnerNode(value1)) {
137+
extractDetail(innerNode(value1), detailName, list);
133138
}
134-
}
135-
return list;
139+
});
140+
}
141+
142+
private static boolean isInnerNode(Object node) {
143+
return node instanceof Map;
144+
}
145+
146+
@SuppressWarnings("unchecked")
147+
private static Map<String, Object> innerNode(Object node) {
148+
return (Map<String, Object>) node;
136149
}
137150

138-
private static String fileToString(String filePath) {
139-
StringBuilder contentBuilder = new StringBuilder();
151+
private static Map<String, Object> loadJson(String relativePath) throws IOException {
140152

141-
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
142-
stream.forEach(s -> contentBuilder.append(s).append("\n"));
143-
} catch (IOException e) {
144-
e.printStackTrace();
145-
}
153+
Path path = Paths.get(System.getProperty("user.dir"), relativePath);
146154

147-
return contentBuilder.toString();
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+
}
148161
}
149162

150163
}

0 commit comments

Comments
 (0)