+
+ // Prepare ComponentInstance & Resource objects used in createComponentInstance() tests
+
+ private Pair<ComponentInstance, Resource> prepareResourcesForCreateComponentInstanceTest() {
+ ComponentInstance instanceToBeCreated = new ComponentInstance();
+ instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
+ instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
+ instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
+ instanceToBeCreated.setOriginType(OriginTypeEnum.VF);
+
+ Resource originComponent = new Resource();
+ originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
+ originComponent.setResourceType(ResourceTypeEnum.VF);
+ originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
+ originComponent.setIcon(ICON_NAME);
+
+ return Pair.of(instanceToBeCreated, originComponent);
+ }
+ // Common part for testing component instance name validation
+
+ private void testCreateComponentInstanceNameValidationFailure(String ciName) {
+ ComponentInstance ci = new ComponentInstance();
+ ci.setName(ciName);
+
+ // Stub for getting component
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+
+ // Expecting ByActionStatusComponentException
+ ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ assertEquals(ActionStatus.INVALID_COMPONENT_NAME, e.getActionStatus());
+ }
+ @TestFactory
+ Iterable<DynamicTest> testCreateComponentInstanceNameValidationFailureFactory() {
+ String longName = String.join("", Collections.nCopies(ValidationUtils.COMPONENT_NAME_MAX_LENGTH + 1, "x"));
+ String invalidName = "componentInstance#name";
+ return Arrays.asList(
+ dynamicTest("instance name is empty", () ->
+ testCreateComponentInstanceNameValidationFailure("")),
+ dynamicTest("instance name is too long", () ->
+ testCreateComponentInstanceNameValidationFailure(longName)),
+ dynamicTest("instance name includes invalid character", () ->
+ testCreateComponentInstanceNameValidationFailure(invalidName))
+ );
+ }
+
+ @Test
+ void testCreateComponentInstanceFailToGetComponent() {
+ ComponentInstance ci = prepareResourcesForCreateComponentInstanceTest().getLeft();
+
+ // Stub for getting component
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+ when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE))
+ .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
+
+ ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ assertThat(e.getActionStatus()).isEqualTo(ActionStatus.RESOURCE_NOT_FOUND);
+ }
+
+ @Test
+ void testCreateComponentInstanceFailureInvalidState() {
+ Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
+ ComponentInstance ci = p.getLeft();
+ Resource originComponent = p.getRight();
+ originComponent.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ // Stub for getting component
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originComponent));
+
+ ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ assertThat(e.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_COMPONENT_IN_STATE);
+ }
+
+ @Test
+ void testCreateComponentInstanceFailureArchived() {
+ Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
+ ComponentInstance ci = p.getLeft();
+ Resource originComponent = p.getRight();
+ originComponent.setArchived(Boolean.TRUE);
+
+ // Stub for getting component
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originComponent));
+
+ ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ assertThat(e.getActionStatus()).isEqualTo(ActionStatus.COMPONENT_IS_ARCHIVED);
+ }
+
+ @Test
+ void testCreateComponentInstanceFailureInvalidOriginType() {
+ Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
+ ComponentInstance ci = p.getLeft();
+ Resource originComponent = p.getRight();
+ ci.setOriginType(OriginTypeEnum.VFC); // Set different type from origin
+
+ // Stub for getting component
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originComponent));
+
+ final ByActionStatusComponentException e = assertThrows(ByActionStatusComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ assertThat(e.getActionStatus()).isEqualTo(ActionStatus.INVALID_CONTENT);
+ }
+
+ @Test
+ void testCreateComponentInstanceFailureCannotContainInstance() {
+ final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
+ final ComponentInstance ci = p.getLeft();
+ final Resource originComponent = p.getRight();
+
+ // Stub for getting component
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originComponent));
+ // Assume services cannot contain VF resource
+ when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
+ .thenReturn(false);
+
+ ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
+ verify(containerInstanceTypeData, times(1)).isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
+
+ //given
+ final Resource resource = createResource();
+ resource.setResourceType(ResourceTypeEnum.VF);
+ resource.setLastUpdaterUserId(USER_ID);
+ //when
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(resource));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originComponent));
+ when(containerInstanceTypeData.isAllowedForResourceComponent(ResourceTypeEnum.VF, ResourceTypeEnum.VF))
+ .thenReturn(false);
+ actualException = assertThrows(ByActionStatusComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(RESOURCE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ //then
+ assertThat(actualException.getActionStatus()).isEqualTo(ActionStatus.CONTAINER_CANNOT_CONTAIN_INSTANCE);
+ }
+
+ @Test
+ void testCreateComponentInstanceFailureAddToGraph() {
+ final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
+ final ComponentInstance ci = p.getLeft();
+ final Resource originComponent = p.getRight();
+
+ // TODO Refactor createComponentInstance() method and reduce these mocks
+ // not to target the internal details too much
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originComponent));
+ when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
+ .thenReturn(true);
+ Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(ci);
+ when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user))
+ .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST));
+ when(componentsUtils.convertFromStorageResponseForResourceInstance(StorageOperationStatus.BAD_REQUEST, true))
+ .thenReturn(ActionStatus.INVALID_CONTENT);
+ when(componentsUtils.getResponseFormatForResourceInstance(ActionStatus.INVALID_CONTENT, "", null))
+ .thenReturn(new ResponseFormat());
+ when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
+ when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+
+ assertThrows(ByResponseFormatComponentException.class, () -> {
+ componentInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, ci);
+ });
+ verify(containerInstanceTypeData, times(1))
+ .isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
+ verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(ci);
+ verify(toscaOperationFacade, times(1))
+ .addComponentInstanceToTopologyTemplate(service, originComponent, ci, false, user);
+ verify(graphLockOperation, times(1)).unlockComponent(COMPONENT_ID, NodeTypeEnum.Service);
+ }
+
+ @Test
+ void testCreateComponentInstanceSuccess() {
+ final Pair<ComponentInstance, Resource> p = prepareResourcesForCreateComponentInstanceTest();
+ final ComponentInstance instanceToBeCreated = p.getLeft();
+ final Resource originComponent = p.getRight();
+
+ final Service updatedService = new Service();
+ updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
+ updatedService.setUniqueId(service.getUniqueId());
+
+ // TODO Refactor createComponentInstance() method and reduce these mocks
+ // not to target the internal details too much
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originComponent));
+ when(containerInstanceTypeData.isAllowedForServiceComponent(ResourceTypeEnum.VF, null))
+ .thenReturn(true);
+ Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
+ when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user))
+ .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
+ when(artifactsBusinessLogic.getArtifacts(
+ ORIGIN_COMPONENT_ID, NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
+ .thenReturn(Either.left(new HashMap<>()));
+ when(toscaOperationFacade
+ .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originComponent.getArtifacts()))
+ .thenReturn(StorageOperationStatus.OK);
+ when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
+ when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+
+ final ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
+ ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
+ assertThat(result).isEqualTo(instanceToBeCreated);
+ assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originComponent.getVersion());
+ assertThat(instanceToBeCreated.getIcon()).isEqualTo(originComponent.getIcon());
+ verify(containerInstanceTypeData, times(1))
+ .isAllowedForServiceComponent(ResourceTypeEnum.VF, null);
+ verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
+ verify(toscaOperationFacade, times(1))
+ .addComponentInstanceToTopologyTemplate(service, originComponent, instanceToBeCreated, false, user);
+ // Check graph db change was committed
+ verify(janusGraphDao, times(1)).commit();
+ }
+
+ @Test
+ void testCreateComponentInstanceServiceSubstitutionSuccess() {
+ ComponentInstance instanceToBeCreated = createServiceSubstitutionComponentInstance();
+ Service originService = createServiceSubstitutionOriginService();
+ Component serviceBaseComponent = createServiceSubstitutionServiceDerivedFromComponent();
+
+ Service updatedService = new Service();
+ updatedService.setComponentInstances(Collections.singletonList(instanceToBeCreated));
+ updatedService.setUniqueId(service.getUniqueId());
+
+ when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(service));
+ when(toscaOperationFacade.getToscaFullElement(ORIGIN_COMPONENT_ID))
+ .thenReturn(Either.left(originService));
+ when(toscaOperationFacade.getLatestByToscaResourceName(eq(originService.getDerivedFromGenericType()), isNull()))
+ .thenReturn(Either.left(serviceBaseComponent));
+ when(toscaOperationFacade.getToscaElement(eq(ORIGIN_COMPONENT_ID), any(ComponentParametersView.class)))
+ .thenReturn(Either.left(originService));
+ Mockito.doNothing().when(compositionBusinessLogic).validateAndSetDefaultCoordinates(instanceToBeCreated);
+ when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user))
+ .thenReturn(Either.left(new ImmutablePair<>(updatedService, COMPONENT_INSTANCE_ID)));
+ when(artifactsBusinessLogic.getArtifacts(
+ "baseComponentId", NodeTypeEnum.Resource, ArtifactGroupTypeEnum.DEPLOYMENT, null))
+ .thenReturn(Either.left(new HashMap<>()));
+ when(toscaOperationFacade
+ .addInformationalArtifactsToInstance(service.getUniqueId(), instanceToBeCreated, originService.getArtifacts()))
+ .thenReturn(StorageOperationStatus.OK);
+ when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
+ when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+
+ ComponentInstance result = componentInstanceBusinessLogic.createComponentInstance(
+ ComponentTypeEnum.SERVICE_PARAM_NAME, COMPONENT_ID, USER_ID, instanceToBeCreated);
+ assertThat(result).isEqualTo(instanceToBeCreated);
+ assertThat(instanceToBeCreated.getComponentVersion()).isEqualTo(originService.getVersion());
+ assertThat(instanceToBeCreated.getIcon()).isEqualTo(originService.getIcon());
+ verify(compositionBusinessLogic, times(1)).validateAndSetDefaultCoordinates(instanceToBeCreated);
+ verify(toscaOperationFacade, times(1))
+ .addComponentInstanceToTopologyTemplate(service, serviceBaseComponent, instanceToBeCreated, false, user);
+ // Check graph db change was committed
+ verify(janusGraphDao, times(1)).commit();
+ }
+
+ @Test
+ void testGetComponentInstanceAttributesById_success() {
+ final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
+ componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
+
+ final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
+ map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
+ resource.setComponentInstancesAttributes(map);
+
+ final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
+ doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
+
+ final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
+ .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
+ assertThat(result).isNotNull().isNotEmpty();
+ verify(toscaOperationFacade, times(1)).getToscaElement(COMPONENT_ID);
+ }
+
+ @Test
+ void testGetComponentInstanceAttributesById_fail_missing_ComponentInstancesAttributes() {
+ final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
+ doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
+
+ final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
+ .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
+ assertThat(result).isNotNull().isEmpty();
+ verify(toscaOperationFacade, times(1)).getToscaElement(COMPONENT_ID);
+ }
+
+ @Test
+ void testGetComponentInstanceAttributesById_fail_getToscaElement() {
+ final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
+ componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
+
+ final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
+ map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
+ resource.setComponentInstancesAttributes(map);
+
+ final Either<Object, StorageOperationStatus> right = Either.right(StorageOperationStatus.BAD_REQUEST);
+ doReturn(right).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
+ doReturn(ActionStatus.BAD_REQUEST_MISSING_RESOURCE).when(componentsUtils).convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST);
+
+ assertThrows(ByActionStatusComponentException.class, () -> {
+ final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
+ .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, TO_INSTANCE_ID, USER_ID);
+
+ });
+
+ }
+
+ @Test
+ void testGetComponentInstanceAttributesById_fail_getResourceInstanceById() {
+ final ComponentInstanceAttribute componentInstanceAttribute = new ComponentInstanceAttribute();
+ componentInstanceAttribute.setComponentInstanceId(TO_INSTANCE_ID);
+
+ final HashMap<String, List<ComponentInstanceAttribute>> map = new HashMap<>();
+ map.put(TO_INSTANCE_ID, Arrays.asList(componentInstanceAttribute));
+ resource.setComponentInstancesAttributes(map);
+
+ final Either<Component, StorageOperationStatus> leftServiceOp = Either.left(resource);
+ doReturn(leftServiceOp).when(toscaOperationFacade).getToscaElement(COMPONENT_ID);
+ doReturn(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE).when(componentsUtils).convertFromStorageResponse(StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND);
+
+ assertThrows(ByActionStatusComponentException.class, () -> {
+ final List<ComponentInstanceAttribute> result = componentInstanceBusinessLogic
+ .getComponentInstanceAttributesById(RESOURCE_PARAM_NAME, COMPONENT_ID, "", USER_ID);
+
+ });
+
+ }
+
+ @Test
+ void updateInstanceCapabilitySuccessTest() {
+ var containerComponentId = "containerComponentId";
+ var componentInstanceUniqueId = "componentInstanceUniqueId";
+ var capabilityDefinition = new CapabilityDefinition();
+ capabilityDefinition.setUniqueId("uniqueId");
+
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(USER_ID);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var componentInstance = new ComponentInstance();
+ componentInstance.setUniqueId(componentInstanceUniqueId);
+ component.setComponentInstances(Collections.singletonList(componentInstance));
+
+ when(toscaOperationFacade.getToscaFullElement(containerComponentId))
+ .thenReturn(Either.left(component));
+ when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
+ .thenReturn(capabilityDefinition);
+ when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
+ .thenReturn(Either.left(component));
+ when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+
+ final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
+ .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
+ assertTrue(resultEither.isLeft());
+ final CapabilityDefinition actualCapabilityDefinition = resultEither.left().value();
+ assertNotEquals(capabilityDefinition, actualCapabilityDefinition);
+ assertEquals(capabilityDefinition.getUniqueId(), actualCapabilityDefinition.getUniqueId());
+ }
+
+ @Test
+ void updateInstanceCapabilityNoContainerComponentTypeTest() {
+ var responseFormat = new ResponseFormat();
+ when(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED)).thenReturn(responseFormat);
+ final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
+ .updateInstanceCapability(null, "containerComponentId", "componentInstanceUniqueId", new CapabilityDefinition(), USER_ID);
+ assertTrue(resultEither.isRight(), "Either return should be right");
+ final ResponseFormat actualResponseFormat = resultEither.right().value();
+ assertEquals(responseFormat, actualResponseFormat);
+ }
+
+ @Test
+ void updateInstanceCapabilityContainerComponentNotFoundTest() {
+ var containerComponentId = "containerComponentId";
+ when(toscaOperationFacade.getToscaFullElement(containerComponentId)).thenReturn(Either.right(null));
+ var responseFormat = new ResponseFormat();
+ when(componentsUtils.getResponseFormat(ActionStatus.COMPONENT_NOT_FOUND, containerComponentId)).thenReturn(responseFormat);
+ final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
+ .updateInstanceCapability(ComponentTypeEnum.SERVICE, "containerComponentId", "componentInstanceUniqueId", new CapabilityDefinition(), USER_ID);
+ assertTrue(resultEither.isRight(), "Either return should be right");
+ final ResponseFormat actualResponseFormat = resultEither.right().value();
+ assertEquals(responseFormat, actualResponseFormat);
+ }
+
+ @Test
+ void updateInstanceCapabilityCannotWorkOnComponentTest() {
+ var containerComponentId = "containerComponentId";
+ var componentInstanceUniqueId = "componentInstanceUniqueId";
+
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId("anotherUse");
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var expectedResponseFormat = new ResponseFormat();
+
+ when(toscaOperationFacade.getToscaFullElement(containerComponentId))
+ .thenReturn(Either.left(component));
+ when(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION))
+ .thenReturn(expectedResponseFormat);
+
+ final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
+ .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, new CapabilityDefinition(), USER_ID);
+ assertTrue(resultEither.isRight(), "Either return should be right");
+ final ResponseFormat actualResponseFormat = resultEither.right().value();
+ assertEquals(expectedResponseFormat, actualResponseFormat);
+ }
+
+ @Test
+ void updateInstanceCapabilityResourceInstanceNotFoundTest() {
+ var containerComponentId = "containerComponentId";
+ var componentInstanceUniqueId = "componentInstanceUniqueId";
+
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(USER_ID);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var expectedResponseFormat = new ResponseFormat();
+
+ when(toscaOperationFacade.getToscaFullElement(containerComponentId))
+ .thenReturn(Either.left(component));
+ when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_INSTANCE_NOT_FOUND_ON_SERVICE, componentInstanceUniqueId, containerComponentId))
+ .thenReturn(expectedResponseFormat);
+
+ final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
+ .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, new CapabilityDefinition(), USER_ID);
+ assertTrue(resultEither.isRight(), "Either return should be right");
+ final ResponseFormat actualResponseFormat = resultEither.right().value();
+ assertEquals(expectedResponseFormat, actualResponseFormat);
+ }
+
+ @Test
+ void updateInstanceCapabilityUpdateMetadataFailTest() {
+ var containerComponentId = "containerComponentId";
+ var componentInstanceUniqueId = "componentInstanceUniqueId";
+ var capabilityDefinition = new CapabilityDefinition();
+ capabilityDefinition.setUniqueId("uniqueId");
+
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(USER_ID);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var componentInstance = new ComponentInstance();
+ componentInstance.setUniqueId(componentInstanceUniqueId);
+ component.setComponentInstances(Collections.singletonList(componentInstance));
+
+ var expectedResponseFormat = new ResponseFormat();
+
+ when(toscaOperationFacade.getToscaFullElement(containerComponentId))
+ .thenReturn(Either.left(component));
+ when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
+ .thenReturn(capabilityDefinition);
+ when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
+ .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
+ when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR, ComponentTypeEnum.SERVICE))
+ .thenReturn(ActionStatus.GENERAL_ERROR);
+ when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR))
+ .thenReturn(expectedResponseFormat);
+
+ final Either<CapabilityDefinition, ResponseFormat> resultEither = componentInstanceBusinessLogic
+ .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
+ assertTrue(resultEither.isRight(), "Either return should be right");
+ final ResponseFormat actualResponseFormat = resultEither.right().value();
+ assertEquals(expectedResponseFormat, actualResponseFormat);
+ }
+
+ @Test
+ void updateInstanceCapabilityBusinessExceptionHandlingTest() {
+ var containerComponentId = "containerComponentId";
+ var componentInstanceUniqueId = "componentInstanceUniqueId";
+ var capabilityDefinition = new CapabilityDefinition();
+ capabilityDefinition.setUniqueId("uniqueId");
+
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(USER_ID);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var componentInstance = new ComponentInstance();
+ componentInstance.setUniqueId(componentInstanceUniqueId);
+ component.setComponentInstances(Collections.singletonList(componentInstance));
+
+
+ when(toscaOperationFacade.getToscaFullElement(containerComponentId))
+ .thenReturn(Either.left(component));
+ when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
+ .thenThrow(new OperationException(ActionStatus.GENERAL_ERROR));
+
+ final BusinessException businessException = assertThrows(BusinessException.class, () -> {
+ componentInstanceBusinessLogic
+ .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID);
+ });
+ assertTrue(businessException instanceof OperationException);
+ assertEquals(ActionStatus.GENERAL_ERROR, ((OperationException) businessException).getActionStatus());
+ }
+
+ @Test
+ void updateInstanceCapabilityUnknownExceptionHandlingTest() {
+ var containerComponentId = "containerComponentId";
+ var componentInstanceUniqueId = "componentInstanceUniqueId";
+ var capabilityDefinition = new CapabilityDefinition();
+ capabilityDefinition.setUniqueId("uniqueId");
+
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(USER_ID);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var componentInstance = new ComponentInstance();
+ componentInstance.setUniqueId(componentInstanceUniqueId);
+ component.setComponentInstances(Collections.singletonList(componentInstance));
+
+ var expectedResponseFormat = new ResponseFormat();
+
+ when(toscaOperationFacade.getToscaFullElement(containerComponentId))
+ .thenReturn(Either.left(component));
+ when(graphLockOperation.lockComponent(containerComponentId, NodeTypeEnum.Service))
+ .thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.updateComponentInstanceCapability(containerComponentId, componentInstanceUniqueId, capabilityDefinition))
+ .thenThrow(new RuntimeException());
+ when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR))
+ .thenReturn(expectedResponseFormat);
+
+ final Exception exception = assertThrows(BusinessException.class, () ->
+ componentInstanceBusinessLogic
+ .updateInstanceCapability(ComponentTypeEnum.SERVICE, containerComponentId, componentInstanceUniqueId, capabilityDefinition, USER_ID));
+ assertTrue(exception instanceof ByResponseFormatComponentException);
+ final ByResponseFormatComponentException actualException = (ByResponseFormatComponentException) exception;
+ assertEquals(expectedResponseFormat, actualException.getResponseFormat());
+ }
+
+ private ComponentInstance createServiceSubstitutionComponentInstance() {
+ final ComponentInstance instanceToBeCreated = new ComponentInstance();
+ instanceToBeCreated.setName(COMPONENT_INSTANCE_NAME);
+ instanceToBeCreated.setUniqueId(COMPONENT_INSTANCE_ID);
+ instanceToBeCreated.setComponentUid(ORIGIN_COMPONENT_ID);
+ instanceToBeCreated.setOriginType(OriginTypeEnum.ServiceSubstitution);
+
+ return instanceToBeCreated;
+ }
+
+ private Service createServiceSubstitutionOriginService() {
+ final Service originComponent = new Service();
+ originComponent.setLifecycleState(LifecycleStateEnum.CERTIFIED);
+ originComponent.setVersion(ORIGIN_COMPONENT_VERSION);
+ originComponent.setIcon(ICON_NAME);
+ originComponent.setDerivedFromGenericType("org.openecomp.resource.abstract.nodes.service");
+ originComponent.setName("myService");
+ return originComponent;
+ }
+
+ private Component createServiceSubstitutionServiceDerivedFromComponent() {
+ final Resource component = new Resource();
+ component.setLifecycleState(LifecycleStateEnum.CERTIFIED);
+ component.setVersion(ORIGIN_COMPONENT_VERSION);
+ component.setIcon(ICON_NAME);
+ component.setToscaResourceName("org.openecomp.resource.abstract.nodes.service");
+ component.setUniqueId("baseComponentId");
+ return component;
+ }
+
+ private void mockComponentForToscaGetFunctionValidation(final Component component) {
+ when(toscaOperationFacade.getToscaElement(component.getUniqueId(), JsonParseFlagEnum.ParseAll))
+ .thenReturn(Either.left(component));
+ when(graphLockOperation.lockComponent(component.getUniqueId(), NodeTypeEnum.ResourceInstance))
+ .thenReturn(StorageOperationStatus.OK);
+ when(toscaOperationFacade.updateComponentInstanceMetadataOfTopologyTemplate(component))
+ .thenReturn(Either.left(component));
+ when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
+ when(graphLockOperation.unlockComponent(component.getUniqueId(), NodeTypeEnum.ResourceInstance))
+ .thenReturn(StorageOperationStatus.OK);
+ }
+
+ private ToscaGetFunctionDataDefinition createGetToscaFunction(final String propertyName, final String propertyUniqueId,
+ final List<String> propertyPathFromSource,
+ final PropertySource propertySource, final ToscaGetFunctionType functionType,
+ final String sourceUniqueId,
+ final String sourceName) {
+ final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction.setFunctionType(functionType);
+ toscaGetFunction.setPropertyUniqueId(propertyUniqueId);
+ toscaGetFunction.setPropertyName(propertyName);
+ toscaGetFunction.setPropertyPathFromSource(propertyPathFromSource);
+ toscaGetFunction.setPropertySource(propertySource);
+ toscaGetFunction.setSourceName(sourceName);
+ toscaGetFunction.setSourceUniqueId(sourceUniqueId);
+ return toscaGetFunction;
+ }
+
+ private SchemaDefinition createSchema(final String schemaType) {
+ final var schemaDefinition = new SchemaDefinition();
+ final var schemaProperty = new PropertyDefinition();
+ schemaProperty.setType(schemaType);
+ schemaDefinition.setProperty(schemaProperty);
+ return schemaDefinition;
+ }
+
+ private static Stream<Arguments> getToscaFunctionForValidation() {
+ final var toscaGetFunction1 = new ToscaGetFunctionDataDefinition();
+ final ResponseFormat expectedResponse1 = ToscaGetFunctionExceptionSupplier
+ .targetFunctionTypeNotFound().get().getResponseFormat();
+
+ final var toscaGetFunction2 = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction2.setFunctionType(ToscaGetFunctionType.GET_INPUT);
+ final ResponseFormat expectedResponse2 = ToscaGetFunctionExceptionSupplier
+ .targetPropertySourceNotFound(toscaGetFunction2.getFunctionType()).get().getResponseFormat();
+
+ final var toscaGetFunction3 = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction3.setFunctionType(ToscaGetFunctionType.GET_INPUT);
+ toscaGetFunction3.setPropertySource(PropertySource.SELF);
+ final ResponseFormat expectedResponse3 = ToscaGetFunctionExceptionSupplier
+ .targetSourcePathNotFound(toscaGetFunction3.getFunctionType()).get().getResponseFormat();
+
+ final var toscaGetFunction4 = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction4.setFunctionType(ToscaGetFunctionType.GET_INPUT);
+ toscaGetFunction4.setPropertySource(PropertySource.SELF);
+ toscaGetFunction4.setPropertyPathFromSource(List.of("sourcePath"));
+ final ResponseFormat expectedResponse4 = ToscaGetFunctionExceptionSupplier
+ .sourceNameNotFound(toscaGetFunction4.getPropertySource()).get().getResponseFormat();
+
+ final var toscaGetFunction5 = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction5.setFunctionType(ToscaGetFunctionType.GET_INPUT);
+ toscaGetFunction5.setPropertySource(PropertySource.SELF);
+ toscaGetFunction5.setPropertyPathFromSource(List.of("sourcePath"));
+ toscaGetFunction5.setSourceName("sourceName");
+ final ResponseFormat expectedResponse5 = ToscaGetFunctionExceptionSupplier
+ .sourceIdNotFound(toscaGetFunction5.getPropertySource()).get().getResponseFormat();
+
+ final var toscaGetFunction6 = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction6.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
+ toscaGetFunction6.setPropertySource(PropertySource.SELF);
+ toscaGetFunction6.setPropertyPathFromSource(List.of("sourcePath"));
+ toscaGetFunction6.setSourceName("sourceName");
+ toscaGetFunction6.setSourceUniqueId("sourceUniqueId");
+ final ResponseFormat expectedResponse6 = ToscaGetFunctionExceptionSupplier
+ .propertyNameNotFound(toscaGetFunction6.getPropertySource()).get().getResponseFormat();
+
+ final var toscaGetFunction7 = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction7.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
+ toscaGetFunction7.setPropertySource(PropertySource.SELF);
+ toscaGetFunction7.setPropertyPathFromSource(List.of("sourcePath"));
+ toscaGetFunction7.setSourceName("sourceName");
+ toscaGetFunction7.setSourceUniqueId("sourceUniqueId");
+ toscaGetFunction7.setPropertyName("propertyName");
+ final ResponseFormat expectedResponse7 = ToscaGetFunctionExceptionSupplier
+ .propertyIdNotFound(toscaGetFunction7.getPropertySource()).get().getResponseFormat();
+
+ return Stream.of(
+ Arguments.of(toscaGetFunction1, expectedResponse1),
+ Arguments.of(toscaGetFunction2, expectedResponse2),
+ Arguments.of(toscaGetFunction3, expectedResponse3),
+ Arguments.of(toscaGetFunction4, expectedResponse4),
+ Arguments.of(toscaGetFunction5, expectedResponse5),
+ Arguments.of(toscaGetFunction6, expectedResponse6),
+ Arguments.of(toscaGetFunction7, expectedResponse7)
+ );
+ }
+