### Question:
PageRewriteRule implements PageStructureRewriteRule { @Override public Node applyTo(Node root, Set<Node> finalNodes) throws RepositoryException { if (root.hasProperty(NameConstants.PN_DESIGN_PATH)) { root.getProperty(NameConstants.PN_DESIGN_PATH).remove(); } List<String> remove = Arrays.asList(componentsToRemove); NodeIterator it = root.getNodes(); while (it.hasNext()) { Node child = it.nextNode(); if (remove.contains(child.getName())) { child.remove(); } } Property template = root.getProperty(NameConstants.NN_TEMPLATE); template.setValue(editableTemplate); Property resourceType = root.getProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY); resourceType.setValue(slingResourceType); Node responsiveGrid = root.addNode(RESPONSIVE_GRID_NODE_NAME, JcrConstants.NT_UNSTRUCTURED); responsiveGrid.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, RewriteUtils.RESPONSIVE_GRID_RES_TYPE); List<String> names = moveStructure(root, responsiveGrid, nested); for (String key : componentOrdering.keySet()) { if (RESPONSIVE_GRID_NODE_NAME.equals(key)) { continue; } if (!responsiveGrid.hasNode(key)) { Node child = responsiveGrid.addNode(key, JcrConstants.NT_UNSTRUCTURED); child.setProperty(JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY, RewriteUtils.RESPONSIVE_GRID_RES_TYPE); } Node child = responsiveGrid.getNode(key); List<String> cn = moveStructure(root, child, names); reorderChildren(cn, componentOrdering.get(key), child); } reorderChildren(names, componentOrdering.get(RESPONSIVE_GRID_NODE_NAME), responsiveGrid); return root; }  @Override boolean matches(Node root); @Override Node applyTo(Node root, Set<Node> finalNodes); @Override String getStaticTemplate(); @Override int getRanking();  }### Answer:
@Test public void testApplyTo() throws Exception { Node rootNode = context.resourceResolver().getResource(ROOTS_PATH + "/matches/jcr:content").adaptTo(Node.class); ServiceReference ref = bundleContext.getServiceReference(StructureRewriteRule.class.getName()); StructureRewriteRule rule = bundleContext.<StructureRewriteRule>getService(ref); Node rewrittenNode = rule.applyTo(rootNode, new HashSet<>()); assertFalse(rewrittenNode.hasProperty("cq:designPath")); assertEquals(EDITABLE_TEMPLATE, rewrittenNode.getProperty("cq:template").getString()); assertEquals(SLING_RESOURCE_TYPE, rewrittenNode.getProperty("sling:resourceType").getString()); Node rootContainer = rewrittenNode.getNode("root"); assertNotNull(rootContainer); assertEquals("nt:unstructured", rootContainer.getPrimaryNodeType().getName()); assertEquals("wcm/foundation/components/responsivegrid", rootContainer.getProperty("sling:resourceType").getString()); NodeIterator children = rootContainer.getNodes(); assertTrue(children.hasNext()); Node rg = children.nextNode(); assertEquals("responsivegrid", rg.getName()); assertTrue(children.hasNext()); assertEquals("responsivegrid_12345", children.nextNode().getName()); assertFalse(children.hasNext()); NodeIterator rgChildren = rg.getNodes(); assertEquals("header", rgChildren.nextNode().getName()); assertTrue(rgChildren.hasNext()); assertEquals("title", rgChildren.nextNode().getName()); assertTrue(rgChildren.hasNext()); assertEquals("responsivegrid", rgChildren.nextNode().getName()); assertFalse(rgChildren.hasNext()); }