-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runner): add option to control running descendant steps on exist…
…ing nodes
- Loading branch information
1 parent
57162df
commit 38c9557
Showing
8 changed files
with
181 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
src/test/java/com/swisscom/aem/tools/impl/hops/CreateChildNodeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
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 static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
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.ConflictResolution; | ||
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.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
@ExtendWith(AemContextExtension.class) | ||
class CreateChildNodeTest { | ||
|
||
public final AemContext context = new JcrOakAemContext(); | ||
|
||
private RunnerBuilder builder; | ||
private Session session; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
context.create().resource("/content"); | ||
context.create().resource("/content/child"); | ||
context.create().resource("/content/child/one"); | ||
|
||
builder = Runner.builder().addHop(new CreateChildNode()); | ||
session = context.resourceResolver().adaptTo(Session.class); | ||
} | ||
|
||
@Test | ||
public void create_nonexisting() throws RepositoryException, HopperException { | ||
builder.build(new Script(new CreateChildNode.Config().withName("child-two"))).run(session.getNode("/content"), true); | ||
|
||
assertEquals(Arrays.asList("child", "child-two"), childNames(context.resourceResolver().getResource("/content"))); | ||
assertEquals("nt:unstructured", session.getNode("/content/child-two").getPrimaryNodeType().getName()); | ||
} | ||
|
||
@Test | ||
public void create_existing_abort() { | ||
assertThrows(HopperException.class, () -> { | ||
builder | ||
.build( | ||
new Script( | ||
new CreateChildNode.Config().withName("child").withConflict(ConflictResolution.THROW), | ||
new CreateChildNode.Config().withName("child2").withConflict(ConflictResolution.THROW) | ||
) | ||
) | ||
.run(session.getNode("/content"), true); | ||
}); | ||
|
||
assertEquals(Collections.singletonList("child"), childNames(context.resourceResolver().getResource("/content"))); | ||
} | ||
|
||
@Test | ||
public void create_existing_ignore_no_recurse() throws RepositoryException, HopperException { | ||
builder | ||
.build( | ||
new Script( | ||
new CreateChildNode.Config() | ||
.withName("child") | ||
.withConflict(ConflictResolution.IGNORE) | ||
.withRunOnExistingNode(false) | ||
.withHops(Collections.singletonList(new CreateChildNode.Config().withName("../child3"))), | ||
new CreateChildNode.Config().withName("child2").withConflict(ConflictResolution.THROW) | ||
) | ||
) | ||
.run(session.getNode("/content"), true); | ||
assertEquals(Arrays.asList("child", "child2"), childNames(context.resourceResolver().getResource("/content"))); | ||
} | ||
|
||
@Test | ||
public void create_existing_ignore_recurse() throws RepositoryException, HopperException { | ||
builder | ||
.build( | ||
new Script( | ||
new CreateChildNode.Config() | ||
.withName("child") | ||
.withConflict(ConflictResolution.IGNORE) | ||
.withRunOnExistingNode(true) | ||
.withHops(Collections.singletonList(new CreateChildNode.Config().withName("../child3"))), | ||
new CreateChildNode.Config().withName("child2").withConflict(ConflictResolution.THROW) | ||
) | ||
) | ||
.run(session.getNode("/content"), true); | ||
assertEquals(Arrays.asList("child", "child3", "child2"), childNames(context.resourceResolver().getResource("/content"))); | ||
} | ||
|
||
@Test | ||
public void create_existing_force() throws RepositoryException, HopperException { | ||
assertEquals("nt:unstructured", session.getNode("/content/child").getPrimaryNodeType().getName()); | ||
builder | ||
.build( | ||
new Script( | ||
new CreateChildNode.Config() | ||
.withName("child") | ||
.withPrimaryType("cq:Page") | ||
.withConflict(ConflictResolution.FORCE) | ||
.withHops(Collections.singletonList(new CreateChildNode.Config().withName("../child3"))), | ||
new CreateChildNode.Config().withName("child2").withConflict(ConflictResolution.THROW) | ||
) | ||
) | ||
.run(session.getNode("/content"), true); | ||
assertEquals(Arrays.asList("child", "child3", "child2"), childNames(context.resourceResolver().getResource("/content"))); | ||
assertEquals("cq:Page", session.getNode("/content/child").getPrimaryNodeType().getName()); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/test/java/com/swisscom/aem/tools/testsupport/AemUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.swisscom.aem.tools.testsupport; | ||
|
||
import java.util.List; | ||
import java.util.Spliterator; | ||
import java.util.Spliterators; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.StreamSupport; | ||
import org.apache.sling.api.resource.Resource; | ||
|
||
public final class AemUtil { | ||
|
||
public static List<String> childNames(Resource parent) { | ||
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(parent.listChildren(), Spliterator.ORDERED), false) | ||
.map(Resource::getName) | ||
.collect(Collectors.toList()); | ||
} | ||
} |