This is an automated email from the ASF dual-hosted git repository. cschneider pushed a commit to branch SLING-12311 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-api.git
commit 1562536bea1201c0c37ef9e5d9d2c40eaba1332d Author: Christian Schneider <[email protected]> AuthorDate: Thu May 2 15:59:23 2024 +0200 SLING-12311 - Remove empty paths --- .../distribution/SimpleDistributionRequest.java | 17 +++++- .../SimpleDistributionRequestTest.java | 65 ++++++++++++++++++++++ 2 files changed, 80 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..4860009 100644 --- a/src/main/java/org/apache/sling/distribution/SimpleDistributionRequest.java +++ b/src/main/java/org/apache/sling/distribution/SimpleDistributionRequest.java @@ -19,10 +19,13 @@ package org.apache.sling.distribution; import java.util.Arrays; +import java.util.Collection; 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 +83,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 +130,15 @@ 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(path -> path != null && !path.isEmpty()).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(path -> path != null && !path.isEmpty()).collect(Collectors.toSet()); + } } 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..b0db373 --- /dev/null +++ b/src/test/java/org/apache/sling/distribution/SimpleDistributionRequestTest.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.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 testSanitiseSingleEmptyPath() { + 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)); + } +}
