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 2021/11/11 17:24:41 UTC

[sling-whiteboard] branch master updated: Fix gitignore and add first actual store tests

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 c66c139  Fix gitignore and add first actual store tests
c66c139 is described below

commit c66c13986646f051a3217b3b1ed5ed5bb2d85399
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Thu Nov 11 18:23:27 2021 +0100

    Fix gitignore and add first actual store tests
---
 json-store/.gitignore                              |  2 +-
 json-store/pom.xml                                 | 18 +++++-
 json-store/src/test/java/karate-config.js          | 35 ++++++++++
 .../sling/jsonstore/JsonStoreTestSupport.java      |  3 +-
 .../apache/sling/jsonstore/karate/KarateIT.java    | 33 ++++++++++
 .../sling/jsonstore/karate/createContent.feature   | 74 ++++++++++++++++++++++
 .../sling/jsonstore/karate/schema/invalid.json     |  4 ++
 .../sling/jsonstore/karate/schema/minimal.json     | 19 ++++++
 .../sling/jsonstore/karate/schemaStorage.feature   | 59 +++++++++++++++++
 json-store/src/test/java/util/basic-auth-header.js | 23 +++++++
 json-store/src/test/java/util/cleanup-path.feature | 43 +++++++++++++
 json-store/src/test/java/util/cleanup-paths.js     | 26 ++++++++
 json-store/src/test/java/util/sling-ready.feature  | 38 +++++++++++
 13 files changed, 374 insertions(+), 3 deletions(-)

diff --git a/json-store/.gitignore b/json-store/.gitignore
index fb19434..3a86d90 100644
--- a/json-store/.gitignore
+++ b/json-store/.gitignore
@@ -1 +1 @@
-sling/
+/sling/
diff --git a/json-store/pom.xml b/json-store/pom.xml
index 750d68b..ed1aacd 100644
--- a/json-store/pom.xml
+++ b/json-store/pom.xml
@@ -38,9 +38,19 @@
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
     <project.build.outputTimestamp>1</project.build.outputTimestamp>
     <org.ops4j.pax.exam.version>4.13.3</org.ops4j.pax.exam.version>
+    <karate.version>1.1.0</karate.version>
   </properties>
 
   <build>
+    <testResources>
+      <testResource>
+        <!-- include Karate feature + other files as test resources -->
+        <directory>src/test/java</directory>
+        <excludes>
+          <exclude>**/*.java</exclude>
+        </excludes>
+      </testResource>
+    </testResources>
     <plugins>
       <plugin>
         <groupId>biz.aQute.bnd</groupId>
@@ -219,6 +229,12 @@
       <version>2.3.0</version>
       <scope>test</scope>
     </dependency>
-  </dependencies>
+    <dependency>
+      <groupId>com.intuit.karate</groupId>
+      <artifactId>karate-junit4</artifactId>
+      <version>${karate.version}</version>
+      <scope>test</scope>
+  </dependency>
+</dependencies>
 
 </project>
\ No newline at end of file
diff --git a/json-store/src/test/java/karate-config.js b/json-store/src/test/java/karate-config.js
new file mode 100644
index 0000000..4b3b629
--- /dev/null
+++ b/json-store/src/test/java/karate-config.js
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+function() {    
+  var env = karate.env; // get system property 'karate.env'
+  // karate.log('karate.env system property is', env);
+  if (!env) {
+    env = 'dev';
+  }
+  var port = karate.properties['karate.test.port']
+  karate.log('karate test port is ', port);
+  var config = {
+    env: env,
+    baseURL: 'http://localhost:' + port
+  }
+  karate.log('base URL is ', config.baseURL);
+
+  return config;
+}
\ No newline at end of file
diff --git a/json-store/src/test/java/org/apache/sling/jsonstore/JsonStoreTestSupport.java b/json-store/src/test/java/org/apache/sling/jsonstore/JsonStoreTestSupport.java
index de91f8a..339dfe3 100644
--- a/json-store/src/test/java/org/apache/sling/jsonstore/JsonStoreTestSupport.java
+++ b/json-store/src/test/java/org/apache/sling/jsonstore/JsonStoreTestSupport.java
@@ -27,6 +27,7 @@ import java.util.Base64;
 import org.apache.sling.testing.clients.osgi.OsgiConsoleClient;
 import org.apache.sling.testing.paxexam.TestSupport;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.ClassRule;
 import org.ops4j.pax.exam.Configuration;
 import org.ops4j.pax.exam.Option;
@@ -43,7 +44,7 @@ import static org.apache.sling.testing.paxexam.SlingOptions.webconsole;
 import static org.apache.sling.testing.paxexam.SlingOptions.slingQuickstartOakTar;
 
 public class JsonStoreTestSupport extends TestSupport {
-    private static final int httpPort = findFreePort();
+    protected static final int httpPort = findFreePort();
     private static final String CREDENTIALS = new String(Base64.getEncoder().encode("admin:admin".getBytes()));
 
     @ClassRule
diff --git a/json-store/src/test/java/org/apache/sling/jsonstore/karate/KarateIT.java b/json-store/src/test/java/org/apache/sling/jsonstore/karate/KarateIT.java
new file mode 100644
index 0000000..6c792d2
--- /dev/null
+++ b/json-store/src/test/java/org/apache/sling/jsonstore/karate/KarateIT.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.jsonstore.karate;
+
+import com.intuit.karate.junit4.Karate;
+
+import org.apache.sling.jsonstore.JsonStoreTestSupport;
+import org.junit.BeforeClass;
+import org.junit.runner.RunWith;
+
+@RunWith(Karate.class)
+public class KarateIT extends JsonStoreTestSupport {
+    @BeforeClass
+    public static void setKaratePort() {
+        System.setProperty("karate.test.port", String.valueOf(httpPort));
+    }
+}
diff --git a/json-store/src/test/java/org/apache/sling/jsonstore/karate/createContent.feature b/json-store/src/test/java/org/apache/sling/jsonstore/karate/createContent.feature
new file mode 100644
index 0000000..f20d169
--- /dev/null
+++ b/json-store/src/test/java/org/apache/sling/jsonstore/karate/createContent.feature
@@ -0,0 +1,74 @@
+# /*
+# * Licensed to the Apache Software Foundation (ASF) under one
+#  * or more contributor license agreements.  See the NOTICE file
+#  * distributed with this work for additional information
+#  * regarding copyright ownership.  The ASF licenses this file
+#  * to you under the Apache License, Version 2.0 (the
+#  * "License"); you may not use this file except in compliance
+#  * with the License.  You may obtain a copy of the License at
+#  *
+#  *   http://www.apache.org/licenses/LICENSE-2.0
+#  *
+#  * Unless required by applicable law or agreed to in writing,
+#  * software distributed under the License is distributed on an
+#  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  * KIND, either express or implied.  See the License for the
+#  * specific language governing permissions and limitations
+#  * under the License.
+#  */
+
+# ------------------------------------------------------------------------
+@postservlet
+Feature: create content using the Sling POST Servlet
+# ------------------------------------------------------------------------
+
+# ------------------------------------------------------------------------
+Background:
+# ------------------------------------------------------------------------
+
+* url baseURL
+
+# Use admin credentials for all requests
+* configure headers = call read('classpath:util/basic-auth-header.js')
+
+# Sling instance ready?
+* eval karate.call('classpath:util/sling-ready.feature')
+
+* def testID = '' + java.util.UUID.randomUUID()
+* def testFolderPath = '/createContentTest/' + testID
+
+# ------------------------------------------------------------------------
+Scenario: Create a resource, update, read back, delete
+# ------------------------------------------------------------------------
+
+# Create a resource
+Given path testFolderPath, '*'
+And form field f1 = 'v1A' + testID
+And form field f2 = 'v2A'
+When method POST
+Then status 201
+
+# The Location header provides the path where the resource was created
+* def resourcePath = responseHeaders['Location'][0]
+
+# Read back
+Given path resourcePath + '.json'
+When method GET
+Then status 200
+And match response.f1 == 'v1A' + testID
+And match response.f2 == 'v2A'
+
+# Overwrite one field and add a new one
+Given path resourcePath
+And form field f2 = 'v2B'
+And form field f3 = 'v3B'
+When method POST
+Then status 200
+
+# Read modified resource back
+Given path resourcePath + '.json'
+When method GET
+Then status 200
+And match response.f1 == 'v1A' + testID
+And match response.f2 == 'v2B'
+And match response.f3 == 'v3B'
diff --git a/json-store/src/test/java/org/apache/sling/jsonstore/karate/schema/invalid.json b/json-store/src/test/java/org/apache/sling/jsonstore/karate/schema/invalid.json
new file mode 100644
index 0000000..4347bb2
--- /dev/null
+++ b/json-store/src/test/java/org/apache/sling/jsonstore/karate/schema/invalid.json
@@ -0,0 +1,4 @@
+{
+    "$schema": "https://some.invalid.schema.ref",
+    "$id": "https://example.com/invalid"
+}
\ No newline at end of file
diff --git a/json-store/src/test/java/org/apache/sling/jsonstore/karate/schema/minimal.json b/json-store/src/test/java/org/apache/sling/jsonstore/karate/schema/minimal.json
new file mode 100644
index 0000000..33ac40c
--- /dev/null
+++ b/json-store/src/test/java/org/apache/sling/jsonstore/karate/schema/minimal.json
@@ -0,0 +1,19 @@
+{
+    "$schema": "https://json-schema.org/draft/2019-09/schema",
+    "$id": "https://example.com/minimal",
+    "title": "Minimal",
+    "description": "A minimal(ish) valid schema",
+    "type": "object",
+    "properties": {
+      "$schema": {
+        "description": "The schema reference",
+        "type": "string"
+      },
+      "tags": {
+        "description": "Tags that categorize the product",
+        "type": "array"
+      }
+    },
+    "additionalProperties":false,
+    "required": [ "$schema", "tags" ]
+  }
\ No newline at end of file
diff --git a/json-store/src/test/java/org/apache/sling/jsonstore/karate/schemaStorage.feature b/json-store/src/test/java/org/apache/sling/jsonstore/karate/schemaStorage.feature
new file mode 100644
index 0000000..2818dab
--- /dev/null
+++ b/json-store/src/test/java/org/apache/sling/jsonstore/karate/schemaStorage.feature
@@ -0,0 +1,59 @@
+# /*
+# * Licensed to the Apache Software Foundation (ASF) under one
+#  * or more contributor license agreements.  See the NOTICE file
+#  * distributed with this work for additional information
+#  * regarding copyright ownership.  The ASF licenses this file
+#  * to you under the Apache License, Version 2.0 (the
+#  * "License"); you may not use this file except in compliance
+#  * with the License.  You may obtain a copy of the License at
+#  *
+#  *   http://www.apache.org/licenses/LICENSE-2.0
+#  *
+#  * Unless required by applicable law or agreed to in writing,
+#  * software distributed under the License is distributed on an
+#  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  * KIND, either express or implied.  See the License for the
+#  * specific language governing permissions and limitations
+#  * under the License.
+#  */
+
+# ------------------------------------------------------------------------
+@schema
+Feature: Test schema storage
+# ------------------------------------------------------------------------
+
+# ------------------------------------------------------------------------
+Background:
+# ------------------------------------------------------------------------
+
+* url baseURL
+
+# Use admin credentials for all requests
+* configure headers = call read('classpath:util/basic-auth-header.js')
+
+# Sling instance ready?
+* eval karate.call('classpath:util/sling-ready.feature')
+
+# ------------------------------------------------------------------------
+Scenario: Store a valid schema
+# ------------------------------------------------------------------------
+Given request read('/schema/minimal.json')
+And path 'content/sites/example.com/schema/minimal'
+When method POST
+Then status 200
+
+# ------------------------------------------------------------------------
+Scenario: Retrieve and check the valid schema
+# ------------------------------------------------------------------------
+Given path 'content/sites/example.com/schema/minimal'
+When method GET
+Then status 200
+And match response == read('/schema/minimal.json')
+
+# ------------------------------------------------------------------------
+Scenario: Error trying to store an invalid schema
+# ------------------------------------------------------------------------
+Given request read('/schema/invalid.json')
+And path 'content/sites/example.com/schema/invalid'
+When method POST
+Then status 400
diff --git a/json-store/src/test/java/util/basic-auth-header.js b/json-store/src/test/java/util/basic-auth-header.js
new file mode 100644
index 0000000..0628df8
--- /dev/null
+++ b/json-store/src/test/java/util/basic-auth-header.js
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+function() {
+  var adminAdminInBase64 = 'YWRtaW46YWRtaW4=';
+  return { 'Authorization' : 'Basic ' + adminAdminInBase64 }
+}
diff --git a/json-store/src/test/java/util/cleanup-path.feature b/json-store/src/test/java/util/cleanup-path.feature
new file mode 100644
index 0000000..a262c28
--- /dev/null
+++ b/json-store/src/test/java/util/cleanup-path.feature
@@ -0,0 +1,43 @@
+# /*
+# * Licensed to the Apache Software Foundation (ASF) under one
+#  * or more contributor license agreements.  See the NOTICE file
+#  * distributed with this work for additional information
+#  * regarding copyright ownership.  The ASF licenses this file
+#  * to you under the Apache License, Version 2.0 (the
+#  * "License"); you may not use this file except in compliance
+#  * with the License.  You may obtain a copy of the License at
+#  *
+#  *   http://www.apache.org/licenses/LICENSE-2.0
+#  *
+#  * Unless required by applicable law or agreed to in writing,
+#  * software distributed under the License is distributed on an
+#  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  * KIND, either express or implied.  See the License for the
+#  * specific language governing permissions and limitations
+#  * under the License.
+#  */
+
+# ------------------------------------------------------------------------
+Feature: DELETE a path and verify that it's gone
+# ------------------------------------------------------------------------
+
+# ------------------------------------------------------------------------
+Background:
+# ------------------------------------------------------------------------
+
+* url baseURL
+
+# Use admin credentials for all requests
+* configure headers = call read('classpath:util/basic-auth-header.js')
+
+# ------------------------------------------------------------------------
+Scenario: Delete a given path and verify that it's gone
+# ------------------------------------------------------------------------
+Given path pathToDelete
+When method DELETE
+Then status 204
+
+# Use HEAD to avoid an XML parsing warning
+Given path pathToDelete
+When method HEAD
+Then status 404
\ No newline at end of file
diff --git a/json-store/src/test/java/util/cleanup-paths.js b/json-store/src/test/java/util/cleanup-paths.js
new file mode 100644
index 0000000..585fec4
--- /dev/null
+++ b/json-store/src/test/java/util/cleanup-paths.js
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+function(paths) {
+  // Cleanup a set of paths
+  for(i in paths) {
+    var args = { "pathToDelete" : paths[i] }
+    karate.call("classpath:util/cleanup-path.feature", args);
+  }
+}
\ No newline at end of file
diff --git a/json-store/src/test/java/util/sling-ready.feature b/json-store/src/test/java/util/sling-ready.feature
new file mode 100644
index 0000000..8397dc9
--- /dev/null
+++ b/json-store/src/test/java/util/sling-ready.feature
@@ -0,0 +1,38 @@
+# /*
+# * Licensed to the Apache Software Foundation (ASF) under one
+#  * or more contributor license agreements.  See the NOTICE file
+#  * distributed with this work for additional information
+#  * regarding copyright ownership.  The ASF licenses this file
+#  * to you under the Apache License, Version 2.0 (the
+#  * "License"); you may not use this file except in compliance
+#  * with the License.  You may obtain a copy of the License at
+#  *
+#  *   http://www.apache.org/licenses/LICENSE-2.0
+#  *
+#  * Unless required by applicable law or agreed to in writing,
+#  * software distributed under the License is distributed on an
+#  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  * KIND, either express or implied.  See the License for the
+#  * specific language governing permissions and limitations
+#  * under the License.
+#  */
+
+# ------------------------------------------------------------------------
+Feature: Check access to the Sling instance under test
+# ------------------------------------------------------------------------
+
+# ------------------------------------------------------------------------
+Background:
+# ------------------------------------------------------------------------
+
+* url baseURL
+
+# Use admin credentials for all requests
+* configure headers = call read('classpath:util/basic-auth-header.js')
+
+# ------------------------------------------------------------------------
+Scenario: Check access to HTTP root
+# ------------------------------------------------------------------------
+Given path '/.json'
+When method GET
+Then status 200