You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by pa...@apache.org on 2017/05/30 08:41:19 UTC

svn commit: r1796802 - in /sling/trunk/testing/http/clients: pom.xml src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java

Author: pauls
Date: Tue May 30 08:41:19 2017
New Revision: 1796802

URL: http://svn.apache.org/viewvc?rev=1796802&view=rev
Log:
SLING-6905: Remove commons.json from testing http clients by using already used jackson library. Patch provided by Valentin Olteanu. This closes #235.

Modified:
    sling/trunk/testing/http/clients/pom.xml
    sling/trunk/testing/http/clients/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java

Modified: sling/trunk/testing/http/clients/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/http/clients/pom.xml?rev=1796802&r1=1796801&r2=1796802&view=diff
==============================================================================
--- sling/trunk/testing/http/clients/pom.xml (original)
+++ sling/trunk/testing/http/clients/pom.xml Tue May 30 08:41:19 2017
@@ -131,11 +131,6 @@
             <artifactId>org.apache.sling.xss</artifactId>
             <version>1.0.4</version>
         </dependency>
-        <dependency>
-            <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.commons.json</artifactId>
-            <version>2.0.16</version>
-        </dependency>
 
         <!-- For tests -->
         <dependency>

Modified: sling/trunk/testing/http/clients/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/http/clients/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java?rev=1796802&r1=1796801&r2=1796802&view=diff
==============================================================================
--- sling/trunk/testing/http/clients/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java (original)
+++ sling/trunk/testing/http/clients/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java Tue May 30 08:41:19 2017
@@ -20,9 +20,6 @@ package org.apache.sling.testing.clients
 import org.apache.http.Header;
 import org.apache.http.entity.mime.MultipartEntityBuilder;
 import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.sling.commons.json.JSONArray;
-import org.apache.sling.commons.json.JSONException;
-import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.testing.clients.ClientException;
 import org.apache.sling.testing.clients.SlingClient;
 import org.apache.sling.testing.clients.SlingClientConfig;
@@ -511,12 +508,14 @@ public class OsgiConsoleClient extends S
      * @throws ClientException if the id cannot be retrieved
      */
     public long getBundleId(String symbolicName) throws ClientException {
-        final JSONObject bundle = getBundleData(symbolicName);
-        try {
-            return bundle.getLong(JSON_KEY_ID);
-        } catch (JSONException e) {
-            throw new ClientException("Cannot get id from json", e);
+        final JsonNode bundle = getBundleData(symbolicName);
+        final JsonNode idNode = bundle.get(JSON_KEY_ID);
+
+        if (idNode == null) {
+            throw new ClientException("Cannot get id from bundle json");
         }
+
+        return idNode.getLongValue();
     }
 
     /**
@@ -526,12 +525,14 @@ public class OsgiConsoleClient extends S
      * @throws ClientException
      */
     public String getBundleVersion(String symbolicName) throws ClientException {
-        final JSONObject bundle = getBundleData(symbolicName);
-        try {
-            return bundle.getString(JSON_KEY_VERSION);
-        } catch (JSONException e) {
-            throw new ClientException("Cannot get version from json", e);
+        final JsonNode bundle = getBundleData(symbolicName);
+        final JsonNode versionNode = bundle.get(JSON_KEY_VERSION);
+
+        if (versionNode == null) {
+            throw new ClientException("Cannot get version from bundle json");
         }
+
+        return versionNode.getTextValue();
     }
 
     /**
@@ -541,12 +542,14 @@ public class OsgiConsoleClient extends S
      * @throws ClientException if the state cannot be retrieved
      */
     public String getBundleState(String symbolicName) throws ClientException {
-        final JSONObject bundle = getBundleData(symbolicName);
-        try {
-            return bundle.getString(JSON_KEY_STATE);
-        } catch (JSONException e) {
-            throw new ClientException("Cannot get state from json", e);
+        final JsonNode bundle = getBundleData(symbolicName);
+        final JsonNode stateNode = bundle.get(JSON_KEY_STATE);
+
+        if (stateNode == null) {
+            throw new ClientException("Cannot get state from bundle json");
         }
+
+        return stateNode.getTextValue();
     }
 
     /**
@@ -635,31 +638,26 @@ public class OsgiConsoleClient extends S
      *   }]
      * }
      */
-    private JSONObject getBundleData(String symbolicName) throws ClientException {
+    private JsonNode getBundleData(String symbolicName) throws ClientException {
         final String path = getBundlePath(symbolicName, ".json");
         final String content = this.doGet(path, SC_OK).getContent();
+        final JsonNode root = JsonUtils.getJsonNodeFromString(content);
 
-        try {
-            final JSONObject root = new JSONObject(content);
-
-            if (!root.has(JSON_KEY_DATA)) {
-                throw new ClientException(path + " does not provide '" + JSON_KEY_DATA + "' element, JSON content=" + content);
-            }
-
-            final JSONArray data = root.getJSONArray(JSON_KEY_DATA);
-            if (data.length() < 1) {
-                throw new ClientException(path + "." + JSON_KEY_DATA + " is empty, JSON content=" + content);
-            }
+        if (root.get(JSON_KEY_DATA) == null) {
+            throw new ClientException(path + " does not provide '" + JSON_KEY_DATA + "' element, JSON content=" + content);
+        }
 
-            final JSONObject bundle = data.getJSONObject(0);
-            if (!bundle.has(JSON_KEY_STATE)) {
-                throw new ClientException(path + ".data[0].state missing, JSON content=" + content);
-            }
+        Iterator<JsonNode> data = root.get(JSON_KEY_DATA).getElements();
+        if (!data.hasNext()) {
+            throw new ClientException(path + "." + JSON_KEY_DATA + " is empty, JSON content=" + content);
+        }
 
-            return bundle;
-        } catch (JSONException e) {
-            throw new ClientException("Cannot get json", e);
+        final JsonNode bundle = data.next();
+        if (bundle.get(JSON_KEY_STATE) == null) {
+            throw new ClientException(path + ".data[0].state missing, JSON content=" + content);
         }
+
+        return bundle;
     }
 
     //