+ @Test
+ public void validateHandleArtifactRequestReturnsProperResponseMessage() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final String componentId = "testComponent";
+ final String userId = "testUser";
+ final String artifactId = "testArtifact";
+ final String origMd5 = "testOrigMd5";
+ final String originData = "testOriginData";
+ final String interfaceUuid = "testInterfaceUuid";
+ final String operationUuid = "testOperationUuid";
+ final String parentId = "testParentId";
+ final String containerComponentType = "services";
+ User testUser = new User();
+ ComponentTypeEnum componentType = ComponentTypeEnum.SERVICE_INSTANCE;
+
+ ArtifactsBusinessLogic.ArtifactOperationInfo operation = Mockito.mock(ArtifactsBusinessLogic.ArtifactOperationInfo.class);
+ when(operation.getArtifactOperationEnum()).thenReturn(ArtifactsBusinessLogic.ArtifactOperationEnum.DOWNLOAD);
+
+ UserValidations testUserValidation = Mockito.mock(UserValidations.class);
+ when(testUserValidation.validateUserExists(eq(userId),any(String.class),anyBoolean())).thenReturn(testUser);
+
+ ResponseFormat responseFormat = Mockito.mock(ResponseFormat.class);
+
+ ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
+ when(componentsUtils.getResponseFormat(any(ActionStatus.class),eq(componentId)) ).thenReturn(responseFormat);
+
+ ArtifactDefinition artifactInfo = new ArtifactDefinition();
+
+ Either<Component, StorageOperationStatus> storageStatus = Either.right(StorageOperationStatus.OK);//.RightProjection<Component, StorageOperationStatus>() ;
+ when(toscaOperationFacade.getToscaFullElement(eq(componentId))).thenReturn(storageStatus);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+ testArtifactsBusinessLogic.setUserValidations(testUserValidation);
+ Either<Either<ArtifactDefinition, Operation>, ResponseFormat> response = testArtifactsBusinessLogic.handleArtifactRequest(
+ componentId, userId, componentType, operation,
+ artifactId, artifactInfo, origMd5, originData,
+ interfaceUuid, operationUuid, parentId, containerComponentType);
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(), responseFormat);
+ }
+
+
+ @Test
+ public void validateHandleArtifactRequestWithNoUserReturnsMissingInformationResponseMessage() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final String componentId = "testComponent";
+ final String artifactId = "testArtifact";
+ final String origMd5 = "testOrigMd5";
+ final String originData = "testOriginData";
+ final String interfaceUuid = "testInterfaceUuid";
+ final String operationUuid = "testOperationUuid";
+ final String parentId = "testParentId";
+ final String containerComponentType = "services";
+ ArtifactDefinition artifactInfo = new ArtifactDefinition();
+ ComponentTypeEnum componentType = ComponentTypeEnum.SERVICE_INSTANCE;
+
+ ArtifactsBusinessLogic.ArtifactOperationInfo operation =
+ Mockito.mock(ArtifactsBusinessLogic.ArtifactOperationInfo.class);
+ when(operation.getArtifactOperationEnum()).thenReturn(ArtifactsBusinessLogic.ArtifactOperationEnum.UPDATE);
+
+ ResponseFormat responseFormat = Mockito.mock(ResponseFormat.class);
+
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.MISSING_INFORMATION)) ).thenReturn(responseFormat);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+ Either<Either<ArtifactDefinition, Operation>, ResponseFormat> response =
+ testArtifactsBusinessLogic.handleArtifactRequest(
+ componentId, null, componentType, operation,
+ artifactId, artifactInfo, origMd5, originData,
+ interfaceUuid, operationUuid, parentId, containerComponentType
+ );
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(), responseFormat);
+ }
+
+ @Test
+ public void validateValidateAndHandleArtifactWillCallAuditResourceWithProperParameters() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final String componentUniqueId = "testComponentId";
+ final ComponentTypeEnum componentType = ComponentTypeEnum.RESOURCE;
+ final ArtifactsBusinessLogic.ArtifactOperationInfo operation = Mockito.mock(ArtifactsBusinessLogic.ArtifactOperationInfo.class);
+ final String artifactUniqueId = "testArtifactId";
+ final String artifactName = "testArtifact";
+ final String artifactType = "testData";
+ final ArtifactDefinition artifactDefinition = new ArtifactDefinition();
+ artifactDefinition.setArtifactType("testArtifact");
+ final String origMd5 = GeneralUtility.calculateMD5Base64EncodedByString(artifactType);
+ final String interfaceUuid = "testInterfaceUUID";
+ final String operationName = "testOperation";
+ final User user = new User();
+ final Resource component = Mockito.mock(Resource.class);
+ when(component.getName()).thenReturn(artifactName);
+ final boolean shouldLock = false;
+ final boolean inTransaction = false;
+ final boolean needUpdateGroup = false;
+
+ when(operation.getArtifactOperationEnum()).thenReturn(ArtifactsBusinessLogic.ArtifactOperationEnum.CREATE);
+
+ when(componentsUtils.isExternalApiEvent(AuditingActionEnum.ARTIFACT_UPLOAD)).thenReturn(false);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+ Either<Either<ArtifactDefinition, Operation>, ResponseFormat> response =
+ testArtifactsBusinessLogic.validateAndHandleArtifact(
+ componentUniqueId, componentType, operation,
+ artifactUniqueId, artifactDefinition, origMd5,
+ artifactType, interfaceUuid, operationName,
+ user, component, shouldLock, inTransaction, needUpdateGroup
+ );
+
+ assertTrue(response.isRight());
+ assertNull(response.right().value());
+ verify(componentsUtils).auditResource(
+ eq(null), eq(user), eq(component),
+ eq(artifactName), eq(AuditingActionEnum.ARTIFACT_UPLOAD), any(ResourceVersionInfo.class),
+ eq(null), eq(null));
+ }
+
+ @Test
+ public void validateGenerateAndSaveToscaArtifactStoresProperArtifact() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+
+ final ArtifactDefinition artifactDefinition = Mockito.mock(ArtifactDefinition.class);
+ when(artifactDefinition.getArtifactType()).thenReturn(ArtifactTypeEnum.TOSCA_CSAR.getType());
+ final Component component = Mockito.mock(Component.class);
+ final User user = new User();
+ final boolean isInCertificationRequest = false;
+ final boolean shouldLock = false;
+ final boolean inTransaction= false;
+ final boolean fetchTemplatesFromDB = false;
+
+
+ when(csarUtils.createCsar(eq(component), eq(false), eq(false))).thenReturn(Either.right(expectedResponseFormat));
+ Either<Either<ArtifactDefinition, Operation>, ResponseFormat> response =
+ testArtifactsBusinessLogic.generateAndSaveToscaArtifact(
+ artifactDefinition, component, user,
+ isInCertificationRequest, shouldLock, inTransaction, fetchTemplatesFromDB);
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(), expectedResponseFormat);
+ }
+
+ @Test
+ public void validateGenerateAndSaveToscaArtifactResponseProperlyToGenerationFail() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+
+ final byte[] byteResponse= "testBytes".getBytes();
+ final byte[] testPayloadData = "testPayloadData".getBytes();
+ final String testESId = "testEsId";
+ final ArtifactDefinition artifactDefinition = Mockito.mock(ArtifactDefinition.class);
+ when(artifactDefinition.getArtifactType()).thenReturn(ArtifactTypeEnum.TOSCA_CSAR.getType());
+ when(artifactDefinition.getPayloadData()).thenReturn(testPayloadData);
+ when(artifactDefinition.getEsId()).thenReturn(testESId);
+ final String artifactName = "testArtifact";
+ final String componentUniqueId = "testUniqueId";
+ final Resource component = Mockito.mock(Resource.class);
+ when(component.getComponentType()).thenReturn(ComponentTypeEnum.RESOURCE);
+ when(component.getUniqueId()).thenReturn(componentUniqueId);
+ when(component.getName()).thenReturn(artifactName);
+ final User user = new User();
+ final boolean isInCertificationRequest = false;
+ final boolean shouldLock = false;
+ final boolean inTransaction= false;
+ final boolean fetchTemplatesFromDB = false;
+ final ComponentsUtils testComponentUtils = Mockito.mock(ComponentsUtils.class);
+ when(testComponentUtils.getResponseFormat(eq(ActionStatus.OK))).thenReturn(expectedResponseFormat);
+
+ when(artifactCassandraDao.saveArtifact(any(ESArtifactData.class))).thenReturn(CassandraOperationStatus.OK);
+ when(artifactToscaOperation.updateArtifactOnResource(
+ eq(artifactDefinition), eq(componentUniqueId), eq(null),
+ eq(NodeTypeEnum.Resource), eq(componentUniqueId)
+ )).thenReturn(Either.left(artifactDefinition));
+ when(csarUtils.createCsar(eq(component), eq(false), eq(false))).thenReturn(Either.left(byteResponse));
+ testArtifactsBusinessLogic.setComponentsUtils(testComponentUtils);
+ Either<Either<ArtifactDefinition, Operation>, ResponseFormat> response =
+ testArtifactsBusinessLogic.generateAndSaveToscaArtifact(
+ artifactDefinition, component, user,
+ isInCertificationRequest, shouldLock, inTransaction, fetchTemplatesFromDB);
+ assertTrue(response.isLeft());
+ assertTrue(response.isLeft());
+ assertEquals(response.left().value().left().value(), artifactDefinition);
+ }
+
+ @Test
+ public void validateHandleDownloadToscaModelRequestReturnsProperResponseFormat() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+
+ final Component component = Mockito.mock(Component.class);
+ final String testESId = "testEsId";
+ final String artifactName = "testArtifact";
+ final ArtifactDefinition artifactDefinition = Mockito.mock(ArtifactDefinition.class);
+ when(artifactDefinition.getEsId()).thenReturn(testESId);
+ when(artifactDefinition.getArtifactDisplayName()).thenReturn(artifactName);
+ final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
+ when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.OK))).thenReturn(ActionStatus.OK);
+ when(componentsUtils.getResponseFormatByArtifactId(
+ eq(ActionStatus.OK), eq(artifactName))).thenReturn(expectedResponseFormat);
+
+ when(artifactCassandraDao.getArtifact(eq(testESId))).thenReturn(Either.right(CassandraOperationStatus.OK));
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ Either<ImmutablePair<String, byte[]>, ResponseFormat> response =
+ testArtifactsBusinessLogic.handleDownloadToscaModelRequest(component,artifactDefinition);
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(), expectedResponseFormat);
+ }
+
+ @Test
+ public void validateHandleDownloadRequestByIdReturnsProperResponseFormat() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+
+ final String componentId = "testComponent";
+ final String artifactId = "testArtifact";
+ final String userId = "testUser";
+ final ComponentTypeEnum componentType = ComponentTypeEnum.SERVICE;
+ final String parentId = "testParent";
+ final String containerComponentType = "products";
+ final User user = new User();
+ final Service component = Mockito.mock(Service.class);
+ when(component.getUniqueId()).thenReturn(componentId);
+ final UserValidations userValidations = Mockito.mock(UserValidations.class);
+ when(userValidations.validateUserExists(
+ eq(userId), eq("ArtifactDownload"), eq(false))).thenReturn(user);
+
+ when(toscaOperationFacade.getToscaFullElement(eq(componentId))).thenReturn(Either.left(component));
+ when(artifactToscaOperation.getArtifactById(componentId, artifactId, componentType, componentId)).
+ thenReturn(Either.right(StorageOperationStatus.OK));
+ when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.OK))).thenReturn(ActionStatus.OK);
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.OK))).thenReturn(expectedResponseFormat);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+ testArtifactsBusinessLogic.setUserValidations(userValidations);
+
+ Either<ImmutablePair<String, byte[]>, ResponseFormat> response =
+ testArtifactsBusinessLogic.handleDownloadRequestById(
+ componentId, artifactId, userId,
+ componentType, parentId, containerComponentType);
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(), expectedResponseFormat);
+ }
+
+ @Test
+ public void testIfValidateArtifactTypeExistsRespondsWithNotSupportedFormat() {
+
+ final Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ when(artifactInfo.getArtifactType()).thenReturn("WrongFormat");
+
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ testArtifactsBusinessLogic.validateArtifactTypeExists(responseWrapper,artifactInfo);
+
+ assertEquals(responseWrapper.getInnerElement().getStatus().intValue(), HttpStatus.SC_BAD_REQUEST);
+ }
+
+ @Test
+ public void testIfValidateFileExtensionRespondsWithCorrectResult() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ when(artifactInfo.getArtifactName()).thenReturn("test.heat");
+ final ArtifactTypeConfig artifactTypeConfig =
+ Mockito.mock(ArtifactTypeConfig.class);
+ when(artifactTypeConfig.getAcceptedTypes()).thenReturn(Collections.singletonList("heat"));
+ final IDeploymentArtifactTypeConfigGetter deploymentConfigGetter =
+ Mockito.mock(IDeploymentArtifactTypeConfigGetter.class);
+ when(deploymentConfigGetter.getDeploymentArtifactConfig()).
+ thenReturn(artifactTypeConfig);
+ final NodeTypeEnum parentType = NodeTypeEnum.Service;
+ final ArtifactTypeEnum artifactType = ArtifactTypeEnum.HEAT;
+
+ testArtifactsBusinessLogic.validateFileExtension(
+ responseWrapper, deploymentConfigGetter, artifactInfo,
+ parentType, artifactType);
+
+ assertTrue(responseWrapper.isEmpty());
+ }
+
+ @Test
+ public void testIfValidateFileExtensionRespondsWithGeneralErrorIfNodeTypeIsWrong() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+ final Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ final IDeploymentArtifactTypeConfigGetter deploymentConfigGetter =
+ Mockito.mock(IDeploymentArtifactTypeConfigGetter.class);
+ final NodeTypeEnum parentType = NodeTypeEnum.Group;
+ final ArtifactTypeEnum artifactType = ArtifactTypeEnum.HEAT;
+
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.GENERAL_ERROR))).thenReturn(expectedResponseFormat);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ testArtifactsBusinessLogic.validateFileExtension(
+ responseWrapper, deploymentConfigGetter, artifactInfo,
+ parentType, artifactType);
+
+ assertFalse(responseWrapper.isEmpty());
+ assertEquals(responseWrapper.getInnerElement(),expectedResponseFormat);
+ }
+
+ @Test
+ public void testIfValidateFileExtensionRespondsWithArtifactTypeNotSupportedIfAcceptedTypeIsNull() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+ final Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
+ final String testArtifactType = "testArtifact";
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ when(artifactInfo.getArtifactType()).thenReturn(testArtifactType);
+ final IDeploymentArtifactTypeConfigGetter deploymentConfigGetter =
+ Mockito.mock(IDeploymentArtifactTypeConfigGetter.class);
+ final NodeTypeEnum parentType = NodeTypeEnum.Resource;
+ final ArtifactTypeEnum artifactType = ArtifactTypeEnum.HEAT;
+
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.ARTIFACT_TYPE_NOT_SUPPORTED), eq(testArtifactType))).thenReturn(expectedResponseFormat);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ testArtifactsBusinessLogic.validateFileExtension(
+ responseWrapper, deploymentConfigGetter, artifactInfo,
+ parentType, artifactType);
+
+ assertFalse(responseWrapper.isEmpty());
+ assertEquals(responseWrapper.getInnerElement(),expectedResponseFormat);
+ }
+
+ @Test
+ public void testIfValidateFileExtensionRespondsWithWrongArtifactTypeExtensionIfExtensionIsNotAccepted() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+ final Wrapper<ResponseFormat> responseWrapper = new Wrapper<>();
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ when(artifactInfo.getArtifactName()).thenReturn("test.heat");
+ final ArtifactTypeConfig artifactTypeConfig =
+ Mockito.mock(ArtifactTypeConfig.class);
+ when(artifactTypeConfig.getAcceptedTypes()).thenReturn(Collections.singletonList("yaml"));
+ final IDeploymentArtifactTypeConfigGetter deploymentConfigGetter =
+ Mockito.mock(IDeploymentArtifactTypeConfigGetter.class);
+ when(deploymentConfigGetter.getDeploymentArtifactConfig()).
+ thenReturn(artifactTypeConfig);
+ final NodeTypeEnum parentType = NodeTypeEnum.Service;
+ final ArtifactTypeEnum artifactType = ArtifactTypeEnum.HEAT;
+
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.WRONG_ARTIFACT_FILE_EXTENSION), eq(artifactType.getType()))).thenReturn(expectedResponseFormat);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ testArtifactsBusinessLogic.validateFileExtension(
+ responseWrapper, deploymentConfigGetter, artifactInfo,
+ parentType, artifactType);
+
+ assertFalse(responseWrapper.isEmpty());
+ assertEquals(responseWrapper.getInnerElement(),expectedResponseFormat);
+ }
+
+ @Test
+ public void validateFillArtifactPayloadValidationReturnsNoErrorIfCalledWithProperParameters() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ when(artifactInfo.getPayloadData()).thenReturn("artifactInfoPayload".getBytes());
+ final Wrapper<byte[]> payloadWrapper = new Wrapper<>();
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ testArtifactsBusinessLogic.fillArtifactPayloadValidation(errorWrapper, payloadWrapper, artifactInfo);
+
+ assertEquals(artifactInfo.getPayloadData(),payloadWrapper.getInnerElement());
+ }
+
+ @Test
+ public void validateFillArtifactPayloadValidationReturnsNoErrorIfCalledWithEmptyArtifactPayloadButPayloadIsInCasandraDao() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
+ final String esId = "testEsId";
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ when(artifactInfo.getPayloadData()).thenReturn("".getBytes());
+ when(artifactInfo.getEsId()).thenReturn(esId);
+ final Wrapper<byte[]> payloadWrapper = new Wrapper<>();
+ final byte[] payloadArtifactData = "testArtifactData".getBytes();
+ final byte[] base64PayloadArtifactData = Base64.decodeBase64(payloadArtifactData);
+ final ESArtifactData artifactData = Mockito.mock(ESArtifactData.class);
+ when(artifactData.getDataAsArray()).thenReturn(base64PayloadArtifactData);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ when(artifactCassandraDao.getArtifact(esId)).thenReturn(Either.left(artifactData));
+
+ testArtifactsBusinessLogic.fillArtifactPayloadValidation(errorWrapper, payloadWrapper, artifactInfo);
+
+ assertFalse(payloadWrapper.isEmpty());
+ assertArrayEquals(payloadWrapper.getInnerElement(), payloadArtifactData);
+ }
+
+ @Test
+ public void validateFillArtifactPayloadValidationReturnsErrorIfCalledWithEmptyArtifactPayloadAndNoPayloadInCasandraDao() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+ final Wrapper<ResponseFormat> errorWrapper = new Wrapper<>();
+ final String esId = "testEsId";
+ final ArtifactDefinition artifactInfo = Mockito.mock(ArtifactDefinition.class);
+ when(artifactInfo.getPayloadData()).thenReturn("".getBytes());
+ when(artifactInfo.getEsId()).thenReturn(esId);
+ final Wrapper<byte[]> payloadWrapper = new Wrapper<>();
+
+ when(artifactCassandraDao.getArtifact(esId)).thenReturn(Either.right(CassandraOperationStatus.GENERAL_ERROR));
+ when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR)).thenReturn(ActionStatus.ERROR_DURING_CSAR_CREATION);
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.ERROR_DURING_CSAR_CREATION))).thenReturn(expectedResponseFormat);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+ testArtifactsBusinessLogic.fillArtifactPayloadValidation(errorWrapper, payloadWrapper, artifactInfo);
+
+ assertFalse(errorWrapper.isEmpty());
+ assertEquals(errorWrapper.getInnerElement(),expectedResponseFormat);
+ }
+
+ @Test
+ public void validateGetDeploymentArtifactsReturnsCorrectArtifactLists() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final Component parentComponent = Mockito.mock(Component.class);
+ final ArtifactDefinition artifactDefinition = Mockito.mock(ArtifactDefinition.class);
+ when(parentComponent.getDeploymentArtifacts()).thenReturn(Collections.singletonMap("testService", artifactDefinition));
+ final NodeTypeEnum parentType = NodeTypeEnum.Service;
+ final String ciId = "testCiId";
+
+ List<ArtifactDefinition> result = testArtifactsBusinessLogic.getDeploymentArtifacts(parentComponent, parentType, ciId);
+
+ assertEquals(result.size(), 1);
+ assertEquals(result.get(0), artifactDefinition);
+ }
+
+ @Test
+ public void validateGetDeploymentArtifactsReturnsCorrectArtifactListsForResourceInstance() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final String ciId = "testCiId";
+ final ArtifactDefinition deploymentArtifact = Mockito.mock(ArtifactDefinition.class);
+ final Map<String, ArtifactDefinition> deploymentArtifacts = Collections.singletonMap("",deploymentArtifact);
+ final ComponentInstance componentInstance = Mockito.mock(ComponentInstance.class);
+ when(componentInstance.getUniqueId()).thenReturn(ciId);
+ when(componentInstance.getDeploymentArtifacts()).thenReturn(deploymentArtifacts);
+ final Component parentComponent = Mockito.mock(Component.class);
+ when(parentComponent.getComponentInstances()).thenReturn(Collections.singletonList(componentInstance));
+ final NodeTypeEnum parentType = NodeTypeEnum.ResourceInstance;
+
+ List<ArtifactDefinition> result = testArtifactsBusinessLogic.getDeploymentArtifacts(parentComponent, parentType, ciId);
+
+ assertEquals(result.size(), 1);
+ assertEquals(result.get(0), deploymentArtifact);
+ }
+
+ @Test
+ public void validateHandleGetArtifactsByTypeReturnsProperArtifact() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+ final String containerComponentType = "services";
+ final String parentId = "testParentId";
+ final ComponentTypeEnum componentType = ComponentTypeEnum.SERVICE;
+ final String componentId = "testComponentId";
+ final String artifactGroupType = "testArtifactGroupType";
+ final String userId = "testUserId";
+ final User user = new User();
+
+ final UserValidations userValidations = Mockito.mock(UserValidations.class);
+ when(userValidations.validateUserExists(eq(userId), eq("get artifacts"), eq(false)))
+ .thenReturn(user);
+
+
+ when(toscaOperationFacade.getToscaElement(eq(componentId), any(ComponentParametersView.class)))
+ .thenReturn(Either.right(StorageOperationStatus.OK));
+ when(componentsUtils.convertFromStorageResponse(
+ eq(StorageOperationStatus.OK), eq(ComponentTypeEnum.SERVICE)))
+ .thenReturn(ActionStatus.OK);
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.OK), eq(componentId)))
+ .thenReturn(expectedResponseFormat);
+
+ testArtifactsBusinessLogic.setUserValidations(userValidations);
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ Either<Map<String, ArtifactDefinition>, ResponseFormat> response =
+ testArtifactsBusinessLogic.handleGetArtifactsByType(
+ containerComponentType, parentId, componentType,
+ componentId, artifactGroupType, userId
+ );
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(), expectedResponseFormat);
+ }
+
+ @Test
+ public void validateHandleGetArtifactsByTypeReturnsMissingInformationIfUserIdIsNull() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+ final String containerComponentType = "services";
+ final String parentId = "testParentId";
+ final ComponentTypeEnum componentType = ComponentTypeEnum.SERVICE;
+ final String componentId = "testComponentId";
+ final String artifactGroupType = "testArtifactGroupType";
+ final String userId = null;
+
+ when(componentsUtils.getResponseFormat(eq(ActionStatus.MISSING_INFORMATION)))
+ .thenReturn(expectedResponseFormat);
+
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ Either<Map<String, ArtifactDefinition>, ResponseFormat> response =
+ testArtifactsBusinessLogic.handleGetArtifactsByType(
+ containerComponentType, parentId, componentType,
+ componentId, artifactGroupType, userId
+ );
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(), expectedResponseFormat);
+ }
+
+ @Test
+ public void validateDeleteArtifactByInterfaceReturnsProperResponse() {
+ ArtifactsBusinessLogic testArtifactsBusinessLogic = getArtifactsBusinessLogic();
+
+ final ResponseFormat expectedResponseFormat = Mockito.mock(ResponseFormat.class);
+ final String resourceId = "testResources";
+ final String userId = "testUser";
+ final String artifactId = "testArtifact";
+ final boolean inTransaction = false;
+ final String serviceId = "testService";
+ final Resource resource = Mockito.mock(Resource.class);
+ when(resource.getUniqueId()).thenReturn(serviceId);
+
+ when(toscaOperationFacade.getToscaElement(resourceId, JsonParseFlagEnum.ParseMetadata))
+ .thenReturn(Either.left(resource));
+ when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.right(StorageOperationStatus.OK));
+ when(componentsUtils.getResponseFormat(ActionStatus.OK))
+ .thenReturn(expectedResponseFormat);
+ when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.OK))
+ .thenReturn(ActionStatus.OK);
+ when(componentsUtils.getResponseFormatByArtifactId(ActionStatus.OK, artifactId))
+ .thenReturn(expectedResponseFormat);
+ testArtifactsBusinessLogic.setComponentsUtils(componentsUtils);
+
+ Either<Operation, ResponseFormat> response =
+ testArtifactsBusinessLogic.deleteArtifactByInterface(
+ resourceId, userId, artifactId, inTransaction
+ );
+
+ assertTrue(response.isRight());
+ assertEquals(response.right().value(),expectedResponseFormat);
+ }
+