You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2020/04/21 14:16:07 UTC

[sling-whiteboard] branch master updated: Use jsonpath for testing

This is an automated email from the ASF dual-hosted git repository.

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git


The following commit(s) were added to refs/heads/master by this push:
     new 435983c  Use jsonpath for testing
435983c is described below

commit 435983cdd06887d67863a8322b762c6c18593a09
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Tue Apr 21 16:15:53 2020 +0200

    Use jsonpath for testing
---
 graphql-scripting/pom.xml                          | 26 +++++++++++++++++++++-
 .../gql/engine/GraphQLResourceQueryTest.java       | 17 ++++++++++----
 .../sling/scripting/graphql/it/BasicContentIT.java | 11 +++++----
 .../graphql/it/GraphQLScriptingTestSupport.java    | 13 +++++++++++
 .../scripting/graphql/it/GraphQLServletIT.java     | 11 +++++----
 5 files changed, 65 insertions(+), 13 deletions(-)

diff --git a/graphql-scripting/pom.xml b/graphql-scripting/pom.xml
index 9a7d790..1451885 100644
--- a/graphql-scripting/pom.xml
+++ b/graphql-scripting/pom.xml
@@ -40,7 +40,7 @@
 
     <!-- GSON requires Java 11 - we might use something else later -->
     <sling.java.version>11</sling.java.version>
-    <org.ops4j.pax.exam.version>4.13.1</org.ops4j.pax.exam.version>
+    <org.ops4j.pax.exam.version>4.13.3</org.ops4j.pax.exam.version>
 
     <!-- To debug the pax process, override this with -D -->
     <pax.vm.options>-Xmx512M</pax.vm.options>
@@ -237,6 +237,30 @@
       <version>${org.ops4j.pax.exam.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.ops4j.pax.url</groupId>
+      <artifactId>pax-url-wrap</artifactId>
+      <version>2.3.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.jayway.jsonpath</groupId>
+      <artifactId>json-path</artifactId>
+      <version>2.4.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>com.jayway.jsonpath</groupId>
+      <artifactId>json-path-assert</artifactId>
+      <version>2.2.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.servicemix.bundles</groupId>
+      <artifactId>org.apache.servicemix.bundles.hamcrest</artifactId>
+      <version>1.3_1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/gql/engine/GraphQLResourceQueryTest.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/gql/engine/GraphQLResourceQueryTest.java
index 4e89cd3..acc18f2 100644
--- a/graphql-scripting/src/test/java/org/apache/sling/scripting/gql/engine/GraphQLResourceQueryTest.java
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/gql/engine/GraphQLResourceQueryTest.java
@@ -18,11 +18,18 @@
  */
 package org.apache.sling.scripting.gql.engine;
 
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
 import static org.junit.Assert.fail;
 
+import static org.hamcrest.Matchers.equalTo;
+
 import java.util.UUID;
 
+import com.google.gson.Gson;
+
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.scripting.gql.schema.GraphQLSchemaProvider;
 import org.junit.Test;
@@ -44,11 +51,13 @@ public class GraphQLResourceQueryTest {
         final GraphQLResourceQuery q = new GraphQLResourceQuery();
         final ExecutionResult result = q.executeQuery(schemaProvider, r, "{ currentResource { path resourceType } }");
 
-        if(!result.getErrors().isEmpty()) {
+        if (!result.getErrors().isEmpty()) {
             fail("Errors:" + result.getErrors());
         }
-        // TODO brittle test...
-        final String expected = "{currentResource={path=" + path + ", resourceType=" + resourceType + "}}";
-        assertEquals(expected, result.getData().toString());
+
+        final String json = new Gson().toJson(result);
+        assertThat(json, hasJsonPath("$.data.currentResource"));
+        assertThat(json, hasJsonPath("$.data.currentResource.path", equalTo(path)));
+        assertThat(json, hasJsonPath("$.data.currentResource.resourceType", equalTo(resourceType)));
     }
 }
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java
index fd7777c..a652340 100644
--- a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/BasicContentIT.java
@@ -20,6 +20,8 @@ package org.apache.sling.scripting.graphql.it;
 
 import javax.inject.Inject;
 
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+
 import org.apache.sling.resource.presence.ResourcePresence;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -30,7 +32,8 @@ import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
 import org.ops4j.pax.exam.util.Filter;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
 import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.factoryConfiguration;
 
 @RunWith(PaxExam.class)
@@ -55,8 +58,8 @@ public class BasicContentIT extends GraphQLScriptingTestSupport {
     public void testJsonContent() throws Exception {
         final String path = "/graphql/one";
         final String json = getContent(path + ".json");
-        // TODO we should really parse this..or run detailed tests in unit tests, and just the basics here
-        final String expected = "{\"currentResource\":{\"path\":\"/content/graphql/one\",\"resourceType\":\"graphql/test/one\"}}";
-        assertEquals(expected, json);
+        assertThat(json, hasJsonPath("$.currentResource"));
+        assertThat(json, hasJsonPath("$.currentResource.path", equalTo("/content/graphql/one")));
+        assertThat(json, hasJsonPath("$.currentResource.resourceType", equalTo("graphql/test/one")));
     }
 }
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptingTestSupport.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptingTestSupport.java
index 3c4c267..8f6a629 100644
--- a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptingTestSupport.java
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLScriptingTestSupport.java
@@ -48,6 +48,7 @@ import static org.ops4j.pax.exam.CoreOptions.junitBundles;
 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
 import static org.ops4j.pax.exam.CoreOptions.when;
 import static org.ops4j.pax.exam.CoreOptions.vmOption;
+import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
 import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.newConfiguration;
 
 import java.util.ArrayList;
@@ -84,6 +85,7 @@ public abstract class GraphQLScriptingTestSupport extends TestSupport {
             mavenBundle().groupId("org.apache.sling").artifactId("org.apache.sling.servlet-helpers").versionAsInProject(),
             mavenBundle().groupId("com.google.code.gson").artifactId("gson").versionAsInProject(),
             slingResourcePresence(),
+            jsonPath(),
             junitBundles()
         );
     }
@@ -104,6 +106,17 @@ public abstract class GraphQLScriptingTestSupport extends TestSupport {
         );
     }
 
+    protected Option jsonPath() {
+        return composite(
+            mavenBundle().groupId("com.jayway.jsonpath").artifactId("json-path").versionAsInProject(),
+            mavenBundle().groupId("net.minidev").artifactId("json-smart").versionAsInProject(),
+            mavenBundle().groupId("net.minidev").artifactId("accessors-smart").versionAsInProject(),
+            mavenBundle().groupId("org.ow2.asm").artifactId("asm").versionAsInProject(),
+            mavenBundle().groupId("com.jayway.jsonpath").artifactId("json-path-assert").versionAsInProject(),
+            mavenBundle().groupId("org.apache.servicemix.bundles").artifactId("org.apache.servicemix.bundles.hamcrest").versionAsInProject()
+        );
+    }
+
     protected Option graphQLJava() {
         return composite(
             mavenBundle().groupId("com.graphql-java").artifactId("graphql-java").versionAsInProject(),
diff --git a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java
index 9d25beb..f3d5f72 100644
--- a/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java
+++ b/graphql-scripting/src/test/java/org/apache/sling/scripting/graphql/it/GraphQLServletIT.java
@@ -20,6 +20,8 @@ package org.apache.sling.scripting.graphql.it;
 
 import javax.inject.Inject;
 
+import static com.jayway.jsonpath.matchers.JsonPathMatchers.hasJsonPath;
+
 import org.apache.sling.resource.presence.ResourcePresence;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -30,7 +32,8 @@ import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
 import org.ops4j.pax.exam.spi.reactors.PerClass;
 import org.ops4j.pax.exam.util.Filter;
 
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
 import static org.ops4j.pax.exam.cm.ConfigurationAdminOptions.factoryConfiguration;
 
 @RunWith(PaxExam.class)
@@ -55,8 +58,8 @@ public class GraphQLServletIT extends GraphQLScriptingTestSupport {
     public void testJsonContent() throws Exception {
         final String path = "/graphql/one";
         final String json = getContent(path + ".gql");
-        // TODO we should really parse this..or run detailed tests in unit tests, and just the basics here
-        final String expected = "{\"currentResource\":{\"path\":\"/content/graphql/one\",\"resourceType\":\"graphql/test/one\"}}";
-        assertEquals(expected, json);
+        assertThat(json, hasJsonPath("$.currentResource"));
+        assertThat(json, hasJsonPath("$.currentResource.path", equalTo("/content/graphql/one")));
+        assertThat(json, hasJsonPath("$.currentResource.resourceType", equalTo("graphql/test/one")));
     }
 }