This is an automated email from the ASF dual-hosted git repository.
jensdeppe pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git
The following commit(s) were added to refs/heads/develop by this push:
new 98b094a GEODE-5014: Remove usage of org.json from geode-assembly
module (#2132)
98b094a is described below
commit 98b094af94253ec638fb40d70a6257999dc18e80
Author: Jens Deppe <[email protected]>
AuthorDate: Mon Jul 16 06:39:14 2018 -0700
GEODE-5014: Remove usage of org.json from geode-assembly module (#2132)
---
.../rest/internal/web/RestRegionAPIDUnitTest.java | 43 +++++++-------
.../internal/web/RestSecurityIntegrationTest.java | 16 ++---
.../web/RestSecurityPostProcessorTest.java | 69 +++++++++++-----------
.../internal/web/RestServersIntegrationTest.java | 11 ++--
.../web/SwaggerVerificationIntegrationTest.java | 18 +++---
.../internal/web/controllers/RestAPITestBase.java | 17 +++++-
.../controllers/RestAPIsAndInterOpsDUnitTest.java | 63 ++++++++++----------
.../web/controllers/RestAPIsWithSSLDUnitTest.java | 16 ++---
.../RestQueryAndFunctionIntegrationTest.java | 38 ++++++------
.../geode/test/junit/rules/HttpResponseAssert.java | 27 ++++-----
10 files changed, 160 insertions(+), 158 deletions(-)
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestRegionAPIDUnitTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestRegionAPIDUnitTest.java
index faed3fe..093db75 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestRegionAPIDUnitTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestRegionAPIDUnitTest.java
@@ -18,7 +18,6 @@ package org.apache.geode.rest.internal.web;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
@@ -30,10 +29,10 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
-import org.json.JSONArray;
-import org.json.JSONObject;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
@@ -96,27 +95,27 @@ public class RestRegionAPIDUnitTest {
}
@Test
- public void getRegionAWhenHasData() {
+ public void getRegionAWhenHasData() throws Exception {
Region region = getRegionA();
region.put("customer1", new Customer(1L, "jon", "doe", "123-456-789"));
region.put("customer2", new Customer(2L, "jane", "doe", "123-456-999"));
- JSONObject jsonObject = restClient.doGetAndAssert("/regionA")
+ JsonNode jsonObject = restClient.doGetAndAssert("/regionA")
.statusIsOk().getJsonObject();
- assertThat(jsonObject.getJSONArray("regionA").length()).isEqualTo(2);
+ assertThat(jsonObject.get("regionA").size()).isEqualTo(2);
}
@Test
- public void getSingleKey() {
+ public void getSingleKey() throws Exception {
Region region = getRegionA();
region.put("customer1", new Customer(1L, "jon", "doe", "123-456-789"));
- JSONObject jsonObject = restClient.doGetAndAssert("/regionA/customer1")
+ JsonNode jsonObject = restClient.doGetAndAssert("/regionA/customer1")
.statusIsOk().getJsonObject();
System.out.println(jsonObject.toString());
- assertThat(jsonObject.get("firstName").toString()).isEqualTo("jon");
+ assertThat(jsonObject.get("firstName").asText()).isEqualTo("jon");
}
@Test
@@ -250,18 +249,18 @@ public class RestRegionAPIDUnitTest {
}
@Test
- public void listKeys() {
+ public void listKeys() throws Exception {
Region region = getRegionA();
region.put("customer1", new Customer(1L, "jon", "doe", "123-456-789"));
region.put("customer2", new Customer(2L, "jane", "doe", "123-456-999"));
- JSONObject jsonObject =
+ JsonNode jsonObject =
restClient.doGetAndAssert("/regionA/keys").statusIsOk().getJsonObject();
assertThat(jsonObject.get("keys").toString()).isEqualTo("[\"customer1\",\"customer2\"]");
}
@Test
- public void adhocQuery() throws UnsupportedEncodingException {
+ public void adhocQuery() throws Exception {
restClient.doPutAndAssert("/regionA/1", DOCUMENT).statusIsOk();
restClient.doPutAndAssert("/regionA/2", DOCUMENT1).statusIsOk();
restClient.doPutAndAssert("/regionA/3", DOCUMENT2).statusIsOk();
@@ -271,13 +270,13 @@ public class RestRegionAPIDUnitTest {
"SELECT book.displayprice FROM /regionA e, e.store.book book
WHERE book.displayprice > 5",
"UTF-8");
-
restClient.doGetAndAssert(urlPrefix).statusIsOk().hasJsonArray().hasSize(12)
+
restClient.doGetAndAssert(urlPrefix).statusIsOk().hasJsonArrayOfDoubles().hasSize(12)
.containsExactlyInAnyOrder(18.95, 18.95, 8.99, 8.99, 8.99, 8.95,
22.99, 22.99, 22.99, 12.99,
112.99, 112.99);
}
@Test
- public void preparedQuery() throws UnsupportedEncodingException {
+ public void preparedQuery() throws Exception {
restClient.doPutAndAssert("/regionA/1", DOCUMENT).statusIsOk();
restClient.doPutAndAssert("/regionA/2", DOCUMENT1).statusIsOk();
restClient.doPutAndAssert("/regionA/3", DOCUMENT2).statusIsOk();
@@ -291,15 +290,14 @@ public class RestRegionAPIDUnitTest {
}
// get the list of defined queries and verify the size of them
- JSONObject jsonObject =
restClient.doGetAndAssert("/queries").statusIsOk().getJsonObject();
- JSONArray queriesArray = (JSONArray) jsonObject.get("queries");
- assertThat(queriesArray.length()).isEqualTo(5);
+ JsonNode jsonObject =
restClient.doGetAndAssert("/queries").statusIsOk().getJsonObject();
+ assertThat(jsonObject.get("queries").size()).isEqualTo(5);
// execute each defined queries
for (int i = 0; i < 5; i++) {
restClient.doPostAndAssert("/queries/Query" + i,
"[{\"@type\":\"double\",\"@value\":8.99}]")
.statusIsOk()
- .hasJsonArray().hasSize(8)
+ .hasJsonArrayOfDoubles().hasSize(8)
.containsExactlyInAnyOrder(18.95, 18.95, 22.99, 22.99, 22.99, 12.99,
112.99, 112.99);
}
}
@@ -366,12 +364,13 @@ public class RestRegionAPIDUnitTest {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URI uri = classLoader.getResource("sampleJson.json").toURI();
- String json = IOUtils.toString(uri, Charset.defaultCharset());
+ String rawJson = IOUtils.toString(uri, Charset.defaultCharset());
- JSONArray jsonArray = new JSONArray(json);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode json = mapper.readTree(rawJson);
- for (int i = 0; i < jsonArray.length(); i++) {
- jsonDocuments.add(jsonArray.get(i).toString());
+ for (int i = 0; i < json.size(); i++) {
+ jsonDocuments.add(json.get(i).toString());
}
}
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityIntegrationTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityIntegrationTest.java
index 28a8414..f2d1e52 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityIntegrationTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityIntegrationTest.java
@@ -19,8 +19,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import org.json.JSONArray;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.JsonNode;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
@@ -171,16 +170,17 @@ public class RestSecurityIntegrationTest {
*/
@Test
public void getRegions() throws Exception {
- JSONObject jsonObject = assertResponse(restClient.doGet("", "dataRead",
"dataRead"))
+ JsonNode jsonObject = assertResponse(restClient.doGet("", "dataRead",
"dataRead"))
.hasStatusCode(200).hasContentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.getJsonObject();
- JSONArray regions = jsonObject.getJSONArray("regions");
+ JsonNode regions = jsonObject.get("regions");
assertNotNull(regions);
- assertTrue(regions.length() > 0);
- JSONObject region = regions.getJSONObject(0);
- assertEquals("AuthRegion", region.get("name"));
- assertEquals("REPLICATE", region.get("type"));
+ assertTrue(regions.size() > 0);
+
+ JsonNode region = regions.get(0);
+ assertEquals("AuthRegion", region.get("name").asText());
+ assertEquals("REPLICATE", region.get("type").asText());
// List regions with an unknown user - 401
assertResponse(restClient.doGet("", "user", "wrongPswd"))
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityPostProcessorTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityPostProcessorTest.java
index 2f69feb..da10944 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityPostProcessorTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestSecurityPostProcessorTest.java
@@ -22,8 +22,7 @@ import static org.junit.Assert.assertTrue;
import java.net.URLEncoder;
-import org.json.JSONArray;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.JsonNode;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
@@ -76,58 +75,58 @@ public class RestSecurityPostProcessorTest {
@Test
public void getRegionKey() throws Exception {
// Test a single key
- JSONObject jsonObject =
+ JsonNode jsonNode =
assertResponse(restClient.doGet("/customers/1", "dataReader",
"1234567"))
.hasStatusCode(200)
.hasContentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.getJsonObject();
- assertEquals("*********", jsonObject.getString("socialSecurityNumber"));
- assertEquals(1L, jsonObject.getLong("customerId"));
+ assertEquals("*********", jsonNode.get("socialSecurityNumber").asText());
+ assertEquals(1L, jsonNode.get("customerId").asLong());
// Try with super-user
- jsonObject =
+ jsonNode =
assertResponse(restClient.doGet("/customers/1", "super-user",
"1234567"))
.hasStatusCode(200)
.hasContentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.getJsonObject();
- assertEquals("555555555", jsonObject.getString("socialSecurityNumber"));
- assertEquals(1L, jsonObject.getLong("customerId"));
+ assertEquals("555555555", jsonNode.get("socialSecurityNumber").asText());
+ assertEquals(1L, jsonNode.get("customerId").asLong());
}
// Test multiple keys
@Test
public void getMultipleRegionKeys() throws Exception {
- JSONObject jsonObject =
+ JsonNode jsonNode =
assertResponse(restClient.doGet("/customers/1,3", "dataReader",
"1234567"))
.hasStatusCode(200)
.hasContentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.getJsonObject();
- JSONArray jsonArray = jsonObject.getJSONArray("customers");
- final int length = jsonArray.length();
+ JsonNode customers = jsonNode.get("customers");
+ final int length = customers.size();
assertEquals(2, length);
- JSONObject customer = jsonArray.getJSONObject(0);
- assertEquals("*********", customer.getString("socialSecurityNumber"));
- assertEquals(1, customer.getLong("customerId"));
- customer = jsonArray.getJSONObject(1);
- assertEquals("*********", customer.getString("socialSecurityNumber"));
- assertEquals(3, customer.getLong("customerId"));
+ JsonNode customer = customers.get(0);
+ assertEquals("*********", customer.get("socialSecurityNumber").asText());
+ assertEquals(1, customer.get("customerId").asLong());
+ customer = customers.get(1);
+ assertEquals("*********", customer.get("socialSecurityNumber").asText());
+ assertEquals(3, customer.get("customerId").asLong());
}
@Test
public void getRegion() throws Exception {
- JSONObject jsonObject = assertResponse(restClient.doGet("/customers",
"dataReader", "1234567"))
+ JsonNode jsonNode = assertResponse(restClient.doGet("/customers",
"dataReader", "1234567"))
.hasStatusCode(200)
.hasContentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.getJsonObject();
- JSONArray jsonArray = jsonObject.getJSONArray("customers");
- final int length = jsonArray.length();
+ JsonNode customers = jsonNode.get("customers");
+ final int length = customers.size();
for (int index = 0; index < length; ++index) {
- JSONObject customer = jsonArray.getJSONObject(index);
- assertEquals("*********", customer.getString("socialSecurityNumber"));
- assertEquals((long) index + 1, customer.getLong("customerId"));
+ JsonNode customer = customers.get(index);
+ assertEquals("*********", customer.get("socialSecurityNumber").asText());
+ assertEquals((long) index + 1, customer.get("customerId").asLong());
}
}
@@ -135,16 +134,16 @@ public class RestSecurityPostProcessorTest {
public void adhocQuery() throws Exception {
String query = "/queries/adhoc?q="
+ URLEncoder.encode("SELECT * FROM /customers order by customerId",
"UTF-8");
- JSONArray jsonArray = assertResponse(restClient.doGet(query, "dataReader",
"1234567"))
+ JsonNode jsonArray = assertResponse(restClient.doGet(query, "dataReader",
"1234567"))
.hasStatusCode(200)
.hasContentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
- .getJsonArray();
+ .getJsonObject();
- final int length = jsonArray.length();
+ final int length = jsonArray.size();
for (int index = 0; index < length; ++index) {
- JSONObject customer = jsonArray.getJSONObject(index);
- assertEquals("*********", customer.getString("socialSecurityNumber"));
- assertEquals((long) index + 1, customer.getLong("customerId"));
+ JsonNode customer = jsonArray.get(index);
+ assertEquals("*********", customer.get("socialSecurityNumber").asText());
+ assertEquals((long) index + 1, customer.get("customerId").asLong());
}
}
@@ -166,15 +165,15 @@ public class RestSecurityPostProcessorTest {
.hasContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
// Execute the query
- JSONArray jsonArray =
+ JsonNode jsonArray =
assertResponse(restClient.doPost("/queries/selectCustomer",
"dataReader", "1234567",
"{" + "\"@type\": \"int\"," + "\"@value\": 1" + "}"))
.hasStatusCode(200)
- .getJsonArray();
+ .getJsonObject();
- assertTrue(jsonArray.length() == 1);
- JSONObject customer = jsonArray.getJSONObject(0);
- assertEquals("*********", customer.getString("socialSecurityNumber"));
- assertEquals(1L, customer.getLong("customerId"));
+ assertTrue(jsonArray.size() == 1);
+ JsonNode customer = jsonArray.get(0);
+ assertEquals("*********", customer.get("socialSecurityNumber").asText());
+ assertEquals(1L, customer.get("customerId").asLong());
}
}
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestServersIntegrationTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestServersIntegrationTest.java
index b0af99d..b703d55 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestServersIntegrationTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/RestServersIntegrationTest.java
@@ -19,8 +19,8 @@ import static
org.apache.geode.test.junit.rules.HttpResponseAssert.assertRespons
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assume.assumeTrue;
+import com.fasterxml.jackson.databind.JsonNode;
import org.apache.http.HttpStatus;
-import org.json.JSONArray;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
@@ -64,9 +64,10 @@ public class RestServersIntegrationTest {
@Test
public void testServerStartedOnDefaultPort() throws Exception {
- JSONArray jsonArray = assertResponse(restClient.doGet("/servers", null,
null))
- .hasStatusCode(HttpStatus.SC_OK).getJsonArray();
- assertThat(jsonArray.length()).isEqualTo(1);
- assertThat(jsonArray.getString(0)).isEqualTo("http://localhost:" +
DEFAULT_HTTP_SERVICE_PORT);
+ JsonNode jsonArray = assertResponse(restClient.doGet("/servers", null,
null))
+ .hasStatusCode(HttpStatus.SC_OK).getJsonObject();
+ assertThat(jsonArray.size()).isEqualTo(1);
+ assertThat(jsonArray.get(0).asText())
+ .isEqualTo("http://localhost:" + DEFAULT_HTTP_SERVICE_PORT);
}
}
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/SwaggerVerificationIntegrationTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/SwaggerVerificationIntegrationTest.java
index 4631534..f62e79d 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/SwaggerVerificationIntegrationTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/SwaggerVerificationIntegrationTest.java
@@ -19,7 +19,7 @@ import static
org.apache.geode.test.junit.rules.HttpResponseAssert.assertRespons
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.JsonNode;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
@@ -53,19 +53,19 @@ public class SwaggerVerificationIntegrationTest {
assertResponse(client.get("/geode/swagger-ui.html")).hasStatusCode(200);
// Check the JSON
- JSONObject json =
+ JsonNode json =
assertResponse(client.get("/geode/v2/api-docs")).hasStatusCode(200).getJsonObject();
- assertThat(json.get("swagger"), is("2.0"));
+ assertThat(json.get("swagger").asText(), is("2.0"));
- JSONObject info = json.getJSONObject("info");
- assertThat(info.getString("description"),
+ JsonNode info = json.get("info");
+ assertThat(info.get("description").asText(),
is(LocalizedStrings.SwaggerConfig_DESCRIPTOR.toLocalizedString()));
- assertThat(info.getString("title"),
+ assertThat(info.get("title").asText(),
is(LocalizedStrings.SwaggerConfig_VENDOR_PRODUCT_LINE.toLocalizedString()));
- JSONObject license = info.getJSONObject("license");
- assertThat(license.getString("name"), is("Apache License, version 2.0"));
- assertThat(license.getString("url"),
is("http://www.apache.org/licenses/"));
+ JsonNode license = info.get("license");
+ assertThat(license.get("name").asText(), is("Apache License, version
2.0"));
+ assertThat(license.get("url").asText(),
is("http://www.apache.org/licenses/"));
}
}
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPITestBase.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPITestBase.java
index 1251455..7e33ea4 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPITestBase.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPITestBase.java
@@ -21,6 +21,7 @@ import static
org.apache.geode.distributed.ConfigurationProperties.START_DEV_RES
import static org.apache.geode.test.dunit.Assert.assertEquals;
import static org.apache.geode.test.dunit.Assert.assertNotNull;
import static org.apache.geode.test.dunit.Assert.fail;
+import static org.assertj.core.api.Assertions.assertThat;
import java.io.BufferedReader;
import java.io.IOException;
@@ -31,6 +32,8 @@ import java.util.List;
import java.util.Properties;
import java.util.Random;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
@@ -40,7 +43,6 @@ import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
-import org.json.JSONArray;
import org.junit.experimental.categories.Category;
import org.apache.geode.cache.Cache;
@@ -192,8 +194,17 @@ class RestAPITestBase extends JUnit4DistributedTestCase {
response.close();
System.out.println("Response : " + httpResponseString);
// verify function execution result
- JSONArray resultArray = new JSONArray(httpResponseString);
- assertEquals(expectedServerResponses, resultArray.length());
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode json = mapper.readTree(httpResponseString);
+
+ if (json.isArray()) {
+ assertEquals(expectedServerResponses, json.size());
+ } else {
+ assertThat(expectedServerResponses)
+ .as("Did not receive an expected JSON array. Instead, received a
%s type.",
+ json.getNodeType().name())
+ .isEqualTo(0);
+ }
} catch (Exception e) {
// fail("exception", e);
}
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java
index 5bebd41..2a1ecf7 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsAndInterOpsDUnitTest.java
@@ -35,6 +35,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
@@ -44,8 +46,6 @@ import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
-import org.json.JSONArray;
-import org.json.JSONObject;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
@@ -307,11 +307,12 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
// Check whether received response contains expected query IDs.
- JSONObject jsonObject = new JSONObject(sb.toString());
- JSONArray jsonArray = jsonObject.getJSONArray("queries");
- for (int i = 0; i < jsonArray.length(); i++) {
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode jsonObject = mapper.readTree(sb.toString());
+ JsonNode queries = jsonObject.get("queries");
+ for (int i = 0; i < queries.size(); i++) {
assertThat(Arrays.asList(PARAM_QUERY_IDS_ARRAY))
- .contains(jsonArray.getJSONObject(i).getString("id"));
+ .contains(queries.get(i).get("id").asText());
}
// Query TestCase-3 :: Run the specified named query passing in scalar
values for query
@@ -444,10 +445,11 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
// validate the status code
assertThat(response.getStatusLine().getStatusCode()).isEqualTo(200);
- JSONArray jsonArray = new JSONArray(sb.toString());
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode jsonArray = mapper.readTree(sb.toString());
// verify total number of REST service endpoints in DS
- assertThat(jsonArray.length()).isEqualTo(2);
+ assertThat(jsonArray.size()).isEqualTo(2);
}
private void doGetsUsingRestApis(String restEndpoint) throws IOException {
@@ -468,13 +470,14 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
sb.append(line);
}
- JSONObject jObject = new JSONObject(sb.toString());
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode json = mapper.readTree(sb.toString());
- assertThat(jObject.get("id")).isEqualTo(101);
- assertThat(jObject.get("firstName")).isEqualTo("Mithali");
- assertThat(jObject.get("middleName")).isEqualTo("Dorai");
- assertThat(jObject.get("lastName")).isEqualTo("Raj");
- assertThat(jObject.get("gender")).isEqualTo(Gender.FEMALE.name());
+ assertThat(json.get("id").asInt()).isEqualTo(101);
+ assertThat(json.get("firstName").asText()).isEqualTo("Mithali");
+ assertThat(json.get("middleName").asText()).isEqualTo("Dorai");
+ assertThat(json.get("lastName").asText()).isEqualTo("Raj");
+ assertThat(json.get("gender").asText()).isEqualTo(Gender.FEMALE.name());
// 2. Get on key="16" and validate result.
@@ -492,13 +495,13 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
sb.append(line);
}
- jObject = new JSONObject(sb.toString());
+ json = mapper.readTree(sb.toString());
- assertThat(jObject.get("id")).isEqualTo(104);
- assertThat(jObject.get("firstName")).isEqualTo("Shila");
- assertThat(jObject.get("middleName")).isEqualTo("kumari");
- assertThat(jObject.get("lastName")).isEqualTo("Dixit");
- assertThat(jObject.get("gender")).isEqualTo(Gender.FEMALE.name());
+ assertThat(json.get("id").asInt()).isEqualTo(104);
+ assertThat(json.get("firstName").asText()).isEqualTo("Shila");
+ assertThat(json.get("middleName").asText()).isEqualTo("kumari");
+ assertThat(json.get("lastName").asText()).isEqualTo("Dixit");
+ assertThat(json.get("gender").asText()).isEqualTo(Gender.FEMALE.name());
// 3. Get all (getAll) entries in Region
@@ -519,9 +522,8 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
}
result.close();
- jObject = new JSONObject(sb.toString());
- JSONArray jArray = jObject.getJSONArray("People");
- assertThat(jArray.length()).isEqualTo(16);
+ json = mapper.readTree(sb.toString());
+ assertThat(json.get("People").size()).isEqualTo(16);
// 4. GetAll?limit=10 (10 entries) and verify results
@@ -541,9 +543,8 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
sb.append(line);
}
- jObject = new JSONObject(sb.toString());
- jArray = jObject.getJSONArray("People");
- assertThat(jArray.length()).isEqualTo(10);
+ json = mapper.readTree(sb.toString());
+ assertThat(json.get("People").size()).isEqualTo(10);
// 5. Get keys - List all keys in region
@@ -563,9 +564,8 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
sb.append(line);
}
- jObject = new JSONObject(sb.toString());
- jArray = jObject.getJSONArray("keys");
- assertThat(jArray.length()).isEqualTo(16);
+ json = mapper.readTree(sb.toString());
+ assertThat(json.get("keys").size()).isEqualTo(16);
// 6. Get data for specific keys
@@ -585,9 +585,8 @@ public class RestAPIsAndInterOpsDUnitTest extends
LocatorTestBase {
sb.append(line);
}
- jObject = new JSONObject(sb.toString());
- jArray = jObject.getJSONArray("People");
- assertThat(jArray.length()).isEqualTo(6);
+ json = mapper.readTree(sb.toString());
+ assertThat(json.get("People").size()).isEqualTo(6);
}
private void createRegionInClientCache() {
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java
index d5d0efd..3a18c87 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestAPIsWithSSLDUnitTest.java
@@ -71,6 +71,8 @@ import java.util.Properties;
import javax.net.ssl.SSLContext;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -81,7 +83,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
-import org.json.JSONObject;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@@ -500,13 +501,14 @@ public class RestAPIsWithSSLDUnitTest extends
LocatorTestBase {
str.append(line);
}
- JSONObject jObject = new JSONObject(str.toString());
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode json = mapper.readTree(str.toString());
- assertEquals(jObject.get("id"), 101);
- assertEquals(jObject.get("firstName"), "Mithali");
- assertEquals(jObject.get("middleName"), "Dorai");
- assertEquals(jObject.get("lastName"), "Raj");
- assertEquals(jObject.get("gender"), Gender.FEMALE.name());
+ assertEquals(json.get("id").asInt(), 101);
+ assertEquals(json.get("firstName").asText(), "Mithali");
+ assertEquals(json.get("middleName").asText(), "Dorai");
+ assertEquals(json.get("lastName").asText(), "Raj");
+ assertEquals(json.get("gender").asText(), Gender.FEMALE.name());
}
} catch (Exception e) {
diff --git
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestQueryAndFunctionIntegrationTest.java
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestQueryAndFunctionIntegrationTest.java
index 6cbb5d6..6dd65b5 100644
---
a/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestQueryAndFunctionIntegrationTest.java
+++
b/geode-assembly/src/test/java/org/apache/geode/rest/internal/web/controllers/RestQueryAndFunctionIntegrationTest.java
@@ -31,9 +31,8 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
-import org.json.JSONArray;
-import org.json.JSONException;
-import org.json.JSONObject;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
@@ -688,13 +687,11 @@ public class RestQueryAndFunctionIntegrationTest {
throw new AssertionError(message, cause);
}
- private void validateGetAllResult(int index, ResponseEntity<String> result) {
+ private void validateGetAllResult(int index, ResponseEntity<String> result)
throws Exception {
if (index == 27 || index == 29 || index == 30) {
- try {
- new JSONObject(result.getBody()).getJSONArray("customers");
- } catch (JSONException e) {
- caught("Caught JSONException in validateGetAllResult :: " +
e.getMessage(), e);
- }
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode json = mapper.readTree(result.getBody());
+ assertTrue(json.get("customers").isArray());
}
}
@@ -716,25 +713,26 @@ public class RestQueryAndFunctionIntegrationTest {
if (index == 45) {
try {
- JSONObject jsonObject = new JSONObject(result.getBody());
- JSONArray jsonArray = new
JSONArray(jsonObject.get("queries").toString());
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode jsonObject = mapper.readTree(result.getBody());
+ JsonNode queries = jsonObject.get("queries");
- for (int i = 0; i < jsonArray.length(); i++) {
+ for (int i = 0; i < queries.size(); i++) {
assertTrue("PREPARE_PARAMETERIZED_QUERY: function IDs are not
matched",
-
queryResult.getResult().contains(jsonArray.getJSONObject(i).getString("id")));
+
queryResult.getResult().contains(queries.get(i).get("id").asText()));
}
- } catch (JSONException e) {
- caught("Caught JSONException in validateQueryResult :: " +
e.getMessage(), e);
+ } catch (Exception e) {
+ caught("Caught Exception in validateQueryResult :: " +
e.getMessage(), e);
}
} else if (index == 46 || index == 47 || index == 48) {
- JSONArray jsonArray;
try {
- jsonArray = new JSONArray(result.getBody());
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode json = mapper.readTree(result.getBody());
// verify query result size
- assertEquals(queryResult.getResultSize(), jsonArray.length());
- } catch (JSONException e) {
- caught("Caught JSONException in validateQueryResult :: " +
e.getMessage(), e);
+ assertEquals(queryResult.getResultSize(), json.size());
+ } catch (Exception e) {
+ caught("Caught Exception in validateQueryResult :: " +
e.getMessage(), e);
}
}
}
diff --git
a/geode-assembly/src/test/java/org/apache/geode/test/junit/rules/HttpResponseAssert.java
b/geode-assembly/src/test/java/org/apache/geode/test/junit/rules/HttpResponseAssert.java
index 1005579..eaa3d7a 100644
---
a/geode-assembly/src/test/java/org/apache/geode/test/junit/rules/HttpResponseAssert.java
+++
b/geode-assembly/src/test/java/org/apache/geode/test/junit/rules/HttpResponseAssert.java
@@ -21,15 +21,14 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.logging.log4j.Logger;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractCharSequenceAssert;
import org.assertj.core.api.ListAssert;
-import org.json.JSONArray;
-import org.json.JSONObject;
-import org.json.JSONTokener;
import org.apache.geode.internal.logging.LogService;
import org.apache.geode.internal.util.ArrayUtils;
@@ -87,23 +86,17 @@ public class HttpResponseAssert extends
AbstractAssert<HttpResponseAssert, HttpR
return this;
}
- public JSONObject getJsonObject() {
- JSONTokener tokener = new JSONTokener(responseBody);
- return new JSONObject(tokener);
+ public JsonNode getJsonObject() throws IOException {
+ ObjectMapper mapper = new ObjectMapper();
+ return mapper.readTree(responseBody);
}
- public JSONArray getJsonArray() {
- JSONTokener tokener = new JSONTokener(responseBody);
- JSONArray array = new JSONArray(tokener);
- return array;
- }
+ public ListAssert<Double> hasJsonArrayOfDoubles() throws IOException {
+ JsonNode array = getJsonObject();
+ List<Double> list = new ArrayList<>();
- public ListAssert<Object> hasJsonArray() {
- JSONTokener tokener = new JSONTokener(responseBody);
- JSONArray array = new JSONArray(tokener);
- List<Object> list = new ArrayList<>();
- for (int i = 0; i < array.length(); i++) {
- list.add(array.get(i));
+ for (int i = 0; i < array.size(); i++) {
+ list.add(array.get(i).doubleValue());
}
return assertThat(list);
}