@Test public void testFindAndReplaceAllReferencesSchemasFull() { String referenceId = new String("rootOne"); ObjectSchema referenced = ObjectSchema.createNewWithId(referenceId); HashMap<String, ObjectSchema> registry = new HashMap<String, ObjectSchema>(); // Add the referenced schema to the register. registry.put(referenceId, referenced); // Create a third self ObjectSchema self = ObjectSchema.createNewWithId(new String("self")); ObjectSchema refToSelf = new ObjectSchema(); refToSelf.setRef(new String(ObjectSchema.SELF_REFERENCE)); ObjectSchema referenceToOther = new ObjectSchema(); referenceToOther.setRef(referenceId); // Add references in all places self.putProperty("one", referenceToOther); self.putAdditionalProperty("two", referenceToOther); self.setItems(refToSelf); self.setAdditionalItems(refToSelf); self.setImplements(new ObjectSchema[] {referenceToOther}); // find and replace PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(registry, self); // Make sure there are no references Iterator<ObjectSchema> it = self.getSubSchemaIterator(); while (it.hasNext()) { ObjectSchema toTest = it.next(); assertTrue(toTest.getRef() == null); } }
@Test public void testNestedObjects() throws JSONObjectAdapterException { // Create an object with nesting ObjectSchema root = new ObjectSchema(); root.setName("Root"); root.setId(new String("root")); // Create a child class ObjectSchema child = new ObjectSchema(); child.setName("Child"); child.setType(TYPE.OBJECT); root.putProperty("childInstance1", child); // Create a grand child ObjectSchema grand = new ObjectSchema(); grand.setName("Grand"); grand.setType(TYPE.OBJECT); String grandId = new String("grand"); grand.setId(grandId); child.putProperty("grandChildInstance1", grand); ObjectSchema grandRef = new ObjectSchema(); grandRef.setRef(grandId); child.putProperty("grandChildInstance2", grandRef); System.out.println(root.toJSONString(new JSONObjectAdapterImpl())); List<ObjectSchema> list = new ArrayList<ObjectSchema>(); list.add(root); // Now before the are replaces this should be a references ObjectSchema test = child.getProperties().get("grandChildInstance2"); assertNotNull(test); assertEquals(grandId, test.getRef()); Map<String, ObjectSchema> register = PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list); PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(register, list); // Validate that the nest grand child reference is replaced test = child.getProperties().get("grandChildInstance2"); assertNotNull(test); assertEquals(null, test.getRef()); assertEquals(grand, test); }