You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by dk...@apache.org on 2020/09/25 03:14:13 UTC

[sling-org-apache-sling-app-cms] branch master updated: Adding tests for the resource tree

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

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


The following commit(s) were added to refs/heads/master by this push:
     new c1afa36  Adding tests for the resource tree
c1afa36 is described below

commit c1afa36f68e86f903f59252759f4839ca670a29e
Author: Dan Klco <dk...@apache.org>
AuthorDate: Thu Sep 24 23:13:30 2020 -0400

    Adding tests for the resource tree
---
 api/pom.xml                                        |  10 +-
 .../java/org/apache/sling/cms/ResourceTree.java    |   2 +-
 .../org/apache/sling/cms/ResourceTreeTest.java     |  65 +++++++++++
 api/src/test/resources/content.json                | 127 +++++++++++++++++++++
 4 files changed, 201 insertions(+), 3 deletions(-)

diff --git a/api/pom.xml b/api/pom.xml
index 46eb408..07bbeda 100644
--- a/api/pom.xml
+++ b/api/pom.xml
@@ -83,17 +83,23 @@
         </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
+            <artifactId>osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
             <artifactId>osgi.cmpn</artifactId>
         </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
-            <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
             <artifactId>mockito-core</artifactId>
-            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.sling-mock.junit4</artifactId>
         </dependency>
     </dependencies>
 </project>
diff --git a/api/src/main/java/org/apache/sling/cms/ResourceTree.java b/api/src/main/java/org/apache/sling/cms/ResourceTree.java
index 445d8f5..50b8eb1 100644
--- a/api/src/main/java/org/apache/sling/cms/ResourceTree.java
+++ b/api/src/main/java/org/apache/sling/cms/ResourceTree.java
@@ -58,7 +58,7 @@ public class ResourceTree {
     private Stream<ResourceTree> streamTree(Predicate<Resource> filterTraversal,
             Predicate<ResourceTree> filterInclude) {
         return Stream
-                .concat(Stream.of(this),
+                .concat(Stream.of(this).filter(rt -> filterTraversal.test(rt.getResource())),
                         StreamSupport.stream(root.getChildren().spliterator(), false).filter(filterTraversal)
                                 .map(ResourceTree::new).flatMap(rt -> rt.streamTree(filterTraversal, filterInclude)))
                 .filter(filterInclude);
diff --git a/api/src/test/java/org/apache/sling/cms/ResourceTreeTest.java b/api/src/test/java/org/apache/sling/cms/ResourceTreeTest.java
new file mode 100644
index 0000000..4aa74e6
--- /dev/null
+++ b/api/src/test/java/org/apache/sling/cms/ResourceTreeTest.java
@@ -0,0 +1,65 @@
+/*
+ * 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.cms;
+
+import static org.junit.Assert.assertEquals;
+import java.util.stream.Stream;
+
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class ResourceTreeTest {
+
+    @Rule
+    public final SlingContext context = new SlingContext();
+
+    @Before
+    public void init() {
+        context.load().json("/content.json", "/content");
+    }
+
+    @Test
+    public void testSimple() {
+        context.currentResource("/content");
+        Stream<ResourceTree> tree = ResourceTree.stream(context.currentResource());
+        assertEquals(16, tree.count());
+    }
+
+    @Test
+    public void testFilterType() {
+        context.currentResource("/content/apache/sling-apache-org");
+        Stream<ResourceTree> tree = ResourceTree.stream(context.currentResource(), "sling:File");
+        assertEquals(3, tree.map(rt -> {
+            assertEquals("sling:File", rt.getResource().getResourceType());
+            return rt;
+        }).count());
+    }
+
+    @Test
+    public void testPredicates() {
+        context.currentResource("/content/apache");
+        Stream<ResourceTree> tree = ResourceTree.stream(context.currentResource(), (r) -> true,
+                rt -> "sling:Page".equals(rt.getResource().getResourceType()));
+        assertEquals(2, tree.map(rt -> {
+            assertEquals("sling:Page", rt.getResource().getResourceType());
+            return rt;
+        }).count());
+    }
+
+}
diff --git a/api/src/test/resources/content.json b/api/src/test/resources/content.json
new file mode 100644
index 0000000..7622552
--- /dev/null
+++ b/api/src/test/resources/content.json
@@ -0,0 +1,127 @@
+{
+    "jcr:primaryType": "sling:OrderedFolder",
+    "jcr:mixinTypes": [
+        "rep:AccessControllable"
+    ],
+    "jcr:createdBy": "admin",
+    "jcr:created": "Wed May 15 2019 12:39:57 GMT-0400",
+    "rep:policy": {
+        "jcr:primaryType": "rep:ACL",
+        "allow": {
+            "jcr:primaryType": "rep:GrantACE",
+            "rep:principalName": "authors",
+            "rep:privileges": [
+                "jcr:versionManagement",
+                "rep:write"
+            ]
+        },
+        "allow1": {
+            "jcr:primaryType": "rep:GrantACE",
+            "rep:principalName": "sling-cms-metadata",
+            "rep:privileges": [
+                "jcr:versionManagement",
+                "rep:write"
+            ]
+        },
+        "allow2": {
+            "jcr:primaryType": "rep:GrantACE",
+            "rep:principalName": "sling-cms-versionmgr",
+            "rep:privileges": [
+                "jcr:versionManagement",
+                "rep:write"
+            ]
+        },
+        "allow3": {
+            "jcr:primaryType": "rep:GrantACE",
+            "rep:principalName": "everyone",
+            "rep:privileges": [
+                "jcr:read"
+            ]
+        }
+    },
+    "apache": {
+        "jcr:primaryType": "sling:OrderedFolder",
+        "jcr:createdBy": "admin",
+        "sling:configRef": "/conf/global",
+        "jcr:created": "Wed May 15 2019 12:40:00 GMT-0400",
+        "jcr:content": {
+            "jcr:primaryType": "nt:unstructured",
+            "jcr:title": "Apache Software Foundation"
+        },
+        "sling-apache-org": {
+            "jcr:primaryType": "sling:Site",
+            "jcr:createdBy": "admin",
+            "jcr:title": "Apache Sling",
+            "jcr:language": "en",
+            "sling:url": "https://sling.apache.org",
+            "jcr:created": "Wed May 15 2019 12:40:00 GMT-0400",
+            "unpublished": {
+                "jcr:primaryType": "sling:Page",
+                "jcr:content": {
+                    "jcr:primaryType": "nt:unstructured",
+                    "jcr:title": "Not Published",
+                    "sling:template": "/conf/global/site/templates/base-page",
+                    "sling:resourceType": "reference/components/pages/base",
+                    "sling:published": false
+                }
+            },
+            "index": {
+                "jcr:primaryType": "sling:Page",
+                "jcr:mixinTypes": [
+                    "mix:versionable"
+                ],
+                "jcr:createdBy": "admin",
+                "jcr:versionHistory": "87458e80-83b8-46ee-a5d8-3e39c2f07c10",
+                "jcr:predecessors": [],
+                "jcr:created": "Wed May 15 2019 12:40:00 GMT-0400",
+                "jcr:baseVersion": "9ddd2472-9a0e-4fcb-8a2d-72f0b3f40d61",
+                "jcr:isCheckedOut": false,
+                "jcr:uuid": "fd5c6000-b3b9-44a2-88a0-1c8e13d7c1a7",
+                "jcr:content": {
+                    "jcr:primaryType": "nt:unstructured",
+                    "jcr:title": "Apache Sling - Bringing Back the Fun!",
+                    "jcr:lastModifiedBy": "admin",
+                    "sling:template": "/conf/global/site/templates/base-page",
+                    "sling:taxonomy": "/etc/taxonomy/reference/community",
+                    "jcr:lastModified": "Wed May 15 2019 14:05:46 GMT-0400",
+                    "sling:resourceType": "reference/components/pages/base",
+                    "sling:published": true,
+                    "sling:lastPublication": "Wed May 15 2019 14:05:46 GMT-0400",
+                    "sling:lastPublicationBy": "admin",
+                    "sling:lastPublicationType": "ADD",
+                    "hideInSitemap": false,
+                    "container": {
+                        "jcr:primaryType": "nt:unstructured",
+                        "sling:resourceType": "sling-cms/components/general/container",
+                        "richtext": {
+                            "jcr:primaryType": "nt:unstructured",
+                            "text": "<p>Apache Sling(TM) is a framework for RESTful web-applications based on an extensible content tree.</p>\r\n<p>In a nutshell, Sling maps HTTP request URLs to content resources based on the request's path, extension and selectors. Using convention over configuration, requests are processed by scripts and servlets, dynamically selected based on the current resource. This fosters meaningful URLs and resource driven request processing, while the modular n [...]
+                            "sling:resourceType": "sling-cms/components/general/richtext"
+                        }
+                    },
+                    "menu": {
+                        "jcr:primaryType": "nt:unstructured",
+                        "richtext": {
+                            "jcr:primaryType": "nt:unstructured",
+                            "text": "<p>\r\n                <strong><a href=\"#\">Documentation</a></strong><br>\r\n                <a href=\"#\">Getting Started</a><br>\r\n                <a href=\"#\">The Sling Engine</a><br>\r\n                <a href=\"#\">Development</a><br>\r\n                <a href=\"#\">Bundles</a><br>\r\n                <a href=\"#\">Tutorials &amp; How-Tos</a><br>\r\n                <a href=\"http://sling.apache.org/components/\">Maven Plugins</a><br>\r\n      [...]
+                            "sling:resourceType": "sling-cms/components/general/richtext"
+                        }
+                    }
+                }
+            },
+            "apache.png": {
+                "jcr:primaryType": "sling:File",
+                "jcr:content": {
+                    "jcr:primaryType": "sling:FileContent",
+                    "jcr:mimeType": "image/png"
+                }
+            },
+            "sling.pdf": {
+                "jcr:primaryType": "sling:File"
+            },
+            "sling.docx": {
+                "jcr:primaryType": "sling:File"
+            }
+        }
+    }
+}
\ No newline at end of file