From 47bcc63a9daff1f310125fed006f27c93656fa83 Mon Sep 17 00:00:00 2001 From: aribeiro Date: Thu, 15 Jul 2021 09:18:37 +0100 Subject: Retrieve data types based on component model Issue-ID: SDC-3660 Signed-off-by: aribeiro Change-Id: I09c533eb39277532b29e581e4dd57e9df952e8e6 --- .../openecomp/sdc/be/model/DataTypeDefinition.java | 1 - .../sdc/be/model/cache/ApplicationCache.java | 5 +- .../be/model/cache/ApplicationDataTypeCache.java | 89 ++++++----- .../impl/ComponentInstanceOperation.java | 5 +- .../model/operations/impl/DataTypeOperation.java | 126 +++++++++++++++ .../operations/impl/GroupInstanceOperation.java | 8 +- .../be/model/operations/impl/GroupOperation.java | 12 +- .../model/operations/impl/PropertyOperation.java | 84 +++++----- .../model/cache/ApplicationDataTypeCacheTest.java | 36 +++-- .../operations/impl/DataTypeOperationTest.java | 174 +++++++++++++++++++++ .../operations/impl/PropertyOperationTest.java | 48 +++--- 11 files changed, 459 insertions(+), 129 deletions(-) create mode 100644 catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperationTest.java (limited to 'catalog-model') diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java index 01f6cd1b40..bf9f4d79a6 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java @@ -36,7 +36,6 @@ public class DataTypeDefinition extends DataTypeDataDefinition { private DataTypeDefinition derivedFrom; private List constraints; private List properties; - private String model; public DataTypeDefinition(final DataTypeDataDefinition dataTypeDataDefinition) { super(dataTypeDataDefinition); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java index d0a071909c..bfb20a2f14 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationCache.java @@ -22,10 +22,11 @@ package org.openecomp.sdc.be.model.cache; import fj.data.Either; import java.util.Map; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.model.DataTypeDefinition; public interface ApplicationCache { - Either, JanusGraphOperationStatus> getAll(); + Either, JanusGraphOperationStatus> getAll(final String model); - Either get(String uniqueId); + Either get(final String model, final String uniqueId); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java index a1bafbc6f2..070ebf72e3 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java @@ -20,6 +20,7 @@ package org.openecomp.sdc.be.model.cache; import fj.data.Either; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -43,6 +44,7 @@ import org.openecomp.sdc.be.config.Configuration.ApplicationL1CacheInfo; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; 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.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; @@ -64,13 +66,16 @@ public class ApplicationDataTypeCache implements ApplicationCache scheduledFuture = null; - private Map dataTypesCacheMap = new HashMap<>(); + private Map> dataTypesByModelCacheMap = new HashMap<>(); + private final DataTypeOperation dataTypeOperation; private int firstRunDelayInSec = 30; private int pollingIntervalInSec = 60; - public ApplicationDataTypeCache(final PropertyOperation propertyOperation, final ApplicationEventPublisher applicationEventPublisher) { + public ApplicationDataTypeCache(final PropertyOperation propertyOperation, final ApplicationEventPublisher applicationEventPublisher, + final DataTypeOperation dataTypeOperation) { this.propertyOperation = propertyOperation; this.applicationEventPublisher = applicationEventPublisher; + this.dataTypeOperation = dataTypeOperation; scheduledPollingService = Executors .newScheduledThreadPool(1, new BasicThreadFactory.Builder().namingPattern("ApplicationDataTypeCacheThread-%d").build()); } @@ -156,32 +161,34 @@ public class ApplicationDataTypeCache implements ApplicationCache, JanusGraphOperationStatus> getAllDataTypesFromGraph() { + private Either>, JanusGraphOperationStatus> getAllDataTypesFromGraph() { return propertyOperation.getAllDataTypes(); } - @Override - public Either, JanusGraphOperationStatus> getAll() { + public Either, JanusGraphOperationStatus> getAll(final String model) { try { readWriteLock.readLock().lock(); - if (MapUtils.isEmpty(dataTypesCacheMap)) { - return getAllDataTypesFromGraph(); + if (MapUtils.isEmpty(dataTypesByModelCacheMap)) { + final var dataTypesFound = getAllDataTypesFromGraph(); + if (dataTypesFound.isRight()) { + return Either.right(dataTypesFound.right().value()); + } + dataTypesByModelCacheMap = dataTypesFound.left().value(); } - return Either.left(new HashMap<>(dataTypesCacheMap)); + return Either.left(getDataTypeDefinitionMapByModel(model)); } finally { readWriteLock.readLock().unlock(); } } @Override - public Either get(String uniqueId) { + public Either get(final String model, final String uniqueId) { try { readWriteLock.readLock().lock(); - if (MapUtils.isEmpty(dataTypesCacheMap)) { + if (MapUtils.isEmpty(dataTypesByModelCacheMap)) { return propertyOperation.getDataTypeByUid(uniqueId); } - - final Optional dataTypeDefinition = dataTypesCacheMap.values().stream() + final Optional dataTypeDefinition = getDataTypeDefinitionMapByModel(model).values().stream() .filter(p -> p.getUniqueId().equals(uniqueId)).findFirst(); if (dataTypeDefinition.isEmpty()) { return propertyOperation.getDataTypeByUid(uniqueId); @@ -192,6 +199,10 @@ public class ApplicationDataTypeCache implements ApplicationCache getDataTypeDefinitionMapByModel(final String model) { + return dataTypesByModelCacheMap.containsKey(model) ? dataTypesByModelCacheMap.get(model) : new HashMap<>(); + } + @Override public void run() { try { @@ -218,10 +229,9 @@ public class ApplicationDataTypeCache implements ApplicationCache dataTypeListFromDatabase = findAllDataTypesLazy(); - final Map dataTypesCacheCopyMap = copyDataTypeCache(); - - if (dataTypeListFromDatabase.size() != dataTypesCacheCopyMap.size()) { - log.debug("Total of cached data types '{}' differs from the actual '{}'", dataTypeListFromDatabase.size(), dataTypesCacheCopyMap.size()); + final int dataTypesCacheCopyMap = dataTypesCacheMapSize(); + if (dataTypeListFromDatabase.size() != dataTypesCacheCopyMap) { + log.debug("Total of cached data types '{}' differs from the actual '{}'", dataTypeListFromDatabase.size(), dataTypesCacheCopyMap); return true; } @@ -230,13 +240,23 @@ public class ApplicationDataTypeCache implements ApplicationCache dataTypeListFromDatabase, final Map dataTypesCacheCopyMap) { + private int dataTypesCacheMapSize() { + var count = 0; + for (var i = 0; i < copyDataTypeCache().size(); i++) { + count += new ArrayList<>(copyDataTypeCache().values()).get(i).size(); + + } + return count; + } + + private boolean hasDataTypesChanged(final List dataTypeListFromDatabase, final Map> dataTypesCacheCopyMap) { return dataTypeListFromDatabase.stream().map(DataTypeData::getDataTypeDataDefinition).anyMatch(actualDataTypeDefinition -> { final String dataTypeName = actualDataTypeDefinition.getName(); - final DataTypeDefinition cachedDataTypeDefinition = dataTypesCacheCopyMap.get(dataTypeName); + final String model = actualDataTypeDefinition.getModel(); + final DataTypeDefinition cachedDataTypeDefinition = dataTypesCacheCopyMap.get(model).get(dataTypeName); if (cachedDataTypeDefinition == null) { log.debug("Datatype '{}' is not present in the cache. ", dataTypeName); return true; @@ -245,8 +265,8 @@ public class ApplicationDataTypeCache implements ApplicationCache copyDataTypeCache() { + private Map> copyDataTypeCache() { try { readWriteLock.readLock().lock(); - return new HashMap<>(this.dataTypesCacheMap); + return new HashMap<>(this.dataTypesByModelCacheMap); } finally { readWriteLock.readLock().unlock(); } } private void refreshDataTypesCache() { - final Map dataTypesDefinitionMap = findAllDataTypesEager(); + final Map> dataTypesDefinitionMap = findAllDataTypesEager(); if (dataTypesDefinitionMap.isEmpty()) { return; } try { readWriteLock.writeLock().lock(); - dataTypesCacheMap = dataTypesDefinitionMap; + dataTypesByModelCacheMap = dataTypesDefinitionMap; onDataChangeEventEmit(); BeEcompErrorManager.getInstance() .logInternalFlowError("ReplaceDataTypesCache", "Succeed to replace the data types cache", ErrorSeverity.INFO); @@ -288,10 +308,10 @@ public class ApplicationDataTypeCache implements ApplicationCache findAllDataTypesEager() { + private Map> findAllDataTypesEager() { log.trace("Fetching data types from database, eager mode"); final long startTime = System.currentTimeMillis(); - final Either, JanusGraphOperationStatus> allDataTypes = propertyOperation.getAllDataTypes(); + final Either>, JanusGraphOperationStatus> allDataTypes = propertyOperation.getAllDataTypes(); log.trace("Finish fetching data types from database. Took {}ms", (System.currentTimeMillis() - startTime)); if (allDataTypes.isRight()) { final JanusGraphOperationStatus status = allDataTypes.right().value(); @@ -306,16 +326,9 @@ public class ApplicationDataTypeCache implements ApplicationCache findAllDataTypesLazy() { log.trace("Fetching data types from database, lazy mode"); final long startTime = System.currentTimeMillis(); - final Either, JanusGraphOperationStatus> allDataTypes = propertyOperation.getAllDataTypeNodes(); + final List allDataTypes = dataTypeOperation.getAllDataTypeNodes(); log.trace("Finish fetching data types from database. Took {}ms", (System.currentTimeMillis() - startTime)); - if (allDataTypes.isRight()) { - final JanusGraphOperationStatus status = allDataTypes.right().value(); - var errorMsg= String.format("Failed to fetch data types from database. Status is %s", status); - log.error(EcompLoggerErrorCode.UNKNOWN_ERROR, ApplicationDataTypeCache.class.getName(), errorMsg); - BeEcompErrorManager.getInstance().logInternalConnectionError(APPLICATION_DATA_TYPES_CACHE, errorMsg, ErrorSeverity.ERROR); - return Collections.emptyList(); - } - return allDataTypes.left().value(); + return allDataTypes; } private void onDataChangeEventEmit() { @@ -329,9 +342,9 @@ public class ApplicationDataTypeCache implements ApplicationCache newData; + private final Map> newData; - public DataTypesCacheChangedEvent(final Object source, final Map newData) { + public DataTypesCacheChangedEvent(final Object source, final Map> newData) { super(source); this.newData = newData; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java index 1097e5d204..97dd3ea718 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ComponentInstanceOperation.java @@ -70,7 +70,7 @@ public class ComponentInstanceOperation extends AbstractOperation { @Autowired private IInputsOperation inputOperation; @Autowired - private ApplicationDataTypeCache dataTypeCache; + private ApplicationDataTypeCache applicationDataTypeCache; public ComponentInstanceOperation() { super(); @@ -408,7 +408,8 @@ public class ComponentInstanceOperation extends AbstractOperation { innerType = propDef.getType(); } log.debug("Before validateAndUpdatePropertyValue"); - Either, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either, JanusGraphOperationStatus> allDataTypes = + applicationDataTypeCache.getAll(resourceInstanceInput.getModel()); if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java new file mode 100644 index 0000000000..158cb2569b --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java @@ -0,0 +1,126 @@ +/* + * ============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 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.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.openecomp.sdc.be.config.BeEcompErrorManager; +import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; +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.resources.data.DataTypeData; +import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component("dataType-operation") +public class DataTypeOperation extends AbstractOperation { + + private static final Logger LOGGER = LoggerFactory.getLogger(DataTypeOperation.class); + + private final ModelOperation modelOperation; + + @Autowired + public DataTypeOperation(final HealingJanusGraphGenericDao janusGraphGenericDao, + final ModelOperation modelOperation) { + this.janusGraphGenericDao = janusGraphGenericDao; + this.modelOperation = modelOperation; + } + + public List getAllDataTypeNodes() { + final List dataTypesFound = new ArrayList<>(); + final Either, JanusGraphOperationStatus> getAllDataTypesWithNullModel = + janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); + + final var dataTypesValidated = validateDataType(getAllDataTypesWithNullModel, null); + if (CollectionUtils.isNotEmpty(dataTypesValidated)) { + dataTypesFound.addAll(dataTypesValidated); + } + + final List allDataTypeNodesWithModel = getAllDataTypesWithModel(); + if(CollectionUtils.isNotEmpty(allDataTypeNodesWithModel)) { + dataTypesFound.addAll(allDataTypeNodesWithModel); + } + return dataTypesFound; + } + + public List getAllDataTypesWithModel() { + final List dataTypesWithModel = new ArrayList<>(); + modelOperation.findAllModels() + .forEach(model -> { + final var modelName = model.getName(); + final Either, JanusGraphOperationStatus> getAllDataTypesByModel = janusGraphGenericDao + .getByCriteriaForModel(NodeTypeEnum.DataType, null, modelName, DataTypeData.class); + final var dataTypesValidated = validateDataType(getAllDataTypesByModel, modelName); + dataTypesWithModel.addAll(dataTypesValidated); + }); + return dataTypesWithModel; + } + + private List validateDataType(final Either, JanusGraphOperationStatus> getDataTypes, final String modelName) { + if (getDataTypes.isRight() && getDataTypes.right().value() == JanusGraphOperationStatus.NOT_FOUND) { + return Collections.emptyList(); + } + if (getDataTypes.isRight()) { + final var status = getDataTypes.right().value(); + if (LOGGER.isErrorEnabled()) { + final var errorMsg= String.format("Failed to fetch data types from database with model %s. Status is %s", modelName, status); + LOGGER.error(String.valueOf(EcompLoggerErrorCode.UNKNOWN_ERROR), DataTypeOperation.class.getName(), errorMsg); + BeEcompErrorManager.getInstance().logInternalConnectionError(DataTypeOperation.class.getName(), errorMsg, ErrorSeverity.ERROR); + } + return Collections.emptyList(); + } + return getDataTypes.left().value(); + } + + public Map> mapDataTypesDefinitionByModel(final Map allDataTypes) { + final Map dataTypesWithNullModelMap = new HashMap<>(); + final Map dataTypesWithModelMap = new HashMap<>(); + final Map> dataTypesMappedByModel = new HashMap<>(); + allDataTypes.values().stream().forEach(dataTypeDefinition -> { + final var model = dataTypeDefinition.getModel(); + final var dataTypeDefinitionName = dataTypeDefinition.getName(); + if (model == null) { + dataTypesWithNullModelMap.put(dataTypeDefinitionName, dataTypeDefinition); + } else { + dataTypesWithModelMap.put(dataTypeDefinitionName, dataTypeDefinition); + if (dataTypesMappedByModel.containsKey(model)) { + dataTypesMappedByModel.get(model).put(dataTypeDefinitionName, dataTypeDefinition); + } else { + dataTypesMappedByModel.put(model, dataTypesWithModelMap); + } + } + }); + if(MapUtils.isNotEmpty(dataTypesWithNullModelMap)) { + dataTypesMappedByModel.put(null, dataTypesWithNullModelMap); + } + return dataTypesMappedByModel; + } + +} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupInstanceOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupInstanceOperation.java index 45df37c4b8..c463af691c 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupInstanceOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupInstanceOperation.java @@ -76,7 +76,7 @@ public class GroupInstanceOperation extends AbstractOperation implements IGroupI @Autowired PropertyOperation propertyOperation; @javax.annotation.Resource - private ApplicationDataTypeCache dataTypeCache; + private ApplicationDataTypeCache applicationDataTypeCache; public Either, StorageOperationStatus> getAllGroupInstances(String parentId, NodeTypeEnum parentType) { Either, StorageOperationStatus> result = null; @@ -285,7 +285,8 @@ public class GroupInstanceOperation extends AbstractOperation implements IGroupI innerType = propDef.getType(); } log.debug("Before validateAndUpdatePropertyValue"); - Either, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either, JanusGraphOperationStatus> allDataTypes = + applicationDataTypeCache.getAll(groupInstanceProperty.getModel()); if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); BeEcompErrorManager.getInstance() @@ -388,7 +389,8 @@ public class GroupInstanceOperation extends AbstractOperation implements IGroupI innerType = propDef.getType(); } // Specific Update Logic - Either, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either, JanusGraphOperationStatus> allDataTypes = + applicationDataTypeCache.getAll(groupInstanceProerty.getModel()); if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); BeEcompErrorManager.getInstance() diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java index effdbebf4c..b9378b8eca 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/GroupOperation.java @@ -72,15 +72,15 @@ public class GroupOperation extends AbstractOperation implements IGroupOperation private final TopologyTemplateOperation topologyTemplateOperation; private final PropertyOperation propertyOperation; private final GroupTypeOperation groupTypeOperation; - private final ApplicationDataTypeCache dataTypeCache; + private final ApplicationDataTypeCache applicationDataTypeCache; public GroupOperation(JanusGraphDao janusGraphDao, TopologyTemplateOperation topologyTemplateOperation, PropertyOperation propertyOperation, - GroupTypeOperation groupTypeOperation, ApplicationDataTypeCache dataTypeCache) { + GroupTypeOperation groupTypeOperation, ApplicationDataTypeCache applicationDataTypeCache) { this.janusGraphDao = janusGraphDao; this.topologyTemplateOperation = topologyTemplateOperation; this.propertyOperation = propertyOperation; this.groupTypeOperation = groupTypeOperation; - this.dataTypeCache = dataTypeCache; + this.applicationDataTypeCache = applicationDataTypeCache; } private GroupDefinition convertGroupDataToGroupDefinition(GroupData groupData) { @@ -485,7 +485,7 @@ public class GroupOperation extends AbstractOperation implements IGroupOperation StorageOperationStatus result = null; String innerType = property.getSchema() == null ? null : property.getSchema().getProperty() == null ? null : property.getSchema().getProperty().getType(); - Either, JanusGraphOperationStatus> allDataTypes = dataTypeCache.getAll(); + Either, JanusGraphOperationStatus> allDataTypes = applicationDataTypeCache.getAll(property.getModel()); Either isValid = null; if (allDataTypes.isRight()) { JanusGraphOperationStatus status = allDataTypes.right().value(); @@ -493,8 +493,8 @@ public class GroupOperation extends AbstractOperation implements IGroupOperation result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status); } if (result == null) { - isValid = propertyOperation - .validateAndUpdatePropertyValue(property.getType(), property.getValue(), innerType, allDataTypes.left().value()); + isValid = propertyOperation.validateAndUpdatePropertyValue(property.getType(), property.getValue(), innerType, + allDataTypes.left().value()); if (isValid.isRight()) { log.debug("Failed to validate property value {}. Status is {}. ", property.getValue(), StorageOperationStatus.INVALID_PROPERTY); result = StorageOperationStatus.INVALID_PROPERTY; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java index 1f4fca0685..726efcf847 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java @@ -21,6 +21,20 @@ package org.openecomp.sdc.be.model.operations.impl; import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.google.common.collect.Maps; +import com.google.gson.JsonArray; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import fj.data.Either; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; @@ -82,6 +96,7 @@ import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint; import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter; import org.openecomp.sdc.be.resources.data.ComponentInstanceData; import org.openecomp.sdc.be.resources.data.DataTypeData; +import org.openecomp.sdc.be.resources.data.ModelData; import org.openecomp.sdc.be.resources.data.PropertyData; import org.openecomp.sdc.be.resources.data.PropertyValueData; import org.openecomp.sdc.be.resources.data.ResourceMetadataData; @@ -89,20 +104,6 @@ import org.openecomp.sdc.be.resources.data.UniqueIdData; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import com.fasterxml.jackson.core.ObjectCodec; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonNode; -import com.google.common.collect.Maps; -import com.google.gson.JsonArray; -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonParser; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; -import fj.data.Either; @Component("property-operation") public class PropertyOperation extends AbstractOperation implements IPropertyOperation { @@ -120,11 +121,14 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe private static final String UPDATE_DATA_TYPE = "UpdateDataType"; private static Logger log = Logger.getLogger(PropertyOperation.class.getName()); private DerivedFromOperation derivedFromOperation; + private DataTypeOperation dataTypeOperation; @Autowired - public PropertyOperation(HealingJanusGraphGenericDao janusGraphGenericDao, DerivedFromOperation derivedFromOperation) { + public PropertyOperation(HealingJanusGraphGenericDao janusGraphGenericDao, DerivedFromOperation derivedFromOperation, + DataTypeOperation dataTypeOperation) { this.janusGraphGenericDao = janusGraphGenericDao; this.derivedFromOperation = derivedFromOperation; + this.dataTypeOperation = dataTypeOperation; } public PropertyDefinition convertPropertyDataToPropertyDefinition(PropertyData propertyDataResult, String propertyName, String resourceId) { @@ -1506,28 +1510,21 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe return Either.left(true); } - public Either, JanusGraphOperationStatus> getAllDataTypes() { - Map dataTypes = new HashMap<>(); - Either, JanusGraphOperationStatus> result = Either.left(dataTypes); - Either, JanusGraphOperationStatus> getAllDataTypes = janusGraphGenericDao - .getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); - if (getAllDataTypes.isRight()) { - JanusGraphOperationStatus status = getAllDataTypes.right().value(); - if (status != JanusGraphOperationStatus.NOT_FOUND) { - return Either.right(status); - } else { - return result; - } - } - List list = getAllDataTypes.left().value(); + public Either>, JanusGraphOperationStatus> getAllDataTypes() { + final Map> dataTypes = new HashMap<>(); + Either>, JanusGraphOperationStatus> result = Either.left(dataTypes); + final Map allDataTypesFound = new HashMap<>(); + final List list = dataTypeOperation.getAllDataTypeNodes(); if (list != null) { log.trace("Number of data types to load is {}", list.size()); List collect = list.stream().map(p -> p.getDataTypeDataDefinition().getName()).collect(Collectors.toList()); log.trace("The data types to load are {}", collect); for (DataTypeData dataTypeData : list) { - log.trace("Going to fetch data type {}. uid is {}", dataTypeData.getDataTypeDataDefinition().getName(), dataTypeData.getUniqueId()); + final String dataTypeName = dataTypeData.getDataTypeDataDefinition().getName(); + final String dataTypeUniqueId = dataTypeData.getUniqueId(); + log.trace("Going to fetch data type {}. uid is {}", dataTypeName, dataTypeUniqueId); Either dataTypeByUid = this - .getAndAddDataTypeByUid(dataTypeData.getUniqueId(), dataTypes); + .getAndAddDataTypeByUid(dataTypeUniqueId, allDataTypesFound); if (dataTypeByUid.isRight()) { JanusGraphOperationStatus status = dataTypeByUid.right().value(); if (status == JanusGraphOperationStatus.NOT_FOUND) { @@ -1535,13 +1532,14 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } return Either.right(status); } + result = Either.left(dataTypeOperation.mapDataTypesDefinitionByModel(allDataTypesFound)); } } if (log.isTraceEnabled()) { if (result.isRight()) { log.trace("After fetching all data types {}", result); } else { - Map map = result.left().value(); + Map> map = result.left().value(); if (map != null) { String types = map.keySet().stream().collect(Collectors.joining(",", "[", "]")); log.trace("After fetching all data types {} ", types); @@ -1577,7 +1575,7 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe log.error(FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE, uniqueId); return Either.right(propertiesStatus); } - allDataTypes.put(dataTypeDefinition.getName(), dataTypeDefinition); + allDataTypes.put(dataTypeDefinition.getUniqueId(), dataTypeDefinition); String derivedFrom = dataTypeDefinition.getDerivedFromName(); if (allDataTypes.containsKey(derivedFrom)) { DataTypeDefinition parentDataTypeDefinition = allDataTypes.get(derivedFrom); @@ -1606,26 +1604,28 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe } DataTypeDefinition parentDataTypeDefinition = dataTypeByUid.left().value(); dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition); + final var model = getModel(uniqueId); + if (StringUtils.isNotEmpty(model)) { + dataTypeDefinition.setModel(model); + } } result = Either.left(dataTypeDefinition); return result; } + private String getModel(final String uniqueId) { + final Either, JanusGraphOperationStatus> model = janusGraphGenericDao.getParentNode( + UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, GraphEdgeLabels.MODEL_ELEMENT, + NodeTypeEnum.Model, ModelData.class); + return model.isLeft() ? model.left().value().getLeft().getName() : StringUtils.EMPTY; + } + public Either checkInnerType(PropertyDataDefinition propDataDef) { String propertyType = propDataDef.getType(); ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType); return getInnerType(type, propDataDef::getSchema); } - public Either, JanusGraphOperationStatus> getAllDataTypeNodes() { - final Either, JanusGraphOperationStatus> getAllDataTypes = - janusGraphGenericDao.getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class); - if (getAllDataTypes.isRight() && getAllDataTypes.right().value() == JanusGraphOperationStatus.NOT_FOUND) { - return Either.left(Collections.emptyList()); - } - return getAllDataTypes; - } - public Either validateAndUpdatePropertyValue(String propertyType, String value, boolean isValidate, String innerType, Map dataTypes) { log.trace("Going to validate property value and its type. type = {}, value = {}", propertyType, value); 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; @@ -58,6 +59,9 @@ class ApplicationDataTypeCacheTest { @Mock private PropertyOperation propertyOperation; + @Mock + private DataTypeOperation dataTypeOperation; + @Mock private ApplicationEventPublisher applicationEventPublisher; @@ -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 modifiedDataTypeDefinitionMap = - Map.of(testDataType1.getName(), testDataType1, testDataType2.getName(), testDataType2); + final Map dataTypeDefinitionMap = Map.of(testDataType1.getName(), testDataType1, testDataType2.getName(), testDataType2); + final Map> 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> dataTypesMappedByModel = new HashMap<>(); final Map 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 dataTypeDefinitionMap = new HashMap<>(); + final Map> dataTypeDefinitionMap = new HashMap<>(); when(propertyOperation.getAllDataTypes()).thenReturn(Either.left(dataTypeDefinitionMap)); - final Either, JanusGraphOperationStatus> response = applicationDataTypeCache.getAll(); + final Either, 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 dataTypeEither = applicationDataTypeCache.get("uniqueId"); + final Either 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 dataTypeEither = applicationDataTypeCache.get("test.data.type1"); + final Either 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> 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 expectedDataTypeCache) { - Either, JanusGraphOperationStatus> dataTypeCacheMapEither = applicationDataTypeCache.getAll(); + Either, JanusGraphOperationStatus> dataTypeCacheMapEither = applicationDataTypeCache.getAll(null); assertNotNull(dataTypeCacheMapEither); assertTrue(dataTypeCacheMapEither.isLeft()); final Map 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 dataTypesWithoutModel = new ArrayList<>(); + private final List dataTypesWithModel = new ArrayList<>(); + final Map> dataTypesMappedByModel = new HashMap<>(); + final Map 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, JanusGraphOperationStatus> result; + Either>, 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, JanusGraphOperationStatus> result; - - // default test - testSubject = createTestSubject(); - result = testSubject.getAllDataTypeNodes(); - } - - @Test public void testValidateAndUpdatePropertyValue() throws Exception { PropertyOperation testSubject; -- cgit 1.2.3-korg