Skip to content

Commit

Permalink
Replaced org.json in integration tests with Jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Nov 15, 2023
1 parent 6a1624b commit 4de7070
Show file tree
Hide file tree
Showing 26 changed files with 1,417 additions and 1,608 deletions.
5 changes: 0 additions & 5 deletions FROST-Server.Tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,6 @@
<artifactId>jetty-servlet</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.fail;

import com.fasterxml.jackson.databind.JsonNode;
import dasniko.testcontainers.keycloak.KeycloakContainer;
import de.fraunhofer.iosb.ilt.frostserver.FrostMqttServer;
import de.fraunhofer.iosb.ilt.frostserver.http.common.DatabaseStatus;
Expand Down Expand Up @@ -86,6 +87,7 @@
import de.fraunhofer.iosb.ilt.statests.f03metadata.MetadataTests11;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import java.io.IOException;
import java.net.ServerSocket;
import java.nio.file.Files;
Expand All @@ -106,9 +108,6 @@
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -558,29 +557,29 @@ public void checkServiceRootUri(ServerSettings serverSettings, ServerVersion ver
return;
}

JSONObject jsonResponse;
JSONArray entities;
JsonNode jsonResponse;
JsonNode entities;
try {
jsonResponse = new JSONObject(response.response);
entities = jsonResponse.getJSONArray("value");
} catch (JSONException e) {
jsonResponse = Utils.MAPPER.readTree(response.response);
entities = jsonResponse.get("value");
} catch (IOException | NullPointerException e) {
LOGGER.error("The service response for the root URI '" + rootUri + "' is not JSON.", e);
fail("The service response for the root URI '" + rootUri + "' is not JSON.");
return;
}
boolean hasActuation = false;
boolean hasMultiDatastream = false;
for (int i = 0; i < entities.length(); i++) {
JSONObject entity;
for (int i = 0; i < entities.size(); i++) {
JsonNode entity;
String name;
try {
entity = entities.getJSONObject(i);
entity = entities.get(i);
if (!entity.has("name")) {
fail("The name component of Service root URI response is not available.");
return;
}
name = entity.getString("name");
} catch (JSONException e) {
name = entity.get("name").textValue();
} catch (NullPointerException e) {
LOGGER.error("The service response for the root URI '" + rootUri + "' is not JSON.", e);
fail("The service response for the root URI '" + rootUri + "' is not JSON.");
return;
Expand Down Expand Up @@ -611,10 +610,10 @@ public void checkServiceRootUri(ServerSettings serverSettings, ServerVersion ver
}
}
if (version == ServerVersion.v_1_1) {
JSONObject serverSettingsObject = jsonResponse.getJSONObject("serverSettings");
JSONArray conformanceArray = serverSettingsObject.getJSONArray("conformance");
for (Object reqItem : conformanceArray.toList()) {
Set<Requirement> allMatching = Requirement.getAllMatching(reqItem.toString());
JsonNode serverSettingsObject = jsonResponse.get("serverSettings");
JsonNode conformanceArray = serverSettingsObject.get("conformance");
for (JsonNode reqItem : conformanceArray) {
Set<Requirement> allMatching = Requirement.getAllMatching(reqItem.textValue());
if (allMatching.isEmpty()) {
LOGGER.info("Server implements unknown requirement: {}", reqItem);
}
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import com.fasterxml.jackson.databind.JsonNode;
import de.fraunhofer.iosb.ilt.statests.ServerSettings;
import de.fraunhofer.iosb.ilt.statests.ServerVersion;
import de.fraunhofer.iosb.ilt.statests.util.EntityType;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods;
import de.fraunhofer.iosb.ilt.statests.util.HTTPMethods.HttpResponse;
import de.fraunhofer.iosb.ilt.statests.util.ServiceUrlHelper;
import de.fraunhofer.iosb.ilt.statests.util.Utils;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -121,10 +120,10 @@ private static String getEntities(String rootUri, EntityType entityType) {

private static int countEntitiesInResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
JSONArray entities = jsonResponse.getJSONArray("value");
return entities.length();
} catch (JSONException e) {
JsonNode jsonResponse = Utils.MAPPER.readTree(response);
JsonNode entities = jsonResponse.get("value");
return entities.size();
} catch (IOException e) {
LOGGER.error("Exception: ", e);
fail("An Exception occurred during testing!:\n" + e.getMessage());
}
Expand Down
Loading

0 comments on commit 4de7070

Please sign in to comment.