+ @Test
+ void testToscaGetFunctionValidation() {
+ final String userId = "userId";
+ final String containerComponentId = "containerComponentId";
+ final String containerComponentName = "containerComponentName";
+ final String resourceInstanceId = "resourceInstanceId";
+ final String inputName = "myInputToGet";
+ final String inputId = String.format("%s.%s", containerComponentId, inputName);
+ final String schemaType = "string";
+ //creating instance list of string property with get_input value
+ final ComponentInstanceProperty propertyGetInput = new ComponentInstanceProperty();
+ propertyGetInput.setName("getInputProperty");
+ propertyGetInput.setPropertyId(String.format("%s.%s", containerComponentId, "getInputProperty"));
+ propertyGetInput.setValue(String.format("get_input: [\"%s\"]", inputName));
+ propertyGetInput.setType("list");
+ final SchemaDefinition listStringPropertySchema = createSchema(schemaType);
+ propertyGetInput.setSchema(listStringPropertySchema);
+ propertyGetInput.setToscaGetFunction(
+ createGetToscaFunction(inputName, inputId, List.of(propertyGetInput.getName()), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
+ containerComponentId, containerComponentName)
+ );
+ //creating instance map of string property with get_input value to a second level property:
+ // get_input: ["property1", "subProperty1", "subProperty2"]
+ final String getPropertyPropertyName = "getPropertyProperty";
+ final List<String> containerPropertyPath = List.of("property1", "subProperty1", "subProperty2");
+ final String containerPropertyId = String.format("%s.%s", containerComponentId, containerPropertyPath.get(0));
+ final String mapToscaType = "map";
+ final ComponentInstanceProperty propertyGetProperty = createComponentInstanceProperty(
+ String.format("%s.%s", containerComponentId, getPropertyPropertyName),
+ getPropertyPropertyName,
+ mapToscaType,
+ "string",
+ String.format("get_property: [\"%s\"]", String.join(",", containerPropertyPath)),
+ createGetToscaFunction(containerPropertyPath.get(containerPropertyPath.size() - 1), containerPropertyId,
+ containerPropertyPath, PropertySource.SELF, ToscaGetFunctionType.GET_PROPERTY, containerComponentId, containerComponentName)
+ );
+
+ //creating component that has the instance properties
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setName(containerComponentName);
+ component.setLastUpdaterUserId(userId);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+ //adding instance properties to the component
+ final List<ComponentInstanceProperty> resourceInstanceProperties = List.of(propertyGetInput, propertyGetProperty);
+ final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
+ componentInstanceProps.put(resourceInstanceId, resourceInstanceProperties);
+ component.setComponentInstancesProperties(componentInstanceProps);
+
+ //creating component input that will be gotten by the get_input instance property
+ final var inputDefinition = new InputDefinition();
+ inputDefinition.setName(inputName);
+ inputDefinition.setUniqueId(inputId);
+ inputDefinition.setType(propertyGetInput.getType());
+ inputDefinition.setSchema(listStringPropertySchema);
+ component.setInputs(List.of(inputDefinition));
+
+ //creating component property that contains the sub property that will be gotten by the get_property instance property
+ final var propertyDefinition = new PropertyDefinition();
+ propertyDefinition.setName(containerPropertyPath.get(0));
+ propertyDefinition.setUniqueId(containerPropertyId);
+ final String property1Type = "property1.datatype";
+ propertyDefinition.setType(property1Type);
+ component.setProperties(List.of(propertyDefinition));
+ //creating resource instance to be added to the component
+ final ComponentInstance resourceInstance = createComponentInstance("resourceInstance");
+ resourceInstance.setUniqueId(resourceInstanceId);
+ component.setComponentInstances(List.of(resourceInstance));
+
+ mockComponentForToscaGetFunctionValidation(component);
+
+ //creating data types for "map", and sub properties
+ final Map<String, DataTypeDefinition> allDataTypesMap = new HashMap<>();
+ allDataTypesMap.put(mapToscaType, new DataTypeDefinition());
+
+ final String subProperty1Type = "subProperty1.datatype";
+ allDataTypesMap.put(property1Type, createDataType(property1Type, Map.of(containerPropertyPath.get(1), subProperty1Type)));
+
+ final var subProperty2Property = new PropertyDefinition();
+ subProperty2Property.setName(containerPropertyPath.get(2));
+ subProperty2Property.setType(propertyGetProperty.getType());
+ subProperty2Property.setSchema(propertyGetProperty.getSchema());
+ allDataTypesMap.put(subProperty1Type, createDataType(subProperty1Type, List.of(subProperty2Property)));
+
+ when(applicationDataTypeCache.getAll(component.getModel())).thenReturn(Either.left(allDataTypesMap));
+ //when
+ final Either<List<ComponentInstanceProperty>, ResponseFormat> actualResponseFormat = componentInstanceBusinessLogic
+ .createOrUpdatePropertiesValues(
+ ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, resourceInstanceProperties, userId);
+ //then
+ assertTrue(actualResponseFormat.isLeft());
+ assertThat(actualResponseFormat.left().value()).isEqualTo(resourceInstanceProperties);
+ }
+
+ private DataTypeDefinition createDataType(final String name, final Map<String, String> propertyNameAndTypeMap) {
+ final var dataTypeDefinition = new DataTypeDefinition();
+ dataTypeDefinition.setName(name);
+ if (MapUtils.isNotEmpty(propertyNameAndTypeMap)) {
+ for (final Entry<String, String> propertyEntry : propertyNameAndTypeMap.entrySet()) {
+ final var propertyDefinition = new PropertyDefinition();
+ propertyDefinition.setName(propertyEntry.getKey());
+ propertyDefinition.setType(propertyEntry.getValue());
+ dataTypeDefinition.setProperties(List.of(propertyDefinition));
+ }
+ }
+ return dataTypeDefinition;
+ }
+
+ private DataTypeDefinition createDataType(final String name, final List<PropertyDefinition> propertyList) {
+ final var dataTypeDefinition = new DataTypeDefinition();
+ dataTypeDefinition.setName(name);
+ if (CollectionUtils.isNotEmpty(propertyList)) {
+ dataTypeDefinition.setProperties(propertyList);
+ }
+ return dataTypeDefinition;
+ }
+
+ private ComponentInstanceProperty createComponentInstanceProperty(final String uniqueId, final String name, final String type,
+ final String schemaType, final String value,
+ final ToscaGetFunctionDataDefinition toscaGetFunction) {
+ final var componentInstanceProperty = new ComponentInstanceProperty();
+ componentInstanceProperty.setName(name);
+ componentInstanceProperty.setUniqueId(uniqueId);
+ componentInstanceProperty.setType(type);
+ componentInstanceProperty.setValue(value);
+ if (schemaType != null) {
+ final SchemaDefinition schemaDefinition = createSchema(schemaType);
+ componentInstanceProperty.setSchema(schemaDefinition);
+ }
+ if (toscaGetFunction != null) {
+ componentInstanceProperty.setToscaGetFunction(toscaGetFunction);
+ }
+
+ return componentInstanceProperty;
+ }
+
+ @Test
+ void testToscaGetFunctionValidation_schemaDivergeTest() {
+ final String userId = "userId";
+ final String containerComponentId = "containerComponentId";
+ final String containerComponentName = "containerComponentName";
+ final String resourceInstanceId = "resourceInstanceId";
+ final String inputName = "myInputToGet";
+ final String inputId = String.format("%s.%s", containerComponentId, inputName);
+ final String propertyName = "getInputProperty";
+ final String propertyId = String.format("%s.%s", containerComponentId, propertyName);
+ final String propertyType = "list";
+ final List<ComponentInstanceProperty> properties = new ArrayList<>();
+ final ComponentInstanceProperty propertyGetInput = createComponentInstanceProperty(
+ propertyId,
+ "getInputProperty",
+ propertyType,
+ "string",
+ String.format("get_input: [\"%s\"]", inputName),
+ createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
+ containerComponentId, containerComponentName)
+ );
+ properties.add(propertyGetInput);
+
+ final Component component = new Service();
+ component.setUniqueId(containerComponentId);
+ component.setName(containerComponentName);
+ component.setLastUpdaterUserId(userId);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var inputDefinition = new InputDefinition();
+ inputDefinition.setName(inputName);
+ inputDefinition.setUniqueId(inputId);
+ inputDefinition.setType(propertyType);
+ inputDefinition.setSchema(createSchema("integer"));
+ component.setInputs(List.of(inputDefinition));
+
+ final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
+ componentInstanceProps.put(resourceInstanceId, properties);
+ component.setComponentInstancesProperties(componentInstanceProps);
+
+ final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
+ resourceInstance.setUniqueId(resourceInstanceId);
+ component.setComponentInstances(List.of(resourceInstance));
+
+ mockComponentForToscaGetFunctionValidation(component);
+ //when
+ final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
+ componentInstanceBusinessLogic
+ .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
+ //then
+ assertTrue(responseFormatEither.isRight(), "Expecting an error");
+ final ResponseFormat actualResponse = responseFormatEither.right().value();
+ final ResponseFormat expectedResponse =
+ ToscaGetFunctionExceptionSupplier
+ .propertySchemaDiverge(propertyGetInput.getToscaGetFunction().getFunctionType(), inputDefinition.getSchemaType(),
+ propertyGetInput.getSchemaType())
+ .get().getResponseFormat();
+ assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
+ assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
+ }
+
+ @Test
+ void testToscaGetFunctionValidation_propertyTypeDivergeTest() {
+ final String userId = "userId";
+ final String containerComponentId = "containerComponentId";
+ final String containerComponentName = "containerComponentName";
+ final String resourceInstanceId = "resourceInstanceId";
+ final String inputName = "myInputToGet";
+ final String inputId = String.format("%s.%s", containerComponentId, inputName);
+ final String propertyName = "getInputProperty";
+ final String propertyId = String.format("%s.%s", containerComponentId, propertyName);
+ final String propertyType = "string";
+ final List<ComponentInstanceProperty> properties = new ArrayList<>();
+ final ComponentInstanceProperty propertyGetInput = createComponentInstanceProperty(
+ propertyId,
+ "getInputProperty",
+ propertyType,
+ "string",
+ String.format("get_input: [\"%s\"]", inputName),
+ createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
+ containerComponentId, containerComponentName)
+ );
+ properties.add(propertyGetInput);
+
+ final Component component = new Service();
+ component.setName(containerComponentName);
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(userId);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ var inputDefinition = new InputDefinition();
+ inputDefinition.setName(inputName);
+ inputDefinition.setUniqueId(inputId);
+ inputDefinition.setType("integer");
+ component.setInputs(List.of(inputDefinition));
+
+ final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
+ componentInstanceProps.put(resourceInstanceId, properties);
+ component.setComponentInstancesProperties(componentInstanceProps);
+
+ final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
+ resourceInstance.setUniqueId(resourceInstanceId);
+ component.setComponentInstances(List.of(resourceInstance));
+
+ mockComponentForToscaGetFunctionValidation(component);
+ //when
+ final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
+ componentInstanceBusinessLogic
+ .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
+ //then
+ assertTrue(responseFormatEither.isRight(), "Expecting an error");
+ final ResponseFormat actualResponse = responseFormatEither.right().value();
+ final ResponseFormat expectedResponse =
+ ToscaGetFunctionExceptionSupplier
+ .propertyTypeDiverge(propertyGetInput.getToscaGetFunction().getFunctionType(), inputDefinition.getType(), propertyGetInput.getType())
+ .get().getResponseFormat();
+ assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
+ assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
+ }
+
+ @Test
+ void testToscaGetFunctionValidation_toscaFunctionNotSupportedTest() {
+ final String userId = "userId";
+ final String containerComponentId = "containerComponentId";
+ final String containerComponentName = "containerComponentName";
+ final String resourceInstanceId = "resourceInstanceId";
+ final List<ComponentInstanceProperty> properties = new ArrayList<>();
+ final ComponentInstanceProperty propertyGetInput = new ComponentInstanceProperty();
+ propertyGetInput.setName("anyName");
+ final var toscaGetFunction = new ToscaGetFunctionDataDefinition();
+ toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE);
+ propertyGetInput.setToscaGetFunction(toscaGetFunction);
+ properties.add(propertyGetInput);
+
+ final Component component = new Service();
+ component.setName(containerComponentName);
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(userId);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
+ componentInstanceProps.put(resourceInstanceId, properties);
+ component.setComponentInstancesProperties(componentInstanceProps);
+
+ final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
+ resourceInstance.setUniqueId(resourceInstanceId);
+ component.setComponentInstances(List.of(resourceInstance));
+
+ mockComponentForToscaGetFunctionValidation(component);
+ //when
+ final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
+ componentInstanceBusinessLogic
+ .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
+ //then
+ assertTrue(responseFormatEither.isRight(), "Expecting an error");
+ final ResponseFormat actualResponse = responseFormatEither.right().value();
+ final ResponseFormat expectedResponse =
+ ToscaGetFunctionExceptionSupplier.functionNotSupported(toscaGetFunction.getFunctionType()).get().getResponseFormat();
+ assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
+ assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
+ }
+
+ @Test
+ void testToscaGetFunctionValidation_propertyNotFoundTest() {
+ final String userId = "userId";
+ final String containerComponentId = "containerComponentId";
+ final String containerComponentName = "containerComponentName";
+ final String resourceInstanceId = "resourceInstanceId";
+ final String inputName = "myInputToGet";
+ final String inputId = String.format("%s.%s", containerComponentId, inputName);
+ final String propertyName = "getInputProperty";
+ final String propertyId = String.format("%s.%s", containerComponentId, propertyName);
+ final String propertyType = "string";
+ final List<ComponentInstanceProperty> properties = new ArrayList<>();
+ final ComponentInstanceProperty propertyGetInput = createComponentInstanceProperty(
+ propertyId,
+ "getInputProperty",
+ propertyType,
+ "string",
+ String.format("get_input: [\"%s\"]", inputName),
+ createGetToscaFunction(inputName, inputId, List.of(propertyName), PropertySource.SELF, ToscaGetFunctionType.GET_INPUT,
+ containerComponentId, containerComponentName)
+ );
+ properties.add(propertyGetInput);
+
+ final Component component = new Service();
+ component.setName(containerComponentName);
+ component.setUniqueId(containerComponentId);
+ component.setLastUpdaterUserId(userId);
+ component.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
+
+ final Map<String, List<ComponentInstanceProperty>> componentInstanceProps = new HashMap<>();
+ componentInstanceProps.put(resourceInstanceId, properties);
+ component.setComponentInstancesProperties(componentInstanceProps);
+
+ final ComponentInstance resourceInstance = createComponentInstance("componentInstance1");
+ resourceInstance.setUniqueId(resourceInstanceId);
+ component.setComponentInstances(List.of(resourceInstance));
+
+ mockComponentForToscaGetFunctionValidation(component);
+ //when
+ final Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither =
+ componentInstanceBusinessLogic
+ .createOrUpdatePropertiesValues(ComponentTypeEnum.RESOURCE_INSTANCE, containerComponentId, resourceInstanceId, properties, userId);
+ //then
+ assertTrue(responseFormatEither.isRight(), "Expecting an error");
+ final ResponseFormat actualResponse = responseFormatEither.right().value();
+ final ResponseFormat expectedResponse =
+ ToscaGetFunctionExceptionSupplier
+ .propertyNotFoundOnTarget(inputName, PropertySource.SELF, ToscaGetFunctionType.GET_INPUT)
+ .get().getResponseFormat();
+ assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage());
+ assertEquals(expectedResponse.getStatus(), actualResponse.getStatus());
+ }
+