TreeUtil { public static void autoCreateItems(@NotNull Tree tree, @NotNull Tree type, @NotNull Tree typeRoot, @Nullable String userID) throws RepositoryException { Tree properties = type.getChild(REP_NAMED_PROPERTY_DEFINITIONS); for (Tree definitions : properties.getChildren()) { String name = definitions.getName(); if (name.equals(NodeTypeConstants.REP_PRIMARY_TYPE) || name.equals(NodeTypeConstants.REP_MIXIN_TYPES)) { continue; } else if (name.equals(NodeTypeConstants.REP_UUID)) { name = JCR_UUID; } for (Tree definition : definitions.getChildren()) { if (getBoolean(definition, JCR_AUTOCREATED)) { if (!tree.hasProperty(name)) { PropertyState property = autoCreateProperty(name, definition, userID); if (property != null) { tree.setProperty(property); } else { throw new RepositoryException( "Unable to auto-create value for " + PathUtils.concat(tree.getPath(), name)); } } break; } } } Tree childNodes = type.getChild(REP_NAMED_CHILD_NODE_DEFINITIONS); for (Tree definitions : childNodes.getChildren()) { String name = definitions.getName(); for (Tree definition : definitions.getChildren()) { if (getBoolean(definition, JCR_AUTOCREATED)) { if (!tree.hasChild(name)) { String typeName = getName(definition, JCR_DEFAULTPRIMARYTYPE); addChild(tree, name, typeName, typeRoot, userID); } break; } } } } private  TreeUtil(); @Nullable static String getPrimaryTypeName(@NotNull Tree tree); @Nullable static String getPrimaryTypeName(@NotNull Tree tree, @NotNull LazyValue<Tree> readOnlyTree); @NotNull static Iterable<String> getMixinTypeNames(@NotNull Tree tree); @NotNull static Iterable<String> getMixinTypeNames(@NotNull Tree tree, @NotNull LazyValue<Tree> readOnlyTree); @Nullable static Iterable<String> getStrings(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getString(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getString(@NotNull Tree tree, @NotNull String name, @Nullable String defaultValue); static boolean getBoolean(@NotNull Tree tree, @NotNull String propertyName); @Nullable static String getName(@NotNull Tree tree, @NotNull String name); @NotNull static Iterable<String> getNames(@NotNull Tree tree, @NotNull String name); static long getLong(@NotNull Tree tree, @NotNull String name, long defaultValue); @Nullable static Tree getTree(@NotNull Tree tree, @NotNull String path); static Tree addChild(
            @NotNull Tree parent, @NotNull String name,
            @Nullable String typeName, @NotNull Tree typeRoot,
            @Nullable String userID); @NotNull static Tree addChild(@NotNull Tree tree, @NotNull String childName, @NotNull String primaryTypeName); @NotNull static Tree getOrAddChild(@NotNull Tree tree, @NotNull String childName, @NotNull String primaryTypeName); static void addMixin(@NotNull Tree tree, @NotNull String mixinName, @NotNull Tree typeRoot, @Nullable String userID); static void autoCreateItems(@NotNull Tree tree, @NotNull Tree type, @NotNull Tree typeRoot, @Nullable String userID); static PropertyState autoCreateProperty(@NotNull String name,
                                                   @NotNull Tree definition,
                                                   @Nullable String userID); static String getDefaultChildType(
            Tree typeRoot, Tree parent, String childName); static List<Tree> getEffectiveType(Tree tree, Tree typeRoot); static String findDefaultPrimaryType(Tree definitions, boolean sns); static boolean isNodeType(Tree tree, String typeName, Tree typeRoot); static boolean isReadOnlyTree(@NotNull Tree tree);  }
@Test(expected = RepositoryException.class) public void testAutoCreateItemsMissingDefaultValue() throws Exception { Tree definition = when(mock(Tree.class).getProperty(JCR_AUTOCREATED)).thenReturn(PropertyStates.createProperty(JCR_AUTOCREATED, true)).getMock(); Tree definitions = mockTree("/some/path/unknownProperty", true); when(definitions.getChildren()).thenReturn(ImmutableList.of(definition)); Tree defWithExcludedProperties = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of(definitions)).getMock(); when(ntDef.getChild(REP_NAMED_PROPERTY_DEFINITIONS)).thenReturn(defWithExcludedProperties); Tree defWithoutChildren = when(mock(Tree.class).getChildren()).thenReturn(ImmutableList.of()).getMock(); when(ntDef.getChild(REP_NAMED_CHILD_NODE_DEFINITIONS)).thenReturn(defWithoutChildren); when(child.hasProperty("unknownProperty")).thenReturn(false); TreeUtil.autoCreateItems(child, ntDef, typeRoot, "userId"); }