ModelIdToModelContentConverter implements IModelConverter<ModelId, ModelContent> { @Override public ModelContent convert(ModelId modelId, Optional<String> platformKey) { modelId = repositoryFactory.getRepositoryByNamespace(modelId.getNamespace()) .getLatestModelVersionIfLatestTagIsSet(modelId); if (!repositoryFactory.getRepositoryByModel(modelId).exists(modelId)) { throw new ModelNotFoundException( String.format("Model [%s] does not exist", modelId.getPrettyFormat()), null); } ModelWorkspaceReader workspaceReader = getWorkspaceForModel(modelId); ModelContent result = new ModelContent(); result.setRoot(modelId); if (platformKey.isPresent()) { final List<ModelInfo> mappingResources = repositoryFactory.getRepositoryByModel(modelId) .getMappingModelsForTargetPlatform(modelId, platformKey.get(), Optional.empty()); if (!mappingResources.isEmpty()) { mappingResources.forEach(mapping -> workspaceReader.addFile(new ByteArrayInputStream( repositoryFactory.getRepositoryByModel(mapping.getId()) .getFileContent(mapping.getId(), Optional.empty()).get().getContent()), org.eclipse.vorto.model.ModelType.Mapping)); final IModelWorkspace workspace = workspaceReader.read(); workspace.get().forEach(model -> { Optional<MappingModel> mappingModel = getMappingModelForModel(mappingResources, model); if (mappingModel.isPresent()) { AbstractModel createdModel = ModelDtoFactory .createResource(flattenHierarchy(model), mappingModel); createdModel.setTargetPlatformKey(platformKey.get()); result.getModels() .put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel); } else { result.getModels().put( new ModelId(model.getName(), model.getNamespace(), model.getVersion()), ModelDtoFactory.createResource(flattenHierarchy(model), Optional.empty())); } }); } else { final IModelWorkspace workspace = workspaceReader.read(); workspace.get().forEach(model -> { AbstractModel createdModel = ModelDtoFactory .createResource(flattenHierarchy(model), Optional.empty()); createdModel.setTargetPlatformKey(platformKey.get()); result.getModels() .put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel); }); } } else { final IModelWorkspace workspace = workspaceReader.read(); workspace.get().forEach(model -> { AbstractModel createdModel = ModelDtoFactory .createResource(flattenHierarchy(model), Optional.empty()); result.getModels() .put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel); }); } return result; } ModelIdToModelContentConverter(IModelRepositoryFactory repositoryFactory); @Override ModelContent convert(ModelId modelId, Optional<String> platformKey);  }
@Test public void testConvertFbWithMultipleProperty() { importModel("FbWithMultipleProperty.fbmodel"); ModelIdToModelContentConverter converter = new ModelIdToModelContentConverter(this.repositoryFactory); ModelContent content = converter.convert(ModelId.fromPrettyFormat("org.eclipse.vorto:TestFb:1.0.0"), Optional.empty()); FunctionblockModel fbm = (FunctionblockModel) content.getModels().get(content.getRoot()); assertTrue(fbm.getStatusProperty("foos").get().isMultiple()); }
@Test public void testConvertWithTargetPlatform() throws Exception { importModel("Color.type"); importModel("sample.mapping"); ModelIdToModelContentConverter converter = new ModelIdToModelContentConverter(this.repositoryFactory); ModelContent content = converter.convert(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:1.0.0"), Optional.of("ios")); assertNotNull(content); assertEquals(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:1.0.0"),content.getModels().get(content.getRoot()).getId()); assertEquals("colortype",((EntityModel)content.getModels().get(content.getRoot())).getStereotypes().get(0).getName()); }
@Test public void testConvertWithoutTargetPlatform() throws Exception { importModel("Color.type"); importModel("sample.mapping"); ModelIdToModelContentConverter converter = new ModelIdToModelContentConverter(this.repositoryFactory); ModelContent content = converter.convert(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:1.0.0"), Optional.empty()); assertEquals(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:1.0.0"),content.getModels().get(content.getRoot()).getId()); assertEquals(0,((EntityModel)content.getModels().get(content.getRoot())).getStereotypes().size()); }
@Test public void testConvertWithoutTargetPlatformLatestTag() throws Exception { setupTestDataForLatestTag(); ModelIdToModelContentConverter converter = new ModelIdToModelContentConverter(this.repositoryFactory); ModelContent content = converter.convert(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:latest"), Optional.empty()); assertEquals(ModelId.fromPrettyFormat("org.eclipse.vorto.examples.type:Color:1.0.1"),content.getModels().get(content.getRoot()).getId()); assertEquals(0,((EntityModel)content.getModels().get(content.getRoot())).getStereotypes().size()); }