EDBConverter { public List<EDBObject> convertModelToEDBObject(Object model, ConnectorInformation info) { if (!OpenEngSBModel.class.isAssignableFrom(model.getClass())) { throw new IllegalArgumentException("This function need to get a model passed"); } List<EDBObject> objects = new ArrayList<>(); if (model != null) { convertSubModel((OpenEngSBModel) model, objects, info); } return objects; } EDBConverter(EngineeringDatabaseService edbService); @SuppressWarnings("unchecked") T convertEDBObjectToModel(Class<T> model, EDBObject object); List<T> convertEDBObjectsToModelObjects(Class<T> model, List<EDBObject> objects); ConvertedCommit convertEKBCommit(EKBCommit commit); List<EDBObject> convertModelsToEDBObjects(List<OpenEngSBModel> models, ConnectorInformation info); List<EDBObject> convertModelToEDBObject(Object model, ConnectorInformation info); static String getEntryNameForMapKey(String property, Integer index); static String getEntryNameForMapValue(String property, Integer index); static String getEntryNameForList(String property, Integer index); static String getEOReferenceStringFromAnnotation(OpenEngSBForeignKey key); static final String FILEWRAPPER_FILENAME_SUFFIX; static final String REFERENCE_PREFIX; }
@Test public void primitivePropertyConversion_toEDBObject_works() { PrimitivePropertiesModel model = new PrimitivePropertiesModel(); model.setId("test"); model.setBooleanByGet(true); model.setBooleanByIs(true); model.setPrimitiveChar(Character.MAX_VALUE); model.setPrimitiveDouble(Double.MAX_VALUE); model.setPrimitiveFloat(Float.MAX_VALUE); model.setPrimitiveInt(Integer.MAX_VALUE); model.setPrimitiveLong(Long.MAX_VALUE); model.setPrimitiveShort(Short.MAX_VALUE); ConnectorInformation id = getTestConnectorInformation(); List<EDBObject> objects = converter.convertModelToEDBObject(model, id); EDBObject object = objects.get(0); assertThat(object.get("booleanByGet").getType(), is(Boolean.class.getName())); assertThat(object.get("booleanByIs").getType(), is(Boolean.class.getName())); assertThat(object.get("primitiveChar").getType(), is(Character.class.getName())); assertThat(object.get("primitiveShort").getType(), is(Short.class.getName())); assertThat(object.get("primitiveInt").getType(), is(Integer.class.getName())); assertThat(object.get("primitiveLong").getType(), is(Long.class.getName())); assertThat(object.get("primitiveFloat").getType(), is(Float.class.getName())); assertThat(object.get("primitiveDouble").getType(), is(Double.class.getName())); assertThat(object.getBoolean("booleanByGet"), is(true)); assertThat(object.getBoolean("booleanByIs"), is(true)); assertThat(object.getChar("primitiveChar"), is(Character.MAX_VALUE)); assertThat(object.getShort("primitiveShort"), is(Short.MAX_VALUE)); assertThat(object.getInteger("primitiveInt"), is(Integer.MAX_VALUE)); assertThat(object.getLong("primitiveLong"), is(Long.MAX_VALUE)); assertThat(object.getFloat("primitiveFloat"), is(Float.MAX_VALUE)); assertThat(object.getDouble("primitiveDouble"), is(Double.MAX_VALUE)); }
@Test public void wrappedPropertyConversion_toEDBObject_works() { WrappedPropertiesModel model = new WrappedPropertiesModel(); model.setId("test"); model.setBooleanByGet(true); model.setBooleanByIs(true); model.setWrappedChar(Character.MAX_VALUE); model.setWrappedDouble(Double.MAX_VALUE); model.setWrappedFloat(Float.MAX_VALUE); model.setWrappedInt(Integer.MAX_VALUE); model.setWrappedLong(Long.MAX_VALUE); model.setWrappedShort(Short.MAX_VALUE); ConnectorInformation id = getTestConnectorInformation(); List<EDBObject> objects = converter.convertModelToEDBObject(model, id); EDBObject object = objects.get(0); assertThat(object.get("booleanByGet").getType(), is(Boolean.class.getName())); assertThat(object.get("booleanByIs").getType(), is(Boolean.class.getName())); assertThat(object.get("wrappedChar").getType(), is(Character.class.getName())); assertThat(object.get("wrappedShort").getType(), is(Short.class.getName())); assertThat(object.get("wrappedInt").getType(), is(Integer.class.getName())); assertThat(object.get("wrappedLong").getType(), is(Long.class.getName())); assertThat(object.get("wrappedFloat").getType(), is(Float.class.getName())); assertThat(object.get("wrappedDouble").getType(), is(Double.class.getName())); assertThat(object.getBoolean("booleanByGet"), is(true)); assertThat(object.getBoolean("booleanByIs"), is(true)); assertThat(object.getChar("wrappedChar"), is(Character.MAX_VALUE)); assertThat(object.getShort("wrappedShort"), is(Short.MAX_VALUE)); assertThat(object.getInteger("wrappedInt"), is(Integer.MAX_VALUE)); assertThat(object.getLong("wrappedLong"), is(Long.MAX_VALUE)); assertThat(object.getFloat("wrappedFloat"), is(Float.MAX_VALUE)); assertThat(object.getDouble("wrappedDouble"), is(Double.MAX_VALUE)); }
@Test public void testSimpleModelToEDBObjectConversion_shouldWork() throws Exception { TestModel model = new TestModel(); model.setId("test"); Date date = new Date(); model.setDate(date); model.setEnumeration(ENUM.A); model.setName("testobject"); model.setCheck(false); model.setCheck2(true); model.setCheck3(false); ConnectorInformation id = getTestConnectorInformation(); List<EDBObject> objects = converter.convertModelToEDBObject(model, id); EDBObject object = objects.get(0); assertThat(object.getString("connectorId"), is("testconnector")); assertThat(object.getString("id"), is("test")); assertThat(object.getString("oid"), is(ModelWrapper.wrap(model).getCompleteModelOID())); assertThat(object.getString("domainId"), is("testdomain")); assertThat(object.getString("name"), is("testobject")); assertThat(object.getString("instanceId"), is("testinstance")); assertThat(object.getObject("enumeration", ENUM.class), is(ENUM.A)); assertThat(object.getObject("date", Date.class), is(date)); assertThat(object.getObject("check", Boolean.class), is(false)); assertThat(object.getObject("check2", Boolean.class), is(true)); assertThat(object.getObject("check3", Boolean.class), is(false)); assertThat(object.getString(EDBConstants.MODEL_TYPE), is(TestModel.class.getName())); }
@Test public void testComplexModelToEDBObjectConversion_shouldWork() throws Exception { TestModel model = new TestModel(); model.setId("test"); SubModel sub = new SubModel(); sub.setId("sub"); sub.setValue("teststring"); model.setSub(sub); ConnectorInformation id = getTestConnectorInformation(); List<EDBObject> objects = converter.convertModelToEDBObject(model, id); EDBObject object = objects.get(1); assertThat(object.getString("sub"), is(ModelWrapper.wrap(sub).getCompleteModelOID())); EDBObject subObject = objects.get(0); assertThat(subObject.getString("id"), is("sub")); assertThat(subObject.getString(EDBConstants.MODEL_TYPE), is(SubModel.class.getName())); }
@Test public void testRecursiveModelToEDBObjectConversion_shouldWork() throws Exception { RecursiveModel root = new RecursiveModel(); root.setId("root"); RecursiveModel rootChild = new RecursiveModel(); rootChild.setId("root_child"); RecursiveModel rootChildChild = new RecursiveModel(); rootChildChild.setId("root_child_child"); RecursiveModel rootChildChildChild = new RecursiveModel(); rootChildChildChild.setId("root_child_child_child"); root.setChild(rootChild); rootChild.setChild(rootChildChild); rootChildChild.setChild(rootChildChildChild); ConnectorInformation id = getTestConnectorInformation(); List<EDBObject> objects = converter.convertModelToEDBObject(root, id); EDBObject obj = objects.get(3); assertThat(obj.getString("id"), is("root")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); assertThat(obj.getString("child"), is(ModelWrapper.wrap(rootChild).getCompleteModelOID())); obj = objects.get(2); assertThat(obj.getString("id"), is("root_child")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); assertThat(obj.getString("child"), is(ModelWrapper.wrap(rootChildChild).getCompleteModelOID())); obj = objects.get(1); assertThat(obj.getString("id"), is("root_child_child")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); assertThat(obj.getString("child"), is(ModelWrapper.wrap(rootChildChildChild).getCompleteModelOID())); obj = objects.get(0); assertThat(obj.getString("id"), is("root_child_child_child")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); assertThat(obj.getString("child"), nullValue()); }
@Test public void testRecursiveModelWithCompositionToEDBObjectConversion_shouldWork() throws Exception { RecursiveModel root = new RecursiveModel("root"); RecursiveModel rootChild1 = new RecursiveModel("root_child1"); RecursiveModel rootChild2 = new RecursiveModel("root_child2"); RecursiveModel child1Child1 = new RecursiveModel("child1_child1"); RecursiveModel child2Child1 = new RecursiveModel("child2_child1"); RecursiveModel child2Child2 = new RecursiveModel("child2_child2"); List<RecursiveModel> rootChildren = Arrays.asList(new RecursiveModel[]{ rootChild1, rootChild2 }); List<RecursiveModel> child2Children = Arrays.asList(new RecursiveModel[]{ child2Child1, child2Child2 }); root.setChildren(rootChildren); rootChild1.setChild(child1Child1); rootChild2.setChildren(child2Children); ConnectorInformation id = getTestConnectorInformation(); List<EDBObject> objects = converter.convertModelToEDBObject(root, id); EDBObject obj = objects.get(5); assertThat(obj.getString("id"), is("root")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); assertThat(obj.getString("children.0"), is(ModelWrapper.wrap(rootChild1).getCompleteModelOID())); assertThat(obj.getString("children.1"), is(ModelWrapper.wrap(rootChild2).getCompleteModelOID())); obj = objects.get(4); assertThat(obj.getString("id"), is("root_child2")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); assertThat(obj.getString("children.0"), is(ModelWrapper.wrap(child2Child1).getCompleteModelOID())); assertThat(obj.getString("children.1"), is(ModelWrapper.wrap(child2Child2).getCompleteModelOID())); obj = objects.get(3); assertThat(obj.getString("id"), is("child2_child2")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); obj = objects.get(2); assertThat(obj.getString("id"), is("child2_child1")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); obj = objects.get(1); assertThat(obj.getString("id"), is("root_child1")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); assertThat(obj.getString("child"), is(ModelWrapper.wrap(child1Child1).getCompleteModelOID())); obj = objects.get(0); assertThat(obj.getString("id"), is("child1_child1")); assertThat(obj.getString(EDBConstants.MODEL_TYPE), is(RecursiveModel.class.getName())); }
@Test public void testIfEDBObjectsContainCorrectType_shouldWork() throws Exception { TestModel model = new TestModel(); model.setNumber(42); ConnectorInformation id = getTestConnectorInformation(); List<EDBObject> objects = converter.convertModelToEDBObject(model, id); EDBObject object = objects.get(0); EDBObjectEntry entry = object.get("number"); assertThat(entry.getType(), is(Integer.class.getName())); }
@Test public void testIfEngineeringObjectInformationIsAdded_shouldAddEOInformation() throws Exception { EngineeringObjectModel model = new EngineeringObjectModel(); model.setModelAId("testReferenceToModelA"); model.setModelBId("testReferenceToModelB"); List<EDBObject> objects = converter.convertModelToEDBObject(model, getTestConnectorInformation()); assertThat(objects.size(), is(1)); EDBObject result = objects.get(0); String key1 = getReferenceString(model.getClass(), "modelAId"); String key2 = getReferenceString(model.getClass(), "modelBId"); assertThat(result.getString(key1), is(CONTEXT_ID + "/testReferenceToModelA")); assertThat(result.getString(key2), is(CONTEXT_ID + "/testReferenceToModelB")); }