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

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


The following commit(s) were added to refs/heads/master by this push:
     new 3263570  SLING-12311 - Remove empty paths (#14)
3263570 is described below

commit 3263570a1fc7d188ef81a616613bfc8fae2ce1ec
Author: Christian Schneider <[email protected]>
AuthorDate: Thu May 2 18:32:29 2024 +0200

    SLING-12311 - Remove empty paths (#14)
    
    * SLING-12311 - Remove empty paths
    
    * SLING-12311 - Also remove paths with just space
    
    * SLING-12311 - Check for null
---
 .../distribution/SimpleDistributionRequest.java    | 20 +++++-
 .../SimpleDistributionRequestTest.java             | 77 ++++++++++++++++++++++
 2 files changed, 95 insertions(+), 2 deletions(-)

diff --git 
a/src/main/java/org/apache/sling/distribution/SimpleDistributionRequest.java 
b/src/main/java/org/apache/sling/distribution/SimpleDistributionRequest.java
index 123d27a..a76e341 100644
--- a/src/main/java/org/apache/sling/distribution/SimpleDistributionRequest.java
+++ b/src/main/java/org/apache/sling/distribution/SimpleDistributionRequest.java
@@ -21,8 +21,10 @@ package org.apache.sling.distribution;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import javax.annotation.Nonnull;
 
@@ -80,8 +82,8 @@ public final class SimpleDistributionRequest implements 
DistributionRequest {
      */
     public SimpleDistributionRequest(DistributionRequestType requestType, 
String[] paths, Set<String> deepPaths, Map<String, String[]> pathFilters) {
         this.requestType = requestType;
-        this.paths = paths;
-        this.deepPaths = deepPaths;
+        this.paths = sanitise(paths);
+        this.deepPaths = sanitise(deepPaths);
         this.pathFilters = pathFilters;
     }
 
@@ -127,5 +129,19 @@ public final class SimpleDistributionRequest implements 
DistributionRequest {
                 '}';
     }
 
+    private String[] sanitise(String[] paths) {
+        if (paths == null) return new String[] {};
+        List<String> pathsOut = 
Arrays.asList(paths).stream().filter(this::notEmpty).collect(Collectors.toList());
+        return pathsOut.toArray(new String[] {});
+    }
+
+    private Set<String> sanitise(Set<String> paths) {
+        if (paths == null) return new HashSet<>();
+        return 
paths.stream().filter(this::notEmpty).collect(Collectors.toSet());
+    }
+    
+    private boolean notEmpty(String path) {
+        return path != null && !path.trim().isEmpty();
+    }
 
 }
diff --git 
a/src/test/java/org/apache/sling/distribution/SimpleDistributionRequestTest.java
 
b/src/test/java/org/apache/sling/distribution/SimpleDistributionRequestTest.java
new file mode 100644
index 0000000..a21c9b2
--- /dev/null
+++ 
b/src/test/java/org/apache/sling/distribution/SimpleDistributionRequestTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.distribution;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Test;
+
+public class SimpleDistributionRequestTest {
+
+    @Test
+    public void testSanitiseNoPaths() {
+        SimpleDistributionRequest req = new 
SimpleDistributionRequest(DistributionRequestType.ADD);
+        assertThat(req.getPaths().length, equalTo(0));
+    }
+    
+    @Test
+    public void testSanitiseNullPaths() {
+        SimpleDistributionRequest req = new 
SimpleDistributionRequest(DistributionRequestType.ADD, (String[])null);
+        assertThat(req.getPaths().length, equalTo(0));
+    }
+    
+    @Test
+    public void testSanitiseSingleEmptyPath() {
+        SimpleDistributionRequest req = new 
SimpleDistributionRequest(DistributionRequestType.ADD, "");
+        assertThat(req.getPaths().length, equalTo(0));
+    }
+    
+    @Test
+    public void testSanitiseSingleSpacePath() {
+        SimpleDistributionRequest req = new 
SimpleDistributionRequest(DistributionRequestType.ADD, " ");
+        assertThat(req.getPaths().length, equalTo(0));
+    }
+    
+    @Test
+    public void testSanitiseOkPaths() {
+        SimpleDistributionRequest req = new 
SimpleDistributionRequest(DistributionRequestType.ADD, "test", "test2");
+        assertThat(req.getPaths().length, equalTo(2));
+    }
+    
+    @Test
+    public void testSanitiseSomeEmptyPaths() {
+        SimpleDistributionRequest req = new 
SimpleDistributionRequest(DistributionRequestType.ADD, "test", "", "test2");
+        assertThat(req.getPaths().length, equalTo(2));
+        assertThat(req.getPaths()[0], equalTo("test"));
+        assertThat(req.getPaths()[1], equalTo("test2"));
+    }
+    
+    @Test
+    public void testSanitiseDeepPaths() {
+        Set<String> deepPaths = new HashSet<>(Arrays.asList("test", "", 
"test2"));
+        SimpleDistributionRequest req = new 
SimpleDistributionRequest(DistributionRequestType.ADD, new String[]{"test"} , 
deepPaths);
+        assertThat(req.isDeep("test"), equalTo(true));
+        assertThat(req.isDeep("test2"), equalTo(true));
+    }
+}

Reply via email to