Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/org/apache/sling/jcr/repoinit/impl/AclVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
}
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/apache/sling/jcr/repoinit/DeletePathTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down