mirror of
https://gitee.com/dcren/initializr.git
synced 2025-09-18 17:48:14 +08:00
Remove use of org.json
This commit removes all use of org.json in production code and moves the json api to a test only dependency. Closes gh-507
This commit is contained in:
@@ -16,8 +16,8 @@
|
||||
|
||||
package io.spring.initializr.actuate;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.spring.initializr.web.AbstractFullStackInitializrIntegrationTests;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
@@ -45,35 +45,32 @@ public class ActuatorIntegrationTests
|
||||
@Test
|
||||
public void metricsAvailableByDefault() {
|
||||
downloadZip("/starter.zip?packaging=jar&javaVersion=1.8&style=web&style=jpa");
|
||||
JSONObject result = metricsEndpoint();
|
||||
int requests = result.getInt("counter.initializr.requests");
|
||||
int packaging = result.getInt("counter.initializr.packaging.jar");
|
||||
int javaVersion = result.getInt("counter.initializr.java_version.1_8");
|
||||
int webDependency = result.getInt("counter.initializr.dependency.web");
|
||||
int jpaDependency = result.getInt("counter.initializr.dependency.data-jpa");
|
||||
JsonNode result = metricsEndpoint();
|
||||
int requests = result.get("counter.initializr.requests").intValue();
|
||||
int packaging = result.get("counter.initializr.packaging.jar").intValue();
|
||||
int javaVersion = result.get("counter.initializr.java_version.1_8").intValue();
|
||||
int webDependency = result.get("counter.initializr.dependency.web").intValue();
|
||||
int jpaDependency = result.get("counter.initializr.dependency.data-jpa")
|
||||
.intValue();
|
||||
|
||||
// No jpa dep this time
|
||||
downloadZip("/starter.zip?packaging=jar&javaVersion=1.8&style=web");
|
||||
|
||||
JSONObject updatedResult = metricsEndpoint();
|
||||
JsonNode updatedResult = metricsEndpoint();
|
||||
assertEquals("Number of request should have increased", requests + 1,
|
||||
updatedResult.getInt("counter.initializr.requests"));
|
||||
updatedResult.get("counter.initializr.requests").intValue());
|
||||
assertEquals("jar packaging metric should have increased", packaging + 1,
|
||||
updatedResult.getInt("counter.initializr.packaging.jar"));
|
||||
updatedResult.get("counter.initializr.packaging.jar").intValue());
|
||||
assertEquals("java version metric should have increased", javaVersion + 1,
|
||||
updatedResult.getInt("counter.initializr.java_version.1_8"));
|
||||
updatedResult.get("counter.initializr.java_version.1_8").intValue());
|
||||
assertEquals("web dependency metric should have increased", webDependency + 1,
|
||||
updatedResult.getInt("counter.initializr.dependency.web"));
|
||||
updatedResult.get("counter.initializr.dependency.web").intValue());
|
||||
assertEquals("jpa dependency metric should not have increased", jpaDependency,
|
||||
updatedResult.getInt("counter.initializr.dependency.data-jpa"));
|
||||
updatedResult.get("counter.initializr.dependency.data-jpa").intValue());
|
||||
}
|
||||
|
||||
private JSONObject metricsEndpoint() {
|
||||
private JsonNode metricsEndpoint() {
|
||||
return parseJson(getRestTemplate().getForObject(createUrl("/metrics"), String.class));
|
||||
}
|
||||
|
||||
private JSONObject parseJson(String content) {
|
||||
return new JSONObject(content);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -20,10 +20,9 @@ import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.spring.initializr.actuate.stat.MainControllerStatsIntegrationTests.StatsMockController;
|
||||
import io.spring.initializr.web.AbstractFullStackInitializrIntegrationTests;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -74,12 +73,12 @@ public class MainControllerStatsIntegrationTests
|
||||
assertEquals("No stat got generated", 1, statsMockController.stats.size());
|
||||
StatsMockController.Content content = statsMockController.stats.get(0);
|
||||
|
||||
JSONObject json = new JSONObject(content.json);
|
||||
assertEquals("com.foo", json.get("groupId"));
|
||||
assertEquals("bar", json.get("artifactId"));
|
||||
JSONArray list = json.getJSONArray("dependencies");
|
||||
assertEquals(1, list.length());
|
||||
assertEquals("web", list.get(0));
|
||||
JsonNode json = parseJson(content.json);
|
||||
assertEquals("com.foo", json.get("groupId").textValue());
|
||||
assertEquals("bar", json.get("artifactId").textValue());
|
||||
JsonNode list = json.get("dependencies");
|
||||
assertEquals(1, list.size());
|
||||
assertEquals("web", list.get(0).textValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -104,7 +103,7 @@ public class MainControllerStatsIntegrationTests
|
||||
assertEquals("No stat got generated", 1, statsMockController.stats.size());
|
||||
StatsMockController.Content content = statsMockController.stats.get(0);
|
||||
|
||||
JSONObject json = new JSONObject(content.json);
|
||||
JsonNode json = parseJson(content.json);
|
||||
assertFalse("requestIp property should not be set", json.has("requestIp"));
|
||||
}
|
||||
|
||||
@@ -116,8 +115,8 @@ public class MainControllerStatsIntegrationTests
|
||||
assertEquals("No stat got generated", 1, statsMockController.stats.size());
|
||||
StatsMockController.Content content = statsMockController.stats.get(0);
|
||||
|
||||
JSONObject json = new JSONObject(content.json);
|
||||
assertEquals("Wrong requestIp", "10.0.0.123", json.get("requestIp"));
|
||||
JsonNode json = parseJson(content.json);
|
||||
assertEquals("Wrong requestIp", "10.0.0.123", json.get("requestIp").textValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -128,7 +127,7 @@ public class MainControllerStatsIntegrationTests
|
||||
assertEquals("No stat got generated", 1, statsMockController.stats.size());
|
||||
StatsMockController.Content content = statsMockController.stats.get(0);
|
||||
|
||||
JSONObject json = new JSONObject(content.json);
|
||||
JsonNode json = parseJson(content.json);
|
||||
assertFalse("requestIpv4 property should not be set if value is not a valid IPv4",
|
||||
json.has("requestIpv4"));
|
||||
}
|
||||
@@ -141,7 +140,7 @@ public class MainControllerStatsIntegrationTests
|
||||
assertEquals("No stat got generated", 1, statsMockController.stats.size());
|
||||
StatsMockController.Content content = statsMockController.stats.get(0);
|
||||
|
||||
JSONObject json = new JSONObject(content.json);
|
||||
JsonNode json = parseJson(content.json);
|
||||
assertFalse("requestCountry property should not be set if value is set to xx",
|
||||
json.has("requestCountry"));
|
||||
}
|
||||
@@ -158,13 +157,13 @@ public class MainControllerStatsIntegrationTests
|
||||
assertEquals("No stat got generated", 1, statsMockController.stats.size());
|
||||
StatsMockController.Content content = statsMockController.stats.get(0);
|
||||
|
||||
JSONObject json = new JSONObject(content.json);
|
||||
assertEquals("com.example", json.get("groupId"));
|
||||
assertEquals("demo", json.get("artifactId"));
|
||||
assertEquals(true, json.get("invalid"));
|
||||
assertEquals(true, json.get("invalidType"));
|
||||
JsonNode json = parseJson(content.json);
|
||||
assertEquals("com.example", json.get("groupId").textValue());
|
||||
assertEquals("demo", json.get("artifactId").textValue());
|
||||
assertEquals(true, json.get("invalid").booleanValue());
|
||||
assertEquals(true, json.get("invalidType").booleanValue());
|
||||
assertNotNull(json.get("errorMessage"));
|
||||
assertTrue(((String) json.get("errorMessage")).contains("invalid-type"));
|
||||
assertTrue(json.get("errorMessage").textValue().contains("invalid-type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user