import static org.mockito.Mockito.contains;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.eq;
+import static org.mockito.Mockito.isNull;
import static org.mockito.Mockito.matches;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.isNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.openecomp.sdc.be.components.impl.ServiceImportBusinessLogic.CREATE_RESOURCE;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.json.simple.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
+import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
import org.openecomp.sdc.be.externalapi.servlet.ArtifactExternalServlet;
import org.openecomp.sdc.be.impl.ServletUtils;
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;
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;
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;
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;
@Test
void testCreateService_OK() {
Service oldService = createServiceObject(true);
+ oldService.setComponentInstances(creatComponentInstances());
+ oldService.setComponentInstancesProperties(
+ Collections.singletonMap(COMPONENT_ID, Collections.singletonList(new ComponentInstanceProperty())));
String payloadName = "valid_vf";
Map<String, byte[]> payload = crateCsarFromPayload();
Service newService = createServiceObject(true);
newService.setComponentInstances(creatComponentInstances());
+ ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
+ componentInstanceProperty.setName("propertiesName");
+ final JSONObject jsonObject = new JSONObject();
+ 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());
when(serviceBusinessLogic.validateServiceBeforeCreate(eq(newService), any(User.class), any(AuditingActionEnum.class)))
.thenReturn(Either.left(newService));
when(mockJanusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
when(graphLockOperation.unlockComponentByName(anyString(), anyString(), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
when(serviceImportParseLogic.createOutputsOnService(any(Service.class), any(), anyString())).thenReturn(newService);
-
+ when(toscaOperationFacade.updateInputsToComponent(anyList(), eq(newService.getUniqueId()))).thenReturn(Either.left(new ArrayList<>()));
+
when(applicationDataTypeCache.get(any(), contains("tosca.datatypes.test_"))).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));
- when(applicationDataTypeCache.get(any(), matches("^((?!tosca.datatypes.test_).)*$"))).thenReturn(Either.left(new DataTypeDefinition()));
-
- when(toscaOperationFacade.getLatestByToscaResourceName(contains("org.openecomp.resource"), isNull())).thenReturn(Either.left(null));
- when(toscaOperationFacade.getLatestByToscaResourceName(contains("tosca.nodes."), isNull())).thenReturn(Either.left(null));
+ DataTypeDefinition typeToBeUpdated = new DataTypeDefinition();
+ List<PropertyDefinition> properties = new ArrayList<>();
+ PropertyDefinition nameProperty = new PropertyDefinition();
+ nameProperty.setName("name");
+ nameProperty.setType("string");
+ properties.add(nameProperty);
+ PropertyDefinition versionProperty = new PropertyDefinition();
+ versionProperty.setName("version");
+ versionProperty.setType("string");
+ properties.add(versionProperty);
+ typeToBeUpdated.setProperties(properties);
+ 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.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);
assertEquals(1, result.getComponentInstances().get(0).getRequirements().size());
assertNotNull(result.getCategories());
assertEquals(1, result.getCategories().size());
-
+
ArgumentCaptor<String> yaml = ArgumentCaptor.forClass(String.class);
verify(dataTypeBusinessLogic).createDataTypeFromYaml(yaml.capture(), isNull(), anyBoolean());
Map<String, Object> yamlMap = new Yaml().load(yaml.getValue());
- assertEquals(2, yamlMap.size());
+ assertEquals(3, yamlMap.size());
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
List<UploadPropInfo> propertyList = getPropertyList();
Assertions.assertNotNull(resource);
Assertions.assertNotNull(currPropertiesMap);
- sIBL.processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList);
+ sIBL.processProperty(resource, allDataTypes, currPropertiesMap, instPropList, propertyList);
}
@Test
propertyList.add(propertyInfo);
Assertions.assertNotNull(resource);
- sIBL.processProperty(resource, currentCompInstance, allDataTypes, currPropertiesMap, instPropList, propertyList);
+ sIBL.processProperty(resource, allDataTypes, currPropertiesMap, instPropList, propertyList);
}
@Test
componentInstance.setUniqueId("uniqueId");
componentInstance.setComponentUid("componentUid");
componentInstance.setName("zxjTestImportServiceAb");
+ componentInstance.setNormalizedName("zxjTestImportServiceAb");
+ componentInstance.setProperties(getProperties());
componentInstances.add(componentInstance);
return componentInstances;
}
protected ServiceCsarInfo getCsarInfo() {
String csarUuid = "0010";
User user = new User("jh0003");
-
+
try {
File csarFile = new File(
- ServiceImportBusinessLogicTest.class.getClassLoader().getResource("csars/service-Ser09080002-csar.csar").toURI());
+ ServiceImportBusinessLogicTest.class.getClassLoader().getResource("csars/service-Ser09080002-csar.csar").toURI());
Map<String, byte[]> csar = ZipUtils.readZip(csarFile, false);
-
+
String vfReousrceName = "resouceName";
String mainTemplateName = "Definitions/service_import_template.yml";
-
+
Optional<String> keyOp = csar.keySet().stream().filter(k -> k.endsWith("service-Ser09080002-template.yml")).findAny();
byte[] mainTemplateService = keyOp.map(csar::get).orElse(null);
assertNotNull(mainTemplateService);
final String mainTemplateContent = new String(mainTemplateService);
-
+
return new ServiceCsarInfo(user, csarUuid, csar, vfReousrceName, mainTemplateName, mainTemplateContent, false);
} catch (URISyntaxException | ZipException e) {
fail(e);
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;