diff options
author | ojasdubey <ojas.dubey@amdocs.com> | 2019-05-23 12:09:35 +0530 |
---|---|---|
committer | ojasdubey <ojas.dubey@amdocs.com> | 2019-05-23 12:09:35 +0530 |
commit | f3a1aa08a8339ce312013154550c8e8b637f0fc3 (patch) | |
tree | c5580d623c1a6ab42d398e27792a2f402c8fea62 /catalog-be/src | |
parent | 19fe5d27ecddbc587aa1db4649c75b206155bbe4 (diff) |
Fix service proxy node type
Added properties, interfaces and requirements
of the child service to the node type of proxy
in the parent
cherry picked commit from
dublin: e50b5687b62edbafa12bf3825b0441077c5a4820
Change-Id: I2b4bb695c0b3819ada889b144d536b48dd9d1f30
Issue-ID: SDC-2313
Signed-off-by: ojasdubey <ojas.dubey@amdocs.com>
Diffstat (limited to 'catalog-be/src')
6 files changed, 645 insertions, 186 deletions
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 bd797c970b..245eb59301 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 @@ -58,6 +58,7 @@ import static org.apache.commons.lang3.StringUtils.isNoneBlank; public class CapabilityRequirementConverter { private static final String NO_CAPABILITIES = "No Capabilities for node type"; + private static final String NO_REQUIREMENTS = "No Requirements for node type"; private static CapabilityRequirementConverter instance; private static final Logger logger = Logger.getLogger(CapabilityRequirementConverter.class); private static final String PATH_DELIMITER = "."; @@ -84,6 +85,18 @@ public class CapabilityRequirementConverter { return c.getPreviousName(); } + public String buildRequirementNameForComponentInstance(Map<String,Component> componentCache, + ComponentInstance componentInstance, + RequirementDefinition r) { + String prefix = buildCapReqNamePrefix(componentInstance.getNormalizedName()); + if (ComponentUtilities.isNotUpdatedCapReqName(prefix, r.getName(), r.getPreviousName())) { + return buildSubstitutedName(componentCache, r.getName(), r.getPreviousName(), r.getPath(), r.getOwnerId(), + componentInstance).left() + .orValue(r.getName()); + } + return r.getPreviousName(); + } + private String buildCapReqNamePrefix(String normalizedName) { return normalizedName + PATH_DELIMITER; } @@ -187,6 +200,38 @@ public class CapabilityRequirementConverter { return result; } + /** + * Allows to convert requirements of a server proxy node type to tosca template requirements + * @param instanceProxy + * @return converted tosca template requirements + */ + List<Map<String, ToscaRequirement>> convertProxyRequirements(Map<String, Component> componentCache, + ComponentInstance instanceProxy) { + Map<String, List<RequirementDefinition>> requirements = instanceProxy.getRequirements(); + List<Map<String, ToscaRequirement>> toscaRequirements = new ArrayList<>(); + if (requirements != null) { + requirements.entrySet().stream() + .flatMap(e -> e.getValue().stream()) + .forEach(req -> { + ImmutablePair<String, ToscaRequirement> pair = convertProxyRequirement( + buildRequirementNameForComponentInstance(componentCache, instanceProxy, req), req); + Map<String, ToscaRequirement> requirement = new HashMap<>(); + requirement.put(pair.left, pair.right); + toscaRequirements.add(requirement); + }); + } else { + logger.debug(NO_REQUIREMENTS); + } + + return toscaRequirements; + } + + private ImmutablePair<String, ToscaRequirement> convertProxyRequirement(String requirementName, + RequirementDefinition r) { + ToscaRequirement toscaRequirement = createToscaRequirement(r); + return new ImmutablePair<>(requirementName, toscaRequirement); + } + private List<Map<String, ToscaRequirement>> convertRequirementsAsList(Map<String, Component> componentsCache, Component component) { Map<String, List<RequirementDefinition>> requirements = component.getRequirements(); List<Map<String, ToscaRequirement>> toscaRequirements = new ArrayList<>(); @@ -202,7 +247,7 @@ public class CapabilityRequirementConverter { logger.debug("Finish convert Requirements for node type"); } } else { - logger.debug("No Requirements for node type"); + logger.debug(NO_REQUIREMENTS); } return toscaRequirements; } @@ -384,6 +429,11 @@ public class CapabilityRequirementConverter { name = buildReqNamePerOwnerByPath(componentsCache, component, r); } logger.debug("the requirement {} belongs to resource {} ", name, component.getUniqueId()); + ToscaRequirement toscaRequirement = createToscaRequirement(r); + return new ImmutablePair<>(name, toscaRequirement); + } + + private ToscaRequirement createToscaRequirement(RequirementDefinition r) { ToscaRequirement toscaRequirement = new ToscaRequirement(); List<Object> occurrences = new ArrayList<>(); @@ -397,8 +447,7 @@ public class CapabilityRequirementConverter { toscaRequirement.setNode(r.getNode()); toscaRequirement.setCapability(r.getCapability()); toscaRequirement.setRelationship(r.getRelationship()); - - return new ImmutablePair<>(name, toscaRequirement); + return toscaRequirement; } /** @@ -424,17 +473,16 @@ public class CapabilityRequirementConverter { /** * Allows to convert capabilities of a server proxy node type to tosca template capabilities - * @param component - * @param proxyComponent * @param instanceProxy * @param dataTypes * @return */ - public Map<String, ToscaCapability> convertProxyCapabilities(Map<String, Component> componentCache, Component component, Component proxyComponent, ComponentInstance instanceProxy, Map<String, DataTypeDefinition> dataTypes) { + public Map<String, ToscaCapability> convertProxyCapabilities(Map<String, Component> componentCache, + ComponentInstance instanceProxy, + Map<String, DataTypeDefinition> dataTypes) { Map<String, List<CapabilityDefinition>> capabilities = instanceProxy.getCapabilities(); Map<String, ToscaCapability> toscaCapabilities = new HashMap<>(); if (capabilities != null) { - boolean isNodeType = ModelConverter.isAtomicComponent(component); for (Map.Entry<String, List<CapabilityDefinition>> entry : capabilities.entrySet()) { entry.getValue() .stream() 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 0813650b3a..1301a1c7a9 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 @@ -45,7 +45,6 @@ import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; -import org.openecomp.sdc.be.model.tosca.ToscaFunctions; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter; import org.openecomp.sdc.be.tosca.model.*; import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil; @@ -78,6 +77,10 @@ import static org.apache.commons.collections.CollectionUtils.isNotEmpty; import static org.apache.commons.collections.MapUtils.isNotEmpty; 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; +import static org.openecomp.sdc.be.tosca.utils.ToscaExportUtils.resolvePropertyDefaultValueFromInput; @org.springframework.stereotype.Component("tosca-export-handler") public class ToscaExportHandler { @@ -223,7 +226,8 @@ public class ToscaExportHandler { } toscaNode = importsRes.left().value().left; Map<String, Component> componentCache = importsRes.left().value().right; - Either<Map<String, ToscaNodeType>, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache , component ); + Either<Map<String, ToscaNodeType>, ToscaError> nodeTypesMapEither = createProxyNodeTypes(componentCache, + component); if (nodeTypesMapEither.isRight()) { log.debug("Failed to fetch normative service proxy resource by tosca name, error {}", nodeTypesMapEither.right().value()); @@ -234,6 +238,16 @@ public class ToscaExportHandler { toscaNode.setNode_types(nodeTypesMap); } + Either<Map<String, Object>, ToscaError> proxyInterfaceTypesEither = createProxyInterfaceTypes(component); + if (proxyInterfaceTypesEither.isRight()) { + log.debug("Failed to populate service proxy local interface types in tosca, error {}", + nodeTypesMapEither.right().value()); + return Either.right(proxyInterfaceTypesEither.right().value()); + } + Map<String, Object> proxyInterfaceTypes = proxyInterfaceTypesEither.left().value(); + if (MapUtils.isNotEmpty(proxyInterfaceTypes)) { + toscaNode.setInterface_types(proxyInterfaceTypes); + } Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll(); if (dataTypesEither.isRight()) { @@ -545,14 +559,11 @@ public class ToscaExportHandler { List<InputDefinition> inputDef = component.getInputs(); Map<String, ToscaProperty> mergedProperties = new HashMap<>(); addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); - if (inputDef != null) { - addInputsToProperties(dataTypes, inputDef, mergedProperties); - } + addInputsToProperties(dataTypes, inputDef, mergedProperties); if(CollectionUtils.isNotEmpty(component.getProperties())) { List<PropertyDefinition> properties = component.getProperties(); - Map<String, ToscaProperty> convertedProperties; - convertedProperties = properties.stream().collect(Collectors.toMap( + Map<String, ToscaProperty> convertedProperties = properties.stream().collect(Collectors.toMap( PropertyDataDefinition::getName, property -> propertyConvertor.convertProperty(dataTypes, property, PropertyConvertor.PropertyType.PROPERTY))); @@ -560,9 +571,7 @@ public class ToscaExportHandler { mergedProperties.putAll(convertedProperties); } if (MapUtils.isNotEmpty(mergedProperties)) { - if (Objects.nonNull(inputDef)) { - resolveDefaultPropertyValue(inputDef, mergedProperties, dataTypes); - } + resolvePropertyDefaultValueFromInput(inputDef, mergedProperties, dataTypes); toscaNodeType.setProperties(mergedProperties); } @@ -593,50 +602,6 @@ public class ToscaExportHandler { return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes); } - private void resolveDefaultPropertyValue(List<InputDefinition> inputDef, - Map<String, ToscaProperty> mergedProperties, - Map<String, DataTypeDefinition> dataTypes) { - for (Map.Entry<String, ToscaProperty> mergedPropertyEntry : mergedProperties.entrySet()) { - ToscaProperty value = mergedPropertyEntry.getValue(); - if (Objects.nonNull(value) && value.getDefaultp() instanceof Map) { - Map<String, Object> valueAsMap = (Map<String, Object>) value.getDefaultp(); - Object getInputValue = valueAsMap.get(ToscaFunctions.GET_INPUT.getFunctionName()); - if (getInputValue instanceof String) { - String inputName = (String)getInputValue; - Optional<InputDefinition> matchedInputDefinition = inputDef.stream() - .filter(componentInput -> componentInput.getName().equals(inputName)) - .findFirst(); - if (matchedInputDefinition.isPresent()) { - InputDefinition matchedInput = matchedInputDefinition.get(); - Object resolvedDefaultValue = new PropertyConvertor().convertToToscaObject(matchedInput.getType(), - matchedInput.getDefaultValue(), matchedInput.getSchemaType(), dataTypes, false); - value.setDefaultp(resolvedDefaultValue); - mergedProperties.put(mergedPropertyEntry.getKey(), value); - } - } else if (getInputValue instanceof List) { - // new get_input syntax to refer to sub-element (introduced from TOSCA v1.3) - // e.g. get_input: [input_name, INDEX, inner_property] - // currently resolving default value for the syntax is not supported - log.debug("#resolveDefaultPropertyValue: ignore get_input list syntax. propname={}, val={}", - mergedPropertyEntry.getKey(), getInputValue); - } else { - // Ignore unknown get_input syntax - log.debug("#resolveDefaultPropertyValue: ignore unknown get_input syntax. propname={}, val={}", - mergedPropertyEntry.getKey(), getInputValue); - } - } - } - } - - private void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes, - List<InputDefinition> inputDef, - Map<String, ToscaProperty> mergedProperties) { - for(InputDefinition input : inputDef) { - ToscaProperty property = propertyConvertor.convertProperty(dataTypes, input, PropertyConvertor.PropertyType.INPUT); - mergedProperties.put(input.getName(), property); - } - } - private Either<ToscaTemplate, ToscaError> convertReqCapAndTypeName(Map<String, Component> componentsCache, Component component, ToscaTemplate toscaNode, Map<String, ToscaNodeType> nodeTypes, ToscaNodeType toscaNodeType, Map<String, DataTypeDefinition> dataTypes) { @@ -948,7 +913,59 @@ public class ToscaExportHandler { return toscaNodeType; } - private Either<Map<String, ToscaNodeType>, ToscaError> createProxyNodeTypes(Map<String, Component> componentCache ,Component container ) { + private Either<Map<String, Object>, ToscaError> createProxyInterfaceTypes(Component container) { + + Map<String, Object> proxyInterfaceTypes = new HashMap<>(); + Either<Map<String, Object>, ToscaError> res = Either.left(proxyInterfaceTypes); + List<ComponentInstance> componentInstances = container.getComponentInstances(); + if (CollectionUtils.isEmpty(componentInstances)) { + return res; + } + Map<String, ComponentInstance> serviceProxyInstanceList = new HashMap<>(); + componentInstances.stream() + .filter(this::isComponentOfTypeServiceProxy) + .forEach(inst -> serviceProxyInstanceList.put(inst.getToscaComponentName(), inst)); + if (MapUtils.isEmpty(serviceProxyInstanceList)) { + return res; + } + for (Entry<String, ComponentInstance> entryProxy : serviceProxyInstanceList.entrySet()) { + Component serviceComponent; + ComponentParametersView componentParametersView = new ComponentParametersView(); + componentParametersView.disableAll(); + componentParametersView.setIgnoreInterfaces(false); + Either<Component, StorageOperationStatus> service = toscaOperationFacade + .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView); + if (service.isRight()) { + log.debug("Failed to fetch original service component with id {} for instance {}", + entryProxy.getValue().getSourceModelUid(), entryProxy.getValue().getName()); + return Either.right(ToscaError.GENERAL_ERROR); + } else { + serviceComponent = service.left().value(); + } + + Either<Map<String, InterfaceDefinition>, StorageOperationStatus> lifecycleTypeEither = + interfaceLifecycleOperation.getAllInterfaceLifecycleTypes(); + if(lifecycleTypeEither.isRight()){ + log.debug("Failed to retrieve global interface types :", lifecycleTypeEither.right().value()); + return Either.right(ToscaError.GENERAL_ERROR); + } + + List<String> allGlobalInterfaceTypes = lifecycleTypeEither.left().value().values().stream() + .map(InterfaceDataDefinition::getType) + .collect(Collectors.toList()); + //Add interface types for local interfaces in the original service component for proxy + Map<String, Object> localInterfaceTypes = addInterfaceTypeElement(serviceComponent, + allGlobalInterfaceTypes); + if (MapUtils.isNotEmpty(localInterfaceTypes)) { + proxyInterfaceTypes.putAll(localInterfaceTypes); + } + + } + return Either.left(proxyInterfaceTypes); + } + + private Either<Map<String, ToscaNodeType>, ToscaError> createProxyNodeTypes(Map<String, Component> componentCache, + Component container) { Map<String, ToscaNodeType> nodeTypesMap = new HashMap<>(); Either<Map<String, ToscaNodeType>, ToscaError> res = Either.left(nodeTypesMap); @@ -971,9 +988,6 @@ public class ToscaExportHandler { if (serviceProxyInstanceList.isEmpty()) { return res; } - ComponentParametersView filter = new ComponentParametersView(true); - filter.setIgnoreCapabilities(false); - filter.setIgnoreComponentInstances(false); Either<Resource, StorageOperationStatus> serviceProxyOrigin = toscaOperationFacade .getLatestByName("serviceProxy"); if (serviceProxyOrigin.isRight()) { @@ -988,6 +1002,10 @@ public class ToscaExportHandler { ComponentParametersView componentParametersView = new ComponentParametersView(); componentParametersView.disableAll(); componentParametersView.setIgnoreCategories(false); + componentParametersView.setIgnoreProperties(false); + componentParametersView.setIgnoreInputs(false); + componentParametersView.setIgnoreInterfaces(false); + componentParametersView.setIgnoreRequirements(false); Either<Component, StorageOperationStatus> service = toscaOperationFacade .getToscaElement(entryProxy.getValue().getSourceModelUid(), componentParametersView); if (service.isRight()) { @@ -996,15 +1014,16 @@ public class ToscaExportHandler { serviceComponent = service.left().value(); } - ToscaNodeType toscaNodeType = createProxyNodeType(componentCache , origComponent, serviceComponent, entryProxy.getValue()); + ToscaNodeType toscaNodeType = createProxyNodeType(componentCache, origComponent, serviceComponent, + entryProxy.getValue()); nodeTypesMap.put(entryProxy.getKey(), toscaNodeType); } return Either.left(nodeTypesMap); } - private ToscaNodeType createProxyNodeType(Map<String, Component> componentCache , Component origComponent, Component proxyComponent, - ComponentInstance instance) { + private ToscaNodeType createProxyNodeType(Map<String, Component> componentCache , Component origComponent, + Component proxyComponent, ComponentInstance instance) { ToscaNodeType toscaNodeType = new ToscaNodeType(); String derivedFrom = ((Resource) origComponent).getToscaResourceName(); @@ -1015,9 +1034,21 @@ public class ToscaExportHandler { } Map<String, DataTypeDefinition> dataTypes = dataTypesEither.left().value(); Map<String, ToscaCapability> capabilities = this.capabilityRequirementConverter - .convertProxyCapabilities( componentCache ,origComponent, proxyComponent, instance, dataTypes); + .convertProxyCapabilities(componentCache, instance, dataTypes); + + if (MapUtils.isNotEmpty(capabilities)) { + toscaNodeType.setCapabilities(capabilities); + } + List<Map<String, ToscaRequirement>> proxyNodeTypeRequirements = this.capabilityRequirementConverter + .convertProxyRequirements(componentCache, instance); + if (CollectionUtils.isNotEmpty(proxyNodeTypeRequirements)) { + toscaNodeType.setRequirements(proxyNodeTypeRequirements); + } + Optional<Map<String, ToscaProperty>> proxyProperties = getProxyNodeTypeProperties(proxyComponent, dataTypes); + proxyProperties.ifPresent(toscaNodeType::setProperties); - toscaNodeType.setCapabilities(capabilities); + Optional<Map<String, Object>> proxyInterfaces = getProxyNodeTypeInterfaces(proxyComponent, dataTypes); + proxyInterfaces.ifPresent(toscaNodeType::setInterfaces); return toscaNodeType; } 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 new file mode 100644 index 0000000000..20e0698276 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/ToscaExportUtils.java @@ -0,0 +1,109 @@ +/* + * 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 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 org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +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.tosca.ToscaFunctions; +import org.openecomp.sdc.be.tosca.PropertyConvertor; +import org.openecomp.sdc.be.tosca.model.ToscaProperty; + +public class ToscaExportUtils { + + private ToscaExportUtils() { + //Hiding implicit default constructor + } + + public static Optional<Map<String, Object>> getProxyNodeTypeInterfaces(Component proxyComponent, + Map<String, DataTypeDefinition> dataTypes) { + if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) { + return Optional.empty(); + } + return Optional.ofNullable(InterfacesOperationsToscaUtil + .getInterfacesMap(proxyComponent, null, proxyComponent.getInterfaces(), dataTypes, false, false)); + } + + public static Optional<Map<String, ToscaProperty>> getProxyNodeTypeProperties(Component proxyComponent, + Map<String, DataTypeDefinition> + dataTypes) { + if (Objects.isNull(proxyComponent)) { + return Optional.empty(); + } + Map<String, ToscaProperty> proxyProperties = new HashMap<>(); + addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties); + if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) { + proxyProperties.putAll(proxyComponent.getProperties().stream() + .collect(Collectors.toMap(PropertyDataDefinition::getName, + property -> PropertyConvertor.getInstance().convertProperty(dataTypes, property, + PropertyConvertor.PropertyType.PROPERTY)))); + } + resolvePropertyDefaultValueFromInput(proxyComponent.getInputs(), proxyProperties, dataTypes); + + return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty(); + } + + + public static void resolvePropertyDefaultValueFromInput(List<InputDefinition> componentInputs, + Map<String, ToscaProperty> mergedProperties, + Map<String, DataTypeDefinition> dataTypes) { + if (MapUtils.isEmpty(mergedProperties) || CollectionUtils.isEmpty(componentInputs)) { + return; + } + for (Map.Entry<String, ToscaProperty> mergedPropertyEntry : mergedProperties.entrySet()) { + ToscaProperty value = mergedPropertyEntry.getValue(); + if (Objects.nonNull(value) && value.getDefaultp() instanceof Map) { + Map<String, String> valueAsMap = (Map<String, String>) value.getDefaultp(); + String inputName = valueAsMap.get(ToscaFunctions.GET_INPUT.getFunctionName()); + Optional<InputDefinition> matchedInputDefinition = componentInputs.stream() + .filter(componentInput -> componentInput.getName().equals(inputName)) + .findFirst(); + if (matchedInputDefinition.isPresent()) { + InputDefinition matchedInput = matchedInputDefinition.get(); + Object resolvedDefaultValue = new PropertyConvertor().convertToToscaObject(matchedInput.getType(), + matchedInput.getDefaultValue(), matchedInput.getSchemaType(), dataTypes, false); + value.setDefaultp(resolvedDefaultValue); + mergedProperties.put(mergedPropertyEntry.getKey(), value); + } + } + } + } + + public static void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes, + List<InputDefinition> componentInputs, + Map<String, ToscaProperty> 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); + } + } + +} 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 675c51d635..ff522e15c5 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 @@ -1,14 +1,31 @@ +/* + * 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 java.util.Iterator; + +import org.junit.Assert; 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.elements.PropertyDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; +import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.Component; @@ -22,12 +39,11 @@ import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.tosca.model.SubstitutionMapping; import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate; -import org.openecomp.sdc.be.tosca.model.ToscaNodeType; -import org.openecomp.sdc.be.tosca.model.ToscaTemplateCapability; import fj.data.Either; import mockit.Deencapsulation; +import org.openecomp.sdc.be.tosca.model.ToscaRequirement; import java.util.*; import java.util.stream.Collectors; @@ -242,7 +258,103 @@ public class CapabilityRequirementConverterTest { vfComponent.setComponentInstances(componentInstances); - testSubject.convertProxyCapabilities(componentsCache, vfComponent, vfComponent, instance, dataTypes); + testSubject.convertProxyCapabilities(componentsCache, instance, dataTypes); + } + + @Test + public void testConvertProxyRequirementsNoRequirements() { + Map<String, Component> componentsCache = new HashMap<>(); + + List<ComponentInstance> componentInstances = new ArrayList<>(); + ComponentInstance instance = new ComponentInstance(); + instance.setUniqueId("id"); + instance.setComponentUid("componentUid"); + componentInstances.add(instance); + + vfComponent.setComponentInstances(componentInstances); + + Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class), + Mockito.any(ComponentParametersView.class))) + .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); + + List<Map<String, ToscaRequirement>> proxyRequirements = + testSubject.convertProxyRequirements(componentsCache, instance); + Assert.assertEquals(0, proxyRequirements.size()); + } + + @Test + public void testConvertProxyRequirementsNotSubstitutedName() { + Map<String, Component> componentsCache = new HashMap<>(); + RequirementDefinition r = new RequirementDefinition(); + r.setName("port0.dependency"); + r.setPreviousName("dependency"); + r.setCapability("tosca.capabilities.Node"); + r.setNode("tosca.nodes.Root"); + r.setRelationship("tosca.relationships.DependsOn"); + r.setMinOccurrences(RequirementDataDefinition.MIN_OCCURRENCES); + r.setMaxOccurrences(RequirementDataDefinition.MAX_OCCURRENCES); + r.setOwnerId("id"); + r.setParentName("parentName"); + + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitions = new ArrayList<>(); + requirementDefinitions.add(r); + requirements.put("dependency", requirementDefinitions); + + List<ComponentInstance> componentInstances = new ArrayList<>(); + ComponentInstance instance = new ComponentInstance(); + instance.setUniqueId("id"); + instance.setComponentUid("componentUid"); + instance.setRequirements(requirements); + instance.setNormalizedName("port0"); + componentInstances.add(instance); + + vfComponent.setComponentInstances(componentInstances); + + Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class), + Mockito.any(ComponentParametersView.class))) + .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); + + List<Map<String, ToscaRequirement>> proxyRequirements = + testSubject.convertProxyRequirements(componentsCache, instance); + Map<String, ToscaRequirement> proxyRequirement = proxyRequirements.get(0); + Assert.assertEquals("dependency", proxyRequirement.keySet().iterator().next()); + } + + @Test + public void testConvertProxyRequirementsSubstitutedName() { + Map<String, Component> componentsCache = new HashMap<>(); + RequirementDefinition r = new RequirementDefinition(); + r.setName("dependency"); + r.setPreviousName("dependency"); + r.setCapability("tosca.capabilities.Node"); + r.setNode("tosca.nodes.Root"); + r.setRelationship("tosca.relationships.DependsOn"); + r.setMinOccurrences(RequirementDataDefinition.MIN_OCCURRENCES); + r.setMaxOccurrences(RequirementDataDefinition.MAX_OCCURRENCES); + r.setOwnerId("id"); + r.setParentName("parentName"); + + Map<String, List<RequirementDefinition>> requirements = new HashMap<>(); + List<RequirementDefinition> requirementDefinitions = new ArrayList<>(); + requirementDefinitions.add(r); + requirements.put("dependency", requirementDefinitions); + + List<ComponentInstance> componentInstances = new ArrayList<>(); + ComponentInstance instance = new ComponentInstance(); + instance.setUniqueId("id"); + instance.setComponentUid("componentUid"); + instance.setRequirements(requirements); + instance.setNormalizedName("port0"); + componentInstances.add(instance); + + vfComponent.setComponentInstances(componentInstances); + + Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class), + Mockito.any(ComponentParametersView.class))) + .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); + + testSubject.convertProxyRequirements(componentsCache, instance); } @Test @@ -272,7 +384,7 @@ public class CapabilityRequirementConverterTest { Mockito.any(ComponentParametersView.class))) .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); - testSubject.convertProxyCapabilities(componentsCache, vfComponent, vfComponent, instance, dataTypes); + testSubject.convertProxyCapabilities(componentsCache, instance, dataTypes); } @Test 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 65298ea81d..90dc926acf 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 @@ -11,6 +11,7 @@ import java.util.stream.Collectors; import mockit.Deencapsulation; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Triple; +import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; @@ -35,12 +36,12 @@ import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.ComponentInstanceInput; import org.openecomp.sdc.be.model.ComponentInstanceProperty; -import org.openecomp.sdc.be.model.ComponentMetadataDefinition; import org.openecomp.sdc.be.model.ComponentParametersView; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.GroupInstance; 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.RelationshipInfo; import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; @@ -831,6 +832,75 @@ public class ToscaExportHandlerTest extends BeConfDependentTest { } @Test + public void testCreateProxyInterfaceTypesComponentNotFound() throws Exception { + Component container = new Service(); + Either<Map<String, ToscaNodeType>, ToscaError> result; + List<ComponentInstance> componentInstances = new ArrayList<>(); + ComponentInstance instance = new ComponentInstance(); + instance.setOriginType(OriginTypeEnum.ServiceProxy); + instance.setSourceModelUid("targetModelUid"); + instance.setToscaComponentName("toscaComponentName"); + + componentInstances.add(instance); + container.setComponentInstances(componentInstances); + Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class), + Mockito.any(ComponentParametersView.class))) + .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); + result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container); + Assert.assertTrue(result.isRight()); + + } + + @Test + public void testCreateProxyInterfaceTypesWhenInterfaceLifecycleFetchFailed() { + Component container = new Service(); + Either<Map<String, ToscaNodeType>, ToscaError> result; + List<ComponentInstance> componentInstances = new ArrayList<>(); + ComponentInstance instance = new ComponentInstance(); + instance.setOriginType(OriginTypeEnum.ServiceProxy); + instance.setSourceModelUid("targetModelUid"); + instance.setToscaComponentName("toscaComponentName"); + componentInstances.add(instance); + container.setComponentInstances(componentInstances); + + Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class), + Mockito.any(ComponentParametersView.class))) + .thenReturn(Either.left(new Resource())); + Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + .thenReturn(Either.right(StorageOperationStatus.BAD_REQUEST)); + result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container); + Assert.assertTrue(result.isRight()); + } + + @Test + public void testCreateProxyInterfaceTypesPositive() { + Component container = new Service(); + Either<Map<String, ToscaNodeType>, ToscaError> result; + List<ComponentInstance> componentInstances = new ArrayList<>(); + ComponentInstance instance = new ComponentInstance(); + instance.setOriginType(OriginTypeEnum.ServiceProxy); + instance.setSourceModelUid("targetModelUid"); + instance.setToscaComponentName("toscaComponentName"); + componentInstances.add(instance); + container.setComponentInstances(componentInstances); + + Mockito.when(interfaceLifecycleOperation.getAllInterfaceLifecycleTypes()) + .thenReturn(Either.left(Collections.emptyMap())); + + Component proxyResource = new Resource(); + Map<String, InterfaceDefinition> proxyInterfaces = new HashMap<>(); + proxyInterfaces.put("Local", new InterfaceDefinition("Local", "desc", new HashMap<>())); + proxyResource.setInterfaces(proxyInterfaces); + Mockito.when(toscaOperationFacade.getToscaElement(Mockito.any(String.class), + Mockito.any(ComponentParametersView.class))) + .thenReturn(Either.left(proxyResource)); + + result = Deencapsulation.invoke(testSubject, "createProxyInterfaceTypes", container); + Assert.assertTrue(result.isLeft()); + Assert.assertEquals(1, result.left().value().size()); + } + + @Test public void testCreateProxyNodeTypes() throws Exception { Map<String, Component> componentCache = new HashMap<>(); Component container = new Resource(); 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 index efc89a9f9d..87cc6e8d20 100644 --- 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 @@ -1,120 +1,209 @@ +/* + * 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 com.google.gson.Gson; -import com.google.gson.GsonBuilder; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.junit.Assert; import org.junit.Test; -import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; -import org.openecomp.sdc.be.model.operations.impl.ComponentInstanceOperation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; +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; public class ToscaExportUtilsTest { - private static final Logger log = LoggerFactory.getLogger(ToscaExportUtilsTest.class); - @javax.annotation.Resource - private ComponentInstanceBusinessLogic componentInstanceBusinessLogic; - @Autowired - private ToscaExportHandler exportUtils; - @Autowired - private ComponentInstanceOperation componentInstanceOperation; - Gson gson = new GsonBuilder().setPrettyPrinting().create(); + private static final Map<String, DataTypeDefinition> dataTypes = new HashMap<>(); + + @Test + public void testGetProxyNodeTypeInterfacesNoInterfaces() { + Component service = new Service(); + Optional<Map<String, Object>> proxyNodeTypeInterfaces = + ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes); + Assert.assertFalse(proxyNodeTypeInterfaces.isPresent()); + } + + @Test + public void testGetProxyNodeTypeInterfaces() { + Component service = getTestComponent(); + Optional<Map<String, Object>> proxyNodeTypeInterfaces = + ToscaExportUtils.getProxyNodeTypeInterfaces(service, dataTypes); + Assert.assertTrue(proxyNodeTypeInterfaces.isPresent()); + Map<String, Object> componentInterfaces = proxyNodeTypeInterfaces.get(); + Assert.assertNotNull(componentInterfaces); + Assert.assertEquals(1, componentInterfaces.size()); + } + + + @Test + public void testGetProxyNodeTypePropertiesComponentNull() { + Optional<Map<String, ToscaProperty>> proxyNodeTypeProperties = + ToscaExportUtils.getProxyNodeTypeProperties(null, dataTypes); + Assert.assertFalse(proxyNodeTypeProperties.isPresent()); + } + + @Test + public void testGetProxyNodeTypePropertiesNoProperties() { + Component service = new Service(); + Optional<Map<String, ToscaProperty>> 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<Map<String, ToscaProperty>> proxyNodeTypeProperties = + ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); + Assert.assertTrue(proxyNodeTypeProperties.isPresent()); + Map<String, ToscaProperty> componentProperties = proxyNodeTypeProperties.get(); + Assert.assertNotNull(componentProperties); + Assert.assertEquals(2, componentProperties.size()); + } @Test - public void testExportService() { - /* Resource resource1 = ResourceTestUtils.prepareResource(0); - resource1.setResourceType(ResourceTypeEnum.VF); - Either<Resource, ResponseFormat> createResource1 = resourceBusinessLogic.createResource(resource1, getAdminUser(), null, null); - assertTrue(createResource1.isLeft()); - Resource certifiedVFC1 = changeResourceStateToCertify(createResource1.left().value()); - - Resource resource2 = ResourceTestUtils.prepareResource(1); - resource2.setResourceType(ResourceTypeEnum.VF); - Either<Resource, ResponseFormat> createResource2 = resourceBusinessLogic.createResource(resource2, getAdminUser(), null, null); - assertTrue(createResource2.isLeft()); - Resource certifiedVFC2 = changeResourceStateToCertify(createResource2.left().value()); - - Service service = ResourceTestUtils.prepareService(0); - Either<Service, ResponseFormat> createService = serviceBusinessLogic.createService(service, getAdminUser()); - assertTrue(createService.isLeft()); - - // add VFC instance to VF - ComponentInstance vfcResourceInstance1 = new ComponentInstance(); - vfcResourceInstance1.setDescription("VFC instance 1"); - vfcResourceInstance1.setName(certifiedVFC1.getName()); - vfcResourceInstance1.setComponentUid(certifiedVFC1.getUniqueId()); - - Either<ComponentInstance, ResponseFormat> createResourceVfcInstance1 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(), - vfcResourceInstance1); - assertTrue(createResourceVfcInstance1.isLeft()); - - ComponentInstance vfcResourceInstance2 = new ComponentInstance(); - vfcResourceInstance2.setDescription("VFC instance 2"); - vfcResourceInstance2.setName(certifiedVFC2.getName()); - vfcResourceInstance2.setComponentUid(certifiedVFC2.getUniqueId()); - Either<ComponentInstance, ResponseFormat> createResourceVfcInstance2 = serviceInstanceBusinessLogic.createComponentInstance(ComponentTypeEnum.SERVICE_PARAM_NAME, createService.left().value().getUniqueId(), adminUser.getAttuid(), - vfcResourceInstance2); - assertTrue(createResourceVfcInstance2.isLeft()); - - Either<Service, ResponseFormat> serviceFetch = serviceBusinessLogic.getService(createService.left().value().getUniqueId(), adminUser); - assertTrue(serviceFetch.isLeft()); - - List<ComponentInstance> componentInstances = serviceFetch.left().value().getComponentInstances(); - String ciname1 = null; - String ciname2 = null; - - for (ComponentInstance ci : componentInstances) { - if (ci.getComponentUid().equals(certifiedVFC1.getUniqueId())) { - ciname1 = ci.getName(); - } - if (ci.getComponentUid().equals(certifiedVFC2.getUniqueId())) { - ciname2 = ci.getName(); - } - } - - Either<ToscaRepresentation, ToscaError> result = exportUtils.exportComponent(serviceFetch.left().value()); - assertTrue(result.isLeft()); - - String mainYaml = result.left().value().getMainYaml(); - assertNotNull(mainYaml); - - YamlToObjectConverter yamlToObjectConverter = new YamlToObjectConverter(); - assertTrue(yamlToObjectConverter.isValidYaml(mainYaml.getBytes())); - log.debug(mainYaml); - - Yaml yaml = new Yaml(); - - InputStream inputStream = new ByteArrayInputStream(mainYaml.getBytes()); - Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream); - Map<String, Object> imports = (Map<String, Object>) load.get("imports"); - assertNotNull(imports); - assertEquals("Validate imports size in yml", 2, imports.size()); - - Map<String, Object> metadata = (Map<String, Object>) load.get("metadata"); - assertNotNull(metadata); - validateMetadata(metadata, serviceFetch.left().value(), false); - - Map<String, Object> vf1 = (Map<String, Object>) imports.get(certifiedVFC1.getName()); - String fileName = (String) vf1.get(ToscaExportHandler.IMPORTS_FILE_KEY); - ArtifactDefinition artifactDefinition = certifiedVFC1.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE); - assertEquals("Validate 1 file name", artifactDefinition.getArtifactName(), fileName); - - Map<String, Object> topology_template = (Map<String, Object>) load.get("topology_template"); - Map<String, Object> node_templates = (Map<String, Object>) topology_template.get("node_templates"); - Map<String, Object> inst1 = (Map<String, Object>) node_templates.get(ciname1); - Map<String, Object> inst2 = (Map<String, Object>) node_templates.get(ciname2); - - Map<String, Object> inst1MD = (Map<String, Object>) inst1.get("metadata"); - Map<String, Object> inst2MD = (Map<String, Object>) inst2.get("metadata"); - - validateMetadata(inst1MD, certifiedVFC1, true); - - Map<String, Object> vf2 = (Map<String, Object>) imports.get(certifiedVFC2.getName()); - fileName = (String) vf2.get(ToscaExportHandler.IMPORTS_FILE_KEY); - artifactDefinition = certifiedVFC2.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE); - assertEquals("Validate 2 file name", artifactDefinition.getArtifactName(), fileName); - - validateMetadata(inst2MD, certifiedVFC2, true);*/ + public void testResolvePropertyDefaultValueFromInputNoInputs() { + Component service = getTestComponent(); + service.setProperties(Collections.singletonList(createMockProperty("componentPropStr", null))); + Optional<Map<String, ToscaProperty>> properties = ToscaExportUtils.getProxyNodeTypeProperties(service, + dataTypes); + Assert.assertTrue(properties.isPresent()); + Map<String, ToscaProperty> nodeTypeProperties = properties.get(); + ToscaExportUtils.resolvePropertyDefaultValueFromInput(null, nodeTypeProperties, dataTypes); + nodeTypeProperties.values().forEach(val -> Assert.assertNull(val.getDefaultp())); } + @Test + public void testResolvePropertyDefaultValueFromInput() { + Component service = getTestComponent(); + service.setProperties(Arrays.asList(createMockProperty("componentPropStr1", "{get_input: componentInputStr1}"), + createMockProperty("componentPropStr2", "Default prop value"), + createMockProperty("componentPropStr3", null))); + Optional<Map<String, ToscaProperty>> properties = ToscaExportUtils.getProxyNodeTypeProperties(service, + dataTypes); + Assert.assertTrue(properties.isPresent()); + Map<String, ToscaProperty> nodeTypeProperties = properties.get(); + List<InputDefinition> componentInputs = Arrays.asList(createMockInput("componentInputStr1", + "Default String Input1"), createMockInput("componentInputStr2", "Default String Input2")); + ToscaExportUtils.resolvePropertyDefaultValueFromInput(componentInputs, nodeTypeProperties, dataTypes); + nodeTypeProperties.entrySet().stream() + .filter(entry -> entry.getKey().equals("componentPropStr1")) + .forEach(entry -> Assert.assertEquals("Default String Input1", + entry.getValue().getDefaultp().toString())); + + nodeTypeProperties.entrySet().stream() + .filter(entry -> entry.getKey().equals("componentPropStr2")) + .forEach(entry -> Assert.assertEquals("Default prop value", + entry.getValue().getDefaultp().toString())); + + nodeTypeProperties.entrySet().stream() + .filter(entry -> entry.getKey().equals("componentPropStr3")) + .forEach(entry -> Assert.assertNull(entry.getValue().getDefaultp())); + } + + @Test + public void testAddInputsToPropertiesNoInputs() { + Component service = getTestComponent(); + service.setProperties(Arrays.asList(createMockProperty("componentPropStr", "Default String Prop"), + createMockProperty("componentPropInt", null))); + Optional<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult = + ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); + + Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); + Map<String, ToscaProperty> 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<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult = + ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); + + Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); + Map<String, ToscaProperty> 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<Map<String, ToscaProperty>> proxyNodeTypePropertiesResult = + ToscaExportUtils.getProxyNodeTypeProperties(service, dataTypes); + + Assert.assertTrue(proxyNodeTypePropertiesResult.isPresent()); + Map<String, ToscaProperty> 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")); + } + + 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; + } } |