From 865692e5607193510a2cafbfee3a7f02560463d8 Mon Sep 17 00:00:00 2001 From: KrupaNagabhushan Date: Wed, 8 Jun 2022 19:29:41 +0100 Subject: Fix getToscaFunction validation for property value Issue-ID: SDC-4039 Signed-off-by: KrupaNagabhushan Change-Id: I75c7c8338490df235c2b43db88f708dc649a9667 --- .../files/default/error-configuration.yaml | 7 ++ .../impl/ComponentInstanceBusinessLogic.java | 26 +++++ .../ToscaGetFunctionExceptionSupplier.java | 34 +++++- .../impl/ComponentInstanceBusinessLogicTest.java | 116 ++++++++++++++++++++- 4 files changed, 180 insertions(+), 3 deletions(-) (limited to 'catalog-be/src') diff --git a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml index 53f7c1d41a..58f3dc337c 100644 --- a/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml +++ b/catalog-be/src/main/docker/backend/chef-repo/cookbooks/sdc-catalog-be/files/default/error-configuration.yaml @@ -2725,3 +2725,10 @@ errors: messageId: "SVC4171" } + #-----------SVC4172--------------------------- + #%1 - TOSCA function attribute + TOSCA_FUNCTION_MISSING_ATTRIBUTE: { + code: 400, + message: "Missing TOSCA function '%1'.", + messageId: "SVC4172" + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java index ec4535a27d..ab01b99957 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java @@ -2369,6 +2369,7 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { private void validateToscaGetFunction(T property, Component parentComponent) { final ToscaGetFunctionDataDefinition toscaGetFunction = property.getToscaGetFunction(); + validateGetToscaFunctionAttributes(toscaGetFunction); validateGetPropertySource(toscaGetFunction.getFunctionType(), toscaGetFunction.getPropertySource()); if (toscaGetFunction.getFunctionType() == ToscaGetFunctionType.GET_INPUT) { validateGetFunction(property, parentComponent.getInputs(), parentComponent.getModel()); @@ -2471,6 +2472,31 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { } } + private void validateGetToscaFunctionAttributes(final ToscaGetFunctionDataDefinition toscaGetFunction) { + if (toscaGetFunction.getFunctionType() == null) { + throw ToscaGetFunctionExceptionSupplier.targetFunctionTypeNotFound().get(); + } + if (toscaGetFunction.getPropertySource() == null) { + throw ToscaGetFunctionExceptionSupplier.targetPropertySourceNotFound(toscaGetFunction.getFunctionType()).get(); + } + if (CollectionUtils.isEmpty(toscaGetFunction.getPropertyPathFromSource())) { + throw ToscaGetFunctionExceptionSupplier + .targetSourcePathNotFound(toscaGetFunction.getFunctionType()).get(); + } + if (StringUtils.isEmpty(toscaGetFunction.getSourceName()) || StringUtils.isBlank(toscaGetFunction.getSourceName())) { + throw ToscaGetFunctionExceptionSupplier.sourceNameNotFound(toscaGetFunction.getPropertySource()).get(); + } + if (StringUtils.isEmpty(toscaGetFunction.getSourceUniqueId()) || StringUtils.isBlank(toscaGetFunction.getSourceUniqueId())) { + throw ToscaGetFunctionExceptionSupplier.sourceIdNotFound(toscaGetFunction.getPropertySource()).get(); + } + if (StringUtils.isEmpty(toscaGetFunction.getPropertyName()) || StringUtils.isBlank(toscaGetFunction.getPropertyName())) { + throw ToscaGetFunctionExceptionSupplier.propertyNameNotFound(toscaGetFunction.getPropertySource()).get(); + } + if (StringUtils.isEmpty(toscaGetFunction.getPropertyUniqueId()) || StringUtils.isBlank(toscaGetFunction.getPropertyUniqueId())) { + throw ToscaGetFunctionExceptionSupplier.propertyIdNotFound(toscaGetFunction.getPropertySource()).get(); + } + } + private ResponseFormat updateInputOnContainerComponent(ComponentInstanceInput input, String newValue, Component containerComponent, ComponentInstance foundResourceInstance) { StorageOperationStatus status; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/exceptions/ToscaGetFunctionExceptionSupplier.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/exceptions/ToscaGetFunctionExceptionSupplier.java index 5e1d005bfa..44d6f50740 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/exceptions/ToscaGetFunctionExceptionSupplier.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/exceptions/ToscaGetFunctionExceptionSupplier.java @@ -26,6 +26,7 @@ import java.util.function.Supplier; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; import org.openecomp.sdc.be.datatypes.enums.PropertySource; import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; @@ -37,13 +38,44 @@ public class ToscaGetFunctionExceptionSupplier { final String errorMsg = String.format("%s on %s", toscaGetFunctionType.getFunctionName(), propertySource.getName()); return () -> new ByActionStatusComponentException(ActionStatus.NOT_SUPPORTED, errorMsg); } - public static Supplier propertyNotFoundOnTarget(final String propertyName, final PropertySource propertySource, final ToscaGetFunctionType functionType) { return propertyNotFoundOnTarget(List.of(propertyName), propertySource, functionType); } + public static Supplier targetFunctionTypeNotFound() { + return () -> new ByActionStatusComponentException(ActionStatus.TOSCA_FUNCTION_MISSING_ATTRIBUTE, "function type"); + } + + public static Supplier targetPropertySourceNotFound(final ToscaGetFunctionType toscaGetFunctionType) { + return () -> new ByActionStatusComponentException(ActionStatus.TOSCA_FUNCTION_MISSING_ATTRIBUTE, "property source of " + toscaGetFunctionType); + } + + public static Supplier targetSourcePathNotFound(final ToscaGetFunctionType toscaGetFunctionType) { + return () -> new ByActionStatusComponentException(ActionStatus.TOSCA_FUNCTION_MISSING_ATTRIBUTE, "source path of " + toscaGetFunctionType); + } + + public static Supplier propertyNameNotFound(final PropertySource propertySource) { + return () -> new ByActionStatusComponentException(ActionStatus.TOSCA_FUNCTION_MISSING_ATTRIBUTE, "property name of source " + + propertySource); + } + + public static Supplier propertyIdNotFound(final PropertySource propertySource) { + return () -> new ByActionStatusComponentException(ActionStatus.TOSCA_FUNCTION_MISSING_ATTRIBUTE, "property id of source " + + propertySource); + } + + public static Supplier sourceNameNotFound(final PropertySource propertySource) { + return () -> new ByActionStatusComponentException(ActionStatus.TOSCA_FUNCTION_MISSING_ATTRIBUTE, "source name of " + + propertySource); + } + + public static Supplier sourceIdNotFound(final PropertySource propertySource) { + return () -> new ByActionStatusComponentException(ActionStatus.TOSCA_FUNCTION_MISSING_ATTRIBUTE, "source id of " + + propertySource); + } + public static Supplier propertyNotFoundOnTarget(final List propertyPathFromSource, final PropertySource propertySource, final ToscaGetFunctionType functionType) { diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java index c7d66bf80f..bf3ec3cb7e 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java @@ -52,6 +52,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import java.util.Set; +import java.util.stream.Stream; import mockit.Deencapsulation; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; @@ -63,6 +64,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @@ -685,6 +689,12 @@ class ComponentInstanceBusinessLogicTest { propertyGetInput.setName("anyName"); final var toscaGetFunction = new ToscaGetFunctionDataDefinition(); toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_ATTRIBUTE); + toscaGetFunction.setPropertySource(PropertySource.SELF); + toscaGetFunction.setPropertyPathFromSource(List.of("sourcePath")); + toscaGetFunction.setSourceName("sourceName"); + toscaGetFunction.setSourceUniqueId("sourceUniqueId"); + toscaGetFunction.setPropertyName("propertyName"); + toscaGetFunction.setPropertyUniqueId("propertyId"); propertyGetInput.setToscaGetFunction(toscaGetFunction); properties.add(propertyGetInput); @@ -710,8 +720,49 @@ class ComponentInstanceBusinessLogicTest { //then assertTrue(responseFormatEither.isRight(), "Expecting an error"); final ResponseFormat actualResponse = responseFormatEither.right().value(); - final ResponseFormat expectedResponse = - ToscaGetFunctionExceptionSupplier.functionNotSupported(toscaGetFunction.getFunctionType()).get().getResponseFormat(); + final ResponseFormat expectedResponse = ToscaGetFunctionExceptionSupplier + .functionNotSupported(toscaGetFunction.getFunctionType()).get().getResponseFormat(); + assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage()); + assertEquals(expectedResponse.getStatus(), actualResponse.getStatus()); + } + + @ParameterizedTest + @MethodSource("getToscaFunctionForValidation") + void testToscaGetFunctionValidation_AttributesNotFoundTest(final ToscaGetFunctionDataDefinition toscaGetFunction, + final ResponseFormat expectedValidationResponse) { + final String userId = "userId"; + final String containerComponentId = "containerComponentId"; + final String containerComponentName = "containerComponentName"; + final String resourceInstanceId = "resourceInstanceId"; + final List properties = new ArrayList<>(); + final ComponentInstanceProperty propertyGetInput = new ComponentInstanceProperty(); + propertyGetInput.setName("anyName"); + 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> 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, 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 = expectedValidationResponse; assertEquals(expectedResponse.getFormattedMessage(), actualResponse.getFormattedMessage()); assertEquals(expectedResponse.getStatus(), actualResponse.getStatus()); } @@ -2655,5 +2706,66 @@ class ComponentInstanceBusinessLogicTest { return schemaDefinition; } + private static Stream 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) + ); + } + } -- cgit 1.2.3-korg