+ resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService, false, new ArrayList<>());
+ assertThat(resultOfUpdate.isLeft()).isTrue();
+ }
+
+ @Test
+ void testCreateDefaultMetadataServiceFunction() {
+ Service currentService = createServiceObject(true);
+ assertThat(currentService.getServiceFunction()).isEmpty();
+ }
+
+ @Test
+ void testCreateCustomMetadataServiceFunction() {
+ String customServiceFunctionName = "customName";
+ Service currentService = createServiceObject(true);
+ currentService.setServiceFunction(customServiceFunctionName);
+ assertThat(currentService.getServiceFunction()).isEqualTo(customServiceFunctionName);
+ }
+
+ @Test
+ void testUpdateMetadataServiceFunction() {
+ Service currentService = createServiceObject(true);
+ Service newService = createServiceObject(false);
+ currentService.setServiceFunction("alice");
+ //valid English word
+ newService.setServiceFunction("bob");
+ Either<Service, ResponseFormat> resultOfUpdate =
+ bl.validateAndUpdateServiceMetadata(user, currentService, newService, false, new ArrayList<>());
+ assertThat(resultOfUpdate.isLeft()).isTrue();
+ Service updatedService = resultOfUpdate.left().value();
+ assertThat(updatedService.getServiceFunction()).isEqualToIgnoringCase("bob");
+ //empty string is valid
+ newService.setServiceFunction("");
+ resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService, false, new ArrayList<>());
+ assertThat(resultOfUpdate.isLeft()).isTrue();
+ //null is valid and assigner to ""
+ newService.setServiceFunction(null);
+ resultOfUpdate = bl.validateAndUpdateServiceMetadata(user, currentService, newService, false, new ArrayList<>());
+ assertThat(resultOfUpdate.isLeft()).isTrue();
+ assertThat(updatedService.getServiceFunction()).isEmpty();
+ }
+
+ @Test
+ void testServiceFunctionExceedLength() {
+ String serviceName = "Service";
+ String serviceFunction =
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
+ Service serviceFunctionExceedLength = createServiceObject(false);
+ serviceFunctionExceedLength.setName(serviceName);
+ serviceFunctionExceedLength.setServiceFunction(serviceFunction);
+ List<String> tgs = new ArrayList<>();
+ tgs.add(serviceName);
+ serviceFunctionExceedLength.setTags(tgs);
+ try {
+ serviceFunctionValidator.validateAndCorrectField(user, serviceFunctionExceedLength, AuditingActionEnum.CREATE_SERVICE);
+ } catch (ComponentException exp) {
+ assertResponse(exp.getResponseFormat(), ActionStatus.PROPERTY_EXCEEDS_LIMIT, SERVICE_FUNCTION);
+ }
+ }
+
+ @Test
+ void testServiceFunctionInvalidCharacter() {
+ String serviceName = "Service";
+ String serviceFunction = "a?";
+ Service serviceFunctionExceedLength = createServiceObject(false);
+ serviceFunctionExceedLength.setName(serviceName);
+ serviceFunctionExceedLength.setServiceFunction(serviceFunction);
+ List<String> tgs = new ArrayList<>();
+ tgs.add(serviceName);
+ serviceFunctionExceedLength.setTags(tgs);
+ try {
+ serviceFunctionValidator.validateAndCorrectField(user, serviceFunctionExceedLength, AuditingActionEnum.CREATE_SERVICE);
+ } catch (ComponentException exp) {
+ assertResponse(exp.getResponseFormat(), ActionStatus.INVALID_PROPERY, SERVICE_FUNCTION);
+ }
+ }
+
+ @Test
+ void testAddPropertyServiceConsumptionServiceNotFound() {
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
+
+ Either<Operation, ResponseFormat> operationEither =
+ bl.addPropertyServiceConsumption("1", "2", "3",
+ user.getUserId(), new ServiceConsumptionData());
+ assertTrue(operationEither.isRight());
+ assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
+ }
+
+ @Test
+ void testAddPropertyServiceConsumptionParentServiceIsEmpty() {
+ Either<Component, StorageOperationStatus> eitherService = Either.left(createNewComponent());
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
+
+ Either<Operation, ResponseFormat> operationEither =
+ bl.addPropertyServiceConsumption("1", "2", "3",
+ user.getUserId(), new ServiceConsumptionData());
+ assertTrue(operationEither.isRight());
+ assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
+ }
+
+ @Test
+ void testAddPropertyServiceConsumptionNoMatchingComponent() {
+ Service aService = createNewService();
+ Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
+
+ String weirdUniqueServiceInstanceId = UUID.randomUUID().toString();
+
+ Either<Operation, ResponseFormat> operationEither =
+ bl.addPropertyServiceConsumption("1", weirdUniqueServiceInstanceId, "3",
+ user.getUserId(), new ServiceConsumptionData());
+ assertTrue(operationEither.isRight());
+ assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
+ }
+
+ @Test
+ void testAddPropertyServiceConsumptionNotComponentInstancesInterfacesOnParentService() {
+ Service aService = createNewService();
+ aService.getComponentInstances().get(0).setUniqueId(aService.getUniqueId());
+ Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
+
+ Either<Operation, ResponseFormat> operationEither =
+ bl.addPropertyServiceConsumption("1", aService.getUniqueId(), "3",
+ user.getUserId(), new ServiceConsumptionData());
+ assertTrue(operationEither.isRight());
+ assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
+ }
+
+ @Test
+ void testAddPropertyServiceConsumptionInterfaceCandidateNotPresent() {
+ Service aService = createNewService();
+ aService.getComponentInstances().get(0).setUniqueId(aService.getUniqueId());
+ Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
+
+ Map<String, List<ComponentInstanceInterface>> componentInstancesInterfacesMap =
+ Maps.newHashMap();
+ componentInstancesInterfacesMap.put(aService.getUniqueId(),
+ Lists.newArrayList(new ComponentInstanceInterface("1", new InterfaceInstanceDataDefinition())));
+
+ aService.setComponentInstancesInterfaces(componentInstancesInterfacesMap);
+
+ Either<Operation, ResponseFormat> operationEither =
+ bl.addPropertyServiceConsumption("1", aService.getUniqueId(), "3",
+ user.getUserId(), new ServiceConsumptionData());
+ assertTrue(operationEither.isRight());
+ assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
+ }
+
+ @Test
+ void testAddPropertyServiceConsumptionNoInputsCandidate() {
+ Service aService = createNewService();
+ aService.getComponentInstances().get(0).setUniqueId(aService.getUniqueId());
+ Either<Component, StorageOperationStatus> eitherService = Either.left(aService);
+ Mockito.when(toscaOperationFacade.getToscaElement(Mockito.anyString())).thenReturn(eitherService);
+
+ String operationId = "operationId";
+ ComponentInstanceInterface componentInstanceInterface =
+ new ComponentInstanceInterface("interfaceId", new InterfaceInstanceDataDefinition());
+ Map<String, Operation> operationsMap = Maps.newHashMap();
+ operationsMap.put(operationId, new Operation(new ArtifactDataDefinition(), "1",
+ new ListDataDefinition<>(), new ListDataDefinition<>()));
+ componentInstanceInterface.setOperationsMap(operationsMap);
+
+ Map<String, List<ComponentInstanceInterface>> componentInstancesInterfacesMap = Maps.newHashMap();
+ componentInstancesInterfacesMap.put(aService.getUniqueId(), Lists.newArrayList(componentInstanceInterface));
+ aService.setComponentInstancesInterfaces(componentInstancesInterfacesMap);
+
+ Either<Operation, ResponseFormat> operationEither =
+ bl.addPropertyServiceConsumption("1", aService.getUniqueId(), operationId,
+ user.getUserId(), new ServiceConsumptionData());
+ assertTrue(operationEither.isRight());
+ assertEquals(HttpStatus.NOT_FOUND.value(), operationEither.right().value().getStatus().intValue());
+ }
+
+ private Resource mockGenericTypeResource() {
+ final Resource genericTypeResource = new Resource();
+ genericTypeResource.setProperties(mockPropertyList());
+ return genericTypeResource;
+ }
+
+ private List<PropertyDefinition> mockPropertyList() {
+ final List<PropertyDefinition> propertyList = new ArrayList<>();
+ final PropertyDefinition propertyDefinition1 = new PropertyDefinition();
+ propertyDefinition1.setName("property1");
+ propertyDefinition1.setType("string");
+ propertyList.add(propertyDefinition1);
+
+ final PropertyDefinition propertyDefinition2 = new PropertyDefinition();
+ propertyDefinition2.setName("property2");
+ propertyDefinition2.setType("boolean");
+ propertyList.add(propertyDefinition2);
+
+ final PropertyDefinition propertyDefinition3 = new PropertyDefinition();
+ propertyDefinition3.setName("property3");
+ propertyDefinition3.setType("string");
+ propertyList.add(propertyDefinition3);
+ return propertyList;
+ }
+
+ @Test
+ void testCreateService_withMultitenancyValidTenant_Success() {
+ Assert.assertTrue(MULTITENANCY_ENABLED);
+ Service service = createServiceObject(false);
+ service.setTenant(TEST_TENANT);
+ when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
+ Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
+
+ if (createResponse.isRight()) {
+ assertEquals(new Integer(200), createResponse.right().value().getStatus());
+ }
+ MatcherAssert.assertThat("Unauthorized Tenant", getTestRoles().contains(service.getTenant()));
+ assertEquals(TEST_TENANT, service.getTenant());
+ assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
+ }
+
+ @Test
+ void testCreateService_withMultitenancyInvalidTenant_Failure() {
+ Service service = createServiceObject(false);
+ service.setTenant("invalid_tenant");
+ when(genericTypeBusinessLogic.fetchDerivedFromGenericType(service, null)).thenReturn(Either.left(genericService));
+ Either<Service, ResponseFormat> createResponse = bl.createService(service, user);
+ MatcherAssert.assertThat("Unauthorized Tenant", !getTestRoles().contains(service.getTenant()));
+ assertNotEquals(TEST_TENANT, service.getTenant());
+ assertEqualsServiceObject(createServiceObject(true), createResponse.left().value());
+ }
+
+ @Test
+ void testUpdateSubstitutionNodeTypeAndVersion() {
+ Service currentService = createServiceObject(true);
+ currentService.setDerivedFromGenericType("genericTypeOne");
+ currentService.setDerivedFromGenericVersion("1.0");
+ Service newService = createServiceObject(false);
+ newService.setDerivedFromGenericType("genericTypeTwo");
+ newService.setDerivedFromGenericVersion("2.0");
+ List<String> subNodePropsToBeRemoved = new ArrayList<>();
+ subNodePropsToBeRemoved.add("testProp");
+ Either<Service, ResponseFormat> resultOfUpdate =
+ bl.validateAndUpdateServiceMetadata(user, currentService, newService, true, subNodePropsToBeRemoved);
+ assertThat(resultOfUpdate.isLeft()).isTrue();
+ Service updatedService = resultOfUpdate.left().value();
+ assertEquals("genericTypeTwo", updatedService.getDerivedFromGenericType());
+ assertEquals("2.0", updatedService.getDerivedFromGenericVersion());
+ }
+
+ @Test
+ void testUpdateSubstitutionNodeTypePropsToRemoveThrowInUseGetProperty() {
+ Service currentService = createServiceObject(true);
+ setComponentInstanceOnServiceWithPropWithToscaFunction(currentService, ToscaGetFunctionType.GET_PROPERTY, "testProp");
+ Service newService = createServiceObject(false);
+ List<String> subNodePropsToBeRemoved = new ArrayList<>();
+ subNodePropsToBeRemoved.add("testProp");
+ Either<Service, ResponseFormat> resultOfUpdate =
+ bl.validateAndUpdateServiceMetadata(user, currentService, newService, true, subNodePropsToBeRemoved);
+ assertThat(resultOfUpdate.isRight()).isTrue();
+ ResponseFormat response = resultOfUpdate.right().value();
+ assertEquals(409, response.getStatus());
+ assertEquals("Cannot change substitution node type as properties of the existing type are referenced by properties %1.", response.getText());
+ }
+
+ @Test
+ void testUpdateSubstitutionNodeTypePropsToRemoveThrowInUseGetAttribute() {
+ Service currentService = createServiceObject(true);
+ setComponentInstanceOnServiceWithPropWithToscaFunction(currentService, ToscaGetFunctionType.GET_ATTRIBUTE, "testProp");
+ Service newService = createServiceObject(false);
+ List<String> subNodePropsToBeRemoved = new ArrayList<>();
+ subNodePropsToBeRemoved.add("testProp");
+ Either<Service, ResponseFormat> resultOfUpdate =
+ bl.validateAndUpdateServiceMetadata(user, currentService, newService, true, subNodePropsToBeRemoved);
+ assertThat(resultOfUpdate.isRight()).isTrue();
+ ResponseFormat response = resultOfUpdate.right().value();
+ assertEquals(409, response.getStatus());
+ assertEquals("Cannot change substitution node type as properties of the existing type are referenced by properties %1.", response.getText());
+ }
+
+ @Test
+ void testUpdateSubstitutionNodeTypePropsToRemoveThrowInUseGetInput() {
+ Service currentService = createServiceObject(true);
+ setComponentInstanceOnServiceWithPropWithToscaFunction(currentService, ToscaGetFunctionType.GET_INPUT, "testProp");
+ Service newService = createServiceObject(false);
+ List<String> subNodePropsToBeRemoved = new ArrayList<>();
+ subNodePropsToBeRemoved.add("testProp");
+ Either<Service, ResponseFormat> resultOfUpdate =
+ bl.validateAndUpdateServiceMetadata(user, currentService, newService, true, subNodePropsToBeRemoved);