Skip to content

Commit 758dc15

Browse files
wikumChamithjayasanka-sack
andauthoredJan 15, 2025
Replace GSON with Jackson (#52)
* Replace GSON with Jackson * Update src/test/java/org/openmrs/performance/utils/CommonUtils.java --------- Co-authored-by: Jayasanka Weerasinghe <33048395+jayasanka-sack@users.noreply.github.com>
1 parent e1d02dc commit 758dc15

File tree

3 files changed

+55
-35
lines changed

3 files changed

+55
-35
lines changed
 

‎pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
<scope>test</scope>
2828
</dependency>
2929
<dependency>
30-
<groupId>com.google.code.gson</groupId>
31-
<artifactId>gson</artifactId>
32-
<version>2.11.0</version>
30+
<groupId>com.fasterxml.jackson.core</groupId>
31+
<artifactId>jackson-databind</artifactId>
32+
<version>2.18.2</version>
3333
</dependency>
3434
<dependency>
3535
<groupId>org.junit.jupiter</groupId>

‎src/test/java/org/openmrs/performance/http/DoctorHttpService.java

+30-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openmrs.performance.http;
22

3-
import com.google.gson.Gson;
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
45
import io.gatling.javaapi.http.HttpRequestActionBuilder;
56

67
import java.time.ZonedDateTime;
@@ -79,7 +80,6 @@ public HttpRequestActionBuilder getAppointments(String patientUuid) {
7980
}
8081

8182
public HttpRequestActionBuilder submitVisitForm(String patientUuid, String visitTypeUuid, String locationUuid) {
82-
Gson gson = new Gson();
8383
ZonedDateTime now = ZonedDateTime.now();
8484
String startDateTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
8585

@@ -88,27 +88,34 @@ public HttpRequestActionBuilder submitVisitForm(String patientUuid, String visit
8888
requestBodyMap.put("startDatetime", startDateTime);
8989
requestBodyMap.put("visitType", visitTypeUuid);
9090
requestBodyMap.put("location", locationUuid);
91-
92-
return http("Submit Visit Form")
93-
.post("/openmrs/ws/rest/v1/visit")
94-
.body(StringBody(gson.toJson(requestBodyMap)))
95-
.check(jsonPath("$.uuid").saveAs("visitUuid"));
96-
}
91+
92+
try {
93+
return http("Submit Visit Form")
94+
.post("/openmrs/ws/rest/v1/visit")
95+
.body(StringBody(new ObjectMapper().writeValueAsString(requestBodyMap)))
96+
.check(jsonPath("$.uuid").saveAs("visitUuid"));
97+
} catch (JsonProcessingException e) {
98+
throw new RuntimeException(e);
99+
}
100+
}
97101

98102
public HttpRequestActionBuilder submitEndVisit(String visitUuid, String locationUuid, String visitTypeUuid) {
99-
Gson gson = new Gson();
100103
ZonedDateTime now = ZonedDateTime.now();
101104
String formattedStopDateTime = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
102105

103106
Map<String, String> requestBodyMap = new HashMap<>();
104107
requestBodyMap.put("location", locationUuid);
105108
requestBodyMap.put("visitType", visitTypeUuid);
106109
requestBodyMap.put("stopDatetime", formattedStopDateTime);
107-
108-
return http("End Visit")
109-
.post("/openmrs/ws/rest/v1/visit/" + visitUuid)
110-
.body(StringBody(gson.toJson(requestBodyMap)));
111-
}
110+
111+
try {
112+
return http("End Visit")
113+
.post("/openmrs/ws/rest/v1/visit/" + visitUuid)
114+
.body(StringBody(new ObjectMapper().writeValueAsString(requestBodyMap)));
115+
} catch (JsonProcessingException e) {
116+
throw new RuntimeException(e);
117+
}
118+
}
112119

113120
public HttpRequestActionBuilder getOrderTypes() {
114121
return http("Get Order Types")
@@ -224,12 +231,13 @@ public HttpRequestActionBuilder saveOrder(String patientUuid, String visitUuid,
224231
encounter.put("visit", visitUuid);
225232
encounter.put("obs", new Object[0]);
226233
encounter.put("orders", new Object[] { order });
227-
228-
Gson gson = new Gson();
229-
String body = gson.toJson(encounter);
230-
231-
return http("Save Drug Order")
232-
.post("/openmrs/ws/rest/v1/encounter")
233-
.body(StringBody(body));
234-
}
234+
235+
try {
236+
return http("Save Drug Order")
237+
.post("/openmrs/ws/rest/v1/encounter")
238+
.body(StringBody(new ObjectMapper().writeValueAsString(encounter)));
239+
} catch (JsonProcessingException e) {
240+
throw new RuntimeException(e);
241+
}
242+
}
235243
}

‎src/test/java/org/openmrs/performance/utils/CommonUtils.java

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
package org.openmrs.performance.utils;
22

3-
import com.google.gson.JsonArray;
4-
import com.google.gson.JsonObject;
5-
import com.google.gson.JsonParser;
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
65

76
import java.util.ArrayList;
87
import java.util.List;
8+
import java.util.Objects;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.StreamSupport;
911

1012
public class CommonUtils {
1113

1214
public static List<String> extractConceptIds(String response) {
1315
List<String> conceptIds = new ArrayList<>();
14-
JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
15-
JsonArray entries = jsonObject.getAsJsonArray("entry");
1616

17-
for (int i = 0; i < entries.size(); i++) {
18-
JsonObject resource = entries.get(i).getAsJsonObject().getAsJsonObject("resource");
19-
JsonArray coding = resource.getAsJsonObject("code").getAsJsonArray("coding");
20-
conceptIds.add(coding.get(0).getAsJsonObject().get("code").getAsString());
17+
try {
18+
ObjectMapper objectMapper = new ObjectMapper();
19+
JsonNode jsonObject = objectMapper.readTree(response);
20+
JsonNode entries = jsonObject.get("entry");
21+
22+
if (entries != null && entries.isArray()) {
23+
return StreamSupport.stream(entries.spliterator(), false)
24+
.map(entry -> entry.get("resource"))
25+
.filter(Objects::nonNull)
26+
.map(resource -> resource.path("code").path("coding"))
27+
.filter(coding -> coding.isArray() && !coding.isEmpty())
28+
.map(coding -> coding.get(0).get("code").asText())
29+
.collect(Collectors.toList());
30+
}
31+
} catch (Exception e) {
32+
throw new RuntimeException(e);
2133
}
22-
34+
2335
return conceptIds;
2436
}
2537

0 commit comments

Comments
 (0)
Failed to load comments.