Service import - Import unknown capability types
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / ServiceImportBusinessLogicTest.java
index 3f8f9c3..944f51c 100644 (file)
@@ -82,6 +82,7 @@ import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts;
 import org.openecomp.sdc.be.model.ArtifactDefinition;
 import org.openecomp.sdc.be.model.AttributeDefinition;
 import org.openecomp.sdc.be.model.CapabilityDefinition;
+import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
 import org.openecomp.sdc.be.model.Component;
 import org.openecomp.sdc.be.model.ComponentInstance;
 import org.openecomp.sdc.be.model.ComponentInstanceInput;
@@ -90,6 +91,7 @@ import org.openecomp.sdc.be.model.ComponentMetadataDefinition;
 import org.openecomp.sdc.be.model.ComponentParametersView;
 import org.openecomp.sdc.be.model.DataTypeDefinition;
 import org.openecomp.sdc.be.model.GroupDefinition;
+import org.openecomp.sdc.be.model.GroupTypeDefinition;
 import org.openecomp.sdc.be.model.IPropertyInputCommon;
 import org.openecomp.sdc.be.model.InputDefinition;
 import org.openecomp.sdc.be.model.InterfaceDefinition;
@@ -112,9 +114,13 @@ import org.openecomp.sdc.be.model.User;
 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
 import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation;
 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
+import org.openecomp.sdc.be.model.operations.impl.ArtifactTypeOperation;
+import org.openecomp.sdc.be.model.operations.impl.CapabilityTypeOperation;
+import org.openecomp.sdc.be.model.operations.impl.GroupTypeOperation;
 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
 import org.openecomp.sdc.be.servlets.AbstractValidationsServlet;
 import org.openecomp.sdc.be.tosca.CsarUtils;
+import org.openecomp.sdc.be.tosca.ToscaExportHandler;
 import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum;
 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
 import org.openecomp.sdc.common.api.Constants;
@@ -125,15 +131,18 @@ import org.yaml.snakeyaml.Yaml;
 
 class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTestSetup {
 
-    private static final String DEFAULT_ICON = "defaulticon";
-
     private final ArtifactDefinition artifactDefinition = mock(ArtifactDefinition.class);
     private final ResourceImportManager resourceImportManager = mock(ResourceImportManager.class);
     private final ServletUtils servletUtils = mock(ServletUtils.class);
     private final AbstractValidationsServlet servlet = new ArtifactExternalServlet(userBusinessLogic,
         componentInstanceBusinessLogic, componentsUtils, servletUtils, resourceImportManager, artifactsBusinessLogic);
     private final ApplicationDataTypeCache applicationDataTypeCache = mock(ApplicationDataTypeCache.class);
+    private final ArtifactTypeOperation artifactTypeOperation = mock(ArtifactTypeOperation.class);
     private final DataTypeBusinessLogic dataTypeBusinessLogic = mock(DataTypeBusinessLogic.class);
+    private final ArtifactTypeImportManager artifactTypeImportManager = mock(ArtifactTypeImportManager.class);
+    private final GroupTypeOperation groupTypeOperation = mock(GroupTypeOperation.class);
+    private final CapabilityTypeOperation capabilityTypeOperation = mock(CapabilityTypeOperation.class);
+    private final CapabilityTypeImportManager capabilityTypeImportManager = mock(CapabilityTypeImportManager.class);
 
     @InjectMocks
     private ServiceImportBusinessLogic sIBL;
@@ -173,6 +182,18 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest
         jsonObject.put(ToscaGetFunctionType.GET_INPUT.getFunctionName(), "zxjTestImportServiceAb_propertiesName");
         componentInstanceProperty.setValue(jsonObject.toJSONString());
 
+        Map<String, ArtifactDefinition> toscaArtifacts = new HashMap<>();
+        ArtifactDefinition artifactDef = new ArtifactDefinition();
+        String artifactUniqueId = "test_extcp_resource.assettoscatemplate";
+        artifactDef.setUniqueId(artifactUniqueId);
+        toscaArtifacts.put(ToscaExportHandler.ASSET_TOSCA_TEMPLATE, artifactDef);
+        Resource resource = new Resource();
+        String resourceUniqueId = "extcp_resource";
+        resource.setUniqueId(resourceUniqueId);
+        resource.setToscaArtifacts(toscaArtifacts);
+        ImmutablePair<String, byte[]> resourceTemplate = getNodeType();
+        String updatedNodeType = "org.openecomp.resource.cp.extCP";
+
         newService.setComponentInstancesProperties(
             Collections.singletonMap(COMPONENT_ID + "." + "zxjTestImportServiceAb", Collections.singletonList(componentInstanceProperty)));
         newService.setProperties(getProperties());
@@ -232,12 +253,24 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest
         when(applicationDataTypeCache.get(any(), eq("onap.datatypes.ToscaConceptIdentifier.datatype"))).thenReturn(Either.left(typeToBeUpdated));
         when(applicationDataTypeCache.get(any(), matches("^((?!(tosca.datatypes.test_|onap.datatypes.ToscaConceptIdentifier)).)*$"))).thenReturn(Either.left(new DataTypeDefinition()));
 
+        when(artifactTypeOperation.getArtifactTypeByUid(contains("tosca.testartifacts.Name"))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+        when(artifactTypeOperation.getArtifactTypeByUid(contains("tosca.artifacts"))).thenReturn(Either.left(null));
 
+        when(capabilityTypeOperation.getCapabilityType(anyString()))
+            .thenReturn(Either.left(new CapabilityTypeDefinition()));
+        when(capabilityTypeOperation.getCapabilityType(contains("tosca.testcapabilitytypes.Name")))
+            .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
 
-        when(toscaOperationFacade.getLatestByToscaResourceName(contains("org.openecomp.resource"), isNull())).thenReturn(Either.left(null));
-        when(toscaOperationFacade.getLatestByToscaResourceName(contains("tosca.nodes."), isNull())).thenReturn(Either.left(null));
+        when(toscaOperationFacade.getLatestByToscaResourceName(contains("org.openecomp.resource"), isNull()))
+                .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+        when(toscaOperationFacade.getLatestByToscaResourceName(contains("tosca.nodes."), isNull()))
+                .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+        when(toscaOperationFacade.getLatestByToscaResourceName(contains(updatedNodeType), isNull())).thenReturn(Either.left(resource));
+        when(artifactsBusinessLogic.handleDownloadRequestById(resourceUniqueId, artifactUniqueId, user.getUserId(), ComponentTypeEnum.RESOURCE, null, null))
+                .thenReturn(resourceTemplate);
         when(toscaOperationFacade.updatePropertyOfComponent(eq(oldService), any(PropertyDefinition.class))).thenReturn(Either.left(null));
         when(toscaOperationFacade.updateComponentInstancePropsToComponent(anyMap(), anyString())).thenReturn(Either.left(null));
+        when(groupTypeOperation.getGroupTypeByUid(anyString())).thenReturn(Either.left(new GroupTypeDefinition()));
 
         Service result = sIBL.createService(oldService, AuditingActionEnum.CREATE_RESOURCE, user, payload, payloadName);
         assertNotNull(result);
@@ -258,6 +291,28 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest
         assertNotNull(yamlMap.get("tosca.datatypes.test_a"));
         assertNotNull(yamlMap.get("tosca.datatypes.test_b"));
         assertNotNull(yamlMap.get("onap.datatypes.ToscaConceptIdentifier"));
+
+        ArgumentCaptor<String> artifactTypes = ArgumentCaptor.forClass(String.class);
+        verify(artifactTypeImportManager).createArtifactTypes(artifactTypes.capture(),isNull(), anyBoolean());
+        Map<String, Object> artifactTypesMap = new Yaml().load(artifactTypes.getValue());
+        assertEquals(1, artifactTypesMap.size());
+        assertNotNull(artifactTypesMap.get("tosca.testartifacts.Name"));
+
+        ArgumentCaptor<String> capabilityTypes = ArgumentCaptor.forClass(String.class);
+        verify(capabilityTypeImportManager).createCapabilityTypes(
+            capabilityTypes.capture(),
+            isNull(),
+            anyBoolean());
+        Map<String, Object> capabilityTypesMap = new Yaml().load(capabilityTypes.getValue());
+        assertEquals(1, capabilityTypesMap.size());
+        assertNotNull(capabilityTypesMap.get("tosca.testcapabilitytypes.Name"));
+
+        ArgumentCaptor<Map<String, Object>> nodeTypes = ArgumentCaptor.forClass(Map.class);
+        verify(resourceImportManager).importAllNormativeResource(nodeTypes.capture(), any(), any(), any(),
+                anyBoolean(), anyBoolean());
+        Map<String, Object> nodeTypesMap = nodeTypes.getValue();
+        Map<String, Object> newUpdatedNodeType = (Map<String, Object>) nodeTypesMap.get(updatedNodeType);
+        assertEquals(8, ((Map<String, Object>) newUpdatedNodeType.get("properties")).size());
     }
 
     @Test
@@ -2431,6 +2486,19 @@ class ServiceImportBusinessLogicTest extends ServiceImportBussinessLogicBaseTest
         return null;
     }
 
+    private ImmutablePair<String, byte[]> getNodeType() {
+        try {
+            File resource = new File(
+                    ServiceImportBusinessLogicTest.class.getClassLoader().getResource("node-types/resource-Extcp-template.yml").toURI());
+            byte[] extcpResource = Files.readAllBytes(resource.toPath());
+
+            return new ImmutablePair<>("org.openecomp.resource.cp.extCP", extcpResource);
+        } catch (URISyntaxException | IOException e) {
+            fail(e);
+        }
+        return null;
+    }
+
     protected CsarUtils.NonMetaArtifactInfo getNonMetaArtifactInfo() {
         String artifactName = "artifactName", path = "/src/test/resources/valid_vf.csar", artifactType = "AAI_SERVICE_MODEL";
         ArtifactGroupTypeEnum artifactGroupType = ArtifactGroupTypeEnum.TOSCA;