ModelContentToEcoreConverter implements IModelConverter<ModelContent, Model> { @Override public Model convert(ModelContent source, Optional<String> platformKey) { IModel model = source.getModels().get(source.getRoot()); return convert(model, source, platformKey); }  @Override Model convert(ModelContent source, Optional<String> platformKey);  }
@Test public void testConvertDatatype() { EntityModel entityModel = EntityModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Location:1.0.0")) .property(ModelProperty.Builder("lng", PrimitiveType.FLOAT) .withConstraint(ConstraintType.MIN, "0").build()) .property(ModelProperty.Builder("lat", PrimitiveType.STRING).build()) .description("Some description").displayname("Location").build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); Entity model = (Entity) converter.convert(ModelContent.Builder(entityModel).build(), Optional.empty()); assertEquals(entityModel.getId().getNamespace(), model.getNamespace()); assertEquals(entityModel.getId().getName(), model.getName()); assertEquals(entityModel.getId().getVersion(), model.getVersion()); assertEquals(entityModel.getDescription(), model.getDescription()); assertEquals(entityModel.getDisplayName(), model.getDisplayname()); assertEquals(entityModel.getCategory(), model.getCategory()); assertEquals(2, model.getProperties().size()); Property property1 = model.getProperties().stream().filter(p -> p.getName().equals("lng")).findAny().get(); assertNotNull(property1); assertEquals(org.eclipse.vorto.core.api.model.datatype.PrimitiveType.FLOAT, ((PrimitivePropertyType) property1.getType()).getType()); assertEquals(1, property1.getConstraintRule().getConstraints().size()); Property property2 = model.getProperties().stream().filter(p -> p.getName().equals("lat")).findAny().get(); assertNotNull(property2); assertEquals(org.eclipse.vorto.core.api.model.datatype.PrimitiveType.STRING, ((PrimitivePropertyType) property2.getType()).getType()); }
@Test public void testEntityWithEnum() { EnumModel enumModel = EnumModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Unit:1.0.0")) .literal("celcius", null).literal("kg", null).build(); EntityModel entityModel = EntityModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Location:1.0.0")) .property(ModelProperty.Builder("lng", PrimitiveType.FLOAT).build()) .property(ModelProperty.Builder("lat", PrimitiveType.STRING).build()) .property(ModelProperty.Builder("unit", enumModel.getId()).build()) .reference(enumModel.getId()).build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); Entity model = (Entity) converter.convert( ModelContent.Builder(entityModel).withDependency(enumModel).build(), Optional.empty()); assertEquals(1, model.getReferences().size()); assertEquals(3, model.getProperties().size()); Property unitProperty = model.getProperties().stream().filter(p -> p.getName().equals("unit")).findAny().get(); ObjectPropertyType type = (ObjectPropertyType) unitProperty.getType(); Enum unit = (Enum) type.getType(); assertNotNull(unit); }
@Test public void testConvertFunctionblockBasic() { FunctionblockModel fbModel = FunctionblockModel .Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")).build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter .convert(ModelContent.Builder(fbModel).build(), Optional.empty()); assertEquals(fbModel.getId().getNamespace(), model.getNamespace()); assertEquals(fbModel.getId().getName(), model.getName()); assertEquals(fbModel.getId().getVersion(), model.getVersion()); assertEquals(fbModel.getDescription(), model.getDescription()); assertEquals(fbModel.getDisplayName(), model.getDisplayname()); assertEquals(fbModel.getCategory(), model.getCategory()); }
@Test public void testConvertFunctionblockConfigStatusProperties() { FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .statusProperty(ModelProperty.Builder("value", PrimitiveType.FLOAT).build()) .configurationProperty(ModelProperty.Builder("enable", PrimitiveType.BOOLEAN).build()) .build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter .convert(ModelContent.Builder(fbModel).build(), Optional.empty()); assertEquals(1, model.getFunctionblock().getStatus().getProperties().size()); assertEquals(1, model.getFunctionblock().getConfiguration().getProperties().size()); }
@Test public void testConvertFunctionblockStatusPropertiesWithMultiple() { FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .statusProperty(ModelProperty.Builder("value", PrimitiveType.FLOAT).multiple().build()) .build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter .convert(ModelContent.Builder(fbModel).build(), Optional.empty()); List<Property> propertyList = model.getFunctionblock().getStatus().getProperties(); boolean isMultiple = false; for (Property prop : propertyList) { isMultiple = prop.isMultiplicity(); } assertEquals(true, isMultiple); }
@Test public void testConvertFunctionblockStatusPropertiesWithoutMultiple() { FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .statusProperty(ModelProperty.Builder("value", PrimitiveType.FLOAT).build()).build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter .convert(ModelContent.Builder(fbModel).build(), Optional.empty()); List<Property> propertyList = model.getFunctionblock().getStatus().getProperties(); boolean isMultiple = false; for (Property prop : propertyList) { isMultiple = prop.isMultiplicity(); } assertEquals(false, isMultiple); }
@Test public void testConvertFunctionblockWithDictionaryTypeWithoutParameters() { FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .statusProperty(ModelProperty.Builder("value", new DictionaryType(null, null)).build()) .build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter .convert(ModelContent.Builder(fbModel).build(), Optional.empty()); assertEquals(1, model.getFunctionblock().getStatus().getProperties().size()); assertTrue(model.getFunctionblock().getStatus().getProperties().get(0) .getType() instanceof DictionaryPropertyType); }
@Test public void testConvertFunctionblockWithDictionaryTypeWithPrimitiveParameters() { FunctionblockModel fbModel = FunctionblockModel .Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .statusProperty(ModelProperty .Builder("value", new DictionaryType(PrimitiveType.STRING, PrimitiveType.INT)).build()) .build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter .convert(ModelContent.Builder(fbModel).build(), Optional.empty()); assertEquals(1, model.getFunctionblock().getStatus().getProperties().size()); assertTrue(model.getFunctionblock().getStatus().getProperties().get(0) .getType() instanceof DictionaryPropertyType); assertNotNull(((DictionaryPropertyType) model.getFunctionblock().getStatus().getProperties() .get(0).getType()).getKeyType() instanceof PrimitivePropertyType); assertNotNull(((DictionaryPropertyType) model.getFunctionblock().getStatus().getProperties() .get(0).getType()).getValueType() instanceof PrimitivePropertyType); }
@Test public void testConvertFunctionblockWithPropertyMappings() { final String targetPlatform = "testPlatform"; FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .statusProperty(ModelProperty.Builder("value", PrimitiveType.FLOAT) .withStereotype("OBJECT", Collections.emptyMap(), targetPlatform).build()) .configurationProperty(ModelProperty.Builder("enable", PrimitiveType.BOOLEAN).build()) .withTargetPlatform(targetPlatform).build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); MappingModel model = (MappingModel) converter.convert(ModelContent.Builder(fbModel).build(), Optional.of(targetPlatform)); assertEquals(targetPlatform, model.getTargetPlatform()); assertEquals(1, model.getRules().size()); assertEquals("OBJECT", ((StereoTypeTarget) model.getRules().get(0).getTarget()).getName()); assertTrue(model.getRules().get(0).getSources().get(0) instanceof StatusSource); assertNotNull(((StatusSource) model.getRules().get(0).getSources().get(0)).getModel()); }
@Test public void testConvertFunctionblockOperationWithPrimitiveTypes() { FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .operation(Operation.Builder("op").description("some operation") .withParam(Param.Builder("p1", PrimitiveType.INT).build()).build()) .operation(Operation.Builder("op2") .withResult(new ReturnType(PrimitiveType.BOOLEAN, false)).build()) .build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter .convert(ModelContent.Builder(fbModel).build(), Optional.empty()); assertEquals(2, model.getFunctionblock().getOperations().size()); org.eclipse.vorto.core.api.model.functionblock.Operation op1 = model.getFunctionblock() .getOperations().stream().filter(p -> p.getName().equals("op")).findAny().get(); assertEquals("some operation", op1.getDescription()); assertEquals(1, op1.getParams().size()); assertEquals("p1", op1.getParams().get(0).getName()); assertEquals(org.eclipse.vorto.core.api.model.datatype.PrimitiveType.INT, ((PrimitiveParam) op1.getParams().get(0)).getType()); org.eclipse.vorto.core.api.model.functionblock.Operation op2 = model.getFunctionblock() .getOperations().stream().filter(p -> p.getName().equals("op2")).findAny().get(); assertEquals(org.eclipse.vorto.core.api.model.datatype.PrimitiveType.BOOLEAN, ((ReturnPrimitiveType) op2.getReturnType()).getReturnType()); }
@Test public void testConvertFunctionblockOperationWithObjectTypes() { EnumModel enumModel = EnumModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Unit:1.0.0")) .literal("celcius", null).literal("kg", null).build(); FunctionblockModel fbModel = FunctionblockModel .Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .operation(Operation.Builder("op").withParam(Param.Builder("p1", enumModel.getId()).build()) .build()) .operation( Operation.Builder("op2").withResult(new ReturnType(enumModel.getId(), false)).build()) .build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter.convert( ModelContent.Builder(fbModel).withDependency(enumModel).build(), Optional.empty()); assertEquals(2, model.getFunctionblock().getOperations().size()); assertEquals(1, model.getReferences().size()); org.eclipse.vorto.core.api.model.functionblock.Operation op1 = model.getFunctionblock() .getOperations().stream().filter(p -> p.getName().equals("op")).findAny().get(); assertEquals(1, op1.getParams().size()); assertEquals("Unit", ((Enum) ((RefParam) op1.getParams().get(0)).getType()).getName()); assertEquals(2, ((Enum) ((RefParam) op1.getParams().get(0)).getType()).getEnums().size()); org.eclipse.vorto.core.api.model.functionblock.Operation op2 = model.getFunctionblock() .getOperations().stream().filter(p -> p.getName().equals("op2")).findAny().get(); assertEquals("Unit", ((Enum) (((ReturnObjectType) op2.getReturnType()).getReturnType())).getName()); assertEquals(2, ((Enum) (((ReturnObjectType) op2.getReturnType()).getReturnType())).getEnums().size()); }
@Test public void testConvertFunctionblockWithEvents() { EnumModel enumModel = EnumModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Unit:1.0.0")) .literal("celcius", null).literal("kg", null).build(); FunctionblockModel fbModel = FunctionblockModel.Builder(ModelId.fromPrettyFormat("org.eclipse.vorto:Sensor:1.0.0")) .event(ModelEvent.Builder("Exceeded") .withProperty(ModelProperty.Builder("unit", enumModel.getId()).build()).build()) .build(); ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter(); org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel model = (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) converter.convert( ModelContent.Builder(fbModel).withDependency(enumModel).build(), Optional.empty()); assertEquals(1, model.getReferences().size()); assertEquals(1, model.getFunctionblock().getEvents().size()); }