ChecklistExporter { Optional<ForcedRoot> nodeToRoot(final Node node, final NamespaceMapping mapping) throws RepositoryException { if (Rules.lastMatch(pathScopes, node.getPath()).isExclude()) { return Optional.empty(); } ForcedRoot forcedRoot = new ForcedRoot(); forcedRoot.setPath(node.getPath()); final String primaryType = node.getPrimaryNodeType().getName(); if (Rules.lastMatch(nodeTypeFilters, QName.parseQName(mapping, QName.Type.NODETYPE, primaryType).toString()).isInclude()) { forcedRoot.setPrimaryType(primaryType); } final List<String> mixinTypes = Stream.of(node.getMixinNodeTypes()) .map(compose1(NodeType::getName, qName -> QName.parseQName(mapping, QName.Type.NODETYPE, qName).toString())) .filter(name -> Rules.lastMatch(nodeTypeFilters, name).isInclude()) .collect(Collectors.toList()); forcedRoot.setMixinTypes(mixinTypes); return Optional.of(forcedRoot); } private  ChecklistExporter(final List<Op> operations,
                              final List<String> exportTypeDefs,
                              final List<Rule> pathScopes,
                              final List<Rule> nodeTypeFilters,
                              final List<JcrNs> jcrNamespaces); void updateChecklist(final WriterOpener writerOpener,
                                final Session session,
                                final Checklist checklist,
                                final ForcedRootUpdatePolicy updatePolicy); List<ForcedRoot> findRoots(final Session session); static final ForcedRootUpdatePolicy DEFAULT_UPDATE_POLICY; static final String COVARIANT_PREFIX; }
@Test public void testNodeToRootSimple() throws Exception { TestUtil.withInMemoryRepo(session -> { final NamespaceMapping mapping = new NamespaceMapping(new SessionNamespaceResolver(session)); TestUtil.installCndFromURL(session, getClass().getResource("/sling_nodetypes.cnd")); ChecklistExporter exporter = new ChecklistExporter.Builder().build(); Node node1 = JcrUtils.getOrCreateByPath("/test/node1", "nt:folder", "sling:Folder", session, true); ForcedRoot root1 = exporter.nodeToRoot(node1, mapping).orElse(null); assertNotNull("root1 should be present", root1); assertEquals("root1 primary type should be sling:Folder", "sling:Folder", root1.getPrimaryType()); assertTrue("root1 mixin types should be empty", root1.getMixinTypes().isEmpty()); Node node2 = JcrUtils.getOrCreateByPath("/test/node2", "nt:folder", "sling:OrderedFolder", session, true); node2.addMixin(String.format("{%s}title", NamespaceHelper.MIX)); ForcedRoot root2 = exporter.nodeToRoot(node2, mapping).orElse(null); assertNotNull("root2 should be present", root2); assertEquals("root2 primary type should be sling:OrderedFolder", "sling:OrderedFolder", root2.getPrimaryType()); assertTrue("root2 mixin types should contain mix:title", root2.getMixinTypes().contains("mix:title")); }); }
@Test public void testNodeToRootPathScoped() throws Exception { TestUtil.withInMemoryRepo(session -> { final NamespaceMapping mapping = new NamespaceMapping(new SessionNamespaceResolver(session)); TestUtil.installCndFromURL(session, getClass().getResource("/sling_nodetypes.cnd")); Node node1 = JcrUtils.getOrCreateByPath("/test_include/node1", "nt:folder", session); Node node2 = JcrUtils.getOrCreateByPath("/test_exclude/node2", "nt:folder", session); ChecklistExporter exporter = new ChecklistExporter.Builder() .withScopePaths(Rules .fromJsonArray(arr(key("type", "include").key("pattern", "/test_include(/.*)?")) .get())).build(); ForcedRoot root1 = exporter.nodeToRoot(node1, mapping).orElse(null); assertNotNull("root1 should not be null", root1); ForcedRoot root2 = exporter.nodeToRoot(node2, mapping).orElse(null); assertNull("root2 should be null", root2); }); }
@Test public void testNodeToRootNodeTypeScoped() throws Exception { TestUtil.withInMemoryRepo(session -> { TestUtil.installCndFromURL(session, getClass().getResource("/sling_nodetypes.cnd")); final NamespaceMapping mapping = new NamespaceMapping(new SessionNamespaceResolver(session)); Node node1 = JcrUtils.getOrCreateByPath("/test/node1", "nt:folder", session); node1.addMixin("mix:title"); node1.addMixin("sling:Resource"); node1.addMixin("sling:ResourceSuperType"); ChecklistExporter mixExporter = new ChecklistExporter.Builder().withNodeTypeFilters( Rules.fromJsonArray(arr(key("type", "include").key("pattern", "mix:.*")) .get())).build(); ChecklistExporter slingExporter = new ChecklistExporter.Builder().withNodeTypeFilters( Rules.fromJsonArray(arr(key("type", "include").key("pattern", "sling:.*")) .get())).build(); ChecklistExporter rtExporter = new ChecklistExporter.Builder().withNodeTypeFilters( Rules.fromJsonArray(arr(key("type", "include").key("pattern", "sling:Resource")) .get())).build(); ForcedRoot mixRoot = mixExporter.nodeToRoot(node1, mapping).orElse(null); assertNotNull("mixRoot should not be null", mixRoot); assertTrue("mixRoot should contain mix:title mixin", mixRoot.getMixinTypes().contains("mix:title")); assertFalse("mixRoot should not contain sling:Resource mixin", mixRoot.getMixinTypes().contains("sling:Resource")); assertFalse("mixRoot should not contain sling:ResourceSuperType mixin", mixRoot.getMixinTypes().contains("sling:ResourceSuperType")); ForcedRoot slingRoot = slingExporter.nodeToRoot(node1, mapping).orElse(null); assertNotNull("slingRoot should not be null", slingRoot); assertFalse("slingRoot should not contain mix:title mixin", slingRoot.getMixinTypes().contains("mix:title")); assertTrue("slingRoot should contain sling:Resource mixin", slingRoot.getMixinTypes().contains("sling:Resource")); assertTrue("slingRoot should contain sling:ResourceSuperType mixin", slingRoot.getMixinTypes().contains("sling:ResourceSuperType")); ForcedRoot rtRoot = rtExporter.nodeToRoot(node1, mapping).orElse(null); assertNotNull("rtRoot should not be null", rtRoot); assertNull("rtRoot primaryType should be null", rtRoot.getPrimaryType()); assertFalse("rtRoot should not contain mix:title mixin", rtRoot.getMixinTypes().contains("mix:title")); assertTrue("rtRoot should contain sling:Resource mixin", rtRoot.getMixinTypes().contains("sling:Resource")); assertFalse("rtRoot should not contain sling:ResourceSuperType mixin", rtRoot.getMixinTypes().contains("sling:ResourceSuperType")); }); }