diff options
Diffstat (limited to 'catalog-be/src/test/java/org')
10 files changed, 328 insertions, 36 deletions
diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogicTest.java index df4ef646ee..beb38889df 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogicTest.java @@ -112,6 +112,9 @@ public class InterfaceOperationBusinessLogicTest { when(interfaceOperationValidation .validateInterfaceOperations(anyObject(), anyObject(), any(), anyMap(), anyBoolean())) .thenReturn(Either.left(true)); + when(interfaceOperationValidation + .validateDeleteOperationContainsNoMappedOutput(anyObject(), anyObject(), any())) + .thenReturn(Either.left(true)); when(titanDao.commit()).thenReturn(TitanOperationStatus.OK); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceInputsRedeclareHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceInputsRedeclareHandlerTest.java index 2e5128c180..f94906b90e 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceInputsRedeclareHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceInputsRedeclareHandlerTest.java @@ -16,6 +16,7 @@ import org.openecomp.sdc.be.components.utils.InputsBuilder; import org.openecomp.sdc.be.components.utils.ResourceBuilder; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.elements.Annotation; +import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.InputDefinition; @@ -57,7 +58,8 @@ public class ComponentInstanceInputsRedeclareHandlerTest { currContainer = new ResourceBuilder() .addInstanceProperty("inst1", "prop1") .addInstanceProperty("inst1", "prop2") - .addInstanceInput("inst2", "prop3") + .addInstanceInput("inst1", "prop1", Collections.singletonList(new GetInputValueDataDefinition())) + .addInstanceInput("inst1", "prop2", Collections.singletonList(new GetInputValueDataDefinition())) .setUniqueId(RESOURCE_ID) .build(); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceInterfacesMergeTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceInterfacesMergeTest.java new file mode 100644 index 0000000000..69caa8e389 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceInterfacesMergeTest.java @@ -0,0 +1,116 @@ +package org.openecomp.sdc.be.components.merge.instance; + +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.when; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.dao.api.ActionStatus; +import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.ComponentInstanceInterface; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.User; +import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.common.api.UserRoleEnum; +import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils; + +public class ComponentInstanceInterfacesMergeTest { + + @InjectMocks + private ComponentInstanceInterfacesMerge componentInstanceInterfacesMerge; + + @Mock + private DataForMergeHolder dataHolder; + + @Mock + private ComponentsUtils componentsUtils; + + @Mock + private Component containerComponent; + + @Mock + private ToscaOperationFacade toscaOperationFacade; + + private Component updatedContainer; + private Component origContainer; + private Component origComponent; + private ComponentInstance currentResourceInstance; + private ComponentInstanceInterface origComponentInstanceInterface; + private ComponentInstanceInterface newComponentInstanceInterface; + private User user; + + @Before + public void setUpData() { + MockitoAnnotations.initMocks(this); + user = new User(); + user.setUserId("44"); + user.setRole(UserRoleEnum.ADMIN.getName()); + + currentResourceInstance = new ComponentInstance(); + currentResourceInstance.setUniqueId("TestUniqueID1"); + currentResourceInstance.setComponentUid("TestUID1"); + + origComponent = new Service(); + origComponent.setUniqueId("TestUniqueID12"); + + dataHolder = new DataForMergeHolder(); + dataHolder.setOrigInstanceNode(origComponent); + + Map<String, InterfaceDefinition> origInterfaceDefinition = + InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface1", "Operation1", "Operation1"); + origComponentInstanceInterface = new ComponentInstanceInterface("TestService1", origInterfaceDefinition.get("Interface1")); + + Map<String, InterfaceDefinition> newInterfaceDefinition = + InterfaceOperationTestUtils.createMockInterfaceDefinitionMap("Interface2", "Operation2", "Operation2"); + newComponentInstanceInterface = new ComponentInstanceInterface("TestService2", newInterfaceDefinition.get("Interface2")); + + when(toscaOperationFacade.updateComponentInstanceInterfaces(any(), anyString())).thenReturn(StorageOperationStatus.OK); + when(componentsUtils.convertFromStorageResponse(any())).thenReturn(ActionStatus.OK); + + ComponentInstance componentInstance = new ComponentInstance(); + componentInstance.setUniqueId("CI_1"); + componentInstance.setInterfaces((Map) newInterfaceDefinition); + + Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>(); + componentInstanceInterfaces.put(componentInstance.getUniqueId(), Collections.singletonList(newComponentInstanceInterface)); + + updatedContainer = new Service(); + updatedContainer.setComponentInstances(Collections.singletonList(componentInstance)); + updatedContainer.setComponentInstancesInterfaces(componentInstanceInterfaces); + + origContainer = new Service(); + origContainer.setComponentInstances(Collections.singletonList(componentInstance)); + origContainer.setComponentInstancesInterfaces(componentInstanceInterfaces); + } + + @Test + public void saveDataBeforeMerge() { + doReturn(Collections.singletonList(origComponentInstanceInterface)).when(containerComponent).safeGetComponentInstanceInterfaces(any()); + componentInstanceInterfacesMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, origComponent); + assertEquals(origComponent, dataHolder.getOrigInstanceNode()); + assertEquals(origComponentInstanceInterface, dataHolder.getOrigComponentInstanceInterfaces().get(0)); + } + + @Test + public void mergeDataAfterCreate() { + doReturn(Collections.singletonList(origComponentInstanceInterface)).when(containerComponent).safeGetComponentInstanceInterfaces(any()); + componentInstanceInterfacesMerge.saveDataBeforeMerge(dataHolder, containerComponent, currentResourceInstance, origComponent); + componentInstanceInterfacesMerge.mergeDataAfterCreate(user, dataHolder, updatedContainer, "CI_1"); + assertEquals(updatedContainer.getComponentInstancesInterfaces().get("CI_1"), origContainer.getComponentInstancesInterfaces().get("CI_1")); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceMergeDataBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceMergeDataBusinessLogicTest.java index 345e93051e..c9c9f666dd 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceMergeDataBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/instance/ComponentInstanceMergeDataBusinessLogicTest.java @@ -98,6 +98,7 @@ public class ComponentInstanceMergeDataBusinessLogicTest { assertFalse(value.isIgnoreComponentInstancesProperties()); assertFalse(value.isIgnoreComponentInstancesInputs()); assertFalse(value.isIgnoreArtifacts()); + assertFalse(value.isIgnoreComponentInstancesInterfaces()); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/ComponentInstancePropertyDeclaratorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/ComponentInstancePropertyDeclaratorTest.java index 7a2cfd5d9d..5752ae272e 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/ComponentInstancePropertyDeclaratorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/property/ComponentInstancePropertyDeclaratorTest.java @@ -133,7 +133,7 @@ public class ComponentInstancePropertyDeclaratorTest extends PropertyDeclaratorT String expectedInputName = generateExpectedInputName(parentProperty, innerProperty); InputDefinition input = inputsByName.get(expectedInputName); assertThat(input.getType()).isEqualTo(innerProperty.getType()); - assertThat(input.getValue()).isEqualTo(null); + assertThat(input.getValue()).isEqualTo(innerProperty.getValue()); // assertThat(input.getDefaultValue()).isEqualTo(innerProperty.getValue());//bug assertThat(input.getUniqueId()).isEqualTo(UniqueIdBuilder.buildPropertyUniqueId(RESOURCE_ID, input.getName())); assertThat(input.getPropertyId()).isEqualTo(parentProperty.getUniqueId()); @@ -197,7 +197,7 @@ public class ComponentInstancePropertyDeclaratorTest extends PropertyDeclaratorT private void verifyCreatedInput(PropertyDataDefinition property, InputDefinition input) { assertThat(input.getType()).isEqualTo(property.getType()); assertThat(input.getName()).isEqualTo(generateExpectedInputName(property)); - assertThat(input.getValue()).isEqualTo(null); + assertThat(input.getValue()).isEqualTo(property.getValue()); assertThat(input.getDefaultValue()).isEqualTo(property.getValue()); assertThat(input.getUniqueId()).isEqualTo(UniqueIdBuilder.buildPropertyUniqueId(RESOURCE_ID, input.getName())); assertThat(input.getPropertyId()).isEqualTo(property.getUniqueId()); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/ComponentBuilder.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/ComponentBuilder.java index dd5849707a..7275814dcc 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/ComponentBuilder.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/ComponentBuilder.java @@ -1,6 +1,7 @@ package org.openecomp.sdc.be.components.utils; import org.openecomp.sdc.be.dao.utils.MapUtil; +import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.model.*; @@ -135,6 +136,15 @@ public abstract class ComponentBuilder<T extends Component, B extends ComponentB return self(); } + public ComponentBuilder<T, B> addInstanceInput(String instanceId, String propName, List<GetInputValueDataDefinition> getInputValues) { + ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput(); + componentInstanceInput.setName(propName); + componentInstanceInput.setUniqueId(propName); + componentInstanceInput.setGetInputValues(getInputValues); + this.addInstanceInput(instanceId, componentInstanceInput); + return self(); + } + public ComponentBuilder<T, B> addRelationship(RequirementCapabilityRelDef requirementCapabilityRelDef) { if (component.getComponentInstancesRelations() == null) { component.setComponentInstancesRelations(new ArrayList<>()); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtilsTest.java index 8ceac17d46..43649f64f8 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtilsTest.java @@ -1,7 +1,10 @@ package org.openecomp.sdc.be.components.utils; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Optional; + import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -17,6 +20,8 @@ public class InterfaceOperationUtilsTest { private static final String operationId = "operationId"; private static final String operationName = "createOperation"; private static final String interfaceId = "interfaceId"; + private static final String operationId1 = "operationId1"; + private static final String interfaceId1 = "interfaceId1"; private static Resource resource; @Before @@ -94,4 +99,28 @@ public class InterfaceOperationUtilsTest { Assert.assertFalse(operationEntry.isPresent()); } + @Test + public void testGetInterfaceDefinitionFromOperationIdSuccess() { + List<InterfaceDefinition> interfaces = new ArrayList<>(); + interfaces.add(InterfaceOperationTestUtils.createMockInterface(interfaceId, operationId, operationName)); + interfaces.add(InterfaceOperationTestUtils.createMockInterface(interfaceId1, operationId1, operationName)); + Assert.assertTrue(InterfaceOperationUtils.getInterfaceDefinitionFromOperationId(interfaces, operationId) + .isPresent()); + } + + @Test + public void testGetInterfaceDefinitionFromOperationIdFailure() { + List<InterfaceDefinition> interfaces = new ArrayList<>(); + interfaces.add(InterfaceOperationTestUtils.createMockInterface(interfaceId1, operationId1, operationName)); + Assert.assertFalse(InterfaceOperationUtils.getInterfaceDefinitionFromOperationId(interfaces, operationId) + .isPresent()); + } + + @Test + public void testGetInterfaceDefinitionFromOperationIdFailureInterfacesEmpty() { + List<InterfaceDefinition> interfaces = new ArrayList<>(); + Assert.assertFalse(InterfaceOperationUtils.getInterfaceDefinitionFromOperationId(interfaces, operationId) + .isPresent()); + } + }
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidationTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidationTest.java index fe1e384546..29bc8ec43f 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidationTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/validation/InterfaceOperationValidationTest.java @@ -480,6 +480,24 @@ public class InterfaceOperationValidationTest { } + @Test + public void shouldFailValidateDeleteOperationOperationWithMappedOutput() { + InterfaceDefinition inputInterfaceDefinition = InterfaceOperationTestUtils.createMockInterface(interfaceType2, + operationId, operationType1); + InterfaceDefinition inputParamOutputMappedInterface = InterfaceOperationTestUtils.createMockInterface( + interfaceType3, operationId, operationType2); + inputParamOutputMappedInterface.getOperationsMap().values() + .forEach(operation -> operation.getInputs().getListToscaDataDefinition() + .forEach(operationInputDefinition -> operationInputDefinition.setInputId(interfaceType2 + + "." + operationType1 + "." + outputName1))); + component.getInterfaces().put(interfaceType3, inputParamOutputMappedInterface); + component.getInterfaces().put(interfaceType2, InterfaceOperationTestUtils.createMockInterface(interfaceType2, + operationId, operationType1)); + Assert.assertTrue(interfaceOperationValidationUtilTest.validateDeleteOperationContainsNoMappedOutput( + inputInterfaceDefinition.getOperationsMap().get(operationId), component, + inputInterfaceDefinition).isRight()); + } + private InterfaceDefinition createInterfaceOperationData(String uniqueID, String description, ArtifactDefinition artifactDefinition, ListDataDefinition<OperationInputDefinition> inputs, diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java index a61ea0410a..d46d234595 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportHandlerTest.java @@ -144,6 +144,8 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Mockito.when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); Mockito.when(capabiltyRequirementConvertor.convertRequirements(Mockito.any(Map.class), Mockito.any(Resource.class), Mockito.any(ToscaNodeType.class))).thenReturn(Either.left(new ToscaNodeType())); + Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + .thenReturn(Either.left(Collections.emptyMap())); // default test when component is Resource result = testSubject.exportComponent(component); @@ -381,7 +383,8 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Either<ToscaTemplate, ToscaError> result; Mockito.when(dataTypeCache.getAll()).thenReturn(Either.right(TitanOperationStatus.ALREADY_EXIST)); - + Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + .thenReturn(Either.left(Collections.emptyMap())); // default test result = Deencapsulation.invoke(testSubject, "convertNodeType",new HashMap<>(), component, toscaNode, nodeTypes); } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java index fc017c8483..3922bc83a9 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java @@ -16,22 +16,24 @@ package org.openecomp.sdc.be.tosca.utils; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.GET_OPERATION_OUTPUT; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.GET_PROPERTY; import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF; import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceDefinitionElement; import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceTypeElement; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; + import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; + import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.be.DummyConfigurationManager; import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; @@ -39,16 +41,19 @@ import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition; import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; +import org.openecomp.sdc.be.model.tosca.ToscaFunctions; import org.openecomp.sdc.be.tosca.ToscaExportHandler; import org.openecomp.sdc.be.tosca.ToscaRepresentation; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaTemplate; import org.openecomp.sdc.common.util.YamlToObjectConverter; + public class InterfacesOperationsToscaUtilTest { private static final String MAPPED_PROPERTY_NAME = "mapped_property"; @@ -57,6 +62,8 @@ public class InterfacesOperationsToscaUtilTest { private static final String NODE_TYPE_NAME = "test"; private String[] inputTypes = {"string", "integer", "float", "boolean"}; private static ObjectMapper mapper; + private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); + @BeforeClass public static void setUp() { @@ -122,7 +129,7 @@ public class InterfacesOperationsToscaUtilTest { component.setInterfaces(new HashMap<>()); component.getInterfaces().put(interfaceType, addedInterface); ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, null, false); + addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); @@ -153,7 +160,7 @@ public class InterfacesOperationsToscaUtilTest { component.setInterfaces(new HashMap<>()); component.getInterfaces().put(interfaceType, addedInterface); ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, null, false); + addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); ToscaTemplate template = new ToscaTemplate("testService"); @@ -172,6 +179,70 @@ public class InterfacesOperationsToscaUtilTest { Assert.assertTrue(mainYaml.contains("com.some.service.or.other.serviceName")); } + + @Test + public void testGetInterfaceAsMapServiceProxy() { + Component component = new Resource(); + component.setNormalizedName("normalizedComponentName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + addedInterface.setToscaResourceName("com.some.resource.or.other.resourceName"); + addedInterface.setType("com.some.resource.or.other.resourceName"); + addOperationsToInterface(component, addedInterface, 3, 2, true, false); + final String interfaceType = "normalizedComponentName-interface"; + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(interfaceType, addedInterface); + Map<String, Object> interfacesMap = InterfacesOperationsToscaUtil + .getInterfacesMap(component, component.getInterfaces(), null, false, true); + ToscaNodeType nodeType = new ToscaNodeType(); + nodeType.setInterfaces(interfacesMap); + ToscaExportHandler handler = new ToscaExportHandler(); + ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); + Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); + nodeTypes.put(NODE_TYPE_NAME, nodeType); + template.setNode_types(nodeTypes); + final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); + + String mainYaml = toscaRepresentation.getMainYaml(); + Assert.assertFalse(mainYaml.contains("operations")); + Assert.assertTrue(mainYaml.contains("resourceName:")); + Assert.assertTrue(mainYaml.contains("inputs:")); + validateServiceProxyOperationInputs(mainYaml); + Assert.assertFalse(mainYaml.contains("defaultp")); + Assert.assertTrue(mainYaml.contains("has description")); + Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME)); + Assert.assertTrue(mainYaml.contains("com.some.resource.or.other.resourceName")); + } + +// @Test +// public void addInterfaceDefinitionElementToService() { +// Component component = new Service(); +// component.setNormalizedName("normalizedServiceComponentName"); +// InterfaceDefinition addedInterface = new InterfaceDefinition(); +// addedInterface.setToscaResourceName("com.some.service.or.other.serviceName"); +// +// addOperationsToInterface(addedInterface, 3, 2, true); +// final String interfaceType = "normalizedServiceComponentName-interface"; +// component.setInterfaces(new HashMap<>()); +// component.getInterfaces().put(interfaceType, addedInterface); +// ToscaNodeType nodeType = new ToscaNodeType(); +// InterfacesOperationsToscaUtil.addInterfaceDefinitionElement(component, nodeType); +// +// ToscaExportHandler handler = new ToscaExportHandler(); +// ToscaTemplate template = new ToscaTemplate("testService"); +// Map<String, ToscaNodeType> nodeTypes = new HashMap<>(); +// nodeTypes.put("test", nodeType); +// template.setNode_types(nodeTypes); +// final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); +// +// Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); +// Assert.assertTrue(toscaRepresentation.getMainYaml().contains("serviceName:")); +// Assert.assertTrue(toscaRepresentation.getMainYaml().contains("inputs:")); +// Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp")); +// Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description")); +// Assert.assertTrue(toscaRepresentation.getMainYaml().contains("naming_function_")); +// Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.service.or.other.serviceName")); +// } + @Test public void addInterfaceDefinitionElement_noInputs() { Component component = new Resource(); @@ -218,7 +289,7 @@ public class InterfacesOperationsToscaUtilTest { component.setInterfaces(new HashMap<>()); component.getInterfaces().put(addedInterfaceType, addedInterface); ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, null, false); + addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); ToscaTemplate template = new ToscaTemplate("test"); @@ -226,7 +297,6 @@ public class InterfacesOperationsToscaUtilTest { nodeTypes.put("test", nodeType); template.setNode_types(nodeTypes); final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - String mainYaml = toscaRepresentation.getMainYaml(); Assert.assertFalse(mainYaml.contains("operations")); Assert.assertTrue(mainYaml.contains("resourceNameInputMappedToOutput:")); @@ -243,30 +313,30 @@ public class InterfacesOperationsToscaUtilTest { addedInterface.setType(addedInterfaceType); addOperationsToInterface(component, addedInterface, 2, 2, true, true); addedInterface.getOperationsMap().values().stream() - .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( - "name_for_op_0")) - .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() - .filter(opInputDef -> opInputDef.getName().contains("integer")) - .forEach(opInputDef -> opInputDef.setInputId( - addedInterfaceType +".name_for_op_1.output_integer_1"))); + .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( + "name_for_op_0")) + .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() + .filter(opInputDef -> opInputDef.getName().contains("integer")) + .forEach(opInputDef -> opInputDef.setInputId( + addedInterfaceType +".name_for_op_1.output_integer_1"))); //Mapping to operation from another interface String secondInterfaceType = "org.test.lifecycle.standard.interfaceType.second"; InterfaceDefinition secondInterface = new InterfaceDefinition(); secondInterface.setType(secondInterfaceType); addOperationsToInterface(component, secondInterface, 2, 2, true, true); secondInterface.getOperationsMap().values().stream() - .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( - "name_for_op_0")) - .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() - .filter(opInputDef -> opInputDef.getName().contains("integer")) - .forEach(opInputDef -> opInputDef.setInputId( - addedInterfaceType +".name_for_op_1.output_integer_1"))); + .filter(operationInputDefinition -> operationInputDefinition.getName().equalsIgnoreCase( + "name_for_op_0")) + .forEach(operation -> operation.getInputs().getListToscaDataDefinition().stream() + .filter(opInputDef -> opInputDef.getName().contains("integer")) + .forEach(opInputDef -> opInputDef.setInputId( + addedInterfaceType +".name_for_op_1.output_integer_1"))); component.setInterfaces(new HashMap<>()); component.getInterfaces().put(addedInterfaceType, addedInterface); component.getInterfaces().put(secondInterfaceType, secondInterface); ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, null,false); + addInterfaceDefinitionElement(component, nodeType, dataTypes, false); ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); ToscaTemplate template = new ToscaTemplate("test"); @@ -289,7 +359,7 @@ public class InterfacesOperationsToscaUtilTest { for (int i = 0; i < numOfOps; i++) { final OperationDataDefinition operation = new OperationDataDefinition(); operation.setName("name_for_op_" + i); - operation.setDescription( "op "+i+" has description"); + operation.setDescription("op " + i + " has description"); final ArtifactDataDefinition implementation = new ArtifactDataDefinition(); implementation.setArtifactName(i + "_createBPMN.bpmn"); operation.setImplementation(implementation); @@ -297,13 +367,13 @@ public class InterfacesOperationsToscaUtilTest { operation.setInputs(createInputs(component, numOfInputsPerOp)); } if (hasOutputs) { - operation.setOutputs(createOutputs(numOfInputsPerOp)); + operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(), + operation.getName(), numOfInputsPerOp)); } addedInterface.getOperations().put(operation.getName(), operation); } } - private ListDataDefinition<OperationInputDefinition> createInputs(Component component, int numOfInputs) { ListDataDefinition<OperationInputDefinition> operationInputDefinitionList = new ListDataDefinition<>(); for (int i = 0; i < numOfInputs; i++) { @@ -326,10 +396,12 @@ public class InterfacesOperationsToscaUtilTest { component.getInputs().add(componentInput); } - private ListDataDefinition<OperationOutputDefinition> createOutputs(int numOfOutputs) { + private ListDataDefinition<OperationOutputDefinition> createOutputs(String interfaceName, + String operationName, + int numOfOutputs) { ListDataDefinition<OperationOutputDefinition> operationOutputDefinitionList = new ListDataDefinition<>(); for (int i = 0; i < numOfOutputs; i++) { - operationOutputDefinitionList.add(createMockOperationOutputDefinition( + operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName, OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i)); } return operationOutputDefinitionList; @@ -341,14 +413,29 @@ public class InterfacesOperationsToscaUtilTest { operationInputDefinition.setInputId(id); operationInputDefinition.setType(inputTypes[index]); operationInputDefinition.setRequired(index % 2 == 0); + Map<String, List<String>> toscaDefaultValueMap = new HashMap<>(); + List<String> toscaDefaultValues = new ArrayList<>(); + toscaDefaultValues.add(SELF); + toscaDefaultValues.add(id.substring(id.lastIndexOf('.') + 1)); + toscaDefaultValueMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), toscaDefaultValues); + operationInputDefinition.setToscaDefaultValue(new Gson().toJson(toscaDefaultValueMap)); + operationInputDefinition.setSource("ServiceInput"); return operationInputDefinition; } - private OperationOutputDefinition createMockOperationOutputDefinition(String name, int index) { + private OperationOutputDefinition createMockOperationOutputDefinition(String interfaceName, String operationName, + String outputName, int index) { OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition(); - operationInputDefinition.setName(name); + operationInputDefinition.setName(outputName); operationInputDefinition.setType(inputTypes[index]); operationInputDefinition.setRequired(index % 2 == 0); + Map<String, List<String>> toscaDefaultValueMap = new HashMap<>(); + List<String> toscaDefaultValues = new ArrayList<>(); + toscaDefaultValues.add(SELF); + toscaDefaultValues.add(interfaceName); + toscaDefaultValues.add(operationName); + toscaDefaultValues.add(outputName); + toscaDefaultValueMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), toscaDefaultValues); return operationInputDefinition; } @@ -392,14 +479,14 @@ public class InterfacesOperationsToscaUtilTest { String inputType, int index, Map<String, Object> inputValueObject) { Map<String, Object> mappedInputValue = (Map<String, Object>) inputValueObject.get("default"); - if(mappedInputValue.containsKey(GET_PROPERTY)) { + if(mappedInputValue.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) { String mappedPropertyValue = MAPPED_PROPERTY_NAME + index; - List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(GET_PROPERTY); + List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(ToscaFunctions.GET_PROPERTY.getFunctionName()); Assert.assertEquals(2, mappedPropertyDefaultValue.size()); Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF)); Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue)); - } else if(mappedInputValue.containsKey(GET_OPERATION_OUTPUT)) { - List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(GET_OPERATION_OUTPUT); + } else if(mappedInputValue.containsKey(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { + List<String> mappedPropertyDefaultValue = (List<String>) mappedInputValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); Assert.assertEquals(4, mappedPropertyDefaultValue.size()); String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index; Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF)); @@ -407,8 +494,31 @@ public class InterfacesOperationsToscaUtilTest { Assert.assertTrue(mappedPropertyDefaultValue.contains(operationName)); Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue)); } else { - Assert.fail("Invalid Tosca function in default value. Allowed values: "+ GET_PROPERTY + - "/"+ GET_OPERATION_OUTPUT); + Assert.fail("Invalid Tosca function in default value. Allowed values: "+ ToscaFunctions.GET_PROPERTY.getFunctionName() + + "/"+ ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); + } + } + + private void validateServiceProxyOperationInputs(String mainYaml) { + String nodeTypeKey = NODE_TYPE_NAME + ":"; + String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(), + mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length()); + YamlUtil yamlUtil = new YamlUtil(); + ToscaNodeType toscaNodeType = yamlUtil.yamlToObject(nodeTypesRepresentation, ToscaNodeType.class); + for (Object interfaceVal : toscaNodeType.getInterfaces().values()) { + Map<String, Object> interfaceDefinition = mapper.convertValue(interfaceVal, Map.class); + for (Object operationVal : interfaceDefinition.values()) { + if (operationVal instanceof Map) { + Map<String, Object> operation = (Map<String, Object>) mapper.convertValue(operationVal, Map.class); + Map<String, Object> operationInputs = (Map<String, Object>) operation.get("inputs"); + for (Object inputValue : operationInputs.values()) { + Map<String, Object> inputValueAsMap = (Map<String, Object>) inputValue; + Assert.assertFalse(inputValueAsMap.keySet().contains("type")); + Assert.assertFalse(inputValueAsMap.keySet().contains("required")); + Assert.assertFalse(inputValueAsMap.keySet().contains("default")); + } + } + } } } } |