This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.testing.clients-1.1.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-testing-clients.git
commit e3911b9bc0d0f0d4afdb7d8fcf30734375b565e5 Author: Karl Pauls <[email protected]> AuthorDate: Tue May 30 08:41:19 2017 +0000 SLING-6905: Remove commons.json from testing http clients by using already used jackson library. Patch provided by Valentin Olteanu. This closes #235. git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/testing/http/clients@1796802 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 5 -- .../testing/clients/osgi/OsgiConsoleClient.java | 72 +++++++++++----------- 2 files changed, 35 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index be389da..1b42b85 100644 --- a/pom.xml +++ b/pom.xml @@ -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> diff --git a/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java b/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java index 8dd8ca2..1253d76 100644 --- a/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java +++ b/src/main/java/org/apache/sling/testing/clients/osgi/OsgiConsoleClient.java @@ -20,9 +20,6 @@ package org.apache.sling.testing.clients.osgi; 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 SlingClient { * @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 SlingClient { * @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 SlingClient { * @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 SlingClient { * }] * } */ - 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; } // -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
