@Test
  public void testCycle() throws JSONObjectAdapterException {
    // Create an object with nesting
    ObjectSchema root = new ObjectSchema();
    root.setName("Root");
    String rootId = new String("root");
    root.setId(rootId);
    // Create a child class
    ObjectSchema child = new ObjectSchema();
    String childId = new String("child");
    child.setName("Child");
    child.setId(childId);
    child.setType(TYPE.OBJECT);
    root.putProperty("childInstance1", child);
    // Add a self reference child
    ObjectSchema rootRef = new ObjectSchema();
    rootRef.setRef(rootId);
    child.putProperty("rootRef", rootRef);
    // Create a grand child

    List<ObjectSchema> list = new ArrayList<ObjectSchema>();
    list.add(root);
    Map<String, ObjectSchema> register =
        PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
    PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(register, list);
  }
 @Test
 public void testLoadedInterfaceNoMembers() throws Exception {
   String[] namesToLoad =
       new String[] {
         "InterfaceA.json", "AImpl.json",
       };
   List<ObjectSchema> schemaList = new ArrayList<ObjectSchema>();
   for (String name : namesToLoad) {
     String fileString =
         FileUtils.loadFileAsStringFromClasspath(
             PojoGeneratorDriverTest.class.getClassLoader(), name);
     ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(fileString));
     schema.setId(schema.getName());
     schemaList.add(schema);
   }
   JCodeModel codeModel = new JCodeModel();
   driver.createAllClasses(codeModel, schemaList);
   // Get the class
   JPackage _package = codeModel._package("");
   JDefinedClass impl = null;
   try {
     impl = _package._class("AImpl");
   } catch (JClassAlreadyExistsException e) {
     impl = e.getExistingClass();
   }
   String classString = declareToString(impl);
   //		System.out.println(classString);
   Map<String, JFieldVar> fields = impl.fields();
   assertNotNull(fields);
   assertNotNull(fields.get("fromInterfaceA"));
   assertNotNull(fields.get("alsoFromInterfaceA"));
 }
 @Before
 public void before() {
   driver = new PojoGeneratorDriver(new HandlerFactoryImpl03());
   schema = new ObjectSchema();
   schema.setName("SampleClass");
   schema.setId("org.sample." + schema.getName());
 }
  @Test
  public void testInterfaceField() throws JSONObjectAdapterException, ClassNotFoundException {
    // Create an object with nesting
    ObjectSchema inter = new ObjectSchema();
    inter.setName("SomeInterface");
    inter.setType(TYPE.INTERFACE);
    inter.setId("example.org.SomeInterface");

    ObjectSchema interRef = new ObjectSchema();
    interRef.setRef(inter.getId());

    ObjectSchema impl = new ObjectSchema();
    impl.setName("SomeInterfaceImpl");
    impl.setType(TYPE.OBJECT);
    impl.setId("example.org.SomeInterfaceImpl");
    impl.setImplements(new ObjectSchema[] {interRef});

    ObjectSchema root = new ObjectSchema();
    root.setName("Root");
    root.setId(new String("root"));
    root.setType(TYPE.OBJECT);

    // Create a child class
    ObjectSchema child = new ObjectSchema();
    String childId = new String("child");
    child.setName("Child");
    child.setId(childId);
    child.setType(TYPE.OBJECT);
    child.setRef(inter.getId());
    root.putProperty("interfaceField", child);

    List<ObjectSchema> list = new ArrayList<ObjectSchema>();
    list.add(inter);
    list.add(impl);
    list.add(root);
    //		Map<String, ObjectSchema> register =
    // PojoGeneratorDriver.registerAllIdentifiedObjectSchemas(list);
    //		List<ObjectSchema> schemaList =
    // PojoGeneratorDriver.findAndReplaceAllReferencesSchemas(register, list);
    JCodeModel codeModel = new JCodeModel();
    driver.createAllClasses(codeModel, list);
  }
 @Test
 public void testRecursivlyCreateAllTypesNumber() throws ClassNotFoundException {
   ObjectSchema schema = new ObjectSchema();
   schema.setType(TYPE.NUMBER);
   JCodeModel codeModel = new JCodeModel();
   schema.setId("org.sample.SampleClass");
   JPackage _package = codeModel._package("org.sample");
   JType type = driver.createOrGetType(codeModel, schema);
   assertNotNull(type);
   assertEquals(Double.class.getName(), type.fullName());
 }
 @Test
 public void testLoadedInterfaces() throws Exception {
   String[] namesToLoad =
       new String[] {
         "InterfaceA.json", "InterfaceB.json", "ABImpl.json",
       };
   List<ObjectSchema> schemaList = new ArrayList<ObjectSchema>();
   for (String name : namesToLoad) {
     String fileString =
         FileUtils.loadFileAsStringFromClasspath(
             PojoGeneratorDriverTest.class.getClassLoader(), name);
     ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(fileString));
     //			schema.setName(name);
     schema.setId(schema.getName());
     schemaList.add(schema);
   }
   JCodeModel codeModel = new JCodeModel();
   driver.createAllClasses(codeModel, schemaList);
   // Get the class
   JPackage _package = codeModel._package("");
   JDefinedClass impl = null;
   try {
     impl = _package._class("ABImpl");
   } catch (JClassAlreadyExistsException e) {
     impl = e.getExistingClass();
   }
   String classString = declareToString(impl);
   //		System.out.println(classString);
   Iterator<JClass> it = impl._implements();
   assertNotNull(it);
   String intA = "InterfaceA";
   String intB = "InterfaceB";
   String jsonEntity = "JSONEntity";
   Map<String, JClass> map = new HashMap<String, JClass>();
   while (it.hasNext()) {
     JClass impClass = it.next();
     if (intA.equals(impClass.name())) {
       map.put(intA, impClass);
     } else if (intB.equals(impClass.name())) {
       map.put(intB, impClass);
     } else if (jsonEntity.equals(impClass.name())) {
       map.put(jsonEntity, impClass);
     }
   }
   assertEquals("Should have implemented two interfaces", 3, map.size());
   // Now get the fields from the object an confirm they are all there
   Map<String, JFieldVar> fields = impl.fields();
   assertNotNull(fields);
   assertEquals(6, fields.size());
   assertNotNull(fields.get("fromInterfaceA"));
   assertNotNull(fields.get("alsoFromInterfaceB"));
   assertNotNull(fields.get("fromMe"));
 }
  @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);
  }
 @Test
 public void testCreateOrGetTypeExtends() throws ClassNotFoundException {
   JCodeModel codeModel = new JCodeModel();
   ObjectSchema parent = new ObjectSchema();
   parent.setType(TYPE.OBJECT);
   parent.setName("ParentClass");
   parent.setId("org.sample." + parent.getName());
   schema.setExtends(parent);
   schema.setType(TYPE.OBJECT);
   schema.setName("ChildClass");
   JType type = driver.createOrGetType(codeModel, schema);
   assertNotNull(type);
   JDefinedClass def = (JDefinedClass) type;
   String classDeffString = declareToString(def);
   assertTrue(classDeffString.indexOf("extends org.sample.ParentClass") > 0);
 }
 @Test
 public void testCreateOrGetTypeImplements() throws ClassNotFoundException {
   JCodeModel codeModel = new JCodeModel();
   ObjectSchema parent = new ObjectSchema();
   parent.setType(TYPE.INTERFACE);
   parent.setName("ParentInterface");
   parent.setId("org.sample." + parent.getName());
   schema.setImplements(new ObjectSchema[] {parent});
   schema.setType(TYPE.OBJECT);
   schema.setName("ChildClass");
   JType type = driver.createOrGetType(codeModel, schema);
   assertNotNull(type);
   JDefinedClass def = (JDefinedClass) type;
   String classDeffString = declareToString(def);
   //		System.out.println(classDeffString);
   assertTrue(
       classDeffString.indexOf(
               "implements java.io.Serializable, org.sagebionetworks.schema.adapter.JSONEntity, org.sample.ParentInterface")
           > 0);
 }
  @Test
  public void testCreateAllClassesEnum() throws Exception {
    String[] namesToLoad =
        new String[] {
          "PetEnum.json",
        };
    List<ObjectSchema> schemaList = new ArrayList<ObjectSchema>();
    for (String name : namesToLoad) {
      String fileString =
          FileUtils.loadFileAsStringFromClasspath(
              PojoGeneratorDriverTest.class.getClassLoader(), name);
      ObjectSchema schema = new ObjectSchema(new JSONObjectAdapterImpl(fileString));
      schema.setId(schema.getName());
      schemaList.add(schema);
    }
    JCodeModel codeModel = new JCodeModel();
    driver.createAllClasses(codeModel, schemaList);
    // Get the class
    JPackage _package = codeModel._package("");
    JDefinedClass impl = null;
    try {
      impl = _package._class("PetEnum");
    } catch (JClassAlreadyExistsException e) {
      impl = e.getExistingClass();
    }
    String classString = declareToString(impl);
    System.out.println(classString);

    Map<String, JFieldVar> fields = impl.fields();
    assertNotNull(fields);
    // Enums should have no fields
    assertEquals(1, fields.size());
    Collection<JMethod> methods = impl.methods();
    assertNotNull(methods);
    // enums should have no methods
    assertEquals(0, methods.size());
    // Enums should have no constructors
    assertFalse(impl.constructors().hasNext());
  }