From 8fea9935b3dd4a020bcfdd7cea3125b8f90edad0 Mon Sep 17 00:00:00 2001 From: talig Date: Thu, 13 Feb 2020 09:02:47 +0200 Subject: Fix PropertyConvertor initialization (use spring) refactor static InterfacesOperationsToscaUtil to be non-static InterfacesOperationsConverter (as it uses PropertyConvertor), move logic from static ToscaExportUtils to ToscaExportHandler (as it uses PropertyConvertor) Change-Id: I917351ca59ee1e792c7b0850d6a8b246b71b4b03 Issue-ID: SDC-2765 Signed-off-by: talig --- .../impl/InterfaceOperationBusinessLogic.java | 2 +- .../be/components/impl/ServiceBusinessLogic.java | 2 +- .../PropertyDataValueMergeBusinessLogic.java | 11 +- .../components/utils/InterfaceOperationUtils.java | 6 +- .../sdc/be/servlets/ServiceConsumptionServlet.java | 2 +- .../be/tosca/CapabilityRequirementConverter.java | 33 +- .../sdc/be/tosca/GroupExportParserImpl.java | 5 +- .../be/tosca/InterfacesOperationsConverter.java | 324 ++++++++++++ .../sdc/be/tosca/PolicyExportParserImpl.java | 5 +- .../openecomp/sdc/be/tosca/PropertyConvertor.java | 60 +-- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 136 +++-- .../sdc/be/tosca/utils/InputConverter.java | 9 +- .../tosca/utils/InterfacesOperationsToscaUtil.java | 317 ------------ .../sdc/be/tosca/utils/ToscaExportUtils.java | 101 ---- .../components/impl/utils/PropertiesUtilsTest.java | 1 - .../PropertyDataValueMergeBusinessLogicTest.java | 33 +- .../tosca/CapabilityRequirementConverterTest.java | 11 + .../sdc/be/tosca/GroupExportParserImplTest.java | 8 +- .../tosca/InputAnnotationConvertToToscaTest.java | 8 +- .../sdc/be/tosca/PolicyExportParserImplTest.java | 9 +- .../sdc/be/tosca/PropertyConvertorTest.java | 68 +-- .../sdc/be/tosca/ToscaExportHandlerTest.java | 164 ++++++ .../sdc/be/tosca/ToscaExportUtilsTest.java | 198 -------- .../utils/InterfacesOperationsConverterTest.java | 552 +++++++++++++++++++++ .../utils/InterfacesOperationsToscaUtilTest.java | 543 -------------------- .../datatypes/elements/PropertyDataDefinition.java | 301 +---------- .../elements/PropertyDataDefinitionTest.java | 3 +- 27 files changed, 1279 insertions(+), 1633 deletions(-) create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java delete mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java delete mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java delete mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportUtilsTest.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java delete mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java index a0ea5c9d04..b6d64d2fc7 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/InterfaceOperationBusinessLogic.java @@ -75,7 +75,7 @@ import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getO import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToComponentInput; import static org.openecomp.sdc.be.components.utils.PropertiesUtils.getPropertyCapabilityFromAllCapProps; import static org.openecomp.sdc.be.components.utils.PropertiesUtils.isCapabilityProperty; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; @Component("interfaceOperationBusinessLogic") public class InterfaceOperationBusinessLogic extends BaseBusinessLogic { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java index c1f7808958..ba04c00cdb 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceBusinessLogic.java @@ -167,7 +167,7 @@ import static org.openecomp.sdc.be.components.utils.ConsumptionUtils.isAssignedV import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getOperationOutputName; import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToOtherOperationOutput; import static org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum.UPDATE_SERVICE_METADATA; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; import static org.openecomp.sdc.be.types.ServiceConsumptionSource.SERVICE_INPUT; import static org.openecomp.sdc.be.types.ServiceConsumptionSource.STATIC; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java index c02eb2820d..b4621498a8 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java @@ -30,6 +30,7 @@ import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.tosca.ToscaFunctions; import org.openecomp.sdc.be.tosca.PropertyConvertor; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; @@ -47,13 +48,15 @@ public class PropertyDataValueMergeBusinessLogic { private final PropertyValueMerger propertyValueMerger; private final ApplicationDataTypeCache dataTypeCache; - private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance(); + private final PropertyConvertor propertyConvertor; private final Gson gson = new Gson(); - - public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) { + @Autowired + public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, + ApplicationDataTypeCache dataTypeCache, PropertyConvertor propertyConvertor) { this.propertyValueMerger = propertyValueMerger; this.dataTypeCache = dataTypeCache; + this.propertyConvertor = propertyConvertor; } /** @@ -94,7 +97,7 @@ public class PropertyDataValueMergeBusinessLogic { String propValue = propertyDataDefinition.getValue() == null ? "": propertyDataDefinition.getValue(); String propertyType = propertyDataDefinition.getType(); String innerType = propertyDataDefinition.getSchemaType(); - return propertyConvertor.convertToToscaObject(propertyType, propValue, innerType, dataTypes, true); + return propertyConvertor.convertToToscaObject(propertyDataDefinition, propValue, dataTypes, true); } protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java index 4c891f8da9..9c82a291f9 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/utils/InterfaceOperationUtils.java @@ -28,7 +28,7 @@ import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.tosca.ToscaFunctions; -import org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil; +import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter; import java.util.ArrayList; import java.util.Arrays; @@ -116,7 +116,7 @@ public class InterfaceOperationUtils { public static Map> createMappedInputPropertyDefaultValue(String propertyName) { Map> getPropertyMap = new HashMap<>(); List values = new ArrayList<>(); - values.add(InterfacesOperationsToscaUtil.SELF); + values.add(InterfacesOperationsConverter.SELF); if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) { values.addAll(Arrays.asList(propertyName.split("\\."))); } @@ -128,7 +128,7 @@ public class InterfaceOperationUtils { String propertyName) { Map> getPropertyMap = new HashMap<>(); List values = new ArrayList<>(); - values.add(InterfacesOperationsToscaUtil.SELF); + values.add(InterfacesOperationsConverter.SELF); values.add(capabilityName); if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java index d438784122..a8d1eb64c4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/ServiceConsumptionServlet.java @@ -75,7 +75,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; @Loggable(prepend = true, value = Loggable.DEBUG, trim = false) @Path("/v1/catalog") diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java index 81380301f8..9a55fb885c 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverter.java @@ -20,9 +20,22 @@ package org.openecomp.sdc.be.tosca; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.apache.commons.lang3.StringUtils.isBlank; +import static org.apache.commons.lang3.StringUtils.isNoneBlank; + import com.google.common.collect.Iterables; import com.google.common.collect.Maps; import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringUtils; @@ -55,20 +68,6 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.function.Function; -import java.util.stream.Collectors; - -import static org.apache.commons.collections.CollectionUtils.isNotEmpty; -import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.apache.commons.lang3.StringUtils.isNoneBlank; - /** * Allows to convert requirements\capabilities of a component to requirements\capabilities of a substitution mappings section of a tosca template * @@ -86,6 +85,8 @@ public class CapabilityRequirementConverter { @Autowired private ToscaOperationFacade toscaOperationFacade; + @Autowired + private PropertyConvertor propertyConvertor; public CapabilityRequirementConverter() {} @@ -182,7 +183,7 @@ public class CapabilityRequirementConverter { innerType = prop.getSchema().getProperty().getType(); } String propValue = prop.getValue() == null ? prop.getDefaultValue() : prop.getValue(); - return PropertyConvertor.getInstance().convertToToscaObject(propertyType, propValue, innerType, dataTypes, false); + return propertyConvertor.convertToToscaObject(prop, propValue, dataTypes, false); } /** * Allows to convert requirements of a node type to tosca template requirements representation @@ -573,7 +574,7 @@ public class CapabilityRequirementConverter { if (isNotEmpty(properties)) { Map toscaProperties = new HashMap<>(); for (PropertyDefinition property : properties) { - ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.CAPABILITY); + ToscaProperty toscaProperty = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.CAPABILITY); toscaProperties.put(property.getName(), toscaProperty); } toscaCapability.setProperties(toscaProperties); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java index 0b6d5d87a6..981c444197 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/GroupExportParserImpl.java @@ -69,11 +69,12 @@ public class GroupExportParserImpl implements GroupExportParser { private Map dataTypes; private ApplicationDataTypeCache dataTypeCache; - private PropertyConvertor propertyConvertor = PropertyConvertor.getInstance(); + private final PropertyConvertor propertyConvertor; @Autowired - public GroupExportParserImpl(ApplicationDataTypeCache dataTypeCache) { + public GroupExportParserImpl(ApplicationDataTypeCache dataTypeCache, PropertyConvertor propertyConvertor) { this.dataTypeCache = dataTypeCache; + this.propertyConvertor = propertyConvertor; this.dataTypes = getDataTypes(); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java new file mode 100644 index 0000000000..0e5c55e087 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java @@ -0,0 +1,324 @@ +/* + * Copyright © 2016-2020 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.be.tosca; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import org.apache.commons.collections.MapUtils; +import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; +import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.ComponentInstance; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.InterfaceDefinition; +import org.openecomp.sdc.be.model.Product; +import org.openecomp.sdc.be.model.tosca.ToscaFunctions; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType; +import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition; +import org.openecomp.sdc.be.tosca.model.ToscaNodeType; +import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +@Service +public class InterfacesOperationsConverter { + + + private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard"; + private static final String DERIVED_FROM_BASE_DEFAULT = "org.openecomp.interfaces.node.lifecycle."; + private static final String OPERATIONS_KEY = "operations"; + + private static final String DEFAULT = "default"; + private static final String DEFAULT_HAS_UNDERSCORE = "_default"; + private static final String DOT = "."; + private static final String DEFAULTP = "defaultp"; + + public static final String SELF = "SELF"; + private static final String LOCAL_INTERFACE_TYPE = "Local"; + + private PropertyConvertor propertyConvertor; + + @Autowired + public InterfacesOperationsConverter(PropertyConvertor propertyConvertor) { + this.propertyConvertor = propertyConvertor; + } + + /** + * Creates the interface_types element. + * + * @param component to work on + * @return the added element + */ + public static Map addInterfaceTypeElement(Component component, List allInterfaceTypes) { + if (component instanceof Product) { + return null; + } + final Map interfaces = component.getInterfaces(); + if (MapUtils.isEmpty(interfaces)) { + return null; + } + + Map toscaInterfaceTypes = new HashMap<>(); + for (InterfaceDefinition interfaceDefinition : interfaces.values()) { + boolean isInterfaceTypeExistInGlobalType = + allInterfaceTypes.stream().anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType())); + if (!isInterfaceTypeExistInGlobalType) { + ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType(); + toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE); + + final Map operations = interfaceDefinition.getOperations(); + Map toscaOperations = new HashMap<>(); + + for (Map.Entry operationEntry : operations.entrySet()) { + toscaOperations.put(operationEntry.getValue().getName(), null); + } + toscaInterfaceType.setOperations(toscaOperations); + Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); + Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS_KEY); + interfacesAsMap.putAll(operationsMap); + + toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); + } + } + return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null; + } + + /** + * Adds the 'interfaces' element to the node type provided. + * + * @param component to work on + * @param nodeType to which the interfaces element will be added + */ + public void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType, + Map dataTypes, + boolean isAssociatedComponent) { + if (component instanceof Product) { + return; + } + final Map interfaces = component.getInterfaces(); + if (MapUtils.isEmpty(interfaces)) { + return; + } + Map toscaInterfaceDefinitions = getInterfacesMap(component, dataTypes, + isAssociatedComponent); + if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) { + nodeType.setInterfaces(toscaInterfaceDefinitions); + } + } + + private Map getInterfacesMap(Component component, + Map dataTypes, + boolean isAssociatedComponent) { + return getInterfacesMap(component, null, component.getInterfaces(), dataTypes, isAssociatedComponent, false); + } + + public Map getInterfacesMap(Component component, + ComponentInstance componentInstance, + Map interfaces, + Map dataTypes, + boolean isAssociatedComponent, + boolean isServiceProxyInterface) { + if(MapUtils.isEmpty(interfaces)) { + return null; + } + + Map toscaInterfaceDefinitions = new HashMap<>(); + for (InterfaceDefinition interfaceDefinition : interfaces.values()) { + ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); + String interfaceType; + if(componentInstance != null && LOCAL_INTERFACE_TYPE.equals(interfaceDefinition.getType())) { + interfaceType = DERIVED_FROM_BASE_DEFAULT + componentInstance.getSourceModelName(); + } else { + interfaceType = getInterfaceType(component, interfaceDefinition.getType()); + } + toscaInterfaceDefinition.setType(interfaceType); + final Map operations = interfaceDefinition.getOperations(); + Map toscaOperations = new HashMap<>(); + + String operationArtifactPath; + for (Map.Entry operationEntry : operations.entrySet()) { + ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition(); + if (isArtifactPresent(operationEntry)) { + operationArtifactPath = OperationArtifactUtil + .createOperationArtifactPath(component, componentInstance, operationEntry.getValue(), + isAssociatedComponent); + toscaOperation.setImplementation(operationArtifactPath); + } + toscaOperation.setDescription(operationEntry.getValue().getDescription()); + fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaOperation, isServiceProxyInterface); + + toscaOperations.put(operationEntry.getValue().getName(), toscaOperation); + } + + toscaInterfaceDefinition.setOperations(toscaOperations); + Map interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition); + Map operationsMap = (Map) interfaceDefAsMap.remove(OPERATIONS_KEY); + if (isServiceProxyInterface) { + //Remove input type and copy default value directly into the proxy node template from the node type + handleServiceProxyOperationInputValue(operationsMap, interfaceType); + } else { + handleDefaults(operationsMap); + } + interfaceDefAsMap.putAll(operationsMap); + toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefAsMap); + } + + return toscaInterfaceDefinitions; + } + + private static void handleServiceProxyOperationInputValue(Map operationsMap, String parentKey) { + for (Map.Entry operationEntry : operationsMap.entrySet()) { + final Object value = operationEntry.getValue(); + final String key = operationEntry.getKey(); + if (value instanceof Map) { + if ("inputs".equals(parentKey)) { + Object defaultValue = getDefaultValue((Map) value); + operationsMap.put(key, defaultValue); + } else { + handleServiceProxyOperationInputValue((Map) value, key); + } + } + } + } + + private static Object getDefaultValue(Map inputValueMap) { + Object defaultValue = null; + for (Map.Entry operationEntry : inputValueMap.entrySet()) { + final Object value = operationEntry.getValue(); + if (value instanceof Map) { + getDefaultValue((Map) value); + } + final String key = operationEntry.getKey(); + if (key.equals(DEFAULTP)) { + defaultValue = inputValueMap.remove(key); + } + } + return defaultValue; + } + + /* + * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in + * ToscaExportHandler so, any string Map key named "defaultp" will have its named changed to "default" + * @param operationsMap the map to update + */ + private static void handleDefaults(Map operationsMap) { + for (Map.Entry operationEntry : operationsMap.entrySet()) { + final Object value = operationEntry.getValue(); + if (value instanceof Map) { + handleDefaults((Map) value); + } + final String key = operationEntry.getKey(); + if (key.equals(DEFAULTP)) { + Object removed = operationsMap.remove(key); + operationsMap.put(DEFAULT, removed); + } + } + } + + private static String getLastPartOfName(String toscaResourceName) { + return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1); + } + + private static boolean isArtifactPresent(Map.Entry operationEntry) { + final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation()); + if (isImplementationPresent) { + return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName()); + } + return false; + } + + private void fillToscaOperationInputs(OperationDataDefinition operation, + Map dataTypes, + ToscaLifecycleOperationDefinition toscaOperation, + boolean isServiceProxyInterface) { + if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) { + toscaOperation.setInputs(null); + return; + } + Map toscaInputs = new HashMap<>(); + + for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) { + ToscaProperty toscaInput = new ToscaProperty(); + toscaInput.setDescription(input.getDescription()); + toscaInput.setType(input.getType()); + toscaInput.setRequired(input.isRequired()); + if (isServiceProxyInterface) { + String inputValue = Objects.nonNull(input.getValue()) ? getInputValue(input.getValue()) : + getInputValue(input.getToscaDefaultValue()); + toscaInput.setDefaultp(propertyConvertor.convertToToscaObject(input, inputValue, dataTypes, false)); + } else { + toscaInput.setDefaultp(propertyConvertor + .convertToToscaObject(input, getInputValue(input.getToscaDefaultValue()), + dataTypes, false)); + } + toscaInputs.put(input.getName(), toscaInput); + } + toscaOperation.setInputs(toscaInputs); + } + + private static String getInputValue(String inputValue) { + String toscaInputValue = inputValue; + if (Objects.nonNull(inputValue) && inputValue.contains(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { + Gson gson = new Gson(); + Map> consumptionValue = gson.fromJson(inputValue, Map.class); + List mappedOutputValue = + consumptionValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); + //Extract the interface name from the interface type + String interfaceType = mappedOutputValue.get(1); + String interfaceName = interfaceType.substring(interfaceType.lastIndexOf('.') + 1); + mappedOutputValue.remove(1); + mappedOutputValue.add(1, interfaceName); + toscaInputValue = gson.toJson(consumptionValue); + } + return toscaInputValue; + } + + private static String getInterfaceType(Component component, String interfaceType) { + if (LOCAL_INTERFACE_TYPE.equals(interfaceType)) { + return DERIVED_FROM_BASE_DEFAULT + + component.getComponentMetadataDefinition() + .getMetadataDataDefinition().getSystemName(); + } + + return interfaceType; + } + + private static Map getObjectAsMap(Object obj) { + ObjectMapper objectMapper = new ObjectMapper(); + if (obj instanceof ToscaInterfaceDefinition) { + //Prevent empty field serialization in interface definition + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + Map objectAsMap = + obj instanceof Map ? (Map) obj : objectMapper.convertValue(obj, Map.class); + + if (objectAsMap.containsKey(DEFAULT)) { + Object defaultValue = objectAsMap.get(DEFAULT); + objectAsMap.remove(DEFAULT); + objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue); + } + return objectAsMap; + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java index 776676f7d8..ca4d40517a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PolicyExportParserImpl.java @@ -58,11 +58,12 @@ public class PolicyExportParserImpl implements PolicyExportParser { private ApplicationDataTypeCache dataTypeCache; private Map dataTypes; - private PropertyConvertor propertyConvertor = PropertyConvertor.getInstance(); + private PropertyConvertor propertyConvertor; @Autowired - public PolicyExportParserImpl(ApplicationDataTypeCache dataTypeCache) { + public PolicyExportParserImpl(ApplicationDataTypeCache dataTypeCache, PropertyConvertor propertyConvertor) { this.dataTypeCache = dataTypeCache; + this.propertyConvertor = propertyConvertor; this.dataTypes = getDataTypes(); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index 4e4afb005d..81c91cce99 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -20,12 +20,17 @@ package org.openecomp.sdc.be.tosca; -import com.google.gson.Gson; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import fj.data.Either; +import java.io.StringReader; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Supplier; import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; @@ -43,16 +48,10 @@ import org.openecomp.sdc.be.tosca.model.EntrySchema; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; import org.openecomp.sdc.common.log.wrappers.Logger; +import org.springframework.stereotype.Service; -import java.io.StringReader; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.function.Supplier; - +@Service public class PropertyConvertor { - private static PropertyConvertor instance; private JsonParser jsonParser = new JsonParser(); private static final Logger log = Logger.getLogger(PropertyConvertor.class); public enum PropertyType { @@ -60,17 +59,7 @@ public class PropertyConvertor { INPUT, PROPERTY } - Gson gson = new Gson(); - public PropertyConvertor() { - } - - public static synchronized PropertyConvertor getInstance() { - if (instance == null) { - instance = new PropertyConvertor(); - } - return instance; - } public Either convertProperties(Component component, ToscaNodeType toscaNodeType, Map dataTypes) { @@ -94,32 +83,19 @@ public class PropertyConvertor { public ToscaProperty convertProperty(Map dataTypes, PropertyDefinition property, PropertyType propertyType) { ToscaProperty prop = new ToscaProperty(); - - String innerType = null; + log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue()); SchemaDefinition schema = property.getSchema(); if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) { - innerType = schema.getProperty().getType(); EntrySchema eschema = new EntrySchema(); - eschema.setType(innerType); + eschema.setType(schema.getProperty().getType()); eschema.setDescription(schema.getProperty().getDescription()); prop.setEntry_schema(eschema); } - return getToscaProperty(dataTypes, property, prop, innerType, propertyType); - - } - - private ToscaProperty getToscaProperty(Map dataTypes, - PropertyDataDefinition property, - ToscaProperty prop, - String innerType, - PropertyType propertyType) { - log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue()); String defaultValue = property.getDefaultValue(); if(Objects.isNull(defaultValue)) { defaultValue = property.getValue(); } - Object convertedObj = - convertToToscaObject(property.getType(), defaultValue, innerType, dataTypes, false); + Object convertedObj = convertToToscaObject(property, defaultValue, dataTypes, false); if (convertedObj != null) { prop.setDefaultp(convertedObj); } @@ -133,7 +109,9 @@ public class PropertyConvertor { } - public Object convertToToscaObject(String propertyType, String value, String innerType, Map dataTypes, boolean preserveEmptyValue) { + public Object convertToToscaObject(PropertyDataDefinition property, String value, Map dataTypes, boolean preserveEmptyValue) { + String propertyType = property.getType(); + String innerType = property.getSchemaType(); log.trace("try to convert propertyType {} , value [{}], innerType {}", propertyType, value, innerType); if (StringUtils.isEmpty(value)) { value = DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(propertyType, dataTypes); @@ -144,7 +122,7 @@ public class PropertyConvertor { try { ToscaMapValueConverter mapConverterInst = ToscaMapValueConverter.getInstance(); ToscaValueConverter innerConverter = null; - Boolean isScalar = true; + boolean isScalar = true; ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType); if (type == null) { @@ -234,12 +212,6 @@ public class PropertyConvertor { private Object convertValue(Map dataTypes, T input, Supplier supplier) { - String propertyType = input.getType(); - String innerType = null; - if (input.getSchema() != null && input.getSchema().getProperty() != null) { - innerType = input.getSchema().getProperty().getType(); - } - return convertToToscaObject(propertyType, supplier.get(), innerType, dataTypes, false); + return convertToToscaObject(input, supplier.get(), dataTypes, false); } - } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java index 995d511acb..976842136f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/ToscaExportHandler.java @@ -20,9 +20,28 @@ package org.openecomp.sdc.be.tosca; +import static org.apache.commons.collections.CollectionUtils.isEmpty; +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.apache.commons.collections.MapUtils.isNotEmpty; +import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import fj.data.Either; +import java.beans.IntrospectionException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.function.Supplier; +import java.util.stream.Collectors; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; @@ -87,7 +106,6 @@ import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement; import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate; import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil; import org.openecomp.sdc.be.tosca.utils.InputConverter; -import org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.externalupload.utils.ServiceUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -104,30 +122,6 @@ import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; -import java.beans.IntrospectionException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -import static org.apache.commons.collections.CollectionUtils.isEmpty; -import static org.apache.commons.collections.CollectionUtils.isNotEmpty; -import static org.apache.commons.collections.MapUtils.isNotEmpty; -import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceDefinitionElement; -import static org.openecomp.sdc.be.tosca.utils.InterfacesOperationsToscaUtil.addInterfaceTypeElement; -import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.addInputsToProperties; -import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.getProxyNodeTypeInterfaces; -import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.getProxyNodeTypeProperties; - @org.springframework.stereotype.Component("tosca-export-handler") public class ToscaExportHandler { @@ -139,19 +133,23 @@ public class ToscaExportHandler { private PropertyConvertor propertyConvertor; private InputConverter inputConverter; private InterfaceLifecycleOperation interfaceLifecycleOperation; + private InterfacesOperationsConverter interfacesOperationsConverter; @Autowired public ToscaExportHandler(ApplicationDataTypeCache dataTypeCache, ToscaOperationFacade toscaOperationFacade, - CapabilityRequirementConverter capabilityRequirementConverter, PolicyExportParser policyExportParser, - GroupExportParser groupExportParser, InputConverter inputConverter, InterfaceLifecycleOperation interfaceLifecycleOperation) { + CapabilityRequirementConverter capabilityRequirementConverter, PolicyExportParser policyExportParser, + GroupExportParser groupExportParser, PropertyConvertor propertyConvertor, InputConverter inputConverter, + InterfaceLifecycleOperation interfaceLifecycleOperation, + InterfacesOperationsConverter interfacesOperationsConverter) { this.dataTypeCache = dataTypeCache; this.toscaOperationFacade = toscaOperationFacade; this.capabilityRequirementConverter = capabilityRequirementConverter; this.policyExportParser = policyExportParser; this.groupExportParser = groupExportParser; - this.propertyConvertor = PropertyConvertor.getInstance(); + this.propertyConvertor = propertyConvertor; this.inputConverter = inputConverter; this.interfaceLifecycleOperation = interfaceLifecycleOperation; + this.interfacesOperationsConverter = interfacesOperationsConverter; } @@ -603,7 +601,7 @@ public class ToscaExportHandler { List inputDef = component.getInputs(); Map mergedProperties = new HashMap<>(); - addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); + interfacesOperationsConverter.addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); addInputsToProperties(dataTypes, inputDef, mergedProperties); if(CollectionUtils.isNotEmpty(component.getProperties())) { @@ -834,7 +832,7 @@ public class ToscaExportHandler { currServiceInterfaces.forEach(instInterface -> tmpInterfaces.put(instInterface .getUniqueId(), instInterface)); - interfaces = InterfacesOperationsToscaUtil + interfaces = interfacesOperationsConverter .getInterfacesMap(parentComponent, componentInstance, tmpInterfaces, dataTypes, true, true); } } else { @@ -942,8 +940,7 @@ public class ToscaExportHandler { if (input.getSchema() != null && input.getSchema().getProperty() != null) { innerType = input.getSchema().getProperty().getType(); } - return propertyConvertor.convertToToscaObject(propertyType, supplier.get(), innerType, - dataTypes, true); + return propertyConvertor.convertToToscaObject(input, supplier.get(), dataTypes, true); } private ToscaNodeType createNodeType(Component component) { @@ -1425,7 +1422,7 @@ public class ToscaExportHandler { } private static class CustomRepresenter extends Representer { - public CustomRepresenter() { + CustomRepresenter() { super(); // null representer is exceptional and it is stored as an instance // variable. @@ -1438,16 +1435,15 @@ public class ToscaExportHandler { Tag customTag) { if (propertyValue == null) { return null; - } else { - // skip not relevant for Tosca property - if ("dependencies".equals(property.getName())) { - return null; - } - NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); - - return "_defaultp_".equals(property.getName()) - ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode; } + // skip not relevant for Tosca property + if ("dependencies".equals(property.getName())) { + return null; + } + NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag); + + return "_defaultp_".equals(property.getName()) + ? new NodeTuple(representData("default"), defaultNode.getValueNode()) : defaultNode; } @Override @@ -1495,5 +1491,61 @@ public class ToscaExportHandler { return interfaceObject; } + + Optional> getProxyNodeTypeProperties(Component proxyComponent, + Map + dataTypes) { + if (Objects.isNull(proxyComponent)) { + return Optional.empty(); + } + Map proxyProperties = new HashMap<>(); + addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties); + if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) { + proxyProperties.putAll(proxyComponent.getProperties().stream() + .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, + proxyComponent.getInputs())) + .collect(Collectors.toMap(PropertyDataDefinition::getName, + property -> propertyConvertor.convertProperty(dataTypes, property, + PropertyConvertor.PropertyType.PROPERTY)))); + } + return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty(); + } + + void addInputsToProperties(Map dataTypes, + List componentInputs, + Map mergedProperties) { + if (CollectionUtils.isEmpty(componentInputs)) { + return; + } + for(InputDefinition input : componentInputs) { + ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input, + PropertyConvertor.PropertyType.INPUT); + mergedProperties.put(input.getName(), property); + } + } + + Optional> getProxyNodeTypeInterfaces(Component proxyComponent, + Map dataTypes) { + if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) { + return Optional.empty(); + } + Map proxyComponentInterfaces = proxyComponent.getInterfaces(); + //Unset artifact path for operation implementation for proxy node types as for operations with artifacts it is + // always available in the proxy node template + removeOperationImplementationForProxyNodeType(proxyComponentInterfaces); + return Optional.ofNullable(interfacesOperationsConverter + .getInterfacesMap(proxyComponent, null, proxyComponentInterfaces, dataTypes, + false, false)); + } + + private static void removeOperationImplementationForProxyNodeType( + Map proxyComponentInterfaces) { + if (MapUtils.isEmpty(proxyComponentInterfaces)) { + return; + } + proxyComponentInterfaces.values().stream().map(InterfaceDataDefinition::getOperations) + .filter(MapUtils::isNotEmpty) + .forEach(operations -> operations.values().forEach(operation -> operation.setImplementation(null))); + } } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java index 46305407f7..475627b827 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InputConverter.java @@ -33,17 +33,18 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.springframework.beans.factory.annotation.Autowired; @org.springframework.stereotype.Component public class InputConverter { private PropertyConvertor propertyConvertor; private static final Logger log = Logger.getLogger(ToscaExportHandler.class); - - public InputConverter() { - this.propertyConvertor = PropertyConvertor.getInstance(); - + @Autowired + public InputConverter(PropertyConvertor propertyConvertor) { + this.propertyConvertor = propertyConvertor; } + /** * This is the converter made for input * input is derived from properties and is similar to properties diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java deleted file mode 100644 index 4c0fb25fcd..0000000000 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtil.java +++ /dev/null @@ -1,317 +0,0 @@ -/* - * Copyright © 2016-2018 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openecomp.sdc.be.tosca.utils; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; -import org.apache.commons.collections.MapUtils; -import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; -import org.openecomp.sdc.be.model.Component; -import org.openecomp.sdc.be.model.ComponentInstance; -import org.openecomp.sdc.be.model.DataTypeDefinition; -import org.openecomp.sdc.be.model.InterfaceDefinition; -import org.openecomp.sdc.be.model.Product; -import org.openecomp.sdc.be.model.tosca.ToscaFunctions; -import org.openecomp.sdc.be.tosca.PropertyConvertor; -import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; -import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType; -import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition; -import org.openecomp.sdc.be.tosca.model.ToscaNodeType; -import org.openecomp.sdc.be.tosca.model.ToscaProperty; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - - -public class InterfacesOperationsToscaUtil { - - private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard"; - private static final String DERIVED_FROM_BASE_DEFAULT = "org.openecomp.interfaces.node.lifecycle."; - private static final String OPERATIONS_KEY = "operations"; - - private static final String DEFAULT = "default"; - private static final String DEFAULT_HAS_UNDERSCORE = "_default"; - private static final String DOT = "."; - private static final String DEFAULTP = "defaultp"; - - public static final String SELF = "SELF"; - private static final String LOCAL_INTERFACE_TYPE = "Local"; - - private InterfacesOperationsToscaUtil() { - } - - /** - * Creates the interface_types element. - * - * @param component to work on - * @return the added element - */ - public static Map addInterfaceTypeElement(Component component, List allInterfaceTypes) { - if (component instanceof Product) { - return null; - } - final Map interfaces = component.getInterfaces(); - if (MapUtils.isEmpty(interfaces)) { - return null; - } - - Map toscaInterfaceTypes = new HashMap<>(); - for (InterfaceDefinition interfaceDefinition : interfaces.values()) { - boolean isInterfaceTypeExistInGlobalType = - allInterfaceTypes.stream().anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType())); - if (!isInterfaceTypeExistInGlobalType) { - ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType(); - toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE); - - final Map operations = interfaceDefinition.getOperations(); - Map toscaOperations = new HashMap<>(); - - for (Map.Entry operationEntry : operations.entrySet()) { - toscaOperations.put(operationEntry.getValue().getName(), null); - } - toscaInterfaceType.setOperations(toscaOperations); - Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); - Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS_KEY); - interfacesAsMap.putAll(operationsMap); - - toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); - } - } - return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null; - } - - /** - * Adds the 'interfaces' element to the node type provided. - * - * @param component to work on - * @param nodeType to which the interfaces element will be added - */ - public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType, - Map dataTypes, - boolean isAssociatedComponent) { - if (component instanceof Product) { - return; - } - final Map interfaces = component.getInterfaces(); - if (MapUtils.isEmpty(interfaces)) { - return; - } - Map toscaInterfaceDefinitions = getInterfacesMap(component, dataTypes, - isAssociatedComponent); - if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) { - nodeType.setInterfaces(toscaInterfaceDefinitions); - } - } - - private static Map getInterfacesMap(Component component, - Map dataTypes, - boolean isAssociatedComponent) { - return getInterfacesMap(component, null, component.getInterfaces(), dataTypes, isAssociatedComponent, false); - } - - public static Map getInterfacesMap(Component component, - ComponentInstance componentInstance, - Map interfaces, - Map dataTypes, - boolean isAssociatedComponent, - boolean isServiceProxyInterface) { - if(MapUtils.isEmpty(interfaces)) { - return null; - } - - Map toscaInterfaceDefinitions = new HashMap<>(); - for (InterfaceDefinition interfaceDefinition : interfaces.values()) { - ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition(); - String interfaceType; - if(componentInstance != null && LOCAL_INTERFACE_TYPE.equals(interfaceDefinition.getType())) { - interfaceType = DERIVED_FROM_BASE_DEFAULT + componentInstance.getSourceModelName(); - } else { - interfaceType = getInterfaceType(component, interfaceDefinition.getType()); - } - toscaInterfaceDefinition.setType(interfaceType); - final Map operations = interfaceDefinition.getOperations(); - Map toscaOperations = new HashMap<>(); - - String operationArtifactPath; - for (Map.Entry operationEntry : operations.entrySet()) { - ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition(); - if (isArtifactPresent(operationEntry)) { - operationArtifactPath = OperationArtifactUtil - .createOperationArtifactPath(component, componentInstance, operationEntry.getValue(), - isAssociatedComponent); - toscaOperation.setImplementation(operationArtifactPath); - } - toscaOperation.setDescription(operationEntry.getValue().getDescription()); - fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaOperation, isServiceProxyInterface); - - toscaOperations.put(operationEntry.getValue().getName(), toscaOperation); - } - - toscaInterfaceDefinition.setOperations(toscaOperations); - Map interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition); - Map operationsMap = (Map) interfaceDefAsMap.remove(OPERATIONS_KEY); - if (isServiceProxyInterface) { - //Remove input type and copy default value directly into the proxy node template from the node type - handleServiceProxyOperationInputValue(operationsMap, interfaceType); - } else { - handleDefaults(operationsMap); - } - interfaceDefAsMap.putAll(operationsMap); - toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefAsMap); - } - - return toscaInterfaceDefinitions; - } - - private static void handleServiceProxyOperationInputValue(Map operationsMap, String parentKey) { - for (Map.Entry operationEntry : operationsMap.entrySet()) { - final Object value = operationEntry.getValue(); - final String key = operationEntry.getKey(); - if (value instanceof Map) { - if ("inputs".equals(parentKey)) { - Object defaultValue = getDefaultValue((Map) value); - operationsMap.put(key, defaultValue); - } else { - handleServiceProxyOperationInputValue((Map) value, key); - } - } - } - } - - private static Object getDefaultValue(Map inputValueMap) { - Object defaultValue = null; - for (Map.Entry operationEntry : inputValueMap.entrySet()) { - final Object value = operationEntry.getValue(); - if (value instanceof Map) { - getDefaultValue((Map) value); - } - final String key = operationEntry.getKey(); - if (key.equals(DEFAULTP)) { - defaultValue = inputValueMap.remove(key); - } - } - return defaultValue; - } - - /* - * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in - * ToscaExportHandler so, any string Map key named "defaultp" will have its named changed to "default" - * @param operationsMap the map to update - */ - private static void handleDefaults(Map operationsMap) { - for (Map.Entry operationEntry : operationsMap.entrySet()) { - final Object value = operationEntry.getValue(); - if (value instanceof Map) { - handleDefaults((Map) value); - } - final String key = operationEntry.getKey(); - if (key.equals(DEFAULTP)) { - Object removed = operationsMap.remove(key); - operationsMap.put(DEFAULT, removed); - } - } - } - - private static String getLastPartOfName(String toscaResourceName) { - return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1); - } - - private static boolean isArtifactPresent(Map.Entry operationEntry) { - final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation()); - if (isImplementationPresent) { - return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName()); - } - return false; - } - - private static void fillToscaOperationInputs(OperationDataDefinition operation, - Map dataTypes, - ToscaLifecycleOperationDefinition toscaOperation, - boolean isServiceProxyInterface) { - if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) { - toscaOperation.setInputs(null); - return; - } - Map toscaInputs = new HashMap<>(); - - for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) { - ToscaProperty toscaInput = new ToscaProperty(); - toscaInput.setDescription(input.getDescription()); - toscaInput.setType(input.getType()); - toscaInput.setRequired(input.isRequired()); - if (isServiceProxyInterface) { - String inputValue = Objects.nonNull(input.getValue()) ? getInputValue(input.getValue()) : - getInputValue(input.getToscaDefaultValue()); - toscaInput.setDefaultp(new PropertyConvertor().convertToToscaObject(input.getType(), - inputValue, input.getSchemaType(), dataTypes, false)); - } else { - toscaInput.setDefaultp(new PropertyConvertor().convertToToscaObject(input.getType(), - getInputValue(input.getToscaDefaultValue()), input.getSchemaType(), dataTypes, false)); - } - toscaInputs.put(input.getName(), toscaInput); - } - toscaOperation.setInputs(toscaInputs); - } - - private static String getInputValue(String inputValue) { - String toscaInputValue = inputValue; - if (Objects.nonNull(inputValue) && inputValue.contains(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { - Gson gson = new Gson(); - Map> consumptionValue = gson.fromJson(inputValue, Map.class); - List mappedOutputValue = - consumptionValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); - //Extract the interface name from the interface type - String interfaceType = mappedOutputValue.get(1); - String interfaceName = interfaceType.substring(interfaceType.lastIndexOf('.') + 1); - mappedOutputValue.remove(1); - mappedOutputValue.add(1, interfaceName); - toscaInputValue = gson.toJson(consumptionValue); - } - return toscaInputValue; - } - - private static String getInterfaceType(Component component, String interfaceType) { - if (LOCAL_INTERFACE_TYPE.equals(interfaceType)) { - return DERIVED_FROM_BASE_DEFAULT - + component.getComponentMetadataDefinition() - .getMetadataDataDefinition().getSystemName(); - } - - return interfaceType; - } - - private static Map getObjectAsMap(Object obj) { - ObjectMapper objectMapper = new ObjectMapper(); - if (obj instanceof ToscaInterfaceDefinition) { - //Prevent empty field serialization in interface definition - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - } - Map objectAsMap = - obj instanceof Map ? (Map) obj : objectMapper.convertValue(obj, Map.class); - - if (objectAsMap.containsKey(DEFAULT)) { - Object defaultValue = objectAsMap.get(DEFAULT); - objectAsMap.remove(DEFAULT); - objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue); - } - return objectAsMap; - } -} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java deleted file mode 100644 index beb9dab7e8..0000000000 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright © 2016-2019 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openecomp.sdc.be.tosca.utils; - -import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; -import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; -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.tosca.PropertyConvertor; -import org.openecomp.sdc.be.tosca.model.ToscaProperty; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; - -import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput; - -public class ToscaExportUtils { - - private ToscaExportUtils() { - //Hiding implicit default constructor - } - - public static Optional> getProxyNodeTypeInterfaces(Component proxyComponent, - Map dataTypes) { - if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) { - return Optional.empty(); - } - Map proxyComponentInterfaces = proxyComponent.getInterfaces(); - //Unset artifact path for operation implementation for proxy node types as for operations with artifacts it is - // always available in the proxy node template - removeOperationImplementationForProxyNodeType(proxyComponentInterfaces); - return Optional.ofNullable(InterfacesOperationsToscaUtil - .getInterfacesMap(proxyComponent, null, proxyComponentInterfaces, dataTypes, false, false)); - } - - public static Optional> getProxyNodeTypeProperties(Component proxyComponent, - Map - dataTypes) { - if (Objects.isNull(proxyComponent)) { - return Optional.empty(); - } - Map proxyProperties = new HashMap<>(); - addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties); - if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) { - proxyProperties.putAll(proxyComponent.getProperties().stream() - .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition, - proxyComponent.getInputs())) - .collect(Collectors.toMap(PropertyDataDefinition::getName, - property -> PropertyConvertor.getInstance().convertProperty(dataTypes, property, - PropertyConvertor.PropertyType.PROPERTY)))); - } - return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty(); - } - - public static void addInputsToProperties(Map dataTypes, - List componentInputs, - Map mergedProperties) { - if (CollectionUtils.isEmpty(componentInputs)) { - return; - } - for(InputDefinition input : componentInputs) { - ToscaProperty property = new PropertyConvertor().convertProperty(dataTypes, input, - PropertyConvertor.PropertyType.INPUT); - mergedProperties.put(input.getName(), property); - } - } - - private static void removeOperationImplementationForProxyNodeType(Map - proxyComponentInterfaces) { - if (MapUtils.isEmpty(proxyComponentInterfaces)) { - return; - } - proxyComponentInterfaces.values().stream() - .map(InterfaceDataDefinition::getOperations) - .filter(MapUtils::isNotEmpty) - .forEach(operations -> operations.values() - .forEach(operation -> operation.setImplementation(null))); - } -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/PropertiesUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/PropertiesUtilsTest.java index d038381c21..8789b15be0 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/PropertiesUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/utils/PropertiesUtilsTest.java @@ -14,7 +14,6 @@ * limitations under the License. */ package org.openecomp.sdc.be.components.impl.utils; - import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogicTest.java index def3f8cc69..bded51db11 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogicTest.java @@ -20,10 +20,24 @@ package org.openecomp.sdc.be.components.merge.property; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.Maps; import fj.data.Either; +import java.io.IOException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -37,21 +51,7 @@ import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.when; +import org.openecomp.sdc.be.tosca.PropertyConvertor; @RunWith(MockitoJUnitRunner.class) public class PropertyDataValueMergeBusinessLogicTest { @@ -67,7 +67,8 @@ public class PropertyDataValueMergeBusinessLogicTest { public void setUp() throws Exception { PropertyValueMerger propertyValueMerger = new PropertyValueMerger(); - testInstance = new PropertyDataValueMergeBusinessLogic(propertyValueMerger, applicationDataTypeCache); + testInstance = new PropertyDataValueMergeBusinessLogic(propertyValueMerger, applicationDataTypeCache, + new PropertyConvertor()); } @Test diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverterTest.java index 5d6c98209c..5254c08dab 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/CapabilityRequirementConverterTest.java @@ -19,6 +19,8 @@ package org.openecomp.sdc.be.tosca; import org.junit.Before; import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; @@ -35,6 +37,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -42,6 +45,14 @@ import static org.mockito.Mockito.doReturn; public class CapabilityRequirementConverterTest { + @InjectMocks + CapabilityRequirementConverter testSubject; + + @Mock + ToscaOperationFacade toscaOperationFacade; + @Mock + private PropertyConvertor propertyConvertorMock; + CapabilityRequirementConverter capabiltyRequirementConvertor = Mockito.spy(new CapabilityRequirementConverter()); ComponentInstance instanceProxy = Mockito.spy( new ComponentInstance() ); ComponentInstance vfInstance = Mockito.spy( new ComponentInstance() ); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/GroupExportParserImplTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/GroupExportParserImplTest.java index 35be3821b2..b55f7d8514 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/GroupExportParserImplTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/GroupExportParserImplTest.java @@ -68,9 +68,10 @@ public class GroupExportParserImplTest { @Mock private ApplicationDataTypeCache dataTypeCache; - @Mock private Component component; + @Mock + private PropertyConvertor propertyConvertor; @Before public void setUp() throws Exception { @@ -79,13 +80,14 @@ public class GroupExportParserImplTest { private void initGroupExportParser() { when(dataTypeCache.getAll()).thenReturn(Either.left(null)); - groupExportParser = new GroupExportParserImpl(dataTypeCache); + groupExportParser = new GroupExportParserImpl(dataTypeCache, propertyConvertor); } @Test public void failToGetAllDataTypes() { when(dataTypeCache.getAll()).thenReturn(Either.right(null)); - assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> groupExportParser = new GroupExportParserImpl(dataTypeCache)); + assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> groupExportParser = new GroupExportParserImpl(dataTypeCache, + propertyConvertor)); } @Test diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InputAnnotationConvertToToscaTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InputAnnotationConvertToToscaTest.java index a3d4ae91ab..2d95fc130d 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InputAnnotationConvertToToscaTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InputAnnotationConvertToToscaTest.java @@ -77,10 +77,10 @@ public class InputAnnotationConvertToToscaTest { property3.setType(ToscaPropertyType.BOOLEAN.getType()); property3.setValue("True"); - dataTypes = new HashMap(); + dataTypes = new HashMap<>(); DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); - List properties = new ArrayList(); + List properties = new ArrayList<>(); properties.add(property1); dataTypeDefinition.setProperties(properties); @@ -91,7 +91,7 @@ public class InputAnnotationConvertToToscaTest { annotation.setName("Annotation1"); annotation.setDescription("description1"); - List propertiesAnnotation = new ArrayList(); + List propertiesAnnotation = new ArrayList<>(); propertiesAnnotation.add(property2); propertiesAnnotation.add(property3); annotation.setProperties(propertiesAnnotation); @@ -107,7 +107,7 @@ public class InputAnnotationConvertToToscaTest { ArrayList inputDefList = new ArrayList<> (); inputDefList.add(inputDefinition); - inputConverter = new InputConverter(); + inputConverter = new InputConverter(new PropertyConvertor()); Map resultInputs ; resultInputs = inputConverter.convertInputs(inputDefList,dataTypes); //verify one Input only diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java index 4e377578a3..b499830138 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PolicyExportParserImplTest.java @@ -63,6 +63,8 @@ public class PolicyExportParserImplTest { @Mock private ApplicationDataTypeCache dataTypeCache; + @Mock + private PropertyConvertor propertyConvertor; @Mock private Component component; @@ -71,7 +73,8 @@ public class PolicyExportParserImplTest { public void failToGetAllDataTypes() { when(dataTypeCache.getAll()).thenReturn(Either.right(null)); - assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> policiyExportParser = new PolicyExportParserImpl(dataTypeCache)); + assertThatExceptionOfType(SdcResourceNotFoundException.class).isThrownBy(() -> policiyExportParser = new PolicyExportParserImpl(dataTypeCache, + propertyConvertor)); } @Test @@ -79,7 +82,7 @@ public class PolicyExportParserImplTest { when(dataTypeCache.getAll()).thenReturn(Either.left(null)); when(component.getPolicies()).thenReturn(null); - policiyExportParser = new PolicyExportParserImpl(dataTypeCache); + policiyExportParser = new PolicyExportParserImpl(dataTypeCache, propertyConvertor); Map policies = policiyExportParser.getPolicies(component); assertThat(policies).isEqualTo(null); } @@ -105,7 +108,7 @@ public class PolicyExportParserImplTest { when(component.getPolicies()).thenReturn(policiesToAdd); when(component.getComponentInstances()).thenReturn(getComponentInstances()); when(component.getGroups()).thenReturn(getGroups()); - policiyExportParser = new PolicyExportParserImpl(dataTypeCache); + policiyExportParser = new PolicyExportParserImpl(dataTypeCache, propertyConvertor); Map policies = policiyExportParser.getPolicies(component); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java index 0e6e171a43..d7c53d5bec 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/PropertyConvertorTest.java @@ -20,9 +20,25 @@ package org.openecomp.sdc.be.tosca; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + import fj.data.Either; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.model.DataTypeDefinition; @@ -32,29 +48,20 @@ import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - +@RunWith(MockitoJUnitRunner.class) public class PropertyConvertorTest { private PropertyDefinition property; - Map dataTypes; + private Map dataTypes; + + @InjectMocks + private PropertyConvertor propertyConvertor; @Before public void setUp(){ property = new PropertyDefinition(); property.setName("myProperty"); property.setType(ToscaPropertyType.INTEGER.getType()); - dataTypes = new HashMap(); + dataTypes = new HashMap<>(); dataTypes.put(property.getName(), new DataTypeDefinition()); } @@ -65,12 +72,12 @@ public class PropertyConvertorTest { property.setSchema(schema); - PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY); + propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY); } @Test public void convertPropertyWhenValueAndDefaultNull() { - ToscaProperty prop = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY); + ToscaProperty prop = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY); assertNotNull(prop); assertNull(prop.getDefaultp()); } @@ -79,7 +86,7 @@ public class PropertyConvertorTest { public void convertPropertyWhenValueNullAndDefaultNotEmpty() { final String def = "1"; property.setDefaultValue(def); - ToscaProperty result = PropertyConvertor.getInstance().convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY); + ToscaProperty result = propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY); assertNotNull(result); assertEquals(Integer.valueOf(def), result.getDefaultp()); } @@ -92,16 +99,15 @@ public class PropertyConvertorTest { property1.setDefaultValue("2"); dataTypes.put(property1.getName(), new DataTypeDefinition()); Resource resource = new Resource(); - List properties = new ArrayList(); + List properties = new ArrayList<>(); properties.add(property); properties.add(property1); resource.setProperties(properties); - Either result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes); + Either result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes); assertTrue(result.isLeft()); assertEquals(2, result.left().value().getProperties().size()); int cnt = 0; - for (Iterator it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) { - ToscaProperty prop = it.next(); + for (ToscaProperty prop : result.left().value().getProperties().values()) { if (prop.getDefaultp() == null) { cnt++; } @@ -118,15 +124,14 @@ public class PropertyConvertorTest { property.setDefaultValue("1"); dataTypes.put(property1.getName(), new DataTypeDefinition()); Resource resource = new Resource(); - List properties = new ArrayList(); + List properties = new ArrayList<>(); properties.add(property); properties.add(property1); resource.setProperties(properties); - Either result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes); + Either result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes); assertTrue(result.isLeft()); assertEquals(2, result.left().value().getProperties().size()); - for (Iterator it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) { - ToscaProperty prop = it.next(); + for (ToscaProperty prop : result.left().value().getProperties().values()) { assertNotNull(prop.getDefaultp()); } } @@ -138,15 +143,14 @@ public class PropertyConvertorTest { property1.setType(ToscaPropertyType.INTEGER.getType()); dataTypes.put(property1.getName(), new DataTypeDefinition()); Resource resource = new Resource(); - List properties = new ArrayList(); + List properties = new ArrayList<>(); properties.add(property); properties.add(property1); resource.setProperties(properties); - Either result = PropertyConvertor.getInstance().convertProperties(resource, new ToscaNodeType(), dataTypes); + Either result = propertyConvertor.convertProperties(resource, new ToscaNodeType(), dataTypes); assertTrue(result.isLeft()); assertEquals(2, result.left().value().getProperties().size()); - for (Iterator it = result.left().value().getProperties().values().iterator(); it.hasNext(); ) { - ToscaProperty prop = it.next(); + for (ToscaProperty prop : result.left().value().getProperties().values()) { assertNull(prop.getDefaultp()); } } @@ -157,7 +161,7 @@ public class PropertyConvertorTest { .setDefaultValue("::") .setType(ToscaPropertyType.STRING.getType()) .build(); - ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY); + ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY); assertThat(toscaProperty.getDefaultp()).isEqualTo("::"); } @@ -167,7 +171,7 @@ public class PropertyConvertorTest { .setDefaultValue("/") .setType(ToscaPropertyType.STRING.getType()) .build(); - ToscaProperty toscaProperty = PropertyConvertor.getInstance().convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY); + ToscaProperty toscaProperty = propertyConvertor.convertProperty(Collections.emptyMap(), property1, PropertyConvertor.PropertyType.PROPERTY); assertThat(toscaProperty.getDefaultp()).isEqualTo("/"); } 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 ca4f569cf3..45cdbbdfaa 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 @@ -23,10 +23,14 @@ package org.openecomp.sdc.be.tosca; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.ArgumentMatchers.anyMap; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.doReturn; +import static org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType.PROPERTY; import fj.data.Either; import java.util.ArrayList; @@ -35,6 +39,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import mockit.Deencapsulation; import org.apache.commons.collections.MapUtils; @@ -50,9 +55,11 @@ import org.mockito.MockitoAnnotations; import org.openecomp.sdc.be.components.BeConfDependentTest; import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ForwardingPathElementDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition; @@ -103,6 +110,7 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { private static final String COMPONENT_INPUT_TYPE = "integer"; private static final String RESOURCE_NAME = "resource"; private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1"; + private static final Map DATA_TYPES = new HashMap<>(); @InjectMocks private ToscaExportHandler testSubject; @@ -122,18 +130,27 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { @Mock private GroupExportParser groupExportParser; + @Mock + private PropertyConvertor propertyConvertor; + @Mock private GroupExportParserImpl groupExportParserImpl; @Mock private InterfaceLifecycleOperation interfaceLifecycleOperation; + @Mock + private InterfacesOperationsConverter interfacesOperationsConverter; + @Mock private PolicyExportParser policyExportParser; @Before public void setUpMock() throws Exception { MockitoAnnotations.initMocks(this); + doReturn(new ToscaProperty()).when(propertyConvertor).convertProperty(any(), any(), eq(PROPERTY)); + doReturn(new HashMap()).when(interfacesOperationsConverter) + .getInterfacesMap(any(), isNull(), anyMap(), anyMap(), anyBoolean(), anyBoolean()); } private Resource getNewResource() { @@ -1427,4 +1444,151 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { Assert.assertTrue(result.get("test_art").getFile().equals("test_file")); Assert.assertTrue(result.get("test_art").getType().equals("test_type")); } + + @Test + public void testGetProxyNodeTypeInterfacesNoInterfaces() { + Component service = new Service(); + Optional> proxyNodeTypeInterfaces = + testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); + Assert.assertFalse(proxyNodeTypeInterfaces.isPresent()); + } + + @Test + public void testGetProxyNodeTypeInterfaces() { + Component service = getTestComponent(); + Optional> proxyNodeTypeInterfaces = + testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); + Assert.assertTrue(proxyNodeTypeInterfaces.isPresent()); + Map componentInterfaces = proxyNodeTypeInterfaces.get(); + Assert.assertNotNull(componentInterfaces); + } + + + @Test + public void testGetProxyNodeTypePropertiesComponentNull() { + Optional> proxyNodeTypeProperties = + testSubject.getProxyNodeTypeProperties(null, DATA_TYPES); + Assert.assertFalse(proxyNodeTypeProperties.isPresent()); + } + + @Test + public void testGetProxyNodeTypePropertiesNoProperties() { + Component service = new Service(); + Optional> proxyNodeTypeProperties = + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + Assert.assertFalse(proxyNodeTypeProperties.isPresent()); + } + + @Test + public void testGetProxyNodeTypeProperties() { + Component service = getTestComponent(); + service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), + createMockProperty("componentPropInt", null))); + Optional> proxyNodeTypeProperties = + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + Assert.assertTrue(proxyNodeTypeProperties.isPresent()); + Map componentProperties = proxyNodeTypeProperties.get(); + Assert.assertNotNull(componentProperties); + Assert.assertEquals(2, componentProperties.size()); + } + + @Test + public void testAddInputsToPropertiesNoInputs() { + Component service = getTestComponent(); + service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), + createMockProperty("componentPropInt", null))); + Optional> proxyNodeTypePropertiesResult = + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + + Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); + Map proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); + testSubject.addInputsToProperties(DATA_TYPES, null, proxyNodeTypeProperties); + Assert.assertNotNull(proxyNodeTypeProperties); + Assert.assertEquals(2, proxyNodeTypeProperties.size()); + testSubject.addInputsToProperties(DATA_TYPES, new ArrayList<>(), proxyNodeTypeProperties); + Assert.assertEquals(2, proxyNodeTypeProperties.size()); + } + + @Test + public void testAddInputsToPropertiesWithInputs() { + Component service = getTestComponent(); + service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), + createMockProperty("componentPropInt", null))); + service.setInputs(Arrays.asList(createMockInput("componentInputStr1", + "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); + Optional> proxyNodeTypePropertiesResult = + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + + Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); + Map proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); + testSubject.addInputsToProperties(DATA_TYPES, service.getInputs(), proxyNodeTypeProperties); + Assert.assertNotNull(proxyNodeTypeProperties); + Assert.assertEquals(4, proxyNodeTypeProperties.size()); + } + + @Test + public void testAddInputsToPropertiesOnlyInputs() { + Component service = getTestComponent(); + service.setInputs(Arrays.asList(createMockInput("componentInputStr1", + "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); + Optional> proxyNodeTypePropertiesResult = + testSubject.getProxyNodeTypeProperties(service, DATA_TYPES); + + Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); + Map proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); + testSubject.addInputsToProperties(DATA_TYPES, service.getInputs(), proxyNodeTypeProperties); + Assert.assertNotNull(proxyNodeTypeProperties); + Assert.assertEquals(2, proxyNodeTypeProperties.size()); + } + + @Test + public void testOperationImplementationInProxyNodeTypeNotPresent() { + Component service = getTestComponent(); + InterfaceDefinition interfaceDefinition = + service.getInterfaces().get("normalizedServiceComponentName-interface"); + interfaceDefinition.setOperations(new HashMap<>()); + final OperationDataDefinition operation = new OperationDataDefinition(); + operation.setName("start"); + operation.setDescription("op description"); + final ArtifactDataDefinition implementation = new ArtifactDataDefinition(); + implementation.setArtifactName("createBPMN.bpmn"); + operation.setImplementation(implementation); + interfaceDefinition.getOperations().put(operation.getName(), operation); + service.getInterfaces().put("normalizedServiceComponentName-interface", interfaceDefinition); + service.setInputs(Arrays.asList(createMockInput("componentInputStr1", + "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); + Optional> proxyNodeTypeInterfaces = + testSubject.getProxyNodeTypeInterfaces(service, DATA_TYPES); + Assert.assertTrue(proxyNodeTypeInterfaces.isPresent()); + Map componentInterfaces = proxyNodeTypeInterfaces.get(); + Assert.assertNotNull(componentInterfaces); + } + + private Component getTestComponent() { + Component component = new Service(); + component.setNormalizedName("normalizedServiceComponentName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + addedInterface.setType("com.some.service.or.other.serviceName"); + final String interfaceType = "normalizedServiceComponentName-interface"; + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(interfaceType, addedInterface); + return component; + } + + private PropertyDefinition createMockProperty(String propertyName, String defaultValue){ + PropertyDefinition propertyDefinition = new PropertyDefinition(); + propertyDefinition.setName(propertyName); + propertyDefinition.setType("string"); + propertyDefinition.setDefaultValue(defaultValue); + return propertyDefinition; + } + + private InputDefinition createMockInput(String inputName, String defaultValue){ + InputDefinition inputDefinition = new InputDefinition(); + inputDefinition.setName(inputName); + inputDefinition.setType("string"); + inputDefinition.setDefaultValue(defaultValue); + return inputDefinition; + } + } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportUtilsTest.java deleted file mode 100644 index 55a2083e7c..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/ToscaExportUtilsTest.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Copyright © 2016-2019 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openecomp.sdc.be.tosca; - -import org.junit.Assert; -import org.junit.Test; -import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; -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.PropertyDefinition; -import org.openecomp.sdc.be.model.Service; -import org.openecomp.sdc.be.tosca.model.ToscaProperty; -import org.openecomp.sdc.be.tosca.utils.ToscaExportUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class ToscaExportUtilsTest { - - private static final Map dataTypes = new HashMap<>(); - - @Test - public void testGetProxyNodeTypeInterfacesNoInterfaces() { - Component service = new Service(); - Optional> proxyNodeTypeInterfaces = - ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes); - Assert.assertFalse(proxyNodeTypeInterfaces.isPresent()); - } - - @Test - public void testGetProxyNodeTypeInterfaces() { - Component service = getTestComponent(); - Optional> proxyNodeTypeInterfaces = - ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes); - Assert.assertTrue(proxyNodeTypeInterfaces.isPresent()); - Map componentInterfaces = proxyNodeTypeInterfaces.get(); - Assert.assertNotNull(componentInterfaces); - Assert.assertEquals(1, componentInterfaces.size()); - } - - - @Test - public void testGetProxyNodeTypePropertiesComponentNull() { - Optional> proxyNodeTypeProperties = - ToscaExportUtils.getProxyNodeTypeProperties(null, dataTypes); - Assert.assertFalse(proxyNodeTypeProperties.isPresent()); - } - - @Test - public void testGetProxyNodeTypePropertiesNoProperties() { - Component service = new Service(); - Optional> proxyNodeTypeProperties = - ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); - Assert.assertFalse(proxyNodeTypeProperties.isPresent()); - } - - @Test - public void testGetProxyNodeTypeProperties() { - Component service = getTestComponent(); - service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), - createMockProperty("componentPropInt", null))); - Optional> proxyNodeTypeProperties = - ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); - Assert.assertTrue(proxyNodeTypeProperties.isPresent()); - Map componentProperties = proxyNodeTypeProperties.get(); - Assert.assertNotNull(componentProperties); - Assert.assertEquals(2, componentProperties.size()); - } - - @Test - public void testAddInputsToPropertiesNoInputs() { - Component service = getTestComponent(); - service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), - createMockProperty("componentPropInt", null))); - Optional> proxyNodeTypePropertiesResult = - ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); - - Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); - Map proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); - ToscaExportUtils.addInputsToProperties(dataTypes, null, proxyNodeTypeProperties); - Assert.assertNotNull(proxyNodeTypeProperties); - Assert.assertEquals(2, proxyNodeTypeProperties.size()); - ToscaExportUtils.addInputsToProperties(dataTypes, new ArrayList<>(), proxyNodeTypeProperties); - Assert.assertEquals(2, proxyNodeTypeProperties.size()); - } - - @Test - public void testAddInputsToPropertiesWithInputs() { - Component service = getTestComponent(); - service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), - createMockProperty("componentPropInt", null))); - service.setInputs(Arrays.asList(createMockInput("componentInputStr1", - "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); - Optional> proxyNodeTypePropertiesResult = - ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); - - Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); - Map proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); - ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties); - Assert.assertNotNull(proxyNodeTypeProperties); - Assert.assertEquals(4, proxyNodeTypeProperties.size()); - Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1")); - Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2")); - } - - @Test - public void testAddInputsToPropertiesOnlyInputs() { - Component service = getTestComponent(); - service.setInputs(Arrays.asList(createMockInput("componentInputStr1", - "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); - Optional> proxyNodeTypePropertiesResult = - ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); - - Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); - Map proxyNodeTypeProperties = proxyNodeTypePropertiesResult.get(); - ToscaExportUtils.addInputsToProperties(dataTypes, service.getInputs(), proxyNodeTypeProperties); - Assert.assertNotNull(proxyNodeTypeProperties); - Assert.assertEquals(2, proxyNodeTypeProperties.size()); - Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr1")); - Assert.assertNotNull(proxyNodeTypeProperties.get("componentInputStr2")); - } - - @Test - public void testOperationImplementationInProxyNodeTypeNotPresent() { - Component service = getTestComponent(); - InterfaceDefinition interfaceDefinition = - service.getInterfaces().get("normalizedServiceComponentName-interface"); - interfaceDefinition.setOperations(new HashMap<>()); - final OperationDataDefinition operation = new OperationDataDefinition(); - operation.setName("start"); - operation.setDescription("op description"); - final ArtifactDataDefinition implementation = new ArtifactDataDefinition(); - implementation.setArtifactName("createBPMN.bpmn"); - operation.setImplementation(implementation); - interfaceDefinition.getOperations().put(operation.getName(), operation); - service.getInterfaces().put("normalizedServiceComponentName-interface", interfaceDefinition); - service.setInputs(Arrays.asList(createMockInput("componentInputStr1", - "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2"))); - Optional> proxyNodeTypeInterfaces = - ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes); - Assert.assertTrue(proxyNodeTypeInterfaces.isPresent()); - Map componentInterfaces = proxyNodeTypeInterfaces.get(); - Assert.assertNotNull(componentInterfaces); - Assert.assertEquals(1, componentInterfaces.size()); - Map proxyInterfaceDefinition = - (Map) componentInterfaces.get("serviceName"); - Map startOperationDefinition = (Map) proxyInterfaceDefinition.get("start"); - Assert.assertNotNull(startOperationDefinition); - Assert.assertNull(startOperationDefinition.get("implementation")); - } - - private Component getTestComponent() { - Component component = new Service(); - component.setNormalizedName("normalizedServiceComponentName"); - InterfaceDefinition addedInterface = new InterfaceDefinition(); - addedInterface.setType("com.some.service.or.other.serviceName"); - final String interfaceType = "normalizedServiceComponentName-interface"; - component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(interfaceType, addedInterface); - return component; - } - - private PropertyDefinition createMockProperty(String propertyName, String defaultValue){ - PropertyDefinition propertyDefinition = new PropertyDefinition(); - propertyDefinition.setName(propertyName); - propertyDefinition.setType("string"); - propertyDefinition.setDefaultValue(defaultValue); - return propertyDefinition; - } - - private InputDefinition createMockInput(String inputName, String defaultValue){ - InputDefinition inputDefinition = new InputDefinition(); - inputDefinition.setName(inputName); - inputDefinition.setType("string"); - inputDefinition.setDefaultValue(defaultValue); - return inputDefinition; - } -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java new file mode 100644 index 0000000000..13bc009d32 --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsConverterTest.java @@ -0,0 +1,552 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.openecomp.sdc.be.tosca.utils; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import org.apache.commons.collections4.MapUtils; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.sdc.be.DummyConfigurationManager; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; +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.ServiceMetadataDefinition; +import org.openecomp.sdc.be.model.tosca.ToscaFunctions; +import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter; +import org.openecomp.sdc.be.tosca.PropertyConvertor; +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; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.SELF; +import static org.openecomp.sdc.be.tosca.InterfacesOperationsConverter.addInterfaceTypeElement; +@RunWith(MockitoJUnitRunner.class) +public class InterfacesOperationsConverterTest { + + private static final String MAPPED_PROPERTY_NAME = "mapped_property"; + private static final String INPUT_NAME_PREFIX = "input_"; + private static final String OUTPUT_NAME_PREFIX = "output_"; + private static final String NODE_TYPE_NAME = "test"; + private String[] inputTypes = {"string", "integer", "float", "boolean"}; + private static ObjectMapper mapper; + private Configuration.EnvironmentContext environmentContext = mock(Configuration.EnvironmentContext.class); + DummyConfigurationManager dummyConfigurationManager = new DummyConfigurationManager(); + private static final Map dataTypes = new HashMap<>(); + + private InterfacesOperationsConverter interfacesOperationsConverter; + + @BeforeClass + public static void setUp() { + mapper = new ObjectMapper(); + mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + } + + @Before + public void setUpBeforeTest() { + interfacesOperationsConverter = + new InterfacesOperationsConverter(new PropertyConvertor()); + } + + @Test + public void addInterfaceTypeElementToResource() { + Component component = new Resource(); + component.setNormalizedName("normalizedComponentName"); + component.setMetadataDefinition(new ServiceMetadataDefinition()); + component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName"); + component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + addedInterface.setType("Local"); + addOperationsToInterface(component, addedInterface, 5, 3, true, false); + final String interfaceType = "normalizedComponentName-interface"; + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(interfaceType, addedInterface); + final Map interfaceTypeElement = + addInterfaceTypeElement(component, new ArrayList<>()); + + ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null, + interfacesOperationsConverter); + ToscaTemplate template = new ToscaTemplate("test"); + template.setInterface_types(interfaceTypeElement); + final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); + + Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); + Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName")); + } + + @Test + public void addInterfaceTypeElementToService() { + Component component = new Service(); + component.setNormalizedName("normalizedServiceComponentName"); + component.setMetadataDefinition(new ServiceMetadataDefinition()); + component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName"); + component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + addedInterface.setType("Local"); + addOperationsToInterface(component, addedInterface, 5, 3, true, false); + final String interfaceType = "normalizedServiceComponentName-interface"; + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(interfaceType, addedInterface); + final Map interfaceTypeElement = + addInterfaceTypeElement(component, new ArrayList<>()); + + ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null, + interfacesOperationsConverter); + ToscaTemplate template = new ToscaTemplate("testService"); + template.setInterface_types(interfaceTypeElement); + final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); + + Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); + Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName")); + } + + @Test + public void addInterfaceDefinitionElementToResource() { + Component component = new Resource(); + component.setNormalizedName("normalizedComponentName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + 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); + ToscaNodeType nodeType = new ToscaNodeType(); + interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); + + ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null, + interfacesOperationsConverter); + ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); + Map 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:")); + validateOperationInputs(mainYaml, 2, null); + 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.setType("com.some.service.or.other.serviceName"); + addOperationsToInterface(component, addedInterface, 3, 2, true, false); + final String interfaceType = "normalizedServiceComponentName-interface"; + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(interfaceType, addedInterface); + ToscaNodeType nodeType = new ToscaNodeType(); + interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); + + ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null, + interfacesOperationsConverter); + ToscaTemplate template = new ToscaTemplate("testService"); + Map 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("serviceName:")); + Assert.assertTrue(mainYaml.contains("inputs:")); + validateOperationInputs(mainYaml, 2, null); + Assert.assertFalse(mainYaml.contains("defaultp")); + Assert.assertTrue(mainYaml.contains("has description")); + Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME)); + 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 interfacesMap = interfacesOperationsConverter + .getInterfacesMap(component, null, component.getInterfaces(), null, false, true); + ToscaNodeType nodeType = new ToscaNodeType(); + nodeType.setInterfaces(interfacesMap); + ToscaExportHandler handler = new ToscaExportHandler(); + ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); + Map 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 addInterfaceDefinitionElement_noInputs() { + Component component = new Resource(); + component.setNormalizedName("normalizedComponentName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs"); + addOperationsToInterface(component, addedInterface, 3, 3, false, false); + final String interfaceType = "normalizedComponentName-interface"; + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(interfaceType, addedInterface); + ToscaNodeType nodeType = new ToscaNodeType(); + interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, null, false); + + ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null, + interfacesOperationsConverter); + ToscaTemplate template = new ToscaTemplate("test"); + Map nodeTypes = new HashMap<>(); + nodeTypes.put("test", nodeType); + template.setNode_types(nodeTypes); + final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); + + Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); + Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX)); + Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp")); + Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:")); + Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description")); + Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName")); + } + + @Test + public void addInterfaceDefinitionElementInputMappedToOtherOperationOutput() { + String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput"; + Component component = new Resource(); + component.setNormalizedName("normalizedComponentName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + 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(operationInputDefinition -> operationInputDefinition.getName().contains("integer")) + .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType + + ".name_for_op_1.output_integer_1"))); + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(addedInterfaceType, addedInterface); + ToscaNodeType nodeType = new ToscaNodeType(); + interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); + + ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null, + interfacesOperationsConverter); + ToscaTemplate template = new ToscaTemplate("test"); + Map nodeTypes = new HashMap<>(); + 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:")); + Assert.assertTrue(mainYaml.contains("inputs:")); + validateOperationInputs(mainYaml, 2, "name_for_op_1"); + } + + @Test + public void addInterfaceDefinitionElementInputMappedToOtherOperationOutputFromOtherInterface() { + String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput"; + Component component = new Resource(); + component.setNormalizedName("normalizedComponentName"); + InterfaceDefinition addedInterface = new InterfaceDefinition(); + 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"))); + //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"))); + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(addedInterfaceType, addedInterface); + component.getInterfaces().put(secondInterfaceType, secondInterface); + + ToscaNodeType nodeType = new ToscaNodeType(); + interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); + + ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null, null, + interfacesOperationsConverter); + ToscaTemplate template = new ToscaTemplate("test"); + Map nodeTypes = new HashMap<>(); + 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:")); + Assert.assertTrue(mainYaml.contains("inputs:")); + validateOperationInputs(mainYaml, 2, "name_for_op_1"); + } + + private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps, + int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) { + + addedInterface.setOperations(new HashMap<>()); + for (int i = 0; i < numOfOps; i++) { + final OperationDataDefinition operation = new OperationDataDefinition(); + operation.setName("name_for_op_" + i); + operation.setDescription("op " + i + " has description"); + final ArtifactDataDefinition implementation = new ArtifactDataDefinition(); + implementation.setArtifactName(i + "_createBPMN.bpmn"); + operation.setImplementation(implementation); + if (hasInputs) { + operation.setInputs(createInputs(component, numOfInputsPerOp)); + } + if (hasOutputs) { + operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(), + operation.getName(), numOfInputsPerOp)); + } + addedInterface.getOperations().put(operation.getName(), operation); + } + } + + private ListDataDefinition createInputs(Component component, int numOfInputs) { + ListDataDefinition operationInputDefinitionList = new ListDataDefinition<>(); + for (int i = 0; i < numOfInputs; i++) { + String mappedPropertyName = java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME + i; + operationInputDefinitionList.add(createMockOperationInputDefinition( + INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i)); + addMappedPropertyAsComponentInput(component, mappedPropertyName); + + } + return operationInputDefinitionList; + } + + private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) { + InputDefinition componentInput = new InputDefinition(); + componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]); + componentInput.setName(mappedPropertyName.split("\\.")[1]); + if (Objects.isNull(component.getInputs())) { + component.setInputs(new ArrayList<>()); + } + component.getInputs().add(componentInput); + } + + private ListDataDefinition createOutputs(String interfaceName, + String operationName, + int numOfOutputs) { + ListDataDefinition operationOutputDefinitionList = new ListDataDefinition<>(); + for (int i = 0; i < numOfOutputs; i++) { + operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName, + OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i)); + } + return operationOutputDefinitionList; + } + + private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) { + OperationInputDefinition operationInputDefinition = new OperationInputDefinition(); + operationInputDefinition.setName(name); + operationInputDefinition.setInputId(id); + operationInputDefinition.setType(inputTypes[index]); + operationInputDefinition.setRequired(index % 2 == 0); + Map> toscaDefaultValueMap = new HashMap<>(); + List 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 interfaceName, String operationName, + String outputName, int index) { + OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition(); + operationInputDefinition.setName(outputName); + operationInputDefinition.setType(inputTypes[index]); + operationInputDefinition.setRequired(index % 2 == 0); + Map> toscaDefaultValueMap = new HashMap<>(); + List 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; + } + + private void validateOperationInputs(String mainYaml, int numOfInputsPerOp, String mappedOperationName) { + String nodeTypeKey = NODE_TYPE_NAME + ":"; + String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(), + mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length() + + String.valueOf(numOfInputsPerOp).length()); + YamlToObjectConverter objectConverter = new YamlToObjectConverter(); + ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class); + Map interfaces = toscaNodeType.getInterfaces(); + for (Map.Entry interfaceEntry : interfaces.entrySet()) { + Map interfaceDefinition = mapper.convertValue(interfaceEntry.getValue(), Map.class); + for (Map.Entry operationEntry : interfaceDefinition.entrySet()) { + Object operationVal = operationEntry.getValue(); + if (operationVal instanceof Map) { + //Since the inputs are mapped to output operations from only first interface so using that name + validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName, + operationVal); + } + } + } + } + + private void validateOperationInputDefinition(String interfaceType, String operationName, Object operationVal) { + Map operation = mapper.convertValue(operationVal, Map.class); + Map inputs = (Map) operation.get("inputs"); + for (Map.Entry inputEntry : inputs.entrySet()) { + String[] inputNameSplit = inputEntry.getKey().split("_"); + Map inputValueObject = (Map) inputEntry.getValue(); + Assert.assertEquals(inputNameSplit[1], inputValueObject.get("type")); + Boolean expectedIsRequired = Integer.parseInt(inputNameSplit[2]) % 2 == 0; + Assert.assertEquals(expectedIsRequired, inputValueObject.get("required")); + validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1], + Integer.parseInt(inputNameSplit[2]), inputValueObject); + } + } + + + private void validateOperationInputDefinitionDefaultValue(String interfaceType, String operationName, + String inputType, int index, + Map inputValueObject) { + Map mappedInputValue = (Map) inputValueObject.get("default"); + if(mappedInputValue.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) { + String mappedPropertyValue = MAPPED_PROPERTY_NAME + index; + List mappedPropertyDefaultValue = (List) 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(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { + List mappedPropertyDefaultValue = (List) mappedInputValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); + Assert.assertEquals(4, mappedPropertyDefaultValue.size()); + String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index; + Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF)); + Assert.assertTrue(mappedPropertyDefaultValue.contains(interfaceType)); + Assert.assertTrue(mappedPropertyDefaultValue.contains(operationName)); + Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue)); + } else { + 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 interfaceDefinition = mapper.convertValue(interfaceVal, Map.class); + for (Object operationVal : interfaceDefinition.values()) { + if (operationVal instanceof Map) { + Map operation = (Map) mapper.convertValue(operationVal, Map.class); + Map operationInputs = (Map) operation.get("inputs"); + for (Object inputValue : operationInputs.values()) { + Map inputValueAsMap = (Map) inputValue; + Assert.assertFalse(inputValueAsMap.keySet().contains("type")); + Assert.assertFalse(inputValueAsMap.keySet().contains("required")); + Assert.assertFalse(inputValueAsMap.keySet().contains("default")); + } + } + } + } + } + + @Test + public void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() { + Service service = new Service(); + service.setMetadataDefinition(new ServiceMetadataDefinition()); + service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface"); + service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface"); + service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>()))); + + Map resultMap = InterfacesOperationsConverter.addInterfaceTypeElement(service, + Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard")); + + Assert.assertTrue(MapUtils.isNotEmpty(resultMap) + && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface")); + } + + @Test + public void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() { + Service service = new Service(); + service.setMetadataDefinition(new ServiceMetadataDefinition()); + service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface"); + service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null, + new HashMap<>()))); + + Map resultMap = interfacesOperationsConverter.getInterfacesMap(service, null, + service.getInterfaces(), null, false, false); + + Assert.assertTrue(MapUtils.isNotEmpty(resultMap) + && resultMap.containsKey("NotLocal")); + } +} 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 deleted file mode 100644 index d6a7b86f16..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/utils/InterfacesOperationsToscaUtilTest.java +++ /dev/null @@ -1,543 +0,0 @@ -/* - * Copyright © 2016-2018 European Support Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.openecomp.sdc.be.tosca.utils; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.gson.Gson; -import org.apache.commons.collections4.MapUtils; -import org.junit.Assert; -import org.junit.Before; -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.config.Configuration; -import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; -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.ServiceMetadataDefinition; -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; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; -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; - -public class InterfacesOperationsToscaUtilTest { - - private static final String MAPPED_PROPERTY_NAME = "mapped_property"; - private static final String INPUT_NAME_PREFIX = "input_"; - private static final String OUTPUT_NAME_PREFIX = "output_"; - private static final String NODE_TYPE_NAME = "test"; - private String[] inputTypes = {"string", "integer", "float", "boolean"}; - private static ObjectMapper mapper; - private Configuration.EnvironmentContext environmentContext = mock(Configuration.EnvironmentContext.class); - DummyConfigurationManager dummyConfigurationManager = new DummyConfigurationManager(); - private static final Map dataTypes = new HashMap<>(); - - - @BeforeClass - public static void setUp() { - mapper = new ObjectMapper(); - mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); - - } - @Before - public void beforeTest() { - when(environmentContext.getDefaultValue()).thenReturn("General_Revenue-Bearing"); - when(dummyConfigurationManager.getConfigurationMock().getEnvironmentContext()) - .thenReturn(environmentContext); - } - - - @Test - public void addInterfaceTypeElementToResource() { - Component component = new Resource(); - component.setNormalizedName("normalizedComponentName"); - component.setMetadataDefinition(new ServiceMetadataDefinition()); - component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName"); - component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName"); - InterfaceDefinition addedInterface = new InterfaceDefinition(); - addedInterface.setType("Local"); - addOperationsToInterface(component, addedInterface, 5, 3, true, false); - final String interfaceType = "normalizedComponentName-interface"; - component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(interfaceType, addedInterface); - final Map interfaceTypeElement = - addInterfaceTypeElement(component, new ArrayList<>()); - - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); - ToscaTemplate template = new ToscaTemplate("test"); - template.setInterface_types(interfaceTypeElement); - final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName")); - } - - @Test - public void addInterfaceTypeElementToService() { - Component component = new Service(); - component.setNormalizedName("normalizedServiceComponentName"); - component.setMetadataDefinition(new ServiceMetadataDefinition()); - component.getComponentMetadataDefinition().getMetadataDataDefinition().setName("NodeTypeName"); - component.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("NodeTypeName"); - InterfaceDefinition addedInterface = new InterfaceDefinition(); - addedInterface.setType("Local"); - addOperationsToInterface(component, addedInterface, 5, 3, true, false); - final String interfaceType = "normalizedServiceComponentName-interface"; - component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(interfaceType, addedInterface); - final Map interfaceTypeElement = - addInterfaceTypeElement(component, new ArrayList<>()); - - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); - ToscaTemplate template = new ToscaTemplate("testService"); - template.setInterface_types(interfaceTypeElement); - final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("NodeTypeName")); - } - - @Test - public void addInterfaceDefinitionElementToResource() { - Component component = new Resource(); - component.setNormalizedName("normalizedComponentName"); - InterfaceDefinition addedInterface = new InterfaceDefinition(); - 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); - ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); - ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); - Map 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:")); - validateOperationInputs(mainYaml, 2, null); - 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.setType("com.some.service.or.other.serviceName"); - addOperationsToInterface(component, addedInterface, 3, 2, true, false); - final String interfaceType = "normalizedServiceComponentName-interface"; - component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(interfaceType, addedInterface); - ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); - ToscaTemplate template = new ToscaTemplate("testService"); - Map 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("serviceName:")); - Assert.assertTrue(mainYaml.contains("inputs:")); - validateOperationInputs(mainYaml, 2, null); - Assert.assertFalse(mainYaml.contains("defaultp")); - Assert.assertTrue(mainYaml.contains("has description")); - Assert.assertTrue(mainYaml.contains(MAPPED_PROPERTY_NAME)); - 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 interfacesMap = InterfacesOperationsToscaUtil - .getInterfacesMap(component, null, component.getInterfaces(), null, false, true); - ToscaNodeType nodeType = new ToscaNodeType(); - nodeType.setInterfaces(interfacesMap); - ToscaExportHandler handler = new ToscaExportHandler(); - ToscaTemplate template = new ToscaTemplate(NODE_TYPE_NAME); - Map 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 addInterfaceDefinitionElement_noInputs() { - Component component = new Resource(); - component.setNormalizedName("normalizedComponentName"); - InterfaceDefinition addedInterface = new InterfaceDefinition(); - addedInterface.setType("com.some.resource.or.other.resourceNameNoInputs"); - addOperationsToInterface(component, addedInterface, 3, 3, false, false); - final String interfaceType = "normalizedComponentName-interface"; - component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(interfaceType, addedInterface); - ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, null, false); - - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); - ToscaTemplate template = new ToscaTemplate("test"); - Map nodeTypes = new HashMap<>(); - nodeTypes.put("test", nodeType); - template.setNode_types(nodeTypes); - final ToscaRepresentation toscaRepresentation = handler.createToscaRepresentation(template); - - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("operations")); - Assert.assertFalse(toscaRepresentation.getMainYaml().contains(INPUT_NAME_PREFIX)); - Assert.assertFalse(toscaRepresentation.getMainYaml().contains("defaultp")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("resourceNameNoInputs:")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("has description")); - Assert.assertTrue(toscaRepresentation.getMainYaml().contains("com.some.resource.or.other.resourceName")); - } - - @Test - public void addInterfaceDefinitionElementInputMappedToOtherOperationOutput() { - String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput"; - Component component = new Resource(); - component.setNormalizedName("normalizedComponentName"); - InterfaceDefinition addedInterface = new InterfaceDefinition(); - 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(operationInputDefinition -> operationInputDefinition.getName().contains("integer")) - .forEach(operationInputDefinition -> operationInputDefinition.setInputId(addedInterfaceType + - ".name_for_op_1.output_integer_1"))); - component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(addedInterfaceType, addedInterface); - ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); - ToscaTemplate template = new ToscaTemplate("test"); - Map nodeTypes = new HashMap<>(); - 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:")); - Assert.assertTrue(mainYaml.contains("inputs:")); - validateOperationInputs(mainYaml, 2, "name_for_op_1"); - } - - @Test - public void addInterfaceDefinitionElementInputMappedToOtherOperationOutputFromOtherInterface() { - String addedInterfaceType = "com.some.resource.or.other.resourceNameInputMappedToOutput"; - Component component = new Resource(); - component.setNormalizedName("normalizedComponentName"); - InterfaceDefinition addedInterface = new InterfaceDefinition(); - 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"))); - //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"))); - component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(addedInterfaceType, addedInterface); - component.getInterfaces().put(secondInterfaceType, secondInterface); - - ToscaNodeType nodeType = new ToscaNodeType(); - addInterfaceDefinitionElement(component, nodeType, dataTypes, false); - - ToscaExportHandler handler = new ToscaExportHandler(null,null,null,null,null,null, null); - ToscaTemplate template = new ToscaTemplate("test"); - Map nodeTypes = new HashMap<>(); - 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:")); - Assert.assertTrue(mainYaml.contains("inputs:")); - validateOperationInputs(mainYaml, 2, "name_for_op_1"); - } - - private void addOperationsToInterface(Component component, InterfaceDefinition addedInterface, int numOfOps, - int numOfInputsPerOp, boolean hasInputs, boolean hasOutputs) { - - addedInterface.setOperations(new HashMap<>()); - for (int i = 0; i < numOfOps; i++) { - final OperationDataDefinition operation = new OperationDataDefinition(); - operation.setName("name_for_op_" + i); - operation.setDescription("op " + i + " has description"); - final ArtifactDataDefinition implementation = new ArtifactDataDefinition(); - implementation.setArtifactName(i + "_createBPMN.bpmn"); - operation.setImplementation(implementation); - if (hasInputs) { - operation.setInputs(createInputs(component, numOfInputsPerOp)); - } - if (hasOutputs) { - operation.setOutputs(createOutputs(addedInterface.getToscaResourceName(), - operation.getName(), numOfInputsPerOp)); - } - addedInterface.getOperations().put(operation.getName(), operation); - } - } - - private ListDataDefinition createInputs(Component component, int numOfInputs) { - ListDataDefinition operationInputDefinitionList = new ListDataDefinition<>(); - for (int i = 0; i < numOfInputs; i++) { - String mappedPropertyName = java.util.UUID.randomUUID().toString() + "." + MAPPED_PROPERTY_NAME + i; - operationInputDefinitionList.add(createMockOperationInputDefinition( - INPUT_NAME_PREFIX + inputTypes[i] + "_" + i, mappedPropertyName, i)); - addMappedPropertyAsComponentInput(component, mappedPropertyName); - - } - return operationInputDefinitionList; - } - - private void addMappedPropertyAsComponentInput(Component component, String mappedPropertyName) { - InputDefinition componentInput = new InputDefinition(); - componentInput.setUniqueId(mappedPropertyName.split("\\.")[0]); - componentInput.setName(mappedPropertyName.split("\\.")[1]); - if (Objects.isNull(component.getInputs())) { - component.setInputs(new ArrayList<>()); - } - component.getInputs().add(componentInput); - } - - private ListDataDefinition createOutputs(String interfaceName, - String operationName, - int numOfOutputs) { - ListDataDefinition operationOutputDefinitionList = new ListDataDefinition<>(); - for (int i = 0; i < numOfOutputs; i++) { - operationOutputDefinitionList.add(createMockOperationOutputDefinition(interfaceName, operationName, - OUTPUT_NAME_PREFIX + inputTypes[i] + "_" + i, i)); - } - return operationOutputDefinitionList; - } - - private OperationInputDefinition createMockOperationInputDefinition(String name, String id, int index) { - OperationInputDefinition operationInputDefinition = new OperationInputDefinition(); - operationInputDefinition.setName(name); - operationInputDefinition.setInputId(id); - operationInputDefinition.setType(inputTypes[index]); - operationInputDefinition.setRequired(index % 2 == 0); - Map> toscaDefaultValueMap = new HashMap<>(); - List 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 interfaceName, String operationName, - String outputName, int index) { - OperationOutputDefinition operationInputDefinition = new OperationOutputDefinition(); - operationInputDefinition.setName(outputName); - operationInputDefinition.setType(inputTypes[index]); - operationInputDefinition.setRequired(index % 2 == 0); - Map> toscaDefaultValueMap = new HashMap<>(); - List 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; - } - - private void validateOperationInputs(String mainYaml, int numOfInputsPerOp, String mappedOperationName) { - String nodeTypeKey = NODE_TYPE_NAME + ":"; - String nodeTypesRepresentation = mainYaml.substring(mainYaml.indexOf(nodeTypeKey) + nodeTypeKey.length(), - mainYaml.lastIndexOf(MAPPED_PROPERTY_NAME) + MAPPED_PROPERTY_NAME.length() - + String.valueOf(numOfInputsPerOp).length()); - YamlToObjectConverter objectConverter = new YamlToObjectConverter(); - ToscaNodeType toscaNodeType = objectConverter.convert(nodeTypesRepresentation.getBytes(), ToscaNodeType.class); - Map interfaces = toscaNodeType.getInterfaces(); - for (Map.Entry interfaceEntry : interfaces.entrySet()) { - Map interfaceDefinition = mapper.convertValue(interfaceEntry.getValue(), Map.class); - for (Map.Entry operationEntry : interfaceDefinition.entrySet()) { - Object operationVal = operationEntry.getValue(); - if (operationVal instanceof Map) { - //Since the inputs are mapped to output operations from only first interface so using that name - validateOperationInputDefinition(interfaces.keySet().iterator().next(), mappedOperationName, - operationVal); - } - } - } - } - - private void validateOperationInputDefinition(String interfaceType, String operationName, Object operationVal) { - Map operation = mapper.convertValue(operationVal, Map.class); - Map inputs = (Map) operation.get("inputs"); - for (Map.Entry inputEntry : inputs.entrySet()) { - String[] inputNameSplit = inputEntry.getKey().split("_"); - Map inputValueObject = (Map) inputEntry.getValue(); - Assert.assertEquals(inputNameSplit[1], inputValueObject.get("type")); - Boolean expectedIsRequired = Integer.parseInt(inputNameSplit[2]) % 2 == 0; - Assert.assertEquals(expectedIsRequired, inputValueObject.get("required")); - validateOperationInputDefinitionDefaultValue(interfaceType, operationName, inputNameSplit[1], - Integer.parseInt(inputNameSplit[2]), inputValueObject); - } - } - - - private void validateOperationInputDefinitionDefaultValue(String interfaceType, String operationName, - String inputType, int index, - Map inputValueObject) { - Map mappedInputValue = (Map) inputValueObject.get("default"); - if(mappedInputValue.containsKey(ToscaFunctions.GET_PROPERTY.getFunctionName())) { - String mappedPropertyValue = MAPPED_PROPERTY_NAME + index; - List mappedPropertyDefaultValue = (List) 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(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName())) { - List mappedPropertyDefaultValue = (List) mappedInputValue.get(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName()); - Assert.assertEquals(4, mappedPropertyDefaultValue.size()); - String mappedPropertyValue = OUTPUT_NAME_PREFIX + inputType + "_" + index; - Assert.assertTrue(mappedPropertyDefaultValue.contains(SELF)); - Assert.assertTrue(mappedPropertyDefaultValue.contains(interfaceType)); - Assert.assertTrue(mappedPropertyDefaultValue.contains(operationName)); - Assert.assertTrue(mappedPropertyDefaultValue.contains(mappedPropertyValue)); - } else { - 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 interfaceDefinition = mapper.convertValue(interfaceVal, Map.class); - for (Object operationVal : interfaceDefinition.values()) { - if (operationVal instanceof Map) { - Map operation = (Map) mapper.convertValue(operationVal, Map.class); - Map operationInputs = (Map) operation.get("inputs"); - for (Object inputValue : operationInputs.values()) { - Map inputValueAsMap = (Map) inputValue; - Assert.assertFalse(inputValueAsMap.keySet().contains("type")); - Assert.assertFalse(inputValueAsMap.keySet().contains("required")); - Assert.assertFalse(inputValueAsMap.keySet().contains("default")); - } - } - } - } - } - - @Test - public void testAddInterfaceTypeElementGetCorrectLocalInterfaceName() { - Service service = new Service(); - service.setMetadataDefinition(new ServiceMetadataDefinition()); - service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface"); - service.getComponentMetadataDefinition().getMetadataDataDefinition().setSystemName("LocalInterface"); - service.setInterfaces(Collections.singletonMap("Local", new InterfaceDefinition("Local", null, new HashMap<>()))); - - Map resultMap = InterfacesOperationsToscaUtil.addInterfaceTypeElement(service, - Collections.singletonList("org.openecomp.interfaces.node.lifecycle.Standard")); - - Assert.assertTrue(MapUtils.isNotEmpty(resultMap) - && resultMap.containsKey("org.openecomp.interfaces.node.lifecycle.LocalInterface")); - } - - @Test - public void testAddInterfaceTypeElementNoTypeChangeIfNotLocal() { - Service service = new Service(); - service.setMetadataDefinition(new ServiceMetadataDefinition()); - service.getComponentMetadataDefinition().getMetadataDataDefinition().setName("LocalInterface"); - service.setInterfaces(Collections.singletonMap("NotLocal", new InterfaceDefinition("NotLocal", null, - new HashMap<>()))); - - Map resultMap = InterfacesOperationsToscaUtil.getInterfacesMap(service, null, - service.getInterfaces(), null, false, false); - - Assert.assertTrue(MapUtils.isNotEmpty(resultMap) - && resultMap.containsKey("NotLocal")); - } -} diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinition.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinition.java index c138617f42..5d9453a731 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinition.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinition.java @@ -29,9 +29,13 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import lombok.Data; +import lombok.EqualsAndHashCode; import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +@EqualsAndHashCode(callSuper = false) +@Data public class PropertyDataDefinition extends ToscaDataDefinition { private String uniqueId; @@ -125,121 +129,25 @@ public class PropertyDataDefinition extends ToscaDataDefinition { this.setIsDeclaredListInput(propertyDataDefinition.getIsDeclaredListInput()); } - public String getParentPropertyType() { - return parentPropertyType; - } - - public void setParentPropertyType(String parentPropertyType) { - this.parentPropertyType = parentPropertyType; - } - - public String getSubPropertyInputPath() { - return subPropertyInputPath; - } - - public void setSubPropertyInputPath(String subPropertyInputPath) { - this.subPropertyInputPath = subPropertyInputPath; - } - - public String getInputPath() { - return inputPath; - } - - public void setInputPath(String inputPath) { - this.inputPath = inputPath; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - // @Override public boolean isDefinition() { return true; } - public void setDefinition(boolean definition) { - this.definition = definition; - } - - public boolean getDefinition() { - return definition; - } - - public String getType() { - return type; - } - - public String getDefaultValue() { - return defaultValue; - } - - public void setDefaultValue(String defaultValue) { - this.defaultValue = defaultValue; - } - - public void setType(String type) { - this.type = type; + public boolean getDefinition() { + return definition; } public Boolean isRequired() { return required; } - public void setRequired(Boolean required) { - this.required = required; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public boolean isPassword() { - return password; - } - - public void setPassword(boolean password) { - this.password = password; - } - - public String getUniqueId() { - return uniqueId; - } - - public void setUniqueId(String uniqueId) { - this.uniqueId = uniqueId; - } - - public SchemaDefinition getSchema() { - return schema; - } - public void setSchemaType(String schemaType) { if (schema != null && schema.getProperty() != null) { schema.getProperty().setType(schemaType); } } - public void setSchema(SchemaDefinition entrySchema) { - this.schema = entrySchema; - } - public String getSchemaType() { if (schema != null && schema.getProperty() != null) { return schema.getProperty().getType(); @@ -255,30 +163,14 @@ public class PropertyDataDefinition extends ToscaDataDefinition { return null; } - public String getLabel() { - return label; - } - - public void setLabel(String label) { - this.label = label; - } - public Boolean isHidden() { return hidden; } - public void setHidden(Boolean hidden) { - this.hidden = hidden; - } - public Boolean isImmutable() { return immutable; } - public void setImmutable(Boolean immutable) { - this.immutable = immutable; - } - public String getParentUniqueId() { return getOwnerId(); } @@ -287,95 +179,10 @@ public class PropertyDataDefinition extends ToscaDataDefinition { setOwnerId(parentUniqueId); } - public List getGetInputValues() { - return getInputValues; - } - - public void setGetInputValues(List getInputValues) { - this.getInputValues = getInputValues; - } - public List safeGetGetPolicyValues() { return CollectionUtils.isEmpty(getPolicyValues) ? new ArrayList<>() : getPolicyValues; } - public List getGetPolicyValues() { - return getPolicyValues; - } - - public void setGetPolicyValues(List getPolicyValues) { - this.getPolicyValues = getPolicyValues; - } - - public String getStatus() { - return status; - } - - public void setStatus(String status) { - this.status = status; - } - - public String getInputId() { - return inputId; - } - - public void setInputId(String inputId) { - this.inputId = inputId; - } - - public String getInstanceUniqueId() { - return instanceUniqueId; - } - - public void setInstanceUniqueId(String instanceUniqueId) { - this.instanceUniqueId = instanceUniqueId; - } - - public String getPropertyId() { - return propertyId; - } - - public void setPropertyId(String propertyId) { - this.propertyId = propertyId; - } - - public Boolean getIsDeclaredListInput() { - return isDeclaredListInput; - } - - public void setIsDeclaredListInput(Boolean isDeclaredListInput) { - this.isDeclaredListInput = isDeclaredListInput; - } - - @Override - public String toString() { - return "PropertyDataDefinition [uniqueId=" + uniqueId + ", type=" + type + ", required=" + required + ", definition=" + definition + ", defaultValue=" + defaultValue + ", description=" + description + ", schema=" + schema + ", password=" - + password + ", name=" + name + ", value=" + value + ", label=" + label + ", hidden=" + hidden + ", immutable=" + immutable + ", inputPath=" + inputPath + ", status=" + status + ", inputId=" + inputId + ", instanceUniqueId=" - + instanceUniqueId + ", propertyId=" + propertyId + ", parentUniqueId=" + parentUniqueId + ", getInputValues=" + getInputValues + ", isDeclaredListInput=" + isDeclaredListInput - + ", parentPropertyType=" + parentPropertyType - + ", subPropertyInputPath=" + subPropertyInputPath + "]"; - } - - @Override - public int hashCode() { - final int hashingValueDefinition = 1231; - final int hashingValueNoDefinition = 1237; - final int prime = 31; - int result = 1; - result = prime * result + ((defaultValue == null) ? 0 : defaultValue.hashCode()); - result = prime * result + (definition ? hashingValueDefinition : hashingValueNoDefinition); - result = prime * result + ((description == null) ? 0 : description.hashCode()); - result = prime * result + (password ? hashingValueDefinition : hashingValueNoDefinition); - result = prime * result + ((required == null) ? 0 : required.hashCode()); - result = prime * result + ((schema == null) ? 0 : schema.hashCode()); - result = prime * result + ((type == null) ? 0 : type.hashCode()); - result = prime * result + ((uniqueId == null) ? 0 : uniqueId.hashCode()); - result = prime * result + ((parentUniqueId == null) ? 0 : parentUniqueId.hashCode()); - result = prime * result + ((status == null) ? 0 : status.hashCode()); - result = prime * result + ((isDeclaredListInput == null) ? 0 : isDeclaredListInput.hashCode()); - return result; - } - public boolean typeEquals(Object obj) { if (this == obj) { return true; @@ -401,90 +208,6 @@ public class PropertyDataDefinition extends ToscaDataDefinition { return thisSchemaType.equals(otherSchemaType); } - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - PropertyDataDefinition other = (PropertyDataDefinition) obj; - if (defaultValue == null) { - if (other.defaultValue != null) { - return false; - } - } else if (!defaultValue.equals(other.defaultValue)) { - return false; - } - if (definition != other.definition) { - return false; - } - if (description == null) { - if (other.description != null) { - return false; - } - } else if (!description.equals(other.description)) { - return false; - } - if (password != other.password) { - return false; - } - if (required == null) { - if (other.required != null) { - return false; - } - } else if (!required.equals(other.required)) { - return false; - } - if (schema == null) { - if (other.schema != null) { - return false; - } - } else if (!schema.equals(other.schema)) { - return false; - } - if (type == null) { - if (other.type != null) { - return false; - } - } else if (!type.equals(other.type)) { - return false; - } - if (parentUniqueId == null) { - if (other.parentUniqueId != null) { - return false; - } - } else if (!parentUniqueId.equals(other.parentUniqueId)) { - return false; - } - if (uniqueId == null) { - if (other.uniqueId != null) { - return false; - } - } else if (!uniqueId.equals(other.uniqueId)) { - return false; - } - if (status == null) { - if (other.status != null) { - return false; - } - } else if (!status.equals(other.status)) { - return false; - } - if (isDeclaredListInput == null) { - if (other.isDeclaredListInput != null) { - return false; - } - } else if (!isDeclaredListInput.equals(other.isDeclaredListInput)) { - return false; - } - return true; - } - @Override public Object getToscaPresentationValue(JsonPresentationFields field) { switch (field) { @@ -571,9 +294,7 @@ public class PropertyDataDefinition extends ToscaDataDefinition { Set annotationSet = isNotEmpty(newAnnotations) ? new HashSet<>(newAnnotations) : new HashSet<>(); //We would to prioritize the new valid annotations over the old ones if the same one existed. if (this.annotations != null) { - for (Annotation oldInputAnnotation : this.annotations) { - annotationSet.add(oldInputAnnotation); - } + annotationSet.addAll(this.annotations); } this.annotations = new ArrayList<>(annotationSet); @@ -583,12 +304,4 @@ public class PropertyDataDefinition extends ToscaDataDefinition { public List getAnnotations() { return (List) getToscaPresentationValue(JsonPresentationFields.ANNOTATIONS); } - - public List getPropertyConstraints() { - return propertyConstraints; - } - - public void setPropertyConstraints(List constraints) { - this.propertyConstraints = constraints; - } } diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinitionTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinitionTest.java index 899022e9ad..2e8b26fcb4 100644 --- a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinitionTest.java +++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/PropertyDataDefinitionTest.java @@ -623,7 +623,8 @@ public class PropertyDataDefinitionTest { Assert.assertEquals(false, result); result = testSubject.equals(testSubject); Assert.assertEquals(true, result); - result = testSubject.equals(createTestSubject()); + PropertyDataDefinition other = createTestSubject(); + result = testSubject.equals(other); Assert.assertEquals(true, result); } -- cgit 1.2.3-korg