From 2f74a4ac81f228bdb0bf0f509e9c0ef296d28d82 Mon Sep 17 00:00:00 2001 From: aribeiro Date: Tue, 17 Nov 2020 10:52:41 +0000 Subject: Fix import VFC with attributes Fix import VFC with default attribute value Fix Update, create and delete attribute action Make attribute definition tosca compliant Issue-ID: SDC-3381 Signed-off-by: aribeiro Change-Id: Ibbd36b105b8c86d1e750f3b6d55752d63fe6530e --- .../be/components/impl/AttributeBusinessLogic.java | 93 ++++++-- .../sdc/be/components/impl/BaseBusinessLogic.java | 29 +-- .../be/components/impl/ComponentBusinessLogic.java | 1 - .../sdc/be/components/impl/ImportUtils.java | 48 ++-- .../be/components/impl/ResourceBusinessLogic.java | 3 +- .../be/components/impl/ResourceImportManager.java | 60 ++--- .../impl/ServiceImportBusinessLogic.java | 51 ++--- .../sdc/be/servlets/AttributeServlet.java | 86 +++---- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 27 ++- .../sdc/be/tosca/model/ToscaNodeType.java | 3 +- .../impl/AttributeBusinessLogicTest.java | 246 +++++++-------------- .../sdc/be/components/impl/ImportUtilsTest.java | 42 ++-- 12 files changed, 305 insertions(+), 384 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogic.java index ef82eba0a4..cef4d8f6df 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogic.java @@ -21,11 +21,17 @@ package org.openecomp.sdc.be.components.impl; import fj.data.Either; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; @@ -37,16 +43,13 @@ import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils; +import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; +import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; - /** * This class holds the business logic relevant for attributes manipulation. * @@ -113,15 +116,15 @@ public class AttributeBusinessLogic extends BaseBusinessLogic { } Map eitherAllDataTypes = getAllDataTypes(applicationDataTypeCache); // validate property default values - Either defaultValuesValidation = validatePropertyDefaultValue((AttributeDefinition)newAttributeDef, eitherAllDataTypes); + Either defaultValuesValidation = validateAttributeDefaultValue(newAttributeDef, eitherAllDataTypes); if (defaultValuesValidation.isRight()) { return Either.right(defaultValuesValidation.right().value()); } - handleDefaultValue((AttributeDefinition)newAttributeDef, eitherAllDataTypes); + handleAttributeDefaultValue(newAttributeDef, eitherAllDataTypes); // add the new attribute to resource on graph - // need to get StorageOpaerationStatus and convert to ActionStatus from + // need to get StorageOperationStatus and convert to ActionStatus from // componentsUtils Either either = toscaOperationFacade.addAttributeOfResource(resource, newAttributeDef); if (either.isRight()) { @@ -135,13 +138,73 @@ public class AttributeBusinessLogic extends BaseBusinessLogic { commitOrRollback(result); graphLockOperation.unlockComponent(resourceId, NodeTypeEnum.Resource); } + } + + private Either validateAttributeDefaultValue(final AttributeDataDefinition attributeDefinition, + final Map dataTypes) { + + if (!attributeOperation.isAttributeTypeValid(attributeDefinition)) { + log.info("Invalid type for attribute '{}' type '{}'", attributeDefinition.getName(), attributeDefinition.getType()); + final ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_TYPE, attributeDefinition + .getType(), attributeDefinition.getName()); + return Either.right(responseFormat); + } + String type = attributeDefinition.getType(); + String innerType = null; + if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { + final ImmutablePair propertyInnerTypeValid = attributeOperation.isAttributeInnerTypeValid( + attributeDefinition, dataTypes); + innerType = propertyInnerTypeValid.getLeft(); + if (!propertyInnerTypeValid.getRight()) { + log.info("Invalid inner type for attribute '{}' type '{}', dataTypeCount '{}'", + attributeDefinition.getName(), attributeDefinition.getType(), dataTypes.size()); + final ResponseFormat responseFormat = componentsUtils + .getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, attributeDefinition.getName()); + return Either.right(responseFormat); + } + } + if (!attributeOperation.isAttributeDefaultValueValid(attributeDefinition, dataTypes)) { + log.info("Invalid default value for attribute '{}' type '{}'", attributeDefinition.getName(), + attributeDefinition.getType()); + ResponseFormat responseFormat; + if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) { + responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, + attributeDefinition.getName(), type, innerType, + (String) attributeDefinition.get_default()); + } else { + responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEFAULT_VALUE, + attributeDefinition.getName(), type, (String) attributeDefinition.get_default()); + } + return Either.right(responseFormat); + + } + return Either.left(true); + } + + private void handleAttributeDefaultValue(final AttributeDataDefinition newAttributeDefinition, + final Map dataTypes) { + final ToscaPropertyType type = ToscaPropertyType.isValidType(newAttributeDefinition.getType()); + final PropertyValueConverter converter = type.getConverter(); + // get inner type + String innerType = null; + final SchemaDefinition schema = newAttributeDefinition.getSchema(); + if (schema != null) { + final PropertyDataDefinition prop = schema.getProperty(); + if (schema.getProperty() != null) { + innerType = prop.getType(); + } + } + if (newAttributeDefinition.get_default() != null) { + newAttributeDefinition.set_default(converter + .convert((String) newAttributeDefinition.get_default(), innerType, dataTypes)); + } } private boolean isAttributeExist(List attributes, String resourceUid, String propertyName) { boolean isExist = false; if (attributes != null) { - isExist = attributes.stream().anyMatch(p -> Objects.equals(p.getName(), propertyName) && Objects.equals(p.getParentUniqueId(), resourceUid)); + isExist = attributes.stream().anyMatch(p -> Objects.equals(p.getName(), propertyName) && Objects.equals(p.getOwnerId(), resourceUid)); } return isExist; @@ -169,8 +232,10 @@ public class AttributeBusinessLogic extends BaseBusinessLogic { return Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_NOT_FOUND, "")); } else { // verify attribute exist in resource - Optional optionalAtt = attributes.stream().filter(att -> att.getUniqueId().equals(attributeId) && resourceId.equals(att.getParentUniqueId())).findAny(); - return optionalAtt.>map(Either::left).orElseGet(() -> Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_NOT_FOUND, ""))); + Optional optionalAtt = attributes.stream().filter(att -> + att.getUniqueId().equals(attributeId)).findAny(); + return optionalAtt.>map(Either::left).orElseGet(() -> + Either.right(componentsUtils.getResponseFormat(ActionStatus.ATTRIBUTE_NOT_FOUND, ""))); } } @@ -213,13 +278,13 @@ public class AttributeBusinessLogic extends BaseBusinessLogic { Map eitherAllDataTypes = getAllDataTypes(applicationDataTypeCache); // validate attribute default values - Either defaultValuesValidation = validatePropertyDefaultValue((AttributeDefinition)newAttDef, eitherAllDataTypes); + Either defaultValuesValidation = validateAttributeDefaultValue(newAttDef, eitherAllDataTypes); if (defaultValuesValidation.isRight()) { return Either.right(defaultValuesValidation.right().value()); } // add the new property to resource on graph - StorageOperationStatus validateAndUpdateAttribute = propertyOperation.validateAndUpdateProperty((AttributeDefinition)newAttDef, eitherAllDataTypes); + StorageOperationStatus validateAndUpdateAttribute = attributeOperation.validateAndUpdateAttribute(newAttDef, eitherAllDataTypes); if (validateAndUpdateAttribute != StorageOperationStatus.OK) { log.debug("Problem while updating attribute with id {}. Reason - {}", attributeId, validateAndUpdateAttribute); result = Either.right(componentsUtils.getResponseFormatByResource(componentsUtils.convertFromStorageResponse(validateAndUpdateAttribute), resource.getName())); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java index 2616801c42..d1df40c091 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java @@ -79,6 +79,7 @@ import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation; import org.openecomp.sdc.be.model.operations.api.IGroupOperation; import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; +import org.openecomp.sdc.be.model.operations.impl.AttributeOperation; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation; @@ -116,6 +117,7 @@ public abstract class BaseBusinessLogic { protected JanusGraphDao janusGraphDao; protected JanusGraphGenericDao janusGraphGenericDao; protected PropertyOperation propertyOperation; + protected AttributeOperation attributeOperation; protected ApplicationDataTypeCache applicationDataTypeCache; protected ToscaOperationFacade toscaOperationFacade; protected ApplicationDataTypeCache dataTypeCache; @@ -195,6 +197,11 @@ public abstract class BaseBusinessLogic { this.propertyOperation = propertyOperation; } + @Autowired + public void setAttributeOperation(AttributeOperation attributeOperation) { + this.attributeOperation = attributeOperation; + } + User validateUserNotEmpty(User user, String ecompErrorContext) { return userValidations.validateUserNotEmpty(user, ecompErrorContext); } @@ -478,28 +485,6 @@ public abstract class BaseBusinessLogic { return Either.left(true); } - - void handleDefaultValue(IComplexDefaultValue newAttributeDef, Map dataTypes) { - // convert property - ToscaPropertyType type = ToscaPropertyType.isValidType(newAttributeDef.getType()); - PropertyValueConverter converter = type.getConverter(); - // get inner type - String innerType = null; - - SchemaDefinition schema = newAttributeDef.getSchema(); - if (schema != null) { - PropertyDataDefinition prop = schema.getProperty(); - if (schema.getProperty() != null) { - innerType = prop.getType(); - } - } - String convertedValue; - if (newAttributeDef.getDefaultValue() != null) { - convertedValue = converter.convert(newAttributeDef.getDefaultValue(), innerType, dataTypes); - newAttributeDef.setDefaultValue(convertedValue); - } - } - void validateComponentTypeEnum(ComponentTypeEnum componentTypeEnum, String errorContext, Wrapper errorWrapper) { if (componentTypeEnum == null) { BeEcompErrorManager.getInstance().logInvalidInputError(errorContext, "invalid component type", ErrorSeverity.INFO); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java index 34236eb644..72aeb2b8f9 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentBusinessLogic.java @@ -59,7 +59,6 @@ import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.facade.operations.CatalogOperation; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.ArtifactDefinition; -import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.CapReqDef; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java index f04eca1744..4127faa820 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ImportUtils.java @@ -20,21 +20,33 @@ package org.openecomp.sdc.be.components.impl; +import static org.apache.commons.collections.CollectionUtils.isEmpty; +import static org.openecomp.sdc.be.components.impl.ResourceImportManager.PROPERTY_NAME_PATTERN_IGNORE_LENGTH; +import static org.openecomp.sdc.be.datatypes.elements.Annotation.setAnnotationsName; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParseException; import com.google.gson.reflect.TypeToken; import fj.data.Either; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.function.Consumer; +import java.util.function.Function; import org.apache.commons.collections.CollectionUtils; -import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.StringEscapeUtils; -import org.apache.tinkerpop.gremlin.structure.T; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.dao.api.ActionStatus; -import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.elements.Annotation; -import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; @@ -42,7 +54,6 @@ import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.model.AnnotationTypeDefinition; import org.openecomp.sdc.be.model.AttributeDefinition; -import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.HeatParameterDefinition; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; @@ -70,25 +81,6 @@ import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Representer; import org.yaml.snakeyaml.resolver.Resolver; -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.function.Consumer; -import java.util.function.Function; - -import static org.apache.commons.collections.CollectionUtils.isEmpty; -import static org.openecomp.sdc.be.components.impl.ResourceImportManager.PROPERTY_NAME_PATTERN_IGNORE_LENGTH; -import static org.openecomp.sdc.be.datatypes.elements.Annotation.setAnnotationsName; -import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataType; -import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementOperation.createDataTypeDefinitionWithName; - @Component public final class ImportUtils { @@ -616,14 +608,6 @@ public final class ImportUtils { } - public static Either, ResultStatusEnum> getAttributes( - Map toscaJson) { - Function elementGenByName = ImportUtils::createAttribute; - Function, AttributeDataDefinition> func = ImportUtils::createModuleAttribute; - - return getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func); - } - public static Either, ResultStatusEnum> getElements(Map toscaJson, TypeUtils.ToscaTagNamesEnum elementTagName, Function elementGenByName, diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index 50b2941f1b..7788066e52 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java @@ -2660,7 +2660,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { resource.getComponentInstances() .stream() .filter(i -> !i.isCreatedFromCsar()) - .forEach(i->processUiComponentInstance(oldResource, i, instCapabilities, instRequirements, instDeploymentArtifacts, instArtifacts, instProperties, instInputs, instAttributes)); + .forEach(i -> processUiComponentInstance(oldResource, i, instCapabilities, instRequirements, + instDeploymentArtifacts, instArtifacts, instProperties, instInputs, instAttributes)); associateComponentInstancePropertiesToComponent(yamlName, resource, instProperties); associateComponentInstanceInputsToComponent(yamlName, resource, instInputs); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index 4e6628c8dd..7ff916df32 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -28,10 +28,22 @@ import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementO import static org.openecomp.sdc.be.utils.TypeUtils.setField; import fj.data.Either; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.function.Function; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import javax.servlet.ServletContext; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -62,9 +74,7 @@ import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.WebAppContextWrapper; import org.openecomp.sdc.be.model.ArtifactDefinition; -import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; -import org.openecomp.sdc.be.model.ComponentInstanceAttribute; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; @@ -96,20 +106,6 @@ import org.springframework.stereotype.Component; import org.springframework.web.context.WebApplicationContext; import org.yaml.snakeyaml.Yaml; -import javax.servlet.ServletContext; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.function.Function; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - @Component("resourceImportManager") public class ResourceImportManager { @@ -359,7 +355,6 @@ public class ResourceImportManager { if (StringUtils.isEmpty(resource.getToscaResourceName())) { setToscaResourceName(toscaJson, resource); } - setAttributes(toscaJson, resource); setCapabilities(toscaJson, resource, parentResource); setProperties(toscaJson, resource); setRequirements(toscaJson, resource, parentResource); @@ -680,28 +675,6 @@ public class ResourceImportManager { } } - private ResultStatusEnum setAttributes(Map toscaJson, Resource resource) { - ResultStatusEnum result = ResultStatusEnum.OK; - Either, ResultStatusEnum> attributes = ImportUtils - .getAttributes(toscaJson); - if (attributes.isLeft()) { - List attributeList = new ArrayList<>(); - Map value = attributes.left().value(); - if (value != null) { - for (Entry entry : value.entrySet()) { - String name = entry.getKey(); - AttributeDataDefinition attributeDef = entry.getValue(); - attributeDef.setName(name); - attributeList.add(attributeDef); - } - } - resource.setAttributes(attributeList); - } else { - result = attributes.right().value(); - } - return result; - } - private Resource setDerivedFrom(Map toscaJson, Resource resource) { Either toscaDerivedFromElement = ImportUtils .findFirstToscaStringElement(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM); @@ -1024,16 +997,17 @@ public class ResourceImportManager { final Map attributeMap = (Map) value; - final AttributeDefinition attributeDefinition = new AttributeDefinition(); + final AttributeDataDefinition attributeDefinition = new AttributeDataDefinition(); attributeDefinition.setName(key); setField(attributeMap, ToscaTagNamesEnum.DESCRIPTION, attributeDefinition::setDescription); setField(attributeMap, ToscaTagNamesEnum.TYPE, attributeDefinition::setType); + setField(attributeMap, ToscaTagNamesEnum.DEFAULT_VALUE, attributeDefinition::set_default); setField(attributeMap, ToscaTagNamesEnum.STATUS, attributeDefinition::setStatus); - setField(attributeMap, ToscaTagNamesEnum.ENTRY_SCHEMA, attributeDefinition::setEntry_schema); + setField(attributeMap, ToscaTagNamesEnum.ENTRY_SCHEMA, attributeDefinition::setSchema); attributeDataDefinitionList.add(attributeDefinition); } else { - final AttributeDefinition attributeDefinition = new AttributeDefinition(); + final AttributeDataDefinition attributeDefinition = new AttributeDataDefinition(); attributeDefinition.setName(key); attributeDataDefinitionList.add(attributeDefinition); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java index b47c3928e8..3b19ea9d86 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java @@ -24,7 +24,6 @@ import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonSt import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN; import fj.data.Either; - import java.util.ArrayList; import java.util.Collection; import java.util.EnumMap; @@ -36,13 +35,10 @@ import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.regex.Pattern; - import lombok.Getter; import lombok.Setter; -import org.apache.commons.codec.binary.Base64; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; -import org.apache.commons.collections4.ListUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.components.csar.CsarArtifactsAndGroupsBusinessLogic; import org.openecomp.sdc.be.components.csar.CsarBusinessLogic; @@ -71,20 +67,13 @@ import org.openecomp.sdc.be.config.BeEcompErrorManager; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; -import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils; import org.openecomp.sdc.be.datamodel.utils.UiComponentDataConverter; -import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.RequirementDataDefinition; -import org.openecomp.sdc.be.datatypes.enums.ComponentFieldsEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; -import org.openecomp.sdc.be.datatypes.enums.CreatedFrom; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.impl.ComponentsUtils; @@ -92,7 +81,6 @@ import org.openecomp.sdc.be.info.NodeTypeInfoToUpdateArtifacts; import org.openecomp.sdc.be.model.ArtifactDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.CapabilityRequirementRelationship; -import org.openecomp.sdc.be.model.CapabilityTypeDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.ComponentInstanceInput; @@ -102,7 +90,6 @@ import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.DistributionStatusEnum; import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.InputDefinition; -import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.LifeCycleTransitionEnum; import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.NodeTypeInfo; @@ -115,16 +102,12 @@ import org.openecomp.sdc.be.model.RequirementCapabilityRelDef; import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; -import org.openecomp.sdc.be.model.UploadCapInfo; import org.openecomp.sdc.be.model.UploadComponentInstanceInfo; -import org.openecomp.sdc.be.model.UploadInfo; import org.openecomp.sdc.be.model.UploadNodeFilterInfo; import org.openecomp.sdc.be.model.UploadPropInfo; import org.openecomp.sdc.be.model.UploadReqInfo; import org.openecomp.sdc.be.model.UploadResourceInfo; import org.openecomp.sdc.be.model.User; -import org.openecomp.sdc.be.model.category.CategoryDefinition; -import org.openecomp.sdc.be.model.category.SubCategoryDefinition; import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation; @@ -132,21 +115,15 @@ import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeFilterOperation; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; import org.openecomp.sdc.be.model.operations.StorageException; -import org.openecomp.sdc.be.model.operations.api.ICapabilityTypeOperation; import org.openecomp.sdc.be.model.operations.api.IElementOperation; import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation; import org.openecomp.sdc.be.model.operations.api.IGroupOperation; import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation; -import org.openecomp.sdc.be.model.operations.api.IInterfaceLifecycleOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; -import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; -import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; -import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo; import org.openecomp.sdc.be.tosca.CsarUtils; -import org.openecomp.sdc.be.utils.CommonBeUtils; import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; import org.openecomp.sdc.common.api.ArtifactTypeEnum; @@ -154,11 +131,9 @@ import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.datastructure.Wrapper; import org.openecomp.sdc.common.kpi.api.ASDCKpiApi; import org.openecomp.sdc.common.log.wrappers.Logger; -import org.openecomp.sdc.common.util.GeneralUtility; import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; -import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @Getter @@ -1297,7 +1272,18 @@ public class ServiceImportBusinessLogic{ return eitherGetResource.left().value(); } - protected void processComponentInstance(String yamlName, Resource resource, List componentInstancesList, Either, JanusGraphOperationStatus> allDataTypes, Map> instProperties, Map>> instCapabilties, Map>> instRequirements, Map> instDeploymentArtifacts, Map> instArtifacts, Map> instAttributes, Map originCompMap, Map> instInputs, UploadComponentInstanceInfo uploadComponentInstanceInfo) { + protected void processComponentInstance(String yamlName, Resource resource, + List componentInstancesList, + Either, JanusGraphOperationStatus> allDataTypes, + Map> instProperties, + Map>> instCapabilties, + Map>> instRequirements, + Map> instDeploymentArtifacts, + Map> instArtifacts, + Map> instAttributes, + Map originCompMap, + Map> instInputs, + UploadComponentInstanceInfo uploadComponentInstanceInfo) { Optional currentCompInstanceOpt = componentInstancesList.stream() .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())) .findFirst(); @@ -1736,7 +1722,18 @@ public class ServiceImportBusinessLogic{ return eitherGetResource.left().value(); } - protected void processComponentInstance(String yamlName, Service service, List componentInstancesList, Either, JanusGraphOperationStatus> allDataTypes, Map> instProperties, Map>> instCapabilties, Map>> instRequirements, Map> instDeploymentArtifacts, Map> instArtifacts, Map> instAttributes, Map originCompMap, Map> instInputs, UploadComponentInstanceInfo uploadComponentInstanceInfo) { + protected void processComponentInstance(String yamlName, Service service, + List componentInstancesList, + Either, JanusGraphOperationStatus> allDataTypes, + Map> instProperties, + Map>> instCapabilties, + Map>> instRequirements, + Map> instDeploymentArtifacts, + Map> instArtifacts, + Map> instAttributes, + Map originCompMap, + Map> instInputs, + UploadComponentInstanceInfo uploadComponentInstanceInfo) { log.debug("enter ServiceImportBusinessLogic processComponentInstance"); Optional currentCompInstanceOpt = componentInstancesList.stream() .filter(i -> i.getName().equals(uploadComponentInstanceInfo.getName())) diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java index 9f54f1fae8..b4b2c15dcb 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/AttributeServlet.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.be.servlets; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.jcabi.aspects.Loggable; @@ -32,6 +33,21 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.servers.Server; import io.swagger.v3.oas.annotations.tags.Tag; +import java.io.IOException; +import javax.inject.Inject; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.openecomp.sdc.be.components.impl.AttributeBusinessLogic; import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic; import org.openecomp.sdc.be.components.impl.ResourceImportManager; @@ -42,31 +58,15 @@ import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; import org.openecomp.sdc.be.impl.ServletUtils; -import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.user.UserBusinessLogic; import org.openecomp.sdc.common.api.Constants; import org.openecomp.sdc.common.datastructure.Wrapper; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.stereotype.Controller; -import javax.inject.Inject; -import javax.servlet.ServletContext; -import javax.servlet.http.HttpServletRequest; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.io.IOException; - /** * Web Servlet for actions on Attributes * @@ -122,18 +122,17 @@ public class AttributeServlet extends AbstractValidationsServlet { log.debug("Start handle request of {} modifier id is {} data is {}", url, userId, data); try { - Wrapper errorWrapper = new Wrapper<>(); - Wrapper attributesWrapper = new Wrapper<>(); - // convert json to AttributeDefinition + final Wrapper errorWrapper = new Wrapper<>(); + AttributeDataDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper); - buildAttributeFromString(data, attributesWrapper, errorWrapper); if (errorWrapper.isEmpty()) { AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class); - Either createAttribute = businessLogic.createAttribute(resourceId, attributesWrapper.getInnerElement(), userId); + Either createAttribute = businessLogic + .createAttribute(resourceId, attributeDataDefinition, userId); if (createAttribute.isRight()) { errorWrapper.setInnerElement(createAttribute.right().value()); } else { - attributesWrapper.setInnerElement(createAttribute.left().value()); + attributeDataDefinition = createAttribute.left().value(); } } @@ -142,10 +141,9 @@ public class AttributeServlet extends AbstractValidationsServlet { log.info("Failed to create Attribute. Reason - ", errorWrapper.getInnerElement()); response = buildErrorResponse(errorWrapper.getInnerElement()); } else { - AttributeDataDefinition createdAttDef = attributesWrapper.getInnerElement(); - log.debug("Attribute {} created successfully with id {}", createdAttDef.getName(), createdAttDef.getUniqueId()); + log.debug("Attribute {} created successfully with id {}", attributeDataDefinition.getName(), attributeDataDefinition.getUniqueId()); ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.CREATED); - response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(createdAttDef)); + response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition)); } return response; @@ -197,21 +195,16 @@ public class AttributeServlet extends AbstractValidationsServlet { log.debug("modifier id is {}", userId); try { - // convert json to PropertyDefinition - Wrapper errorWrapper = new Wrapper<>(); - Wrapper attributesWrapper = new Wrapper<>(); - // convert json to AttributeDefinition - - buildAttributeFromString(data, attributesWrapper, errorWrapper); - + final Wrapper errorWrapper = new Wrapper<>(); + AttributeDataDefinition attributeDataDefinition = convertJsonToObject(data, errorWrapper); if (errorWrapper.isEmpty()) { AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class); - Either eitherUpdateAttribute = businessLogic.updateAttribute(resourceId, attributeId, attributesWrapper.getInnerElement(), userId); - // update property + Either eitherUpdateAttribute = businessLogic + .updateAttribute(resourceId, attributeId, attributeDataDefinition, userId); if (eitherUpdateAttribute.isRight()) { errorWrapper.setInnerElement(eitherUpdateAttribute.right().value()); } else { - attributesWrapper.setInnerElement(eitherUpdateAttribute.left().value()); + attributeDataDefinition = eitherUpdateAttribute.left().value(); } } @@ -220,10 +213,9 @@ public class AttributeServlet extends AbstractValidationsServlet { log.info("Failed to update Attribute. Reason - ", errorWrapper.getInnerElement()); response = buildErrorResponse(errorWrapper.getInnerElement()); } else { - AttributeDataDefinition updatedAttribute = attributesWrapper.getInnerElement(); - log.debug("Attribute id {} updated successfully ", updatedAttribute.getUniqueId()); + log.debug("Attribute id {} updated successfully ", attributeDataDefinition.getUniqueId()); ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK); - response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(updatedAttribute)); + response = buildOkResponse(responseFormat, RepresentationUtils.toRepresentation(attributeDataDefinition)); } return response; @@ -295,7 +287,7 @@ public class AttributeServlet extends AbstractValidationsServlet { Wrapper errorWrapper) { try { Gson gson = new GsonBuilder().setPrettyPrinting().create(); - final AttributeDataDefinition attribute = gson.fromJson(data, AttributeDefinition.class); + final AttributeDataDefinition attribute = gson.fromJson(data, AttributeDataDefinition.class); if (attribute == null) { log.info(ATTRIBUTE_CONTENT_IS_INVALID, data); ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); @@ -311,4 +303,18 @@ public class AttributeServlet extends AbstractValidationsServlet { log.info(ATTRIBUTE_CONTENT_IS_INVALID, data); } } + + private AttributeDataDefinition convertJsonToObject(final String data, + final Wrapper errorWrapper) { + + final ObjectMapper mapper = new ObjectMapper(); + try { + return mapper.readValue(data, AttributeDataDefinition.class); + } catch (final IOException e) { + log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ATTRIBUTE_CONTENT_IS_INVALID, data); + ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); + errorWrapper.setInnerElement(responseFormat); + return null; + } + } } 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 08c3d18157..9eebbc50cb 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 @@ -25,6 +25,10 @@ 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; @@ -46,6 +50,7 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.ImmutableTriple; import org.apache.commons.lang3.tuple.Triple; +import org.onap.sdc.tosca.datatypes.model.AttributeDefinition; import org.onap.sdc.tosca.services.YamlUtil; import org.openecomp.sdc.be.components.impl.exceptions.SdcResourceNotFoundException; import org.openecomp.sdc.be.config.ConfigurationManager; @@ -58,7 +63,6 @@ import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterCapabilityDataDefinition; import org.openecomp.sdc.be.datatypes.elements.RequirementSubstitutionFilterPropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterDataDefinition; import org.openecomp.sdc.be.datatypes.elements.ToscaArtifactDataDefinition; @@ -123,9 +127,6 @@ import org.yaml.snakeyaml.nodes.NodeTuple; import org.yaml.snakeyaml.nodes.Tag; import org.yaml.snakeyaml.representer.Represent; import org.yaml.snakeyaml.representer.Representer; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import fj.data.Either; @org.springframework.stereotype.Component("tosca-export-handler") public class ToscaExportHandler { @@ -1079,14 +1080,28 @@ public class ToscaExportHandler { if (component instanceof Resource) { final List attributes = ((Resource) component).getAttributes(); if (CollectionUtils.isNotEmpty(attributes)) { - final Map attributeDataDefinitionMap - = attributes.stream().collect(Collectors.toMap(AttributeDataDefinition::getName, a -> a)); + final Map attributeDataDefinitionMap = new HashMap<>(); + attributes.forEach(attributeDataDefinition -> + buildAttributeData(attributeDataDefinition, attributeDataDefinitionMap)); + toscaNodeType.setAttributes(attributeDataDefinitionMap); } } return toscaNodeType; } + private void buildAttributeData(final AttributeDataDefinition originalAttributeDataDefinition, + final Map attributeDataDefinitionMap) { + + attributeDataDefinitionMap.put(originalAttributeDataDefinition.getName(), + new ObjectMapper().convertValue(new AttributeDefinition( + originalAttributeDataDefinition.getType(), + originalAttributeDataDefinition.getDescription(), + originalAttributeDataDefinition.get_default(), + originalAttributeDataDefinition.getStatus(), + originalAttributeDataDefinition.getEntry_schema()), Object.class)); + } + private Either, ToscaError> createProxyInterfaceTypes(Component container) { Map proxyInterfaceTypes = new HashMap<>(); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java index b5e5866fe4..856c430f65 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaNodeType.java @@ -25,7 +25,6 @@ import java.util.Map; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; @Getter @Setter @@ -41,6 +40,6 @@ public class ToscaNodeType { private Map capabilities; private List> requirements; - private Map attributes; + private Map attributes; } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogicTest.java index db4c699d04..0b11b2e0cf 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/AttributeBusinessLogicTest.java @@ -17,81 +17,77 @@ * limitations under the License. * ============LICENSE_END========================================================= * Modifications copyright (c) 2019 Nokia + * Modifications Copyright (C) 2020 Nordix Foundation * ================================================================================ */ + package org.openecomp.sdc.be.components.impl; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + import fj.data.Either; -import junit.framework.Assert; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.openecomp.sdc.be.components.validation.UserValidations; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao; +import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.impl.ComponentsUtils; +import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.Component; +import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; -import org.openecomp.sdc.be.model.PropertyDefinition; -import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; -import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; -import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; +import org.openecomp.sdc.be.model.operations.impl.AttributeOperation; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.exception.ResponseFormat; -import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyObject; -import static org.mockito.Mockito.when; - -import org.openecomp.sdc.be.model.DataTypeDefinition; - -public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ +public class AttributeBusinessLogicTest extends BaseBusinessLogicMock { private AttributeBusinessLogic createTestSubject() { return new AttributeBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation ); } - UserValidations userValidations = Mockito.mock(UserValidations.class); - ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class); - JanusGraphDao janusGraphDao = Mockito.mock(JanusGraphDao.class); - ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); - ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class); - PropertyOperation propertyOperation = Mockito.mock(PropertyOperation.class); - Field baseBusinessLogic; - Field baseBusinessLogic1; - Field baseBusinessLogic2; - Field baseBusinessLogic3; - AttributeBusinessLogic attributeBusinessLogic=createTestSubject(); - IGraphLockOperation igraphLockOperation = Mockito.mock(IGraphLockOperation.class); + private UserValidations userValidations = Mockito.mock(UserValidations.class); + private ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class); + private JanusGraphDao janusGraphDao = Mockito.mock(JanusGraphDao.class); + private ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class); + private ApplicationDataTypeCache applicationDataTypeCache = Mockito.mock(ApplicationDataTypeCache.class); + private AttributeOperation attributeOperation = Mockito.mock(AttributeOperation.class); + private Field baseBusinessLogic; + private AttributeBusinessLogic attributeBusinessLogic=createTestSubject(); + private IGraphLockOperation igraphLockOperation = Mockito.mock(IGraphLockOperation.class); @Before - public void setup() throws Exception{ - + public void setup() throws Exception { baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("graphLockOperation"); baseBusinessLogic.setAccessible(true); baseBusinessLogic.set(attributeBusinessLogic, igraphLockOperation); - baseBusinessLogic1 = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("userValidations"); + final Field baseBusinessLogic1 = attributeBusinessLogic.getClass().getSuperclass() + .getDeclaredField("userValidations"); baseBusinessLogic1.setAccessible(true); baseBusinessLogic1.set(attributeBusinessLogic, userValidations); - baseBusinessLogic2 = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("componentsUtils"); + final Field baseBusinessLogic2 = attributeBusinessLogic.getClass().getSuperclass() + .getDeclaredField("componentsUtils"); baseBusinessLogic2.setAccessible(true); baseBusinessLogic2.set(attributeBusinessLogic, componentsUtils); - baseBusinessLogic3 = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao"); + final Field baseBusinessLogic3 = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("janusGraphDao"); baseBusinessLogic3.setAccessible(true); baseBusinessLogic3.set(attributeBusinessLogic, janusGraphDao); @@ -103,85 +99,20 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ baseBusinessLogic.setAccessible(true); baseBusinessLogic.set(attributeBusinessLogic, applicationDataTypeCache); - baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("propertyOperation"); + baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("attributeOperation"); baseBusinessLogic.setAccessible(true); - baseBusinessLogic.set(attributeBusinessLogic, propertyOperation); - } - - @Test - public void testCreateAttribute() throws Exception { - AttributeBusinessLogic testSubject; - String resourceId = ""; - PropertyDefinition newAttributeDef = null; - String userId = ""; - Either result; - - // default test - testSubject = createTestSubject(); - } - - @Test - public void testIsAttributeExist() throws Exception { - AttributeBusinessLogic testSubject;List attributes = null; - String resourceUid = ""; - String propertyName = ""; - boolean result; - - // test 1 - testSubject=createTestSubject();attributes = null; - } - - @Test - public void testGetAttribute() throws Exception { - AttributeBusinessLogic testSubject; - String resourceId = ""; - String attributeId = ""; - String userId = ""; - Either result; - - // default test - testSubject = createTestSubject(); - } - - @Test - public void testUpdateAttribute() throws Exception { - AttributeBusinessLogic testSubject; - String resourceId = ""; - String attributeId = ""; - PropertyDefinition newAttDef = null; - String userId = ""; - Either result; - - // default test - testSubject = createTestSubject(); + baseBusinessLogic.set(attributeBusinessLogic, attributeOperation); } @Test - public void testDeleteAttribute() throws Exception { - AttributeBusinessLogic testSubject; - String resourceId = ""; - String attributeId = ""; - String userId = ""; - Either result; - - // default test - testSubject = createTestSubject(); - } - - @Test - public void createAttribute_lockfail() throws Exception { + public void createAttribute_lockfail() { Either response; - AttributeDataDefinition prop= new AttributeDataDefinition(); - - response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01"); - - Assert.assertEquals(true,response.isRight()); - + response = attributeBusinessLogic.createAttribute("RES01", new AttributeDataDefinition(), "USR01"); + assertTrue(response.isRight()); } @Test - public void createAttribute_Success() throws Exception { - + public void createAttribute_Success() { Component resource = new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(false); @@ -192,32 +123,29 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ when(igraphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK); - //Either toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY); Either toscastatus = Either.left(resource); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); AttributeDataDefinition attributeDataDefinition = new AttributeDataDefinition(); Either either = Either.left(attributeDataDefinition); - when(toscaOperationFacade.addAttributeOfResource(anyObject(), anyObject())).thenReturn(either); + when(toscaOperationFacade.addAttributeOfResource(any(), any())).thenReturn(either); - when(propertyOperation.isPropertyTypeValid(anyObject())).thenReturn(true); + when(attributeOperation.isAttributeTypeValid(any())).thenReturn(true); Map data = new HashMap<>(); data.put("ONE", new DataTypeDefinition()); Either, JanusGraphOperationStatus> allDataTypes = Either.left(data); when(applicationDataTypeCache.getAll()).thenReturn(allDataTypes); - when(propertyOperation.isPropertyDefaultValueValid(anyObject(), anyObject())).thenReturn(true); + when(attributeOperation.isAttributeDefaultValueValid(any(), any())).thenReturn(true); Either response; response = attributeBusinessLogic.createAttribute("RES01", attrib, "USR01"); - Assert.assertEquals(true, response.isLeft()); - + assertTrue(response.isLeft()); } @Test - public void createAttribute_failtogettoscaelement() throws Exception { - + public void createAttribute_failtogettoscaelement() throws NoSuchFieldException, IllegalAccessException { Component resource= new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(false); @@ -231,19 +159,16 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ baseBusinessLogic = attributeBusinessLogic.getClass().getSuperclass().getDeclaredField("toscaOperationFacade"); baseBusinessLogic.setAccessible(true); baseBusinessLogic.set(attributeBusinessLogic, toscaOperationFacade); - //Either toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY); Either toscastatus=Either.right(StorageOperationStatus.GENERAL_ERROR); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01"); - Assert.assertEquals(true,response.isRight()); - + assertTrue(response.isRight()); } @Test - public void createAttribute_componentvalidationfails() throws Exception { - + public void createAttribute_componentvalidationfails() { Component resource= new Resource(); resource.setLifecycleState(LifecycleStateEnum.CERTIFIED); resource.setIsDeleted(false); @@ -254,47 +179,42 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ Either response; AttributeDataDefinition prop= new AttributeDataDefinition(); - //Either toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY); Either toscastatus=Either.left(resource); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); response = attributeBusinessLogic.createAttribute("RES01", prop, "USR01"); - Assert.assertEquals(true,response.isRight()); - + assertTrue(response.isRight()); } @Test - public void createAttribute_componentalreadyexist_fails() throws Exception { - + public void createAttribute_componentalreadyexist_fails() { Either response; AttributeDefinition attrib = new AttributeDefinition(); attrib.setName("RES01"); - attrib.setParentUniqueId("RES01"); + attrib.setOwnerId("RES01"); List attributes = new ArrayList<>(); attributes.add(attrib); - Component resource = new Resource(); + final Resource resource = new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(false); resource.setLastUpdaterUserId("USR01"); - ((Resource) resource).setAttributes(attributes); + resource.setAttributes(attributes); when(igraphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK); - //Either toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY); Either toscastatus = Either.left(resource); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); response = attributeBusinessLogic.createAttribute("RES01", attrib, "USR01"); - Assert.assertEquals(true, response.isRight()); - + assertTrue(response.isRight()); } @Test - public void createAttribute_addresourcetostoragefails() throws Exception { + public void createAttribute_addresourcetostoragefails() { Component resource = new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); @@ -304,33 +224,32 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ IGraphLockOperation igraphLockOperation = Mockito.mock(IGraphLockOperation.class); when(igraphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK); - //Either toscastatus=Either.right(StorageOperationStatus.INVALID_PROPERTY); Either toscastatus = Either.left(resource); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); AttributeDataDefinition attributeDataDefinition = new AttributeDataDefinition(); Either either = Either .right(StorageOperationStatus.CONNECTION_FAILURE); - when(toscaOperationFacade.addAttributeOfResource(anyObject(), anyObject())).thenReturn(either); + when(toscaOperationFacade.addAttributeOfResource(any(),any())).thenReturn(either); - when(propertyOperation.isPropertyTypeValid(anyObject())).thenReturn(true); + when(attributeOperation.isAttributeTypeValid(any())).thenReturn(true); Map data = new HashMap<>(); data.put("ONE", new DataTypeDefinition()); Either, JanusGraphOperationStatus> allDataTypes = Either.left(data); when(applicationDataTypeCache.getAll()).thenReturn(allDataTypes); - when(propertyOperation.isPropertyDefaultValueValid(anyObject(), anyObject())).thenReturn(true); + when(attributeOperation.isAttributeDefaultValueValid(any(),any())).thenReturn(true); Either response; AttributeDataDefinition attrib = new AttributeDefinition(); response = attributeBusinessLogic.createAttribute("RES01", attrib, "USR01"); - Assert.assertEquals(true, response.isRight()); + assertTrue( response.isRight()); } @Test - public void testgetAttribute_ATTRIBUTE_NOT_FOUND() throws Exception { + public void testgetAttribute_ATTRIBUTE_NOT_FOUND() { Either result; Component resource= new Resource(); @@ -341,88 +260,79 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); result=attributeBusinessLogic.getAttribute("RES01","ATTR01", "USR01"); - Assert.assertEquals(true,result.isRight()); + assertTrue(result.isRight()); } @Test - public void testgetAttribute_success() throws Exception { + public void testgetAttribute_success() { Either result; - Component resource = new Resource(); + final Resource resource = new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(false); resource.setLastUpdaterUserId("USR01"); AttributeDefinition attrib = new AttributeDefinition(); attrib.setUniqueId("ATTR01"); - attrib.setParentUniqueId("RES01"); + attrib.setOwnerId("RES01"); List attr = new ArrayList<>(); attr.add(attrib); - ((Resource) resource).setAttributes(attr); + resource.setAttributes(attr); Either toscastatus = Either.left(resource); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); result = attributeBusinessLogic.getAttribute("RES01", "ATTR01", "USR01"); - Assert.assertEquals(true, result.isLeft()); + assertTrue( result.isLeft()); } @Test - public void testgetAttribute_RESOURCE_NOT_FOUND() throws Exception { + public void testgetAttribute_RESOURCE_NOT_FOUND() { Either result; Either toscastatus=Either.right(StorageOperationStatus.PARENT_RESOURCE_NOT_FOUND); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); result=attributeBusinessLogic.getAttribute("RES01","ATTR01", "USR01"); - Assert.assertEquals(true,result.isRight()); + assertTrue(result.isRight()); } @Test - public void testdeleteAttribute_FAILED_TO_LOCK_COMPONENT() throws Exception { + public void testdeleteAttribute_FAILED_TO_LOCK_COMPONENT() { Either result; result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01"); - Assert.assertEquals(true,result.isRight()); + assertTrue(result.isRight()); } @Test - public void testdeleteAttribute_get_RESOURCE_from_DB_failed() throws Exception { + public void testdeleteAttribute_get_RESOURCE_from_DB_failed() { Either result; Either toscastatus=Either.right(StorageOperationStatus.CONNECTION_FAILURE); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01"); - Assert.assertEquals(true,result.isRight()); + assertTrue(result.isRight()); } @Test - public void testdeleteAttribute_get_RESOURCE_verification_failed() throws Exception { + public void testdeleteAttribute_get_RESOURCE_verification_failed() { Either result; - - Component resource= new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(true); resource.setLastUpdaterUserId("USR01"); - - when(igraphLockOperation.lockComponent(any(),any())).thenReturn(StorageOperationStatus.OK); - - - - Either toscastatus=Either.left(resource); when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); - result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01"); - Assert.assertEquals(true,result.isRight()); + assertTrue(result.isRight()); } @Test - public void testdeleteAttribute_nonexistingresource() throws Exception { + public void testdeleteAttribute_nonexistingresource() { Either result; @@ -437,15 +347,13 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus); result=attributeBusinessLogic.deleteAttribute("RES01","ATTR01", "USR01"); - Assert.assertEquals(true,result.isRight()); + assertTrue(result.isRight()); } @Test - public void testdeleteAttribute_success() throws Exception { + public void testdeleteAttribute_success() { Either result; - - - Component resource= new Resource(); + final Resource resource = new Resource(); resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT); resource.setIsDeleted(false); resource.setLastUpdaterUserId("USR01"); @@ -458,13 +366,13 @@ public class AttributeBusinessLogicTest extends BaseBusinessLogicMock{ AttributeDefinition attrib = new AttributeDefinition(); attrib.setUniqueId("ATTR01"); - attrib.setParentUniqueId("RES01"); + attrib.setOwnerId("RES01"); List attributes = new ArrayList<>(); attributes.add(attrib); - ((Resource) resource).setAttributes(attributes); + resource.setAttributes(attributes); result = attributeBusinessLogic.deleteAttribute("RES01", "ATTR01", "USR01"); - Assert.assertEquals(true, result.isLeft()); + assertTrue( result.isLeft()); } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java index 3783c5e1fd..87875d8f20 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ImportUtilsTest.java @@ -20,12 +20,27 @@ package org.openecomp.sdc.be.components.impl; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + import com.google.common.collect.Lists; import fj.data.Either; +import java.io.IOException; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import org.junit.Test; import org.mockito.Mockito; import org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum; import org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum; +import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.model.AttributeDefinition; @@ -33,28 +48,12 @@ import org.openecomp.sdc.be.model.HeatParameterDefinition; import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.PropertyConstraint; import org.openecomp.sdc.be.model.PropertyDefinition; -import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition; import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations; import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.common.api.ArtifactTypeEnum; import org.yaml.snakeyaml.Yaml; -import java.io.IOException; -import java.nio.file.FileSystems; -import java.nio.file.Files; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - public class ImportUtilsTest { @@ -303,17 +302,6 @@ public class ImportUtilsTest { } - @Test - public void testGetAttributesFromYml() throws IOException { - - Map toscaJson = (Map) loadJsonFromFile("importToscaWithAttribute.yml"); - Either, ResultStatusEnum> actualAttributes = ImportUtils.getAttributes(toscaJson); - assertTrue(actualAttributes.isLeft()); - Map> expectedAttributes = getElements(toscaJson, TypeUtils.ToscaTagNamesEnum.ATTRIBUTES); - compareAttributes(expectedAttributes, actualAttributes.left().value()); - - } - @Test public void testGetPropertiesFromYml() throws IOException { -- cgit 1.2.3-korg