+
+ @Test()
+ public void testCreateResourceFromCsarWithModel() throws URISyntaxException, ZipException {
+
+ final File csarFile = new File(
+ ResourceBusinessLogicTest.class.getClassLoader().getResource("csars/nonOnapCsar.csar").toURI());
+ final Map<String, byte[]> csar = ZipUtils.readZip(csarFile, false);
+
+ String resourceYml = new String(csar.get("Definitions/my_vnf.yaml"));
+
+ YamlTemplateParsingHandler yamlTemplateParser = new YamlTemplateParsingHandler(mockJanusGraphDao, null, Mockito.mock(AnnotationBusinessLogic.class), null);
+ final ParsedToscaYamlInfo parsedToscaYamlInfo = yamlTemplateParser.parseResourceInfoFromYAML("Definitions/my_vnf.yml", resourceYml, Collections.EMPTY_MAP, Collections.EMPTY_MAP, "myVnf", resourceResponse);
+
+ when(toscaOperationFacade.getLatestByToscaResourceName(anyString())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+ Resource vduCp = new Resource();
+ vduCp.setToscaResourceName("tosca.nodes.nfv.VduCp");
+ vduCp.setState(LifecycleStateEnum.CERTIFIED);
+ vduCp.setUniqueId("tosca.nodes.nfv.VduCp");
+ vduCp.setProperties(new ArrayList<>());
+ Map<String, List<RequirementDefinition>> requirements = new HashMap<>();
+ RequirementDefinition req = new RequirementDefinition();
+ req.setName("virtual_link");
+ List<RequirementDefinition> listReqs = new ArrayList<>();
+ listReqs.add(req);
+ requirements.put("tosca.nodes.nfv.VduCp", listReqs);
+ vduCp.setRequirements(requirements);
+ PropertyDefinition bitrateProp = new PropertyDefinition();
+ bitrateProp.setName("bitrate_requirement");
+ bitrateProp.setType("integer");
+ vduCp.getProperties().add(bitrateProp);
+ PropertyDefinition virtNiProp = new PropertyDefinition();
+ virtNiProp.setName("virtual_network_interface_requirements");
+ virtNiProp.setType("list");
+ vduCp.getProperties().add(virtNiProp);
+ PropertyDefinition descriptionProp = new PropertyDefinition();
+ descriptionProp.setName("description");
+ descriptionProp.setType("string");
+ vduCp.getProperties().add(descriptionProp);
+ PropertyDefinition roleProp = new PropertyDefinition();
+ roleProp.setName("role");
+ roleProp.setType("string");
+ vduCp.getProperties().add(roleProp);
+ when(toscaOperationFacade.getByToscaResourceNameMatchingVendorRelease("tosca.nodes.nfv.VduCp", "1.0.0")).thenReturn(Either.left(vduCp));
+
+
+ when(yamlTemplateParsingHandler.parseResourceInfoFromYAML(any(), any(), any(), any(), any(), any())).thenReturn(parsedToscaYamlInfo);
+
+ UploadComponentInstanceInfo uploadComponentInstanceInfo = new UploadComponentInstanceInfo();
+ uploadComponentInstanceInfo.setType("myType");
+ resourceResponse.setUniqueId("myVnf");
+ resourceResponse.setName("myVnf");
+ resourceResponse.setSystemName("myVnf");
+ resourceResponse.setModel("testModel");
+ resourceResponse.setResourceType(ResourceTypeEnum.VF);
+ resourceResponse.setProperties(new ArrayList<>());
+
+ Resource derivedFrom = new Resource();
+ List<PropertyDefinition> properties = new ArrayList<>();
+ PropertyDefinition baseTypeProp = new PropertyDefinition();
+ baseTypeProp.setName("propInBase");
+ baseTypeProp.setType("string");
+ properties.add(baseTypeProp);
+ derivedFrom.setProperties(properties );
+ when(genericTypeBusinessLogic.fetchDerivedFromGenericType(any(), eq("tosca.nodes.nfv.VNF"))).thenReturn(Either.left(derivedFrom));
+
+ when(toscaOperationFacade
+ .validateComponentNameAndModelExists("myVnf", "testModel", ResourceTypeEnum.VF, ComponentTypeEnum.RESOURCE)).thenReturn(Either.left(false));
+
+ when(toscaOperationFacade.addPropertyToComponent(any(), any(), any())).thenReturn(Either.left(new PropertyDefinition()));
+ when(toscaOperationFacade.associateComponentInstancePropertiesToComponent(any(), any())).thenReturn(Either.left(Collections.emptyMap()));
+ when(toscaOperationFacade.associateArtifactsToInstances(any(), any())).thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.associateDeploymentArtifactsToInstances(any(), any(), any())).thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.associateInstAttributeToComponentToInstances(any(), any())).thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.associateResourceInstances(any(Component.class), anyString(), anyList())).thenReturn(Either.left(Collections.EMPTY_LIST));
+
+ doAnswer(invocation -> {
+ Map<ComponentInstance, Map<String, List<RequirementDefinition>>> instReqs = invocation.getArgument(1);
+ for (final Entry<ComponentInstance, Map<String, List<RequirementDefinition>>> m: instReqs.entrySet()) {
+ m.getKey().setRequirements(m.getValue());
+ }
+ return StorageOperationStatus.OK;
+ }).
+ when(toscaOperationFacade).associateOrAddCalculatedCapReq(any(), any(), any());
+
+
+ when(toscaOperationFacade.updateCalculatedCapabilitiesRequirements(any(), any(), any())).thenReturn(StorageOperationStatus.OK);
+ when(groupBusinessLogic.validateUpdateVfGroupNames(any(), any())).thenReturn(Either.left(Collections.EMPTY_MAP));
+
+ ComponentInstance ci = new ComponentInstance();
+ List<ComponentInstance> cis = new ArrayList<>();
+ cis.add(ci);
+ doAnswer(invocation -> {
+ List<ComponentInstance> componentInstances = new ArrayList<ComponentInstance>(((Map<ComponentInstance, Resource>)invocation.getArgument(1)).keySet());
+ ((Resource)invocation.getArgument(0)).setComponentInstances(componentInstances);
+ return null;
+ }).when(toscaOperationFacade).associateComponentInstancesToComponent(any(), any(), eq(false), eq(false));
+
+ doAnswer(invocation -> {
+ return Either.left(invocation.getArgument(0));
+ }).when(csarArtifactsAndGroupsBusinessLogic).deleteVFModules(any(Resource.class), any(CsarInfo.class), eq(true), eq(false));
+
+ doAnswer(invocation -> {
+ return Either.left(resourceResponse);
+ }).when(toscaOperationFacade).getToscaFullElement("myVnf");
+
+
+ Resource result = bl.createResourceFromCsar(resourceResponse, user, csar, "1234");
+
+ assertEquals("myDomain.myVnf", result.getToscaResourceName());
+ List<String> propIds = result.getProperties().stream().map(prop -> prop.getUniqueId()).collect(Collectors.toList());
+ assertTrue(propIds.contains("myVnf.propInBase"));
+ assertTrue(propIds.contains("myVnf.descriptor_id"));
+ assertTrue(propIds.contains("myVnf.descriptor_version"));
+ assertTrue(propIds.contains("myVnf.flavour_description"));
+ assertTrue(propIds.contains("myVnf.flavour_id"));
+ assertTrue(propIds.contains("myVnf.product_name"));
+ assertTrue(propIds.contains("myVnf.provider"));
+ assertTrue(propIds.contains("myVnf.software_version"));
+ assertTrue(propIds.contains("myVnf.vnfm_info"));
+
+ final List<String> reqsName = new ArrayList<>();
+
+ final List<ComponentInstance> cisWithExtReq = result.getComponentInstances().stream().filter(instance -> instance.getRequirements().get("tosca.nodes.nfv.VduCp").get(0).isExternal()).collect(Collectors.toList());
+ cisWithExtReq.forEach(instance -> reqsName.add(instance.getRequirements().get("tosca.nodes.nfv.VduCp").get(0).getExternalName()));
+ assertEquals(3, cisWithExtReq.size());
+ }