@Test public void testAddRemoveExistingChildAndStore() throws Exception { // GIVEN JcrNodeAdapter item = new JcrNodeAdapter(baseNode); // Add property to the Item DefaultProperty<String> property = new DefaultProperty<String>(String.class, "propertyValue"); item.addItemProperty("propertyName", property); // Create one new child Item // Create a child node Node child = baseNode.addNode("childNode"); JcrNodeAdapter childItem = new JcrNodeAdapter(child); item.addChild(childItem); assertEquals(true, baseNode.hasNode("childNode")); // Add property to the child Item DefaultProperty<String> childProperty = new DefaultProperty<String>(String.class, "childPropertyValue"); childItem.addItemProperty("childPropertyName", childProperty); // WHEN item.removeChild(childItem); // THEN // Get node Node res = item.applyChanges(); assertNotNull(res); assertEquals(baseNode, res); assertEquals(true, res.hasProperty("propertyName")); assertEquals("propertyValue", res.getProperty("propertyName").getValue().getString()); assertEquals(false, res.hasNode("childNode")); }
@Test public void testReOrderingOfChildNodesWithRemovedChild() throws Exception { // GIVEN ((MockNode) baseNode).setPrimaryNodeType(new MockNodeType(NodeTypes.Content.NAME, null, true)); baseNode.addNode("child-a"); baseNode.addNode("child-b"); baseNode.addNode("child-c"); JcrNodeAdapter item = new JcrNodeAdapter(baseNode); item.removeChild(new JcrNodeAdapter(baseNode.getNode("child-c"))); item.addChild(new JcrNodeAdapter(baseNode.getNode("child-b"))); item.addChild(new JcrNodeAdapter(baseNode.getNode("child-a"))); // WHEN item.applyChanges(); // THEN List<String> nodes = Lists.newArrayList( Iterators.transform( item.getJcrItem().getNodes(), new Function<Node, String>() { @Nullable @Override public String apply(@Nullable Node node) { return NodeUtil.getName(node); } })); assertThat(nodes, contains("child-b", "child-a")); }
@Test public void testAddNewChildAndStore() throws Exception { // GIVEN JcrNodeAdapter item = new JcrNodeAdapter(baseNode); // Add property to the Item DefaultProperty<String> property = new DefaultProperty<String>(String.class, "propertyValue"); item.addItemProperty("propertyName", property); // Create one new child Item JcrNewNodeAdapter newChild = new JcrNewNodeAdapter(baseNode, "mgnl:content", "childNode"); item.addChild(newChild); // Add property to the child Item DefaultProperty<String> childProperty = new DefaultProperty<String>(String.class, "childPropertyValue"); newChild.addItemProperty("childPropertyName", childProperty); // WHEN Node res = item.applyChanges(); // THEN assertNotNull(res); assertEquals(baseNode, res); assertEquals(true, res.hasProperty("propertyName")); assertEquals("propertyValue", res.getProperty("propertyName").getValue().getString()); assertEquals(true, res.hasNode("childNode")); assertEquals(true, res.getNode("childNode").hasProperty("childPropertyName")); assertEquals( "childPropertyValue", res.getNode("childNode").getProperty("childPropertyName").getValue().getString()); }