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 1f9a8694 chore - improving test coverage of Start Content
1f9a8694 is described below

commit 1f9a86940d7e21de33ea7cd6c1327ad632f0e701
Author: Dan Klco <k...@adobe.com>
AuthorDate: Thu Sep 28 11:02:02 2023 -0400

    chore - improving test coverage of Start Content
---
 .../apache/sling/cms/core/models/StartContent.java |  5 +-
 .../sling/cms/core/models/StartContentTest.java    | 61 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git 
a/core/src/main/java/org/apache/sling/cms/core/models/StartContent.java 
b/core/src/main/java/org/apache/sling/cms/core/models/StartContent.java
index b8ced570..4c934bbb 100644
--- a/core/src/main/java/org/apache/sling/cms/core/models/StartContent.java
+++ b/core/src/main/java/org/apache/sling/cms/core/models/StartContent.java
@@ -25,6 +25,7 @@ package org.apache.sling.cms.core.models;
 import java.util.Calendar;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Optional;
 import java.util.Spliterator;
 import java.util.Spliterators;
 import java.util.stream.Collectors;
@@ -61,7 +62,7 @@ public class StartContent {
     public List<Resource> getRelatedContent() {
         return getTenResults(
                 "SELECT * FROM [nt:hierarchyNode] AS s WHERE 
ISDESCENDANTNODE([/content]) AND CONTAINS(s.*,'"
-                        + 
escape(term.replaceAll("[\\Q+-&|!(){}[]^\"~*?:\\/\\E]", "")) + "')");
+                        + 
escape(term).replaceAll("[\\Q+-&|!(){}[]^\"~*?:\\/\\E]", "") + "')");
 
     }
 
@@ -88,7 +89,7 @@ public class StartContent {
     }
 
     private String escape(String str) {
-        return str.replace("'", "''");
+        return Optional.ofNullable(str).map(s -> s.replace("'", 
"''")).orElse("");
     }
 
     private List<Resource> getTenResults(String query) {
diff --git 
a/core/src/test/java/org/apache/sling/cms/core/models/StartContentTest.java 
b/core/src/test/java/org/apache/sling/cms/core/models/StartContentTest.java
new file mode 100644
index 00000000..8dc459d8
--- /dev/null
+++ b/core/src/test/java/org/apache/sling/cms/core/models/StartContentTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.core.models;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.cms.core.helpers.SlingCMSTestHelper;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+public class StartContentTest {
+
+    @Rule
+    public SlingContext context = new SlingContext();
+
+    @Before
+    public void init() {
+        SlingCMSTestHelper.initContext(context);
+    }
+
+    @Test
+    public void canGetRelatedContent() {
+        context.request().addRequestParameter("q", "apache");
+        StartContent startContent = new StartContent(context.request());
+        List<Resource> related = startContent.getRelatedContent();
+        assertNotNull(related);
+    }
+
+    @Test
+    public void canGetRecentDrafts() {
+        StartContent startContent = new StartContent(context.request());
+        List<Resource> related = startContent.getRecentDrafts();
+        assertNotNull(related);
+    }
+
+    @Test
+    public void canGetRecentContent() {
+        StartContent startContent = new StartContent(context.request());
+        List<Resource> related = startContent.getRecentContent();
+        assertNotNull(related);
+    }
+}

Reply via email to