Skip to content

Commit

Permalink
fix(runner): renamed nodes now keep their position
Browse files Browse the repository at this point in the history
closes #16
  • Loading branch information
sabberworm committed Oct 30, 2024
1 parent 3749a7b commit 816ad26
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/main/java/com/swisscom/aem/tools/impl/hops/MoveNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -130,7 +131,27 @@ public void run(Config config, Node node, HopContext context) throws RepositoryE
final String absolutePath = StringUtils.stripEnd(effectiveParent.getPath(), "/") + '/' + descriptor.getNewChildName();
context.info("Moving node from {} to {}", node.getPath(), absolutePath);

String nextSiblingName = getNextSiblingName(node, parent, descriptor.parent);
node.getSession().move(node.getPath(), absolutePath);

if (nextSiblingName != null) {
effectiveParent.orderBefore(descriptor.newChildName, nextSiblingName);
}
}

private static String getNextSiblingName(Node node, Node oldParent, Node newParent) throws RepositoryException {
if (!StringUtils.equals(oldParent.getPath(), newParent.getPath())) {
return null;
}
String nextSibling = null;
final NodeIterator siblingIterator = oldParent.getNodes();
while (siblingIterator.hasNext()) {
if (StringUtils.equals(siblingIterator.nextNode().getPath(), node.getPath()) && siblingIterator.hasNext()) {
nextSibling = siblingIterator.nextNode().getName();
break;
}
}
return nextSibling;
}

@Nonnull
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/com/swisscom/aem/tools/impl/hops/MoveNodeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.swisscom.aem.tools.impl.hops;

import static com.swisscom.aem.tools.testsupport.AemUtil.childNames;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.swisscom.aem.tools.jcrhopper.HopperException;
import com.swisscom.aem.tools.jcrhopper.Runner;
import com.swisscom.aem.tools.jcrhopper.RunnerBuilder;
import com.swisscom.aem.tools.jcrhopper.config.Script;
import io.wcm.testing.mock.aem.junit5.AemContext;
import io.wcm.testing.mock.aem.junit5.AemContextExtension;
import io.wcm.testing.mock.aem.junit5.JcrOakAemContext;
import java.util.Arrays;
import java.util.Collections;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.resource.ResourceResolver;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(AemContextExtension.class)
class MoveNodeTest {

public final AemContext context = new JcrOakAemContext();
private RunnerBuilder builder;
private ResourceResolver resolver;
private Session session;

@BeforeEach
public void setUp() {
context.create().resource("/content");
context.create().resource("/content/test");
context.create().resource("/content/test/child2");
context.create().resource("/content/test/child1");

builder = Runner.builder().addHop(new MoveNode());

resolver = context.resourceResolver();
session = resolver.adaptTo(Session.class);
}

@Test
public void move_sameParent() throws RepositoryException, HopperException {
assertEquals(Arrays.asList("child2", "child1"), childNames(resolver.getResource("/content/test")));

builder.build(new Script(new MoveNode.Config().withNewName("child3"))).run(session.getNode("/content/test/child2"), true);

assertEquals(Arrays.asList("child3", "child1"), childNames(resolver.getResource("/content/test")));
}

@Test
public void move_newParent() throws RepositoryException, HopperException {
builder.build(new Script(new MoveNode.Config().withNewName("../test"))).run(session.getNode("/content/test"), true);

assertEquals(Arrays.asList("child2", "child1"), childNames(resolver.getResource("/test")));
assertEquals(Collections.emptyList(), childNames(resolver.getResource("/content")));
}
}

0 comments on commit 816ad26

Please sign in to comment.