From 0aa493c70aebd63244beeddf0ef22147196c5489 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Tue, 2 Feb 2021 19:18:22 +0000 Subject: Improve import and export VFC TOSCA attributes Improve the import and export VFC TOSCA attributes, addressing the following concerns: - brings the import and export logic very close to the properties logic, as they are very similar structures - fix import/export default values for complex types and list of simple or complex types - fix export of unnecessary/empty attribute entries - creation of attributes during the initialization (base types) - inheritance of parent attributes Change-Id: Ic733e3455fc256595b5c2b1f48e19a13be27b5cc Issue-ID: SDC-3466 Signed-off-by: andre.schmid --- .../sdc/be/components/impl/ImportUtils.java | 50 ++++-- .../be/components/impl/ResourceImportManager.java | 77 +++++---- .../org/openecomp/sdc/be/impl/ComponentsUtils.java | 3 + .../sdc/be/servlets/AttributeServlet.java | 24 +-- .../openecomp/sdc/be/tosca/AttributeConverter.java | 51 ++++-- .../openecomp/sdc/be/tosca/PropertyConvertor.java | 10 +- .../openecomp/sdc/be/tosca/ToscaExportHandler.java | 72 ++++++--- .../tosca/exception/ToscaConversionException.java | 27 ++++ .../openecomp/sdc/be/tosca/model/EntrySchema.java | 43 ----- .../sdc/be/tosca/model/ToscaAttribute.java | 38 +++++ .../sdc/be/tosca/model/ToscaNodeType.java | 2 +- .../sdc/be/tosca/model/ToscaProperty.java | 2 +- .../sdc/be/tosca/model/ToscaSchemaDefinition.java | 31 ++++ .../sdc/be/tosca/utils/OutputConverter.java | 7 +- .../impl/ComponentInstanceBusinessLogicTest.java | 2 - .../sdc/be/tosca/AttributeConverterTest.java | 173 +++++++++++++++++++++ .../sdc/be/tosca/model/EntrySchemaTest.java | 75 --------- .../sdc/be/tosca/model/ToscaAttributeTest.java | 62 ++++++++ .../sdc/be/tosca/model/ToscaPropertyTest.java | 163 ------------------- 19 files changed, 509 insertions(+), 403 deletions(-) create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/exception/ToscaConversionException.java delete mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/EntrySchema.java create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaSchemaDefinition.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java delete mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/EntrySchemaTest.java create mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaAttributeTest.java delete mode 100644 catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyTest.java (limited to 'catalog-be/src') 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 614b261b4a..ed26eade32 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 @@ -43,15 +43,14 @@ import java.util.function.Consumer; import java.util.function.Function; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringEscapeUtils; +import org.onap.sdc.tosca.datatypes.model.EntrySchema; 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.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; -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; @@ -182,7 +181,8 @@ public final class ImportUtils { } public enum ResultStatusEnum { - ELEMENT_NOT_FOUND, GENERAL_ERROR, OK, INVALID_PROPERTY_DEFAULT_VALUE, INVALID_PROPERTY_TYPE, INVALID_PROPERTY_VALUE, MISSING_ENTRY_SCHEMA_TYPE, INVALID_PROPERTY_NAME + ELEMENT_NOT_FOUND, GENERAL_ERROR, OK, INVALID_PROPERTY_DEFAULT_VALUE, INVALID_PROPERTY_TYPE, + INVALID_PROPERTY_VALUE, MISSING_ENTRY_SCHEMA_TYPE, INVALID_PROPERTY_NAME, INVALID_ATTRIBUTE_NAME } public enum ToscaElementTypeEnum { @@ -422,7 +422,7 @@ public final class ImportUtils { setFieldBoolean(propertyValue, TypeUtils.ToscaTagNamesEnum.IS_PASSWORD, pass -> propertyDef.setPassword(Boolean.parseBoolean(pass))); setField(propertyValue, TypeUtils.ToscaTagNamesEnum.STATUS, propertyDef::setStatus); - setScheme(propertyValue, propertyDef); + setSchema(propertyValue, propertyDef); setPropertyConstraints(propertyValue, propertyDef); return propertyDef; @@ -506,7 +506,7 @@ public final class ImportUtils { setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.HIDDEN, hidden -> inputDef.setHidden(Boolean.parseBoolean(hidden))); setFieldBoolean(inputValue, TypeUtils.ToscaTagNamesEnum.IMMUTABLE, immutable -> inputDef.setImmutable(Boolean.parseBoolean(immutable))); - setScheme(inputValue, inputDef); + setSchema(inputValue, inputDef); setPropertyConstraints(inputValue, inputDef); return inputDef; } @@ -542,20 +542,27 @@ public final class ImportUtils { setJsonStringField(attributeMap, TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE, attributeDef.getType(), attributeDef::set_default); - setScheme(attributeMap, attributeDef); + setEntrySchema(attributeMap, attributeDef); return attributeDef; } - private static void setScheme(Map propertyValue, - ToscaDataDefinition toscaDataDefinition) { - Either schemaElementRes = findSchemaElement(propertyValue); + private static void setSchema(final Map propertyValue, + final PropertyDefinition propertyDefinition) { + final Either schemaElementRes = findEntrySchemaElement(propertyValue); if (schemaElementRes.isLeft()) { - SchemaDefinition schemaDef = getSchema(schemaElementRes.left().value()); - toscaDataDefinition.setSchema(schemaDef); + propertyDefinition.setSchema(getSchema(schemaElementRes.left().value())); } } - private static Either findSchemaElement(Map propertyValue) { + private static void setEntrySchema(final Map toscaJsonMap, + final AttributeDefinition attributeDefinition) { + final Either schemaElementRes = findEntrySchemaElement(toscaJsonMap); + if (schemaElementRes.isLeft()) { + attributeDefinition.setEntry_schema(createEntrySchema(schemaElementRes.left().value())); + } + } + + private static Either findEntrySchemaElement(final Map propertyValue) { return findToscaElement(propertyValue, TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA, ToscaElementTypeEnum.ALL); } @@ -573,6 +580,18 @@ public final class ImportUtils { return schema; } + private static EntrySchema createEntrySchema(final Object toscaEntrySchemaObj) { + final EntrySchema entrySchema = new EntrySchema(); + if (toscaEntrySchemaObj instanceof String) { + entrySchema.setType((String) toscaEntrySchemaObj); + } else if (toscaEntrySchemaObj instanceof Map) { + final PropertyDefinition schemeProperty = createModuleProperty((Map) toscaEntrySchemaObj); + entrySchema.setType(schemeProperty.getType()); + entrySchema.setDescription(schemeProperty.getDescription()); + } + return entrySchema; + } + private static void setField(Map toscaJson, TypeUtils.ToscaTagNamesEnum tagName, Consumer setter) { Either fieldStringValue = findFirstToscaStringElement(toscaJson, tagName); @@ -600,6 +619,13 @@ public final class ImportUtils { } + public static Either, ResultStatusEnum> getAttributes(final Map toscaJson) { + final Function elementGenByName = ImportUtils::createAttribute; + final Function, AttributeDefinition> func = ImportUtils::createModuleAttribute; + + return getElements(toscaJson, ToscaTagNamesEnum.ATTRIBUTES, elementGenByName, func); + } + public static Either, ResultStatusEnum> getInputs(Map toscaJson, AnnotationTypeOperations annotationTypeOperations) { Function elementGenByName = ImportUtils::createInputs; 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 7e5e1b9568..383d7cda95 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 @@ -353,6 +353,7 @@ public class ResourceImportManager { } setCapabilities(toscaJson, resource, parentResource); setProperties(toscaJson, resource); + setAttributes(toscaJson, resource); setRequirements(toscaJson, resource, parentResource); setInterfaceLifecycle(toscaJson, resource); } else { @@ -537,6 +538,42 @@ public class ResourceImportManager { } } + private void setAttributes(final Map originalToscaJsonMap, final Resource resource) { + final Map toscaJsonMap = new HashMap<>(originalToscaJsonMap); + ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities"); + final Either, ResultStatusEnum> getAttributeEither = ImportUtils + .getAttributes(toscaJsonMap); + + if (getAttributeEither.isRight()) { + final ResultStatusEnum resultStatus = getAttributeEither.right().value(); + if (resultStatus == ResultStatusEnum.ELEMENT_NOT_FOUND) { + return; + } + throw new ByActionStatusComponentException( + componentsUtils.convertFromResultStatusEnum(resultStatus, JsonPresentationFields.ATTRIBUTES)); + } + + final List attributeDefinitionList = new ArrayList<>(); + final Map attributeMap = getAttributeEither.left().value(); + if (MapUtils.isEmpty(attributeMap)) { + return; + } + for (final Entry entry : attributeMap.entrySet()) { + final String name = entry.getKey(); + if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { + log.debug("Detected attribute with invalid name '{}' during resource '{}' import. ", + name, resource.getName()); + throw new ByActionStatusComponentException(componentsUtils + .convertFromResultStatusEnum(ResultStatusEnum.INVALID_ATTRIBUTE_NAME, + JsonPresentationFields.ATTRIBUTES)); + } + final AttributeDefinition attributeDefinition = entry.getValue(); + attributeDefinition.setName(name); + attributeDefinitionList.add(attributeDefinition); + } + resource.setAttributes(attributeDefinitionList); + } + private Resource setDerivedFrom(Map toscaJson, Resource resource) { Either toscaDerivedFromElement = ImportUtils .findFirstToscaStringElement(toscaJson, TypeUtils.ToscaTagNamesEnum.DERIVED_FROM); @@ -831,52 +868,12 @@ public class ResourceImportManager { final String payloadData = resourceMetaData.getPayloadData(); if (payloadData != null) { resource.setToscaVersion(getToscaVersion(payloadData)); - resource.setAttributes(getAttributes(payloadData)); } final List categories = resourceMetaData.getCategories(); calculateResourceIsAbstract(resource, categories); } - private List getAttributes(final String payloadData) { - final Map mappedToscaTemplate = decodePayload(payloadData); - - final List attributeDataDefinitionList = new ArrayList<>(); - - final Either, ResultStatusEnum> firstToscaMapElement = ImportUtils - .findFirstToscaMapElement(mappedToscaTemplate, ToscaTagNamesEnum.ATTRIBUTES); - if (firstToscaMapElement.isRight()) { - return attributeDataDefinitionList; - } - final Map attributes = firstToscaMapElement.left().value(); - - final Iterator> propertiesNameValue = attributes.entrySet().iterator(); - while (propertiesNameValue.hasNext()) { - final Entry attributeNameValue = propertiesNameValue.next(); - final Object value = attributeNameValue.getValue(); - final String key = attributeNameValue.getKey(); - if (value instanceof Map) { - - final Map attributeMap = (Map) value; - - final AttributeDefinition attributeDefinition = new AttributeDefinition(); - 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::setSchema); - attributeDataDefinitionList.add(attributeDefinition); - } else { - final AttributeDefinition attributeDefinition = new AttributeDefinition(); - attributeDefinition.setName(key); - attributeDataDefinitionList.add(attributeDefinition); - } - } - return attributeDataDefinitionList; - } - private Map decodePayload(final String payloadData) { final String decodedPayload = new String(Base64.decodeBase64(payloadData)); return (Map) new Yaml().load(decodedPayload); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java index 8f1f2c1c21..98b3c84081 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/impl/ComponentsUtils.java @@ -1384,6 +1384,9 @@ public class ComponentsUtils { case MISSING_ENTRY_SCHEMA_TYPE: responseEnum = ActionStatus.INVALID_PROPERTY; break; + case INVALID_ATTRIBUTE_NAME: + responseEnum = ActionStatus.INVALID_ATTRIBUTE; + break; default: responseEnum = ActionStatus.GENERAL_ERROR; break; 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 a1500a4204..227627304a 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 @@ -130,7 +130,8 @@ public class AttributeServlet extends AbstractValidationsServlet { if (errorWrapper.isEmpty()) { AttributeBusinessLogic businessLogic = getClassFromWebAppContext(context, () -> AttributeBusinessLogic.class); - Either createAttribute = businessLogic.createAttribute(resourceId, attributeDataDefinition, userId); + Either createAttribute = businessLogic. + createAttribute(resourceId, attributeDataDefinition, userId); if (createAttribute.isRight()) { errorWrapper.setInnerElement(createAttribute.right().value()); } else { @@ -283,27 +284,6 @@ public class AttributeServlet extends AbstractValidationsServlet { } } - private void buildAttributeFromString(String data, Wrapper attributesWrapper, - Wrapper errorWrapper) { - try { - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - 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); - errorWrapper.setInnerElement(responseFormat); - } else { - attributesWrapper.setInnerElement(attribute); - } - - } catch (Exception e) { - ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT); - errorWrapper.setInnerElement(responseFormat); - log.debug(ATTRIBUTE_CONTENT_IS_INVALID, data, e); - log.info(ATTRIBUTE_CONTENT_IS_INVALID, data); - } - } - private AttributeDefinition convertJsonToObject(final String data, final Wrapper errorWrapper) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java index 085c075add..a0f8d8b44f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/AttributeConverter.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.tosca; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import com.google.gson.stream.JsonReader; import java.io.StringReader; @@ -32,7 +33,10 @@ import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter; +import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.ToscaAttribute; +import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; import org.springframework.beans.factory.config.BeanDefinition; @@ -61,15 +65,16 @@ public class AttributeConverter { } /** - * Converts and {@link AttributeDefinition} to a {@link ToscaAttribute}. + * Converts an {@link AttributeDefinition} to a {@link ToscaAttribute}. * * @param attributeDefinition the attribute definition to be converted * @return the {@link ToscaAttribute} instance based on the the given {@link AttributeDefinition} instance */ - public ToscaAttribute convert(final AttributeDefinition attributeDefinition) { + public ToscaAttribute convert(final AttributeDefinition attributeDefinition) throws ToscaConversionException { final ToscaAttribute toscaAttribute = new ToscaAttribute(); LOGGER.trace("Converting attribute '{}' from type '{}' with default value '{}'", attributeDefinition.getName(), attributeDefinition.getType(), attributeDefinition.getDefaultValue()); + toscaAttribute.setEntrySchema(convert(attributeDefinition.getEntry_schema())); toscaAttribute.setType(attributeDefinition.getType()); toscaAttribute.setDescription(attributeDefinition.getDescription()); toscaAttribute.setStatus(attributeDefinition.getStatus()); @@ -87,8 +92,19 @@ public class AttributeConverter { return toscaAttribute; } + private ToscaSchemaDefinition convert(final EntrySchema entrySchema) { + if (entrySchema == null) { + return null; + } + + final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition(); + toscaSchemaDefinition.setType(entrySchema.getType()); + toscaSchemaDefinition.setDescription(entrySchema.getDescription()); + return toscaSchemaDefinition; + } + private Object convertToToscaObject(final String name, final String attributeType, String value, - final EntrySchema schemaDefinition, final boolean preserveEmptyValue) { + final EntrySchema schemaDefinition, final boolean preserveEmptyValue) throws ToscaConversionException { final String innerType = schemaDefinition == null ? attributeType : schemaDefinition.getType(); LOGGER.trace("Converting attribute '{}' of type '{}', value '{}', innerType '{}'", name, attributeType, value, innerType); @@ -114,12 +130,10 @@ public class AttributeConverter { isScalar = ToscaPropertyType.getTypeIfScalar(predefinedType.getType()) != null; } final JsonElement valueAsJson = parseToJson(value); - if (valueAsJson.isJsonObject()) { - final JsonObject jsonObj = valueAsJson.getAsJsonObject(); - if (jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_ATTRIBUTE.getFunctionName())) { - return ToscaMapValueConverter.getInstance().handleComplexJsonValue(valueAsJson); - } + if (isValueGetAttribute(valueAsJson)) { + return ToscaMapValueConverter.getInstance().handleComplexJsonValue(valueAsJson.getAsJsonObject()); } + //if it has a converter if (predefinedType != null && predefinedType.getValueConverter() != null) { LOGGER.trace("It's well defined type. convert it"); @@ -134,9 +148,16 @@ public class AttributeConverter { return toscaMapValueConverter.convertDataTypeToToscaObject( innerType, dataTypes, null, false, valueAsJson, preserveEmptyValue); + } catch (final JsonParseException e) { + final String errorMsg = "Failed to parse json value"; + LOGGER.error(EcompLoggerErrorCode.SCHEMA_ERROR, "Attribute Converter", + errorMsg, e); + throw new ToscaConversionException(errorMsg, e); } catch (final Exception e) { - LOGGER.debug("convertToToscaValue failed to parse json value :", e); - return null; + final String errorMsg = "Unexpected error occurred while converting attribute value to TOSCA"; + LOGGER.error(EcompLoggerErrorCode.UNKNOWN_ERROR, "Attribute Converter", + errorMsg, e); + throw new ToscaConversionException(errorMsg, e); } } @@ -148,8 +169,16 @@ public class AttributeConverter { return new JsonParser().parse(jsonReader); } + private boolean isValueGetAttribute(final JsonElement valueAsJson) { + if (!valueAsJson.isJsonObject()) { + return false; + } + final JsonObject jsonObj = valueAsJson.getAsJsonObject(); + return jsonObj.entrySet().size() == 1 && jsonObj.has(ToscaFunctions.GET_ATTRIBUTE.getFunctionName()); + } + private String getTypeDefaultValue(final String attributeType) { return DataTypePropertyConverter.getInstance().getDataTypePropertiesDefaultValuesRec(attributeType, dataTypes); } -} \ No newline at end of file +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java index 4c4f19bb87..59b859dae2 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/PropertyConvertor.java @@ -44,7 +44,7 @@ import org.openecomp.sdc.be.model.tosca.converters.DataTypePropertyConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaMapValueConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter; import org.openecomp.sdc.be.model.tosca.converters.ToscaValueConverter; -import org.openecomp.sdc.be.tosca.model.EntrySchema; +import org.openecomp.sdc.be.tosca.model.ToscaSchemaDefinition; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; import org.openecomp.sdc.common.log.wrappers.Logger; @@ -86,10 +86,10 @@ public class PropertyConvertor { log.trace("try to convert property {} from type {} with default value [{}]", property.getName(), property.getType(), property.getDefaultValue()); SchemaDefinition schema = property.getSchema(); if (schema != null && schema.getProperty() != null && schema.getProperty().getType() != null && !schema.getProperty().getType().isEmpty()) { - EntrySchema eschema = new EntrySchema(); - eschema.setType(schema.getProperty().getType()); - eschema.setDescription(schema.getProperty().getDescription()); - prop.setEntry_schema(eschema); + final ToscaSchemaDefinition toscaSchemaDefinition = new ToscaSchemaDefinition(); + toscaSchemaDefinition.setType(schema.getProperty().getType()); + toscaSchemaDefinition.setDescription(schema.getProperty().getDescription()); + prop.setEntry_schema(toscaSchemaDefinition); } String defaultValue = property.getDefaultValue(); if(Objects.isNull(defaultValue)) { 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 0459b5d1c6..1f0270fee9 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 @@ -28,9 +28,7 @@ import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_ATTRIBUTE; import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_INPUT; import static org.openecomp.sdc.tosca.datatypes.ToscaFunctions.GET_PROPERTY; -import com.fasterxml.jackson.databind.ObjectMapper; import fj.data.Either; -import java.beans.IntrospectionException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -102,9 +100,11 @@ import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation; import org.openecomp.sdc.be.tosca.PropertyConvertor.PropertyType; import org.openecomp.sdc.be.tosca.builder.ToscaRelationshipBuilder; +import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.CapabilityFilter; import org.openecomp.sdc.be.tosca.model.NodeFilter; import org.openecomp.sdc.be.tosca.model.SubstitutionMapping; +import org.openecomp.sdc.be.tosca.model.ToscaAttribute; import org.openecomp.sdc.be.tosca.model.ToscaCapability; import org.openecomp.sdc.be.tosca.model.ToscaDataType; import org.openecomp.sdc.be.tosca.model.ToscaGroupTemplate; @@ -122,6 +122,7 @@ import org.openecomp.sdc.be.tosca.model.ToscaTopolgyTemplate; import org.openecomp.sdc.be.tosca.utils.ForwardingPathToscaUtil; import org.openecomp.sdc.be.tosca.utils.InputConverter; import org.openecomp.sdc.be.tosca.utils.OutputConverter; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.yaml.snakeyaml.DumperOptions; @@ -324,7 +325,15 @@ public class ToscaExportHandler { topologyTemplate.setInputs(inputs); } - final Map outputs = outputConverter.convert(component.getOutputs(), dataTypes); + final Map outputs; + try { + outputs = outputConverter.convert(component.getOutputs(), dataTypes); + } catch (final ToscaConversionException e) { + log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaExportHandler.class.getName(), + "Could not parse component '{}' outputs. Component unique id '{}'.", + new Object[]{component.getName(), component.getUniqueId(), e}); + return Either.right(ToscaError.GENERAL_ERROR); + } if (!outputs.isEmpty()) { topologyTemplate.setOutputs(outputs); } @@ -748,6 +757,18 @@ public class ToscaExportHandler { .addInterfaceDefinitionElement(component, toscaNodeType, dataTypes, isAssociatedComponent); addInputsToProperties(dataTypes, inputDef, mergedProperties); + final Map toscaAttributeMap; + try { + toscaAttributeMap = convertToToscaAttributes(component.getAttributes(), dataTypes); + } catch (final ToscaConversionException e) { + log.error(EcompLoggerErrorCode.SCHEMA_ERROR, ToscaExportHandler.class.getName(), + "Could not parse component '{}' attributes. Component unique id '{}'.", + new Object[]{component.getName(), component.getUniqueId(), e}); + return Either.right(ToscaError.GENERAL_ERROR); + } + if (!toscaAttributeMap.isEmpty()) { + toscaNodeType.setAttributes(toscaAttributeMap); + } if (CollectionUtils.isNotEmpty(component.getProperties())) { List properties = component.getProperties(); Map convertedProperties = properties.stream() @@ -790,6 +811,19 @@ public class ToscaExportHandler { return convertReqCapAndTypeName(componentsCache, component, toscaNode, nodeTypes, toscaNodeType, dataTypes); } + private Map convertToToscaAttributes(final List attributeList, + final Map dataTypes) throws ToscaConversionException { + if (CollectionUtils.isEmpty(attributeList)) { + return Collections.emptyMap(); + } + final AttributeConverter attributeConverter = new AttributeConverter(dataTypes); + final Map toscaAttributeMap = new HashMap<>(); + for (final AttributeDefinition attributeDefinition : attributeList) { + toscaAttributeMap.put(attributeDefinition.getName(), attributeConverter.convert(attributeDefinition)); + } + return toscaAttributeMap; + } + private Either convertReqCapAndTypeName(Map componentsCache, Component component, ToscaTemplate toscaNode, Map nodeTypes, @@ -1050,31 +1084,9 @@ public class ToscaExportHandler { : NATIVE_ROOT; toscaNodeType.setDerived_from(derivedFrom); } - if (component instanceof Resource) { - final List attributes = ((Resource) component).getAttributes(); - if (CollectionUtils.isNotEmpty(attributes)) { - final Map attributeDataDefinitionMap = new HashMap<>(); - attributes.forEach(attributeDataDefinition -> - buildAttributeData(attributeDataDefinition, attributeDataDefinitionMap)); - - toscaNodeType.setAttributes(attributeDataDefinitionMap); - } - } return toscaNodeType; } - private void buildAttributeData(final AttributeDefinition originalAttributeDefinition, - final Map attributeDataDefinitionMap) { - - attributeDataDefinitionMap.put(originalAttributeDefinition.getName(), - new ObjectMapper().convertValue(new org.onap.sdc.tosca.datatypes.model.AttributeDefinition( - originalAttributeDefinition.getType(), - originalAttributeDefinition.getDescription(), - originalAttributeDefinition.get_default(), - originalAttributeDefinition.getStatus(), - originalAttributeDefinition.getEntry_schema()), Object.class)); - } - private Either, ToscaError> createProxyInterfaceTypes(Component container) { Map proxyInterfaceTypes = new HashMap<>(); @@ -1679,12 +1691,22 @@ public class ToscaExportHandler { CustomRepresenter() { super(); this.representers.put(ToscaPropertyAssignment.class, new RepresentToscaPropertyAssignment()); + this.representers.put(ToscaAttribute.class, new RepresentToscaAttribute()); // null representer is exceptional and it is stored as an instance // variable. this.nullRepresenter = new RepresentNull(); } + private class RepresentToscaAttribute implements Represent { + + @Override + public Node representData(Object data) { + final ToscaAttribute toscaAttribute = (ToscaAttribute) data; + return represent(toscaAttribute.asToscaMap()); + } + } + private class RepresentToscaPropertyAssignment implements Represent { public Node representData(Object data) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/exception/ToscaConversionException.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/exception/ToscaConversionException.java new file mode 100644 index 0000000000..2d37a35a96 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/exception/ToscaConversionException.java @@ -0,0 +1,27 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.exception; + +public class ToscaConversionException extends Exception { + + public ToscaConversionException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/EntrySchema.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/EntrySchema.java deleted file mode 100644 index f1a7dcb9e5..0000000000 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/EntrySchema.java +++ /dev/null @@ -1,43 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.sdc.be.tosca.model; - -public class EntrySchema { - private String type; - private String description; - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java index 891347d191..ea56e60a17 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaAttribute.java @@ -19,9 +19,12 @@ package org.openecomp.sdc.be.tosca.model; +import java.util.LinkedHashMap; +import java.util.Map; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; /** * Represents a TOSCA Attribute Definition (see TOSCA 1.3, Section 3.6.12 Attribute definition) @@ -42,6 +45,12 @@ public class ToscaAttribute { private String description; @Getter @Setter + private ToscaSchemaDefinition keySchema; + @Getter + @Setter + private ToscaSchemaDefinition entrySchema; + @Getter + @Setter private String status; public Object getDefault() { @@ -52,4 +61,33 @@ public class ToscaAttribute { this.defaultValue = defaultValue; } + /** + * Converts this object to Map, ignoring null fields. The entries of the map are based on the TOSCA Attribute Definition. + * + * @return a Map representing the TOSCA Attribute Definition. + */ + public Map asToscaMap() { + final Map toscaAttributeAsMap = new LinkedHashMap<>(); + if (getType() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.TYPE.getElementName(), getType()); + } + if (getDescription() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.DESCRIPTION.getElementName(), getDescription()); + } + if (getKeySchema() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.KEY_SCHEMA.getElementName(), getKeySchema()); + } + if (getEntrySchema() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.ENTRY_SCHEMA.getElementName(), getEntrySchema()); + } + if (getDefault() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.DEFAULT.getElementName(), getDefault()); + } + if (getStatus() != null) { + toscaAttributeAsMap.put(ToscaTagNamesEnum.STATUS.getElementName(), getStatus()); + } + + return toscaAttributeAsMap; + } + } 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 856c430f65..c43486fab8 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 @@ -40,6 +40,6 @@ public class ToscaNodeType { private Map capabilities; private List> requirements; - private Map attributes; + private Map attributes; } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java index 7ebb2cb00d..fb5c544511 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaProperty.java @@ -44,7 +44,7 @@ public class ToscaProperty { private Boolean required; @Getter @Setter - private EntrySchema entry_schema; + private ToscaSchemaDefinition entry_schema; @Getter @Setter private List constraints; diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaSchemaDefinition.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaSchemaDefinition.java new file mode 100644 index 0000000000..d02e9da6bd --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaSchemaDefinition.java @@ -0,0 +1,31 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class ToscaSchemaDefinition { + private String type; + private String description; +} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java index 85400a747a..1e7fc81a3b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/utils/OutputConverter.java @@ -27,6 +27,7 @@ import org.apache.commons.collections.CollectionUtils; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.OutputDefinition; import org.openecomp.sdc.be.tosca.AttributeConverter; +import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; import org.openecomp.sdc.be.tosca.model.ToscaAttribute; import org.openecomp.sdc.be.tosca.model.ToscaOutput; import org.openecomp.sdc.be.tosca.model.ToscaProperty; @@ -45,17 +46,17 @@ public class OutputConverter { } public Map convert(final List outputDefinitionList, - final Map dataTypes) { + final Map dataTypes) throws ToscaConversionException { final AttributeConverter attributeConverter = this.attributeConverterProvider.getObject(dataTypes); final Map outputMap = new HashMap<>(); if (CollectionUtils.isEmpty(outputDefinitionList)) { return Collections.emptyMap(); } - outputDefinitionList.forEach(outputDefinition -> { + for (final OutputDefinition outputDefinition : outputDefinitionList) { final ToscaAttribute toscaAttribute = attributeConverter.convert(outputDefinition); final ToscaProperty toscaProperty = new ToscaOutput(toscaAttribute); outputMap.put(outputDefinition.getName(), toscaProperty); - }); + } return outputMap; } } diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java index 456074fbba..891d2c83ca 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogicTest.java @@ -1265,8 +1265,6 @@ class ComponentInstanceBusinessLogicTest { ComponentInstanceAttribute attribute = new ComponentInstanceAttribute(); attribute.setType("string"); attribute.setUniqueId("testCreateOrUpdateAttributeValueForCopyPaste"); - SchemaDefinition def = Mockito.mock(SchemaDefinition.class); - attribute.setSchema(def); LifecycleStateEnum oldLifeCycleState = service.getLifecycleState(); String oldLastUpdatedUserId = service.getLastUpdaterUserId(); service.setLastUpdaterUserId(USER_ID); diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java new file mode 100644 index 0000000000..8292568d8e --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/AttributeConverterTest.java @@ -0,0 +1,173 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.junit.jupiter.api.Test; +import org.onap.sdc.tosca.datatypes.model.EntrySchema; +import org.openecomp.sdc.be.model.AttributeDefinition; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; +import org.openecomp.sdc.be.tosca.exception.ToscaConversionException; +import org.openecomp.sdc.be.tosca.model.ToscaAttribute; + +class AttributeConverterTest { + + @Test + void testScalarTypeConversion() throws ToscaConversionException { + //given + final AttributeConverter attributeConverter = new AttributeConverter(Collections.emptyMap()); + final AttributeDefinition attributeDefinition = new AttributeDefinition(); + attributeDefinition.setType(ToscaPropertyType.STRING.getType()); + attributeDefinition.setDescription("aDescription"); + attributeDefinition.setStatus("aStatus"); + attributeDefinition.setDefaultValue("aDefaultValue"); + //when + final ToscaAttribute actualToscaAttribute = attributeConverter.convert(attributeDefinition); + //then + assertAttribute(attributeDefinition, actualToscaAttribute); + } + + @Test + void testScalarNoDefaultValueConversion() throws ToscaConversionException { + //given + final AttributeConverter attributeConverter = new AttributeConverter(Collections.emptyMap()); + final AttributeDefinition attributeDefinition = new AttributeDefinition(); + attributeDefinition.setType(ToscaPropertyType.STRING.getType()); + attributeDefinition.setDescription("aDescription"); + attributeDefinition.setStatus("aStatus"); + //when + final ToscaAttribute actualToscaAttribute = attributeConverter.convert(attributeDefinition); + //then + assertAttribute(attributeDefinition, null, actualToscaAttribute); + } + + @Test + void testListTypeConversion() throws JsonProcessingException, ToscaConversionException { + //given + final Map dataTypes = new HashMap<>(); + final AttributeConverter attributeConverter = new AttributeConverter(dataTypes); + final AttributeDefinition attributeDefinition = new AttributeDefinition(); + attributeDefinition.setType(ToscaPropertyType.LIST.getType()); + attributeDefinition.setDescription("aDescription"); + attributeDefinition.setStatus("aStatus"); + final EntrySchema entrySchema = new EntrySchema(); + entrySchema.setDescription("anEntrySchemaDescription"); + entrySchema.setType(ToscaPropertyType.LIST.getType()); + attributeDefinition.setEntry_schema(entrySchema); + final List defaultValueList = new ArrayList<>(); + defaultValueList.add("entry1"); + defaultValueList.add("entry2"); + defaultValueList.add("entry2"); + attributeDefinition.setDefaultValue(parseToJsonString(defaultValueList)); + //when + final ToscaAttribute actualToscaAttribute = attributeConverter.convert(attributeDefinition); + //then + assertAttribute(attributeDefinition, defaultValueList, actualToscaAttribute); + } + + @Test + void testDataTypeTypeConversion() throws JsonProcessingException, ToscaConversionException { + //given + final Map dataTypes = new HashMap<>(); + final DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); + final List propertyDefinitionList = new ArrayList<>(); + final PropertyDefinition integerProperty = new PropertyDefinition(); + integerProperty.setName("anIntegerProperty"); + integerProperty.setType(ToscaPropertyType.INTEGER.getType()); + propertyDefinitionList.add(integerProperty); + final PropertyDefinition stringProperty = new PropertyDefinition(); + stringProperty.setName("aStringProperty"); + stringProperty.setType(ToscaPropertyType.STRING.getType()); + propertyDefinitionList.add(stringProperty); + dataTypeDefinition.setProperties(propertyDefinitionList); + final String myDataType = "com.data.type.my"; + dataTypeDefinition.setType(myDataType); + dataTypes.put(myDataType, dataTypeDefinition); + + final AttributeConverter attributeConverter = new AttributeConverter(dataTypes); + final AttributeDefinition attributeDefinition = new AttributeDefinition(); + attributeDefinition.setType(myDataType); + attributeDefinition.setDescription("aDescription"); + attributeDefinition.setStatus("aStatus"); + final Map defaultValueMap = new HashMap<>(); + defaultValueMap.put("aStringProperty", "aStringPropertyValue"); + defaultValueMap.put("anIntegerProperty", 0); + final String defaultValue = parseToJsonString(defaultValueMap); + attributeDefinition.setDefaultValue(defaultValue); + //when + final ToscaAttribute actualToscaAttribute = attributeConverter.convert(attributeDefinition); + //then + assertAttribute(attributeDefinition, defaultValueMap, actualToscaAttribute); + } + + + @Test + void testInvalidDefaultValueJsonConversion() { + //given + final AttributeConverter attributeConverter = new AttributeConverter(Collections.emptyMap()); + final AttributeDefinition attributeDefinition = new AttributeDefinition(); + attributeDefinition.setDefaultValue(": thisIsAnInvalidJson"); + //when, then + final ToscaConversionException toscaConversionException = assertThrows(ToscaConversionException.class, + () -> attributeConverter.convert(attributeDefinition)); + assertEquals("Failed to parse json value", toscaConversionException.getMessage()); + } + + private void assertAttribute(final AttributeDefinition expectedAttributeDefinition, + final ToscaAttribute actualToscaAttribute) { + assertAttribute(expectedAttributeDefinition, expectedAttributeDefinition.get_default(), actualToscaAttribute); + } + + private void assertAttribute(final AttributeDefinition expectedAttributeDefinition, final Object expectedDefault, + final ToscaAttribute actualToscaAttribute) { + assertEquals(expectedAttributeDefinition.getType(), actualToscaAttribute.getType()); + assertEquals(expectedAttributeDefinition.getDescription(), actualToscaAttribute.getDescription()); + assertEquals(expectedAttributeDefinition.getStatus(), actualToscaAttribute.getStatus()); + if (expectedAttributeDefinition.getEntry_schema() == null) { + assertNull(actualToscaAttribute.getEntrySchema()); + } else { + assertEquals(expectedAttributeDefinition.getEntry_schema().getType(), + actualToscaAttribute.getEntrySchema().getType()); + assertEquals( + expectedAttributeDefinition.getEntry_schema().getDescription(), + actualToscaAttribute.getEntrySchema().getDescription()); + } + assertEquals(expectedDefault, actualToscaAttribute.getDefault()); + } + + private String parseToJsonString(final Object value) throws JsonProcessingException { + final ObjectMapper objectMapper = new ObjectMapper(); + return objectMapper.writeValueAsString(value); + } + +} \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/EntrySchemaTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/EntrySchemaTest.java deleted file mode 100644 index dc29602bcb..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/EntrySchemaTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.sdc.be.tosca.model; - -import org.junit.Test; - - -public class EntrySchemaTest { - - private EntrySchema createTestSubject() { - return new EntrySchema(); - } - - - @Test - public void testGetType() throws Exception { - EntrySchema testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getType(); - } - - - @Test - public void testSetType() throws Exception { - EntrySchema testSubject; - String type = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setType(type); - } - - - @Test - public void testGetDescription() throws Exception { - EntrySchema testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDescription(); - } - - - @Test - public void testSetDescription() throws Exception { - EntrySchema testSubject; - String description = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setDescription(description); - } -} diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaAttributeTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaAttributeTest.java new file mode 100644 index 0000000000..24738653fc --- /dev/null +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaAttributeTest.java @@ -0,0 +1,62 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.model; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.KEY_SCHEMA; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.STATUS; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE; + +import java.util.Map; +import org.junit.jupiter.api.Test; + +class ToscaAttributeTest { + + @Test + void testMapConversion() { + final ToscaAttribute toscaAttribute = new ToscaAttribute(); + assertToscaAttributeMap(toscaAttribute); + + toscaAttribute.setType("type"); + toscaAttribute.setDescription("description"); + toscaAttribute.setKeySchema(new ToscaSchemaDefinition()); + toscaAttribute.setEntrySchema(new ToscaSchemaDefinition()); + toscaAttribute.setDefault("default"); + toscaAttribute.setStatus("status"); + + assertToscaAttributeMap(toscaAttribute); + } + + private void assertToscaAttributeMap(final ToscaAttribute toscaAttribute) { + final Map toscaAttributeAsMap = toscaAttribute.asToscaMap(); + assertEquals(toscaAttribute.getType(), toscaAttributeAsMap.get(TYPE.getElementName())); + assertEquals(toscaAttribute.getDescription(), + toscaAttributeAsMap.get(DESCRIPTION.getElementName())); + assertEquals(toscaAttribute.getKeySchema(), + toscaAttributeAsMap.get(KEY_SCHEMA.getElementName())); + assertEquals(toscaAttribute.getEntrySchema(), + toscaAttributeAsMap.get(ENTRY_SCHEMA.getElementName())); + assertEquals(toscaAttribute.getDefault(), toscaAttributeAsMap.get(DEFAULT.getElementName())); + assertEquals(toscaAttribute.getStatus(), toscaAttributeAsMap.get(STATUS.getElementName())); + } +} \ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyTest.java deleted file mode 100644 index 19b5dd397c..0000000000 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyTest.java +++ /dev/null @@ -1,163 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * SDC - * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * ============LICENSE_END========================================================= - */ - -package org.openecomp.sdc.be.tosca.model; - -import org.junit.Test; - - -public class ToscaPropertyTest { - - private ToscaProperty createTestSubject() { - return new ToscaProperty(); - } - - - @Test - public void testGetEntry_schema() throws Exception { - ToscaProperty testSubject; - EntrySchema result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getEntry_schema(); - } - - - @Test - public void testSetEntry_schema() throws Exception { - ToscaProperty testSubject; - EntrySchema entry_schema = null; - - // default test - testSubject = createTestSubject(); - testSubject.setEntry_schema(entry_schema); - } - - - @Test - public void testGetType() throws Exception { - ToscaProperty testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getType(); - } - - - @Test - public void testSetType() throws Exception { - ToscaProperty testSubject; - String type = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setType(type); - } - - - @Test - public void testGetDefaultp() throws Exception { - ToscaProperty testSubject; - Object result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDefaultp(); - } - - - @Test - public void testSetDefaultp() throws Exception { - ToscaProperty testSubject; - Object defaultp = null; - - // default test - testSubject = createTestSubject(); - testSubject.setDefaultp(defaultp); - } - - - @Test - public void testGetDescription() throws Exception { - ToscaProperty testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getDescription(); - } - - - @Test - public void testSetDescription() throws Exception { - ToscaProperty testSubject; - String description = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setDescription(description); - } - - - @Test - public void testGetRequired() throws Exception { - ToscaProperty testSubject; - Boolean result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getRequired(); - } - - - @Test - public void testSetRequired() throws Exception { - ToscaProperty testSubject; - Boolean required = null; - - // default test - testSubject = createTestSubject(); - testSubject.setRequired(required); - } - - - @Test - public void testGetStatus() throws Exception { - ToscaProperty testSubject; - String result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getStatus(); - } - - - @Test - public void testSetStatus() throws Exception { - ToscaProperty testSubject; - String status = ""; - - // default test - testSubject = createTestSubject(); - testSubject.setStatus(status); - } -} -- cgit 1.2.3-korg