From 05989a5707b3064b65ffa48d47fa0f1e0c44539f Mon Sep 17 00:00:00 2001 From: Henry Kuijpers Date: Tue, 29 Jun 2021 12:59:11 +0200 Subject: [PATCH] SLING-10136 Add delete path operation --- .../sling/jcr/repoinit/impl/AclVisitor.java | 21 +++++++ .../jcr/repoinit/impl/DoNothingVisitor.java | 5 ++ .../sling/jcr/repoinit/DeletePathTest.java | 62 +++++++++++++++++++ .../sling/jcr/repoinit/impl/TestUtil.java | 3 +- 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/apache/sling/jcr/repoinit/DeletePathTest.java diff --git a/src/main/java/org/apache/sling/jcr/repoinit/impl/AclVisitor.java b/src/main/java/org/apache/sling/jcr/repoinit/impl/AclVisitor.java index 9e363ef..c9ed182 100644 --- a/src/main/java/org/apache/sling/jcr/repoinit/impl/AclVisitor.java +++ b/src/main/java/org/apache/sling/jcr/repoinit/impl/AclVisitor.java @@ -32,6 +32,7 @@ import org.apache.sling.repoinit.parser.operations.CreatePath; import org.apache.sling.repoinit.parser.operations.DeleteAclPrincipalBased; import org.apache.sling.repoinit.parser.operations.DeleteAclPrincipals; +import org.apache.sling.repoinit.parser.operations.DeletePath; import org.apache.sling.repoinit.parser.operations.PathSegmentDefinition; import org.apache.sling.repoinit.parser.operations.DeleteAclPaths; import org.apache.sling.repoinit.parser.operations.RestrictionClause; @@ -167,6 +168,26 @@ public void visitCreatePath(CreatePath cp) { throw new RuntimeException("Session.save failed: " + e, e); } } + + @Override + public void visitDeletePath(final DeletePath dp) { + final String path = dp.getPath(); + try { + if (!session.itemExists(path)) { + log.info("Path does not exist, nothing to do: {}", path); + } else { + log.info("Deleting path {}", path); + session.removeItem(path); + } + } catch (final Exception e) { + throw new RuntimeException("DeletePath execution for path " + path + " failed", e); + } + try { + session.save(); + } catch (Exception e) { + throw new RuntimeException("Session.save failed: " + e, e); + } + } @NotNull private static Node addChildNode(@NotNull Node parent, @NotNull PathSegmentDefinition psd) throws RepositoryException { diff --git a/src/main/java/org/apache/sling/jcr/repoinit/impl/DoNothingVisitor.java b/src/main/java/org/apache/sling/jcr/repoinit/impl/DoNothingVisitor.java index edf159f..151fe9d 100644 --- a/src/main/java/org/apache/sling/jcr/repoinit/impl/DoNothingVisitor.java +++ b/src/main/java/org/apache/sling/jcr/repoinit/impl/DoNothingVisitor.java @@ -25,6 +25,7 @@ import org.apache.sling.repoinit.parser.operations.CreateUser; import org.apache.sling.repoinit.parser.operations.DeleteAclPrincipals; import org.apache.sling.repoinit.parser.operations.DeleteGroup; +import org.apache.sling.repoinit.parser.operations.DeletePath; import org.apache.sling.repoinit.parser.operations.DeleteServiceUser; import org.apache.sling.repoinit.parser.operations.DeleteUser; import org.apache.sling.repoinit.parser.operations.DisableServiceUser; @@ -115,6 +116,10 @@ public void visitDeleteAclPrincipalBased(DeleteAclPrincipalBased s) { public void visitCreatePath(CreatePath cp) { } + @Override + public void visitDeletePath(DeletePath dp) { + } + @Override public void visitRegisterNamespace(RegisterNamespace rn) { } diff --git a/src/test/java/org/apache/sling/jcr/repoinit/DeletePathTest.java b/src/test/java/org/apache/sling/jcr/repoinit/DeletePathTest.java new file mode 100644 index 0000000..81834bf --- /dev/null +++ b/src/test/java/org/apache/sling/jcr/repoinit/DeletePathTest.java @@ -0,0 +1,62 @@ +/* + * 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.jcr.repoinit; + +import org.apache.sling.commons.testing.jcr.RepositoryUtil; +import org.apache.sling.jcr.repoinit.impl.TestUtil; +import org.apache.sling.testing.mock.sling.ResourceResolverType; +import org.apache.sling.testing.mock.sling.junit.SlingContext; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; + +import javax.jcr.Node; +import javax.jcr.RepositoryException; +import java.io.IOException; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DeletePathTest { + + @Rule + public final SlingContext context = new SlingContext(ResourceResolverType.JCR_OAK); + + private TestUtil U; + + @Before + public void setup() throws RepositoryException, IOException { + U = new TestUtil(context); + RepositoryUtil.registerSlingNodeTypes(U.adminSession); + } + + @Test + public void deleteNonExistentPath() throws Exception { + final String path = "/my-path"; + U.parseAndExecute("delete path " + path); + assertFalse(U.adminSession.nodeExists(path)); + } + + @Test + public void deleteExistingPath() throws Exception { + final Node node = U.adminSession.getRootNode().addNode("my-path"); + final String path = node.getPath(); + assertTrue(U.adminSession.nodeExists(path)); + U.parseAndExecute("delete path " + path); + assertFalse(U.adminSession.nodeExists(path)); + } +} diff --git a/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java b/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java index 25b7ed8..5584537 100644 --- a/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java +++ b/src/test/java/org/apache/sling/jcr/repoinit/impl/TestUtil.java @@ -27,6 +27,7 @@ import java.io.Reader; import java.io.StringReader; import java.util.List; +import java.util.Objects; import java.util.UUID; import javax.jcr.Node; @@ -57,7 +58,7 @@ public class TestUtil { public final String username; public TestUtil(SlingContext ctx) { - adminSession = ctx.resourceResolver().adaptTo(Session.class); + adminSession = Objects.requireNonNull(ctx.resourceResolver().adaptTo(Session.class)); id = UUID.randomUUID().toString(); username = "user_" + id; }