diff options
author | aribeiro <anderson.ribeiro@est.tech> | 2021-07-15 09:18:37 +0100 |
---|---|---|
committer | aribeiro <anderson.ribeiro@est.tech> | 2021-08-06 17:22:20 +0100 |
commit | 47bcc63a9daff1f310125fed006f27c93656fa83 (patch) | |
tree | 3c61670b039c0e63d14880f909e6047d54776492 /catalog-model/src/test | |
parent | 0ed125f98ae8ea9e80c855a640c328654660d081 (diff) |
Retrieve data types based on component model
Issue-ID: SDC-3660
Signed-off-by: aribeiro <anderson.ribeiro@est.tech>
Change-Id: I09c533eb39277532b29e581e4dd57e9df952e8e6
Diffstat (limited to 'catalog-model/src/test')
3 files changed, 223 insertions, 35 deletions
diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java index 9126b64659..75a47db634 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCacheTest.java @@ -49,6 +49,7 @@ import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition; import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.operations.impl.DataTypeOperation; import org.openecomp.sdc.be.model.operations.impl.PropertyOperation; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.springframework.context.ApplicationEventPublisher; @@ -59,6 +60,9 @@ class ApplicationDataTypeCacheTest { private PropertyOperation propertyOperation; @Mock + private DataTypeOperation dataTypeOperation; + + @Mock private ApplicationEventPublisher applicationEventPublisher; @InjectMocks @@ -150,18 +154,19 @@ class ApplicationDataTypeCacheTest { final DataTypeDefinition testDataType1 = createDataTypeDefinition("test.data.type1", "test.data.type1", 101L, 1000L); final DataTypeDefinition testDataType2 = createDataTypeDefinition("test.data.type2", "test.data.type2", 101L, 1002L); - final Map<String, DataTypeDefinition> modifiedDataTypeDefinitionMap = - Map.of(testDataType1.getName(), testDataType1, testDataType2.getName(), testDataType2); + final Map<String, DataTypeDefinition> dataTypeDefinitionMap = Map.of(testDataType1.getName(), testDataType1, testDataType2.getName(), testDataType2); + final Map<String, Map<String, DataTypeDefinition>> modifiedDataTypeDefinitionMap = new HashMap<>(); + modifiedDataTypeDefinitionMap.put(null, dataTypeDefinitionMap); when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(modifiedDataTypeDefinitionMap)); final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", "test.data.type1", 101L, 101L); final DataTypeData dataTypeData2 = createDataTypeData("test.data.type2", "test.data.type2", 101L, 1002L); - when(propertyOperation.getAllDataTypeNodes()).thenReturn(Either.left(List.of(dataTypeData1, dataTypeData2))); + when(dataTypeOperation.getAllDataTypeNodes()).thenReturn(List.of(dataTypeData1, dataTypeData2)); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) == 0); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); - assertDataTypeCache(modifiedDataTypeDefinitionMap); + assertDataTypeCache(modifiedDataTypeDefinitionMap.get(null)); } @Test @@ -172,17 +177,19 @@ class ApplicationDataTypeCacheTest { await().until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); assertDataTypeCache(dataTypeDefinitionMap); + final Map<String, Map<String, DataTypeDefinition>> dataTypesMappedByModel = new HashMap<>(); final Map<String, DataTypeDefinition> modifiedDataTypeDefinitionMap = new HashMap<>(); final DataTypeDefinition testDataType1 = createDataTypeDefinition("test.data.type1", "test.data.type1", 1L, 1L); modifiedDataTypeDefinitionMap.put(testDataType1.getName(), testDataType1); final DataTypeDefinition testDataType3 = createDataTypeDefinition("test.data.type3", "test.data.type3", 1L, 1L); modifiedDataTypeDefinitionMap.put(testDataType3.getName(), testDataType3); - when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(modifiedDataTypeDefinitionMap)); + dataTypesMappedByModel.put(null, modifiedDataTypeDefinitionMap); + when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypesMappedByModel)); final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", "test.data.type1", 1L, 1L); final DataTypeData dataTypeData3 = createDataTypeData("test.data.type3", "test.data.type3", 1L, 1L); - when(propertyOperation.getAllDataTypeNodes()).thenReturn(Either.left(List.of(dataTypeData1, dataTypeData3))); + when(dataTypeOperation.getAllDataTypeNodes()).thenReturn(List.of(dataTypeData1, dataTypeData3)); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) == 0); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); @@ -191,9 +198,9 @@ class ApplicationDataTypeCacheTest { @Test void testGetAllWithNoInitialization() { - final Map<String, DataTypeDefinition> dataTypeDefinitionMap = new HashMap<>(); + final Map<String, Map<String, DataTypeDefinition>> dataTypeDefinitionMap = new HashMap<>(); when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypeDefinitionMap)); - final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> response = applicationDataTypeCache.getAll(); + final Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> response = applicationDataTypeCache.getAll(null); assertNotNull(response); assertTrue(response.isLeft()); } @@ -202,7 +209,7 @@ class ApplicationDataTypeCacheTest { void testGetWhenCacheIsEmpty() { var dataTypeDefinition = new DataTypeDefinition(); when(propertyOperation.getDataTypeByUid("uniqueId")).thenReturn(Either.left(dataTypeDefinition)); - final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = applicationDataTypeCache.get("uniqueId"); + final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = applicationDataTypeCache.get(null, "uniqueId"); assertNotNull(dataTypeEither); assertTrue(dataTypeEither.isLeft()); assertEquals(dataTypeDefinition, dataTypeEither.left().value()); @@ -213,7 +220,8 @@ class ApplicationDataTypeCacheTest { defaultInit(); final ScheduledFuture<?> scheduledFuture = applicationDataTypeCache.getScheduledFuture(); await().atMost(Duration.ofSeconds(schedulerPollIntervalInSec + 1)).until(() -> scheduledFuture.getDelay(TimeUnit.SECONDS) != 0); - final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = applicationDataTypeCache.get("test.data.type1"); + final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeEither = + applicationDataTypeCache.get(null, "test.data.type1"); assertNotNull(dataTypeEither); assertTrue(dataTypeEither.isLeft()); final DataTypeDefinition actualDataTypeDefinition = dataTypeEither.left().value(); @@ -236,12 +244,14 @@ class ApplicationDataTypeCacheTest { dataTypeDefinitionMap.put(testDataType1.getName(), testDataType1); final DataTypeDefinition testDataType2 = createDataTypeDefinition("test.data.type2", "test.data.type2", 101L, 1001L); dataTypeDefinitionMap.put(testDataType2.getName(), testDataType2); - when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypeDefinitionMap)); + final Map<String, Map<String, DataTypeDefinition>> dataTypesMappedByModel = new HashMap<>(); + dataTypesMappedByModel.put(null, dataTypeDefinitionMap); + when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypesMappedByModel)); final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", testDataType1.getName(), 100L, 1000L); final DataTypeData dataTypeData2 = createDataTypeData("test.data.type2", testDataType2.getName(), 101L, 1001L); - when(propertyOperation.getAllDataTypeNodes()).thenReturn(Either.left(List.of(dataTypeData1, dataTypeData2))); + when(dataTypeOperation.getAllDataTypeNodes()).thenReturn(List.of(dataTypeData1, dataTypeData2)); applicationDataTypeCache.init(); } @@ -286,7 +296,7 @@ class ApplicationDataTypeCacheTest { } public void assertDataTypeCache(final Map<String, DataTypeDefinition> expectedDataTypeCache) { - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheMapEither = applicationDataTypeCache.getAll(); + Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypeCacheMapEither = applicationDataTypeCache.getAll(null); assertNotNull(dataTypeCacheMapEither); assertTrue(dataTypeCacheMapEither.isLeft()); final Map<String, DataTypeDefinition> actualDataTypeMap = dataTypeCacheMapEither.left().value(); diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java new file mode 100644 index 0000000000..015aedc6a3 --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java @@ -0,0 +1,174 @@ +/* + * ============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.model.operations.impl; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import fj.data.Either; +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.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; +import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.Model; +import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache; +import org.openecomp.sdc.be.resources.data.DataTypeData; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration("classpath:application-context-test.xml") +class DataTypeOperationTest { + + @InjectMocks + private DataTypeOperation dataTypeOperation; + @Mock + private ModelOperation modelOperation; + @Mock + private HealingJanusGraphGenericDao janusGraphGenericDao; + @Mock + private ApplicationDataTypeCache applicationDataTypeCache; + + private final String modelName = "ETSI-SDC-MODEL-TEST"; + private final List<DataTypeData> dataTypesWithoutModel = new ArrayList<>(); + private final List<DataTypeData> dataTypesWithModel = new ArrayList<>(); + final Map<String, Map<String, DataTypeDefinition>> dataTypesMappedByModel = new HashMap<>(); + final Map<String, DataTypeDefinition> allDataTypesFoundDefinitionMap = new HashMap<>(); + private Model model; + + + @BeforeEach + void beforeEachInit() { + MockitoAnnotations.openMocks(this); + initTestData(); + } + + @Test + void getAllDataTypeNodesTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.left(dataTypesWithoutModel)); + when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model)); + when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class)) + .thenReturn(Either.left(dataTypesWithModel)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + assertThat(dataTypesFound.size()).isEqualTo(4); + assertThat(dataTypesFound.containsAll(dataTypesWithoutModel)).isTrue(); + assertThat(dataTypesFound.containsAll(dataTypesWithModel)).isTrue(); + } + + @Test + void getAllDataTypesWithModelTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.left(Collections.emptyList())); + when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model)); + when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class)) + .thenReturn(Either.left(dataTypesWithModel)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + assertThat(dataTypesFound.size()).isEqualTo(2); + assertThat(dataTypesFound.containsAll(dataTypesWithModel)).isTrue(); + assertThat(dataTypesFound.containsAll(dataTypesWithoutModel)).isFalse(); + } + + @Test + void getAllDataTypeNodesWithValidationErrorTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + assertThat(dataTypesFound.isEmpty()).isTrue(); + } + + @Test + void getAllDataTypesWithModelWithValidationErrorTest() { + when(janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class)) + .thenReturn(Either.left(Collections.emptyList())); + when(modelOperation.findAllModels()).thenReturn(Collections.singletonList(model)); + when(janusGraphGenericDao.getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class)) + .thenReturn(Either.right(JanusGraphOperationStatus.GENERAL_ERROR)); + final var dataTypesFound = dataTypeOperation.getAllDataTypeNodes(); + assertThat(dataTypesFound).isEmpty(); + } + + @Test + void mapDataTypesDefinitionByModelTest() { + final var allDataTypesMappedByModel = + dataTypeOperation.mapDataTypesDefinitionByModel(allDataTypesFoundDefinitionMap); + assertThat(allDataTypesMappedByModel.get(modelName).size()).isEqualTo(2); + assertThat(allDataTypesMappedByModel.get(null)).isNotEmpty(); + } + + + private void initTestData() { + model = new Model(modelName); + final String TEST_DATA_TYPE_001 = "test.data.type001"; + final String TEST_DATA_TYPE_002 = "test.data.type002"; + final String TEST_DATA_TYPE_003 = "test.data.type003"; + final String TEST_DATA_TYPE_004 = "test.data.type004"; + final DataTypeData dataTypeData1 = createDataTypeData("test.data.type1", TEST_DATA_TYPE_001, 101L, + 101L, null); + final DataTypeData dataTypeData2 = createDataTypeData("test.data.type2", TEST_DATA_TYPE_002, 101L, + 1002L, null); + dataTypesWithoutModel.add(dataTypeData1); + dataTypesWithoutModel.add(dataTypeData2); + + final DataTypeData dataTypeWithModel1 = createDataTypeData("test.data.type1", TEST_DATA_TYPE_003, 101L, + 101L, modelName); + final DataTypeData dataTypeWithModel2 = createDataTypeData("test.data.type2", TEST_DATA_TYPE_004, 101L, + 1002L, modelName); + dataTypesWithModel.add(dataTypeWithModel1); + dataTypesWithModel.add(dataTypeWithModel2); + + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_001, createDataTypeDefinition("test.data.type1", TEST_DATA_TYPE_001, + 101L, 101L, null)); + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_002, createDataTypeDefinition("test.data.type2", TEST_DATA_TYPE_002, + 101L, 101L, null)); + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_003, createDataTypeDefinition("test.data.type1", TEST_DATA_TYPE_003, + 101L, 101L, modelName)); + allDataTypesFoundDefinitionMap.put(TEST_DATA_TYPE_004, createDataTypeDefinition("test.data.type2", TEST_DATA_TYPE_004, + 101L, 101L, modelName)); + + dataTypesMappedByModel.put(null, allDataTypesFoundDefinitionMap); + } + + private DataTypeData createDataTypeData(final String name, final String uniqueId, final long creationTime, final long modificationTime, + final String model) { + final DataTypeData dataTypeData = new DataTypeData(); + dataTypeData.setDataTypeDataDefinition(createDataTypeDefinition(name, uniqueId, creationTime, modificationTime, model)); + return dataTypeData; + } + + private DataTypeDefinition createDataTypeDefinition(final String name, final String uniqueId, final long creationTime, + final long modificationTime, String model) { + final DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(); + dataTypeDefinition.setName(name); + dataTypeDefinition.setUniqueId(uniqueId); + dataTypeDefinition.setCreationTime(creationTime); + dataTypeDefinition.setModificationTime(modificationTime); + dataTypeDefinition.setModel(model); + return dataTypeDefinition; + } + +} diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java index 6812b44b41..793af71bbd 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperationTest.java @@ -20,14 +20,27 @@ package org.openecomp.sdc.be.model.operations.impl; -import org.janusgraph.core.JanusGraphVertex; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; + 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.List; +import java.util.Map; +import java.util.Set; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.janusgraph.core.JanusGraphVertex; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; -import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge; import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation; import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient; @@ -38,7 +51,12 @@ import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyRule; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.*; +import org.openecomp.sdc.be.model.ComponentInstanceProperty; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.IComplexDefaultValue; +import org.openecomp.sdc.be.model.ModelTestBase; +import org.openecomp.sdc.be.model.PropertyConstraint; +import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.model.tosca.ToscaType; @@ -48,16 +66,14 @@ import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.be.resources.data.PropertyData; import org.openecomp.sdc.be.resources.data.PropertyValueData; -import java.util.*; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; public class PropertyOperationTest extends ModelTestBase { HealingJanusGraphGenericDao janusGraphGenericDao = mock(HealingJanusGraphGenericDao.class); - PropertyOperation propertyOperation = new PropertyOperation(janusGraphGenericDao, null); + final DataTypeOperation dataTypeOperation = mock(DataTypeOperation.class); + + PropertyOperation propertyOperation = new PropertyOperation(janusGraphGenericDao, null, dataTypeOperation); @Before public void setup() { @@ -441,7 +457,7 @@ public class PropertyOperationTest extends ModelTestBase { } private PropertyOperation createTestSubject() { - return new PropertyOperation(new HealingJanusGraphGenericDao(new JanusGraphClient()), null); + return new PropertyOperation(new HealingJanusGraphGenericDao(new JanusGraphClient()), null, dataTypeOperation); } @Test @@ -937,7 +953,7 @@ public class PropertyOperationTest extends ModelTestBase { @Test public void testGetAllDataTypes() throws Exception { PropertyOperation testSubject; - Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> result; + Either<Map<String, Map<String, DataTypeDefinition>>, JanusGraphOperationStatus> result; // default test testSubject = createTestSubject(); @@ -956,18 +972,6 @@ public class PropertyOperationTest extends ModelTestBase { result = testSubject.checkInnerType(propDataDef); } - - @Test - public void testGetAllDataTypeNodes() throws Exception { - PropertyOperation testSubject; - Either<List<DataTypeData>, JanusGraphOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAllDataTypeNodes(); - } - - @Test public void testValidateAndUpdatePropertyValue() throws Exception { PropertyOperation testSubject; |