### Question:
PredefinedFields { public static void ensurePredefinedFields(EntityMetadata md) { ensureObjectType(md); List<ParentNewChild> l = new ArrayList<>(); FieldCursor cursor = md.getFieldCursor(); while (cursor.next()) { FieldTreeNode f = cursor.getCurrentNode(); if (f instanceof ArrayField) { ParentNewChild x = ensureArraySize(md, (ArrayField) f); if (x != null) { l.add(x); } } } for (ParentNewChild x : l) { x.parent.addNew(x.newChild); } } private  PredefinedFields(); static void ensurePredefinedFields(EntityMetadata md); static void updateArraySizes(EntityMetadata md, JsonNodeFactory factory, JsonDoc doc); static boolean isFieldObjectType(String fieldName); static boolean isFieldAnArrayCount(String fieldName, Fields metadataFields); static boolean isPredefinedField(String fieldName, Fields fields); static String createArrayFieldNameFromCountField(String countField); static String createArrayCountFieldName(String arrayFieldName); static final String OBJECTTYPE_FIELD; static final String FIELD_ARRAY_COUNT_POSTFIX; static final Path OBJECTTYPE_PATH; }### Answer:
@Test public void testAddPredef() throws Exception { EntityMetadata md = getMD1(); try { md.resolve(new Path("objectType")); Assert.fail(); } catch (Exception e) { } try { md.resolve(new Path("simpleArr#")); } catch (Exception e) { } try { md.resolve(new Path("nested.objArr#")); } catch (Exception e) { } PredefinedFields.ensurePredefinedFields(md); Extensions<JsonNode> x = new Extensions<>(); x.addDefaultExtensions(); JSONMetadataParser p = new JSONMetadataParser(x, new DefaultTypes(), JsonNodeFactory.withExactBigDecimals(false)); System.out.println(p.convert(md).toString()); Field f = (SimpleField) md.resolve(new Path("objectType")); Assert.assertEquals(StringType.TYPE, f.getType()); Assert.assertTrue(f.getAccess().getFind().getRoles().contains(MetadataConstants.ROLE_ANYONE)); f = (SimpleField) md.resolve(new Path("simpleArr#")); Assert.assertEquals(IntegerType.TYPE, f.getType()); Assert.assertTrue(f.getAccess().getFind().getRoles().contains(MetadataConstants.ROLE_ANYONE)); f = (SimpleField) md.resolve(new Path("obj1.nested.objArr#")); Assert.assertEquals(IntegerType.TYPE, f.getType()); Assert.assertTrue(f.getAccess().getFind().getRoles().contains(MetadataConstants.ROLE_ANYONE)); }
@Test public void testModPredef() throws Exception { EntityMetadata md = getMD2(); PredefinedFields.ensurePredefinedFields(md); Field f = (SimpleField) md.resolve(new Path("objectType")); Assert.assertEquals(StringType.TYPE, f.getType()); Assert.assertTrue(f.getAccess().getFind().getRoles().contains(MetadataConstants.ROLE_ANYONE)); Assert.assertTrue(f.getAccess().getUpdate().getRoles().contains(MetadataConstants.ROLE_NOONE)); f = (SimpleField) md.resolve(new Path("simpleArr#")); Assert.assertEquals(IntegerType.TYPE, f.getType()); Assert.assertTrue(f.getAccess().getFind().getRoles().contains(MetadataConstants.ROLE_ANYONE)); f = (SimpleField) md.resolve(new Path("obj1.nested.objArr#")); Assert.assertEquals(IntegerType.TYPE, f.getType()); Assert.assertTrue(f.getAccess().getFind().getRoles().contains(MetadataConstants.ROLE_ANYONE)); }
@Test public void testInvalidObjectType() throws Exception { EntityMetadata md = new EntityMetadata("test"); md.getFields().addNew(new SimpleField("objectType", IntegerType.TYPE)); try { PredefinedFields.ensurePredefinedFields(md); Assert.fail(); } catch (Exception e) { } }
@Test public void testInvalidArr() throws Exception { EntityMetadata md = new EntityMetadata("test"); md.getFields().addNew(new ArrayField("x", new SimpleArrayElement(IntegerType.TYPE))); md.getFields().addNew(new SimpleField("x#", StringType.TYPE)); try { PredefinedFields.ensurePredefinedFields(md); Assert.fail(); } catch (Exception e) { } }