diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-02-02 19:18:22 +0000 |
---|---|---|
committer | Christophe Closset <christophe.closset@intl.att.com> | 2021-02-25 12:20:17 +0000 |
commit | 0aa493c70aebd63244beeddf0ef22147196c5489 (patch) | |
tree | 6557463275234f84d05d40c89c1bcfc7134298fc /catalog-be/src/test/java/org | |
parent | 70f93dfdd2b3a7c682b341223b6f63af43feb280 (diff) |
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 <andre.schmid@est.tech>
Diffstat (limited to 'catalog-be/src/test/java/org')
5 files changed, 235 insertions, 240 deletions
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<String, DataTypeDefinition> 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<String> 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<String, DataTypeDefinition> dataTypes = new HashMap<>(); + final DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); + final List<PropertyDefinition> 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<String, Object> 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<String, Object> 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); - } -} |