Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace GSON with Jackson #52

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -27,9 +27,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.11.0</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.18.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
52 changes: 30 additions & 22 deletions src/test/java/org/openmrs/performance/http/DoctorHttpService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.openmrs.performance.http;

import com.google.gson.Gson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.gatling.javaapi.http.HttpRequestActionBuilder;

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

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

@@ -88,27 +88,34 @@ public HttpRequestActionBuilder submitVisitForm(String patientUuid, String visit
requestBodyMap.put("startDatetime", startDateTime);
requestBodyMap.put("visitType", visitTypeUuid);
requestBodyMap.put("location", locationUuid);

return http("Submit Visit Form")
.post("/openmrs/ws/rest/v1/visit")
.body(StringBody(gson.toJson(requestBodyMap)))
.check(jsonPath("$.uuid").saveAs("visitUuid"));
}

try {
return http("Submit Visit Form")
.post("/openmrs/ws/rest/v1/visit")
.body(StringBody(new ObjectMapper().writeValueAsString(requestBodyMap)))
.check(jsonPath("$.uuid").saveAs("visitUuid"));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

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

Map<String, String> requestBodyMap = new HashMap<>();
requestBodyMap.put("location", locationUuid);
requestBodyMap.put("visitType", visitTypeUuid);
requestBodyMap.put("stopDatetime", formattedStopDateTime);

return http("End Visit")
.post("/openmrs/ws/rest/v1/visit/" + visitUuid)
.body(StringBody(gson.toJson(requestBodyMap)));
}

try {
return http("End Visit")
.post("/openmrs/ws/rest/v1/visit/" + visitUuid)
.body(StringBody(new ObjectMapper().writeValueAsString(requestBodyMap)));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public HttpRequestActionBuilder getOrderTypes() {
return http("Get Order Types")
@@ -224,12 +231,13 @@ public HttpRequestActionBuilder saveOrder(String patientUuid, String visitUuid,
encounter.put("visit", visitUuid);
encounter.put("obs", new Object[0]);
encounter.put("orders", new Object[] { order });

Gson gson = new Gson();
String body = gson.toJson(encounter);

return http("Save Drug Order")
.post("/openmrs/ws/rest/v1/encounter")
.body(StringBody(body));
}

try {
return http("Save Drug Order")
.post("/openmrs/ws/rest/v1/encounter")
.body(StringBody(new ObjectMapper().writeValueAsString(encounter)));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
32 changes: 22 additions & 10 deletions src/test/java/org/openmrs/performance/utils/CommonUtils.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
package org.openmrs.performance.utils;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

public class CommonUtils {

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

for (int i = 0; i < entries.size(); i++) {
JsonObject resource = entries.get(i).getAsJsonObject().getAsJsonObject("resource");
JsonArray coding = resource.getAsJsonObject("code").getAsJsonArray("coding");
conceptIds.add(coding.get(0).getAsJsonObject().get("code").getAsString());
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonObject = objectMapper.readTree(response);
JsonNode entries = jsonObject.get("entry");

if (entries != null && entries.isArray()) {
return StreamSupport.stream(entries.spliterator(), false)
.map(entry -> entry.get("resource"))
.filter(Objects::nonNull)
.map(resource -> resource.path("code").path("coding"))
.filter(coding -> coding.isArray() && !coding.isEmpty())
.map(coding -> coding.get(0).get("code").asText())
.collect(Collectors.toList());
}
} catch (Exception e) {
throw new RuntimeException(e);
}

return conceptIds;
}

Loading
Oops, something went wrong.