@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 testAddRemoveNewChildAndStore() 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
    item.removeChild(newChild);

    // 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 testRemoveChild() throws Exception {
    // GIVEN
    JcrNodeAdapter item = new JcrNodeAdapter(baseNode);
    // Create a child node
    Node child = baseNode.addNode("child");
    JcrNodeAdapter childItem = new JcrNodeAdapter(child);
    item.addChild(childItem);
    // Create one new child Item
    JcrNewNodeAdapter newChild = new JcrNewNodeAdapter(baseNode, "mgnl:content");
    item.addChild(newChild);

    // WHEN
    boolean resBoolean = item.removeChild(childItem);

    // THEN
    Map<String, AbstractJcrNodeAdapter> res = item.getChildren();
    assertEquals(true, resBoolean);
    assertEquals(1, res.size());
    assertEquals(null, res.get(childItem.getNodeName()));
    assertEquals(newChild, res.get(newChild.getNodeName()));
    assertEquals(item, newChild.getParent());
    Map<String, AbstractJcrNodeAdapter> resRemobed = item.getRemovedChildren();
    assertEquals(1, resRemobed.size());
    assertEquals(childItem, resRemobed.get(childItem.getNodeName()));
  }
  @Test
  public void testAddExistingChildAndStore() 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 a child node
    Node child = baseNode.addNode("childNode");
    JcrNodeAdapter childItem = new JcrNodeAdapter(child);
    item.addChild(childItem);
    // Add property to the child Item
    DefaultProperty<String> childProperty =
        new DefaultProperty<String>(String.class, "childPropertyValue");
    childItem.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());
  }
  @Test
  public void testAddChild() throws Exception {
    // GIVEN
    JcrNodeAdapter item = new JcrNodeAdapter(baseNode);
    // Create a child node
    Node child = baseNode.addNode("child");
    JcrNodeAdapter childItem = new JcrNodeAdapter(child);
    item.addChild(childItem);
    // Create one new child Item
    JcrNewNodeAdapter newChild = new JcrNewNodeAdapter(baseNode, "mgnl:content");
    item.addChild(newChild);

    // WHEN
    Map<String, AbstractJcrNodeAdapter> res = item.getChildren();

    // THEN
    assertNotNull(res);
    assertEquals(2, res.size());
    assertEquals(childItem, res.get(childItem.getNodeName()));
    assertEquals(item, childItem.getParent());
    assertEquals(newChild, res.get(newChild.getNodeName()));
    assertEquals(item, newChild.getParent());
  }