### Question:
ParsysRewriteRule implements StructureRewriteRule { @Override public Node applyTo(Node root, Set<Node> finalNodes) throws RewriteException, RepositoryException { String name = root.getName(); Node parent = root.getParent(); String orderBefore = null; NodeIterator siblings = parent.getNodes(); while (siblings.hasNext()) { Node sib = siblings.nextNode(); if (sib.getName().equals(name)) { orderBefore = siblings.hasNext() ? siblings.nextNode().getName() : null; break; } } RewriteUtils.rename(root); Node grid = parent.addNode(name, root.getPrimaryNodeType().getName()); grid.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, RewriteUtils.RESPONSIVE_GRID_RES_TYPE); parent.orderBefore(name, orderBefore); NodeIterator children = root.getNodes(); while (children.hasNext()) { Node child = children.nextNode(); JcrUtil.copy(child, grid, child.getName()); } root.remove(); return grid; }  @Override boolean matches(Node root); @Override Node applyTo(Node root, Set<Node> finalNodes); @Override int getRanking();  }### Answer:
@Test public void testApplyTo() throws Exception { Node rootNode = context.resourceResolver().getResource(ROOTS_PATH + "/matches/jcr:content/par").adaptTo(Node.class); ParsysRewriteRule rule = new ParsysRewriteRule(); Node rewrittenNode = rule.applyTo(rootNode, new LinkedHashSet<>()); assertEquals(RewriteUtils.RESPONSIVE_GRID_RES_TYPE, rewrittenNode.getProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY).getString()); assertEquals(JcrConstants.NT_UNSTRUCTURED, rewrittenNode.getPrimaryNodeType().getName()); NodeIterator children = rewrittenNode.getNodes(); assertTrue(children.hasNext()); assertEquals("colctrl", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("image", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("title_1", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("text_1", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("col_break", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("image_0", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("title_2", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("text_0", children.nextNode().getName()); assertTrue(children.hasNext()); assertEquals("col_end", children.nextNode().getName()); NodeIterator siblings = rewrittenNode.getParent().getNodes(); assertTrue(siblings.hasNext()); assertEquals("par", siblings.nextNode().getName()); assertTrue(siblings.hasNext()); assertEquals("toBeRemoved", siblings.nextNode().getName()); assertTrue(siblings.hasNext()); assertEquals("title", siblings.nextNode().getName()); assertTrue(siblings.hasNext()); assertEquals("header", siblings.nextNode().getName()); assertTrue(siblings.hasNext()); assertEquals("rightpar", siblings.nextNode().getName()); assertFalse(siblings.hasNext()); }