From 37d72855721caa646144ad323fe51ae78af15507 Mon Sep 17 00:00:00 2001 From: leventecsanyi Date: Wed, 26 Oct 2022 10:44:08 +0200 Subject: Filter on private properties of CM Handles - Moved cm handle query validation to cps-ncmp-service (where it belongs!) - Added new enum type for private/public field types - Created new methods for private and public queries - Added new REST endpoint - Created service methods for filtering on different types of properties - Refactored getPublicPropertyPairs and queryCmHandleAnyProperties - Added unit test for the controller layer - Fixed refactoring suggestions - Imporved code coverage with unit tests - Refactoring - Added new functionality to NcmpRestInputMapper - Updated version number to 3.2.1-SNAPSHOT and updated release-notes.rst Issue-ID: CPS-1236 Change-Id: I0ddf6866473f7c3c6b8507d222d441bf97ca6bdc Signed-off-by: leventecsanyi --- cps-ncmp-service/pom.xml | 2 +- .../api/NetworkCmProxyCmHandlerQueryService.java | 10 +- .../cps/ncmp/api/NetworkCmProxyDataService.java | 9 ++ .../NetworkCmProxyCmHandlerQueryServiceImpl.java | 109 ++++++++++++++++-- .../api/impl/NetworkCmProxyDataServiceImpl.java | 27 +++-- .../api/impl/utils/CmHandleQueryConditions.java | 42 +++++++ .../api/impl/utils/InventoryQueryConditions.java | 42 +++++++ .../impl/utils/RestQueryParametersValidator.java | 125 +++++++++++++++++++++ .../cps/ncmp/api/inventory/CmHandleQueries.java | 14 ++- .../ncmp/api/inventory/CmHandleQueriesImpl.java | 19 +++- .../cps/ncmp/api/inventory/enums/PropertyType.java | 34 ++++++ .../api/models/CmHandleQueryServiceParameters.java | 42 +++++++ .../NetworkCmProxyCmHandlerQueryServiceSpec.groovy | 78 +++++++++++-- .../impl/NetworkCmProxyDataServiceImplSpec.groovy | 27 +++-- ...AsyncRequestResponseEventIntegrationSpec.groovy | 4 +- .../impl/event/lcm/LcmEventsPublisherSpec.groovy | 4 +- .../impl/utils/CmHandleQueryConditionsSpec.groovy | 35 ++++++ .../api/impl/utils/DmiServiceUrlBuilderSpec.groovy | 82 ++++++++++++++ .../impl/utils/InventoryQueryConditionsSpec.groovy | 36 ++++++ .../utils/RestQueryParametersValidatorSpec.groovy | 125 +++++++++++++++++++++ .../api/impl/utils/YangDataConverterSpec.groovy | 41 +++++++ .../api/inventory/CmHandleQueriesImplSpec.groovy | 37 +++++- .../cps/ncmp/api/kafka/MessagingBaseSpec.groovy | 71 ++++++++++++ .../ncmp/api/utils/DmiServiceUrlBuilderSpec.groovy | 83 -------------- .../onap/cps/ncmp/api/utils/MessagingSpec.groovy | 71 ------------ .../ncmp/api/utils/YangDataConverterSpec.groovy | 42 ------- 26 files changed, 968 insertions(+), 243 deletions(-) create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditions.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditions.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidator.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/enums/PropertyType.java create mode 100644 cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleQueryServiceParameters.java create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditionsSpec.groovy create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditionsSpec.groovy create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidatorSpec.groovy create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/YangDataConverterSpec.groovy create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy delete mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/DmiServiceUrlBuilderSpec.groovy delete mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/MessagingSpec.groovy delete mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/YangDataConverterSpec.groovy (limited to 'cps-ncmp-service') diff --git a/cps-ncmp-service/pom.xml b/cps-ncmp-service/pom.xml index 0f5a234be..864ea0de7 100644 --- a/cps-ncmp-service/pom.xml +++ b/cps-ncmp-service/pom.xml @@ -27,7 +27,7 @@ org.onap.cps cps-parent - 3.2.0-SNAPSHOT + 3.2.1-SNAPSHOT ../cps-parent/pom.xml diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyCmHandlerQueryService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyCmHandlerQueryService.java index faf58b95b..7322ebc11 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyCmHandlerQueryService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyCmHandlerQueryService.java @@ -21,8 +21,8 @@ package org.onap.cps.ncmp.api; import java.util.Set; +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; -import org.onap.cps.spi.model.CmHandleQueryServiceParameters; public interface NetworkCmProxyCmHandlerQueryService { /** @@ -40,4 +40,12 @@ public interface NetworkCmProxyCmHandlerQueryService { * @return collection of cm handle ids */ Set queryCmHandleIds(CmHandleQueryServiceParameters cmHandleQueryServiceParameters); + + /** + * Query and return cm handles that match the given query parameters. + * + * @param cmHandleQueryServiceParameters the cm handle query parameters + * @return collection of cm handle ids + */ + Set queryCmHandleIdsForInventory(CmHandleQueryServiceParameters cmHandleQueryServiceParameters); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java index 0ea067428..c9810e9a6 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/NetworkCmProxyDataService.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.Set; import org.onap.cps.ncmp.api.inventory.CompositeState; import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters; +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters; import org.onap.cps.ncmp.api.models.DmiPluginRegistration; import org.onap.cps.ncmp.api.models.DmiPluginRegistrationResponse; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; @@ -183,4 +184,12 @@ public interface NetworkCmProxyDataService { * @return set of cm handle IDs */ Set getAllCmHandleIdsByDmiPluginIdentifier(String dmiPluginIdentifier); + + /** + * Get all cm handle IDs by various search criteria. + * + * @param cmHandleQueryServiceParameters cm handle query parameters + * @return set of cm handle IDs + */ + Set executeCmHandleIdSearchForInventory(CmHandleQueryServiceParameters cmHandleQueryServiceParameters); } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java index 1674c52fc..a8fc6d705 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceImpl.java @@ -20,11 +20,11 @@ package org.onap.cps.ncmp.api.impl; +import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCpsPathConditionProperties; +import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateModuleNameConditionProperties; import static org.onap.cps.ncmp.api.impl.utils.YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle; import static org.onap.cps.spi.FetchDescendantsOption.FETCH_DIRECT_CHILDREN_ONLY; import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS; -import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateCpsPathConditionProperties; -import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateModuleNameConditionProperties; import java.util.ArrayList; import java.util.Collection; @@ -40,16 +40,18 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.cpspath.parser.PathParsingException; import org.onap.cps.ncmp.api.NetworkCmProxyCmHandlerQueryService; +import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions; +import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions; import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; import org.onap.cps.ncmp.api.inventory.CmHandleQueries; import org.onap.cps.ncmp.api.inventory.InventoryPersistence; +import org.onap.cps.ncmp.api.inventory.enums.PropertyType; +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.Anchor; -import org.onap.cps.spi.model.CmHandleQueryServiceParameters; import org.onap.cps.spi.model.ConditionProperties; import org.onap.cps.spi.model.DataNode; -import org.onap.cps.utils.ValidQueryProperties; import org.springframework.stereotype.Service; @Service @@ -113,6 +115,92 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm return moduleNameQueryResult; } + @Override + public Set queryCmHandleIdsForInventory( + final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) { + + if (cmHandleQueryServiceParameters.getCmHandleQueryParameters().isEmpty()) { + return getAllCmHandleIds(); + } + + final Map publicPropertiesQueryResult = queryCmHandlesByPublicProperties( + cmHandleQueryServiceParameters); + if (publicPropertiesQueryResult != null && publicPropertiesQueryResult.isEmpty()) { + return Collections.emptySet(); + } + + final Map privatePropertiesQueryResult = queryCmHandlesByPrivateProperties( + cmHandleQueryServiceParameters); + if (privatePropertiesQueryResult != null && privatePropertiesQueryResult.isEmpty()) { + return Collections.emptySet(); + } + + final Map dmiPropertiesQueryResult = queryCmHandlesByDmiPlugin( + cmHandleQueryServiceParameters); + if (dmiPropertiesQueryResult != null && dmiPropertiesQueryResult.isEmpty()) { + return Collections.emptySet(); + } + + final Map combinedResult = + combineQueryResults(publicPropertiesQueryResult, privatePropertiesQueryResult, dmiPropertiesQueryResult); + + return combinedResult.keySet(); + } + + private Map queryCmHandlesByDmiPlugin( + final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) { + final Map dmiPropertyQueryPairs = + getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(), + InventoryQueryConditions.CM_HANDLE_WITH_DMI_PLUGIN.getName()); + if (dmiPropertyQueryPairs.isEmpty()) { + return NO_QUERY_TO_EXECUTE; + } + + final String dmiPluginIdentifierValue = dmiPropertyQueryPairs.get( + PropertyType.DMI_PLUGIN.getYangContainerName()); + + final Set cmHandlesByDmiPluginIdentifier = cmHandleQueries + .getCmHandlesByDmiPluginIdentifier(dmiPluginIdentifierValue); + + return cmHandlesByDmiPluginIdentifier.stream() + .collect(Collectors.toMap(NcmpServiceCmHandle::getCmHandleId, cmH -> cmH)); + } + + private Map queryCmHandlesByPrivateProperties( + final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) { + + final Map privatePropertyQueryPairs = + getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(), + InventoryQueryConditions.HAS_ALL_ADDITIONAL_PROPERTIES.getName()); + + return privatePropertyQueryPairs.isEmpty() + ? NO_QUERY_TO_EXECUTE + : cmHandleQueries.queryCmHandleAdditionalProperties(privatePropertyQueryPairs); + } + + private Map queryCmHandlesByPublicProperties( + final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) { + + final Map publicPropertyQueryPairs = + getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(), + CmHandleQueryConditions.HAS_ALL_PROPERTIES.getConditionName()); + + return publicPropertyQueryPairs.isEmpty() + ? NO_QUERY_TO_EXECUTE + : cmHandleQueries.queryCmHandlePublicProperties(publicPropertyQueryPairs); + } + + private Map combineQueryResults( + final Map publicPropertiesQueryResult, + final Map privatePropertiesQueryResult, + final Map dmiPropertiesQueryResult) { + + final Map propertiesCombinedResult = cmHandleQueries + .combineCmHandleQueries(publicPropertiesQueryResult, privatePropertiesQueryResult); + return cmHandleQueries + .combineCmHandleQueries(propertiesCombinedResult, dmiPropertiesQueryResult); + } + private Map combineWithModuleNameQuery( final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, final Map previousQueryResult) { @@ -164,7 +252,8 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm } final Map publicPropertyQueryPairs = - getPublicPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters()); + getPropertyPairs(cmHandleQueryServiceParameters.getCmHandleQueryParameters(), + CmHandleQueryConditions.HAS_ALL_PROPERTIES.getConditionName()); final Map propertiesQueryResult = publicPropertyQueryPairs.isEmpty() ? NO_QUERY_TO_EXECUTE : cmHandleQueries.queryCmHandlePublicProperties(publicPropertyQueryPairs); @@ -178,7 +267,7 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm private Collection getModuleNamesForQuery(final List conditionProperties) { final List result = new ArrayList<>(); - getConditions(conditionProperties, ValidQueryProperties.HAS_ALL_MODULES.getQueryProperty()) + getConditions(conditionProperties, CmHandleQueryConditions.HAS_ALL_MODULES.getConditionName()) .parallelStream().forEach( conditionProperty -> { validateModuleNameConditionProperties(conditionProperty); @@ -190,15 +279,15 @@ public class NetworkCmProxyCmHandlerQueryServiceImpl implements NetworkCmProxyCm private Map getCpsPath(final List conditionProperties) { final Map result = new HashMap<>(); - getConditions(conditionProperties, ValidQueryProperties.WITH_CPS_PATH.getQueryProperty()).forEach( + getConditions(conditionProperties, CmHandleQueryConditions.WITH_CPS_PATH.getConditionName()).forEach( result::putAll); return result; } - private Map getPublicPropertyPairs(final List conditionProperties) { + private Map getPropertyPairs(final List conditionProperties, + final String queryProperty) { final Map result = new HashMap<>(); - getConditions(conditionProperties, - ValidQueryProperties.HAS_ALL_PROPERTIES.getQueryProperty()).forEach(result::putAll); + getConditions(conditionProperties, queryProperty).forEach(result::putAll); return result; } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java index 27c3646a5..d00d2119b 100755 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImpl.java @@ -25,7 +25,7 @@ package org.onap.cps.ncmp.api.impl; import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME; import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum; -import static org.onap.cps.utils.CmHandleQueryRestParametersValidator.validateCmHandleQueryParameters; +import static org.onap.cps.ncmp.api.impl.utils.RestQueryParametersValidator.validateCmHandleQueryParameters; import com.hazelcast.map.IMap; import java.time.OffsetDateTime; @@ -45,6 +45,8 @@ import org.onap.cps.ncmp.api.NetworkCmProxyDataService; import org.onap.cps.ncmp.api.impl.event.lcm.LcmEventsCmHandleStateHandler; import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations; import org.onap.cps.ncmp.api.impl.operations.DmiOperations; +import org.onap.cps.ncmp.api.impl.utils.CmHandleQueryConditions; +import org.onap.cps.ncmp.api.impl.utils.InventoryQueryConditions; import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle; import org.onap.cps.ncmp.api.inventory.CmHandleQueries; @@ -54,6 +56,7 @@ import org.onap.cps.ncmp.api.inventory.CompositeStateUtils; import org.onap.cps.ncmp.api.inventory.DataStoreSyncState; import org.onap.cps.ncmp.api.inventory.InventoryPersistence; import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters; +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters; import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse; import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError; import org.onap.cps.ncmp.api.models.DmiPluginRegistration; @@ -64,7 +67,6 @@ import org.onap.cps.spi.exceptions.AlreadyDefinedExceptionBatch; import org.onap.cps.spi.exceptions.CpsException; import org.onap.cps.spi.exceptions.DataNodeNotFoundException; import org.onap.cps.spi.exceptions.DataValidationException; -import org.onap.cps.spi.model.CmHandleQueryServiceParameters; import org.onap.cps.spi.model.ModuleDefinition; import org.onap.cps.spi.model.ModuleReference; import org.onap.cps.utils.JsonObjectMapper; @@ -171,9 +173,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService public Set executeCmHandleSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) { final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType( cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class); - - validateCmHandleQueryParameters(cmHandleQueryServiceParameters); - + validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES); return networkCmProxyCmHandlerQueryService.queryCmHandles(cmHandleQueryServiceParameters); } @@ -187,9 +187,7 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService public Set executeCmHandleIdSearch(final CmHandleQueryApiParameters cmHandleQueryApiParameters) { final CmHandleQueryServiceParameters cmHandleQueryServiceParameters = jsonObjectMapper.convertToValueType( cmHandleQueryApiParameters, CmHandleQueryServiceParameters.class); - - validateCmHandleQueryParameters(cmHandleQueryServiceParameters); - + validateCmHandleQueryParameters(cmHandleQueryServiceParameters, CmHandleQueryConditions.ALL_CONDITION_NAMES); return networkCmProxyCmHandlerQueryService.queryCmHandleIds(cmHandleQueryServiceParameters); } @@ -237,6 +235,19 @@ public class NetworkCmProxyDataServiceImpl implements NetworkCmProxyDataService return cmHandleIds; } + /** + * Get all cm handle IDs by various properties. + * + * @param cmHandleQueryServiceParameters cm handle query parameters + * @return set of cm handle IDs + */ + @Override + public Set executeCmHandleIdSearchForInventory( + final CmHandleQueryServiceParameters cmHandleQueryServiceParameters) { + validateCmHandleQueryParameters(cmHandleQueryServiceParameters, InventoryQueryConditions.ALL_CONDITION_NAMES); + return networkCmProxyCmHandlerQueryService.queryCmHandleIdsForInventory(cmHandleQueryServiceParameters); + } + /** * Retrieve cm handle details for a given cm handle. * diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditions.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditions.java new file mode 100644 index 000000000..b1bb7f767 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditions.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.onap.cps.ncmp.api.impl.utils; + +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Collectors; +import lombok.Getter; + +@Getter +public enum CmHandleQueryConditions { + HAS_ALL_PROPERTIES("hasAllProperties"), + HAS_ALL_MODULES("hasAllModules"), + WITH_CPS_PATH("cmHandleWithCpsPath"); + + public static final Collection ALL_CONDITION_NAMES = Arrays.stream(CmHandleQueryConditions.values()) + .map(CmHandleQueryConditions::getConditionName).collect(Collectors.toList()); + + private final String conditionName; + + CmHandleQueryConditions(final String conditionName) { + this.conditionName = conditionName; + } +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditions.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditions.java new file mode 100644 index 000000000..9437cf0ba --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditions.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.onap.cps.ncmp.api.impl.utils; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum InventoryQueryConditions { + + HAS_ALL_PROPERTIES("hasAllProperties"), + HAS_ALL_ADDITIONAL_PROPERTIES("hasAllAdditionalProperties"), + CM_HANDLE_WITH_DMI_PLUGIN("cmHandleWithDmiPlugin"); + + public static final List ALL_CONDITION_NAMES = Arrays.stream(InventoryQueryConditions.values()) + .map(InventoryQueryConditions::getName).collect(Collectors.toList()); + + private final String name; + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidator.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidator.java new file mode 100644 index 000000000..2ef97cab5 --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidator.java @@ -0,0 +1,125 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.onap.cps.ncmp.api.impl.utils; + +import com.google.common.base.Strings; +import java.util.Collection; +import java.util.Map; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters; +import org.onap.cps.spi.exceptions.DataValidationException; + +@Slf4j +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class RestQueryParametersValidator { + + /** + * Validate query parameters. + * + * @param cmHandleQueryServiceParameters name of data to be validated + * @param validConditionNames valid condition names + */ + public static void validateCmHandleQueryParameters( + final CmHandleQueryServiceParameters cmHandleQueryServiceParameters, + final Collection validConditionNames) { + cmHandleQueryServiceParameters.getCmHandleQueryParameters().forEach( + cmHandleQueryParameter -> { + if (validConditionNames.stream().noneMatch(validConditionName -> + validConditionName.equals(cmHandleQueryParameter.getConditionName()))) { + throw createDataValidationException( + String.format("Wrong 'conditionName': %s - please supply a valid name.", + cmHandleQueryParameter.getConditionName())); + } + if (cmHandleQueryParameter.getConditionParameters().isEmpty()) { + throw createDataValidationException( + "Empty 'conditionsParameters' - please supply a valid condition parameter."); + } + cmHandleQueryParameter.getConditionParameters().forEach( + RestQueryParametersValidator::validateConditionParameter + ); + } + ); + } + + private static void validateConditionParameter(final Map conditionParameter) { + if (conditionParameter.isEmpty()) { + throw createDataValidationException( + "Empty 'conditionsParameter' - please supply a valid condition parameter."); + } + if (conditionParameter.size() > 1) { + throw createDataValidationException("Too many names in one 'conditionsParameter' -" + + " please supply one name in one condition parameter."); + } + conditionParameter.forEach((key, value) -> { + if (Strings.isNullOrEmpty(key)) { + throw createDataValidationException( + "Missing 'conditionsParameterName' - please supply a valid name."); + } + }); + } + + /** + * Validate module name condition properties. + * @param conditionProperty name of data to be validated + */ + public static void validateModuleNameConditionProperties(final Map conditionProperty) { + if (conditionProperty.containsKey("moduleName") && !conditionProperty.get("moduleName").isEmpty()) { + return; + } + throw createDataValidationException("Wrong module condition property. - " + + "please supply a valid condition property."); + } + + /** + * Validate CPS path condition properties. + * @param conditionProperty name of data to be validated + */ + public static boolean validateCpsPathConditionProperties(final Map conditionProperty) { + if (conditionProperty.isEmpty()) { + return true; + } + if (conditionProperty.size() > 1) { + throw createDataValidationException("Only one condition property is allowed for the CPS path query."); + } + if (!conditionProperty.containsKey("cpsPath")) { + throw createDataValidationException( + "Wrong CPS path condition property. - expecting \"cpsPath\" as the condition property."); + } + final String cpsPath = conditionProperty.get("cpsPath"); + if (cpsPath.isBlank()) { + throw createDataValidationException( + "Wrong CPS path. - please supply a valid CPS path."); + } + if (cpsPath.contains("/additional-properties")) { + log.debug("{} - Private metadata cannot be queried. Nothing to be returned", + cpsPath); + return false; + } + return true; + } + + private static DataValidationException createDataValidationException(final String details) { + return new DataValidationException("Invalid Query Parameter.", details); + } + +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java index daabbb56f..bae0262b0 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueries.java @@ -30,12 +30,22 @@ import org.onap.cps.spi.model.DataNode; public interface CmHandleQueries { /** - * Query CmHandles based on PublicProperties. + * Query CmHandles based on additional (private) properties. + * + * @param additionalPropertyQueryPairs private properties for query + * @return CmHandles which have these private properties + */ + Map queryCmHandleAdditionalProperties( + Map additionalPropertyQueryPairs); + + /** + * Query CmHandles based on public properties. * * @param publicPropertyQueryPairs public properties for query * @return CmHandles which have these public properties */ - Map queryCmHandlePublicProperties(Map publicPropertyQueryPairs); + Map queryCmHandlePublicProperties( + Map publicPropertyQueryPairs); /** * Combine Maps of CmHandles. diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImpl.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImpl.java index e9e2fcacf..1a54a824b 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImpl.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImpl.java @@ -34,6 +34,7 @@ import java.util.Set; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.onap.cps.ncmp.api.impl.utils.YangDataConverter; +import org.onap.cps.ncmp.api.inventory.enums.PropertyType; import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle; import org.onap.cps.spi.CpsDataPersistenceService; import org.onap.cps.spi.FetchDescendantsOption; @@ -51,16 +52,28 @@ public class CmHandleQueriesImpl implements CmHandleQueries { private static final Map NO_QUERY_TO_EXECUTE = null; private static final String ANCESTOR_CM_HANDLES = "/ancestor::cm-handles"; + @Override + public Map queryCmHandleAdditionalProperties( + final Map privatePropertyQueryPairs) { + return queryCmHandleAnyProperties(privatePropertyQueryPairs, PropertyType.ADDITIONAL); + } @Override public Map queryCmHandlePublicProperties( final Map publicPropertyQueryPairs) { - if (publicPropertyQueryPairs.isEmpty()) { + return queryCmHandleAnyProperties(publicPropertyQueryPairs, PropertyType.PUBLIC); + } + + private Map queryCmHandleAnyProperties( + final Map propertyQueryPairs, + final PropertyType propertyType) { + if (propertyQueryPairs.isEmpty()) { return Collections.emptyMap(); } Map cmHandleIdToNcmpServiceCmHandles = null; - for (final Map.Entry publicPropertyQueryPair : publicPropertyQueryPairs.entrySet()) { - final String cpsPath = "//public-properties[@name=\"" + publicPropertyQueryPair.getKey() + for (final Map.Entry publicPropertyQueryPair : propertyQueryPairs.entrySet()) { + final String cpsPath = "//" + propertyType.getYangContainerName() + "[@name=\"" + + publicPropertyQueryPair.getKey() + "\" and @value=\"" + publicPropertyQueryPair.getValue() + "\"]"; final Collection dataNodes = queryCmHandleDataNodesByCpsPath(cpsPath, INCLUDE_ALL_DESCENDANTS); diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/enums/PropertyType.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/enums/PropertyType.java new file mode 100644 index 000000000..c3c46c35e --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/inventory/enums/PropertyType.java @@ -0,0 +1,34 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.onap.cps.ncmp.api.inventory.enums; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public enum PropertyType { + ADDITIONAL("additional-properties"), + PUBLIC("public-properties"), + DMI_PLUGIN("dmiPluginName"); + + private final String yangContainerName; +} diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleQueryServiceParameters.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleQueryServiceParameters.java new file mode 100644 index 000000000..774f04b5a --- /dev/null +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/models/CmHandleQueryServiceParameters.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.onap.cps.ncmp.api.models; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.Collections; +import java.util.List; +import javax.validation.Valid; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.Setter; +import org.onap.cps.spi.model.ConditionProperties; + +@Setter +@Getter +@EqualsAndHashCode +@JsonInclude(Include.NON_EMPTY) +public class CmHandleQueryServiceParameters { + @JsonProperty("cmHandleQueryParameters") + @Valid + private List cmHandleQueryParameters = Collections.emptyList(); +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceSpec.groovy index eea53e82d..201f6afe5 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyCmHandlerQueryServiceSpec.groovy @@ -22,13 +22,14 @@ package org.onap.cps.ncmp.api.impl import org.onap.cps.cpspath.parser.PathParsingException import org.onap.cps.ncmp.api.inventory.CmHandleQueries +import org.onap.cps.ncmp.api.inventory.CmHandleQueriesImpl import org.onap.cps.ncmp.api.inventory.InventoryPersistence +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle import org.onap.cps.spi.FetchDescendantsOption import org.onap.cps.spi.exceptions.DataInUseException import org.onap.cps.spi.exceptions.DataValidationException import org.onap.cps.spi.model.Anchor -import org.onap.cps.spi.model.CmHandleQueryServiceParameters import org.onap.cps.spi.model.ConditionProperties import org.onap.cps.spi.model.DataNode import spock.lang.Specification @@ -38,12 +39,16 @@ import java.util.stream.Collectors class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification { def cmHandleQueries = Mock(CmHandleQueries) - def inventoryPersistence = Mock(InventoryPersistence) + def partiallyMockedCmHandleQueries = Spy(CmHandleQueriesImpl) + def mockInventoryPersistence = Mock(InventoryPersistence) def static someCmHandleDataNode = new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'some-cmhandle-id\']', leaves: ['id':'some-cmhandle-id']) def dmiRegistry = new DataNode(xpath: '/dmi-registry', childDataNodes: createDataNodeList(['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'])) - def objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(cmHandleQueries, inventoryPersistence) + static def queryResultCmHandleMap = createCmHandleMap(['H1', 'H2']) + + def objectUnderTest = new NetworkCmProxyCmHandlerQueryServiceImpl(cmHandleQueries, mockInventoryPersistence) + def objectUnderTestSpy = new NetworkCmProxyCmHandlerQueryServiceImpl(partiallyMockedCmHandleQueries, mockInventoryPersistence) def 'Retrieve cm handles with cpsPath when combined with no Module Query.'() { given: 'a cmHandleWithCpsPath condition property' @@ -105,9 +110,9 @@ class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification { and: 'null is returned from the state and public property queries' cmHandleQueries.combineCmHandleQueries(*_) >> null and: '#scenario from the modules query' - inventoryPersistence.queryAnchors(*_) >> returnedAnchors + mockInventoryPersistence.queryAnchors(*_) >> returnedAnchors and: 'the same cmHandles are returned from the persistence service layer' - returnedAnchors.size() * inventoryPersistence.getDataNode(*_) >> returnedCmHandles + returnedAnchors.size() * mockInventoryPersistence.getDataNode(*_) >> returnedCmHandles when: 'the query is executed for both cm handle ids and details' def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters) def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters) @@ -131,7 +136,7 @@ class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification { and: 'cmHandles are returned from the state and public property combined queries' cmHandleQueries.combineCmHandleQueries(*_) >> combinedQueryMap and: 'cmHandles are returned from the module names query' - inventoryPersistence.queryAnchors(['some-module-name']) >> anchorsForModuleQuery + mockInventoryPersistence.queryAnchors(['some-module-name']) >> anchorsForModuleQuery and: 'cmHandleQueries returns a datanode result' 2 * cmHandleQueries.queryCmHandleDataNodesByCpsPath(*_) >> [someCmHandleDataNode] when: 'the query is executed for both cm handle ids and details' @@ -154,9 +159,9 @@ class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification { given: 'We use an empty query' def cmHandleQueryParameters = new CmHandleQueryServiceParameters() and: 'the inventory persistence returns the dmi registry datanode with just ids' - inventoryPersistence.getDataNode("/dmi-registry", FetchDescendantsOption.FETCH_DIRECT_CHILDREN_ONLY) >> dmiRegistry + mockInventoryPersistence.getDataNode("/dmi-registry", FetchDescendantsOption.FETCH_DIRECT_CHILDREN_ONLY) >> dmiRegistry and: 'the inventory persistence returns the dmi registry datanode with data' - inventoryPersistence.getDataNode("/dmi-registry") >> dmiRegistry + mockInventoryPersistence.getDataNode("/dmi-registry") >> dmiRegistry when: 'the query is executed for both cm handle ids and details' def returnedCmHandlesJustIds = objectUnderTest.queryCmHandleIds(cmHandleQueryParameters) def returnedCmHandlesWithData = objectUnderTest.queryCmHandles(cmHandleQueryParameters) @@ -165,13 +170,68 @@ class NetworkCmProxyCmHandlerQueryServiceSpec extends Specification { returnedCmHandlesWithData.stream().map(d -> d.cmHandleId).collect(Collectors.toSet()) == ['PNFDemo1', 'PNFDemo2', 'PNFDemo3', 'PNFDemo4'] as Set } + + def 'Retrieve all CMHandleIds for empty query parameters' () { + given: 'We query without any parameters' + def cmHandleQueryParameters = new CmHandleQueryServiceParameters() + and: 'the inventoryPersistence returns all four CmHandleIds' + mockInventoryPersistence.getDataNode(*_) >> dmiRegistry + when: 'the query executed' + def resultSet = objectUnderTest.queryCmHandleIdsForInventory(cmHandleQueryParameters) + then: 'the size of the result list equals the size of all cmHandleIds.' + resultSet.size() == 4 + } + + def 'Retrieve CMHandleIds when #scenario.' () { + given: 'a query object created with #condition' + def cmHandleQueryParameters = new CmHandleQueryServiceParameters() + def conditionProperties = createConditionProperties(conditionName, [['some-key': 'some-value']]) + cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties]) + and: 'the inventoryPersistence returns different CmHandleIds' + partiallyMockedCmHandleQueries.queryCmHandlePublicProperties(*_) >> cmHandlesWithMatchingPublicProperties + partiallyMockedCmHandleQueries.queryCmHandleAdditionalProperties(*_) >> cmHandlesWithMatchingPrivateProperties + when: 'the query executed' + def result = objectUnderTestSpy.queryCmHandleIdsForInventory(cmHandleQueryParameters) + then: 'the expected number of results are returned.' + assert result.size() == expectedCmHandleIdsSize + where: 'the following data is used' + scenario | conditionName | cmHandlesWithMatchingPublicProperties | cmHandlesWithMatchingPrivateProperties || expectedCmHandleIdsSize + 'all properties, only public matching' | 'hasAllProperties' | queryResultCmHandleMap | null || 2 + 'all properties, no matching cm handles' | 'hasAllProperties' | [:] | [:] || 0 + 'additional properties, some matching cm handles' | 'hasAllAdditionalProperties' | [:] | queryResultCmHandleMap || 2 + 'additional properties, no matching cm handles' | 'hasAllAdditionalProperties' | null | [:] || 0 + } + + def 'Retrieve CMHandleIds by different DMI properties with #scenario.' () { + given: 'a query object created with dmi plugin as condition' + def cmHandleQueryParameters = new CmHandleQueryServiceParameters() + def conditionProperties = createConditionProperties('cmHandleWithDmiPlugin', [['some-key': 'some-value']]) + cmHandleQueryParameters.setCmHandleQueryParameters([conditionProperties]) + and: 'the inventoryPersistence returns different CmHandleIds' + partiallyMockedCmHandleQueries.getCmHandlesByDmiPluginIdentifier(*_) >> cmHandleQueryResult + when: 'the query executed' + def result = objectUnderTestSpy.queryCmHandleIdsForInventory(cmHandleQueryParameters) + then: 'the expected number of results are returned.' + assert result.size() == expectedCmHandleIdsSize + where: 'the following data is used' + scenario | cmHandleQueryResult || expectedCmHandleIdsSize + 'some matches' | queryResultCmHandleMap.values() || 2 + 'no matches' | [] || 0 + } + + static def createCmHandleMap(cmHandleIds) { + def cmHandleMap = [:] + cmHandleIds.each{ cmHandleMap[it] = new NcmpServiceCmHandle(cmHandleId : it) } + return cmHandleMap + } + def createConditionProperties(String conditionName, List> conditionParameters) { return new ConditionProperties(conditionName : conditionName, conditionParameters : conditionParameters) } def static createDataNodeList(dataNodeIds) { def dataNodes =[] - dataNodeIds.forEach(id -> {dataNodes.add(new DataNode(xpath: '/dmi-registry/cm-handles[@id=\'' + id + '\']', leaves: ['id':id]))}) + dataNodeIds.each{ dataNodes << new DataNode(xpath: "/dmi-registry/cm-handles[@id='${it}']", leaves: ['id':it]) } return dataNodes } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy index 58ca06bee..578f7f31c 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplSpec.groovy @@ -33,14 +33,12 @@ import org.onap.cps.ncmp.api.inventory.InventoryPersistence import org.onap.cps.ncmp.api.inventory.LockReasonCategory import org.onap.cps.ncmp.api.inventory.DataStoreSyncState import org.onap.cps.ncmp.api.models.CmHandleQueryApiParameters -import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters import org.onap.cps.ncmp.api.models.ConditionApiProperties import org.onap.cps.ncmp.api.models.DmiPluginRegistration import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle import org.onap.cps.spi.exceptions.CpsException -import org.onap.cps.spi.exceptions.DataNodeNotFoundException -import org.onap.cps.spi.exceptions.DataValidationException -import org.onap.cps.spi.model.CmHandleQueryServiceParameters +import org.onap.cps.spi.model.ConditionProperties import spock.lang.Shared import java.util.stream.Collectors import org.onap.cps.utils.JsonObjectMapper @@ -57,10 +55,6 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum. import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE -import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_DOES_NOT_EXIST -import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.CM_HANDLE_INVALID_ID -import static org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse.RegistrationError.UNKNOWN_ERROR - class NetworkCmProxyDataServiceImplSpec extends Specification { @@ -202,6 +196,23 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { result == [ 'public prop' : 'some public prop' ] } + def 'Execute cm handle id search for inventory'() { + given: 'a ConditionApiProperties object' + def conditionProperties = new ConditionProperties() + conditionProperties.conditionName = 'hasAllProperties' + conditionProperties.conditionParameters = [ [ 'some-key' : 'some-value' ] ] + def conditionServiceProps = new CmHandleQueryServiceParameters() + conditionServiceProps.cmHandleQueryParameters = [conditionProperties] as List + and: 'the system returns an set of cmHandle ids' + mockCpsCmHandlerQueryService.queryCmHandleIdsForInventory(*_) >> [ 'cmHandle1', 'cmHandle2' ] + when: 'getting cm handle id set for a given dmi property' + def result = objectUnderTest.executeCmHandleIdSearchForInventory(conditionServiceProps) + then: 'the result returns the correct 2 elements' + assert result.size() == 2 + assert result.contains('cmHandle1') + assert result.contains('cmHandle2') + } + def 'Get cm handle composite state'() { given: 'a yang modelled cm handle' def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED, diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/async/CpsAsyncRequestResponseEventIntegrationSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/async/CpsAsyncRequestResponseEventIntegrationSpec.groovy index 31f179ab2..51162c74e 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/async/CpsAsyncRequestResponseEventIntegrationSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/async/CpsAsyncRequestResponseEventIntegrationSpec.groovy @@ -23,7 +23,7 @@ package org.onap.cps.ncmp.api.impl.async import com.fasterxml.jackson.databind.ObjectMapper import org.apache.kafka.clients.consumer.KafkaConsumer import org.mapstruct.factory.Mappers -import org.onap.cps.ncmp.api.utils.MessagingSpec +import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec import org.onap.cps.ncmp.event.model.DmiAsyncRequestResponseEvent import org.onap.cps.ncmp.event.model.NcmpAsyncRequestResponseEvent import org.onap.cps.ncmp.utils.TestUtils @@ -39,7 +39,7 @@ import java.time.Duration @SpringBootTest(classes = [NcmpAsyncRequestResponseEventProducer, NcmpAsyncRequestResponseEventConsumer, ObjectMapper, JsonObjectMapper]) @Testcontainers @DirtiesContext -class NcmpAsyncRequestResponseEventProducerIntegrationSpec extends MessagingSpec { +class NcmpAsyncRequestResponseEventProducerIntegrationSpec extends MessagingBaseSpec { @SpringBean NcmpAsyncRequestResponseEventProducer cpsAsyncRequestResponseEventProducerService = diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy index c7e6b6d35..61bf33d19 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/event/lcm/LcmEventsPublisherSpec.groovy @@ -22,7 +22,7 @@ package org.onap.cps.ncmp.api.impl.event.lcm import com.fasterxml.jackson.databind.ObjectMapper import org.apache.kafka.clients.consumer.KafkaConsumer -import org.onap.cps.ncmp.api.utils.MessagingSpec +import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec import org.onap.cps.ncmp.utils.TestUtils import org.onap.cps.utils.JsonObjectMapper import org.onap.ncmp.cmhandle.event.lcm.Event @@ -38,7 +38,7 @@ import java.time.Duration @SpringBootTest(classes = [LcmEventsPublisher, ObjectMapper, JsonObjectMapper]) @Testcontainers @DirtiesContext -class LcmEventsPublisherSpec extends MessagingSpec { +class LcmEventsPublisherSpec extends MessagingBaseSpec { def kafkaConsumer = new KafkaConsumer<>(consumerConfigProperties('ncmp-group')) diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditionsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditionsSpec.groovy new file mode 100644 index 000000000..f0e2d9f0b --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/CmHandleQueryConditionsSpec.groovy @@ -0,0 +1,35 @@ +/* + * ============LICENSE_START======================================================== + * Copyright (c) 2022 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.onap.cps.ncmp.api.impl.utils + +import spock.lang.Specification + +class CmHandleQueryConditionsSpec extends Specification { + + def 'CmHandle query condition names.'() { + expect: '3 conditions with the correct names' + assert CmHandleQueryConditions.ALL_CONDITION_NAMES.size() == 3 + assert CmHandleQueryConditions.ALL_CONDITION_NAMES.containsAll('hasAllProperties', + 'hasAllModules', + 'cmHandleWithCpsPath') + } + +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy new file mode 100644 index 000000000..01569887c --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/DmiServiceUrlBuilderSpec.groovy @@ -0,0 +1,82 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.onap.cps.ncmp.api.impl.utils + +import org.onap.cps.spi.utils.CpsValidator + +import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING + +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle +import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration +import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle +import spock.lang.Shared +import spock.lang.Specification + +class DmiServiceUrlBuilderSpec extends Specification { + + @Shared + YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('dmiServiceName', + 'dmiDataServiceName', 'dmiModuleServiceName', new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')) + + NcmpConfiguration.DmiProperties dmiProperties = new NcmpConfiguration.DmiProperties() + + def mockCpsValidator = Mock(CpsValidator) + + def objectUnderTest = new DmiServiceUrlBuilder(dmiProperties, mockCpsValidator) + + def 'Create the dmi service url with #scenario.'() { + given: 'uri variables' + dmiProperties.dmiBasePath = 'dmi' + def uriVars = objectUnderTest.populateUriVariables(yangModelCmHandle, + "cmHandle", PASSTHROUGH_RUNNING) + and: 'query params' + def uriQueries = objectUnderTest.populateQueryParams(resourceId, + 'optionsParamInQuery', topic) + when: 'a dmi datastore service url is generated' + def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars) + then: 'service url is generated as expected' + assert dmiServiceUrl == expectedDmiServiceUrl + where: 'the following parameters are used' + scenario | topic | resourceId || expectedDmiServiceUrl + 'With valid resourceId' | 'topicParamInQuery' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery' + 'With Empty resourceId' | 'topicParamInQuery' | '' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?options=optionsParamInQuery&topic=topicParamInQuery' + 'With Empty dmi base path' | 'topicParamInQuery' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery' + 'With Empty topicParamInQuery' | '' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery' + } + + def 'Populate dmi data store url #scenario.'() { + given: 'uri variables are created' + dmiProperties.dmiBasePath = dmiBasePath + def uriVars = objectUnderTest.populateUriVariables(yangModelCmHandle, + "cmHandle", PASSTHROUGH_RUNNING) + and: 'null query params' + def uriQueries = objectUnderTest.populateQueryParams(null, + null, null) + when: 'a dmi datastore service url is generated' + def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars) + then: 'the created dmi service url matches the expected' + assert dmiServiceUrl == expectedDmiServiceUrl + where: 'the following parameters are used' + scenario | decription | dmiBasePath || expectedDmiServiceUrl + 'with base path / ' | 'Invalid base path as it starts with /' | '/dmi' || 'dmiServiceName//dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running' + 'without base path / ' | 'Valid path as it does not starts with /' | 'dmi' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running' + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditionsSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditionsSpec.groovy new file mode 100644 index 000000000..00edb8d0d --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/InventoryQueryConditionsSpec.groovy @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================== + * Copyright (c) 2022 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.onap.cps.ncmp.api.impl.utils + +import spock.lang.Specification + +class InventoryQueryConditionsSpec extends Specification { + + def 'Inventory query condition names.'() { + expect: '3 conditions with the correct names' + assert InventoryQueryConditions.ALL_CONDITION_NAMES.size() == 3 + assert InventoryQueryConditions.ALL_CONDITION_NAMES.containsAll('hasAllProperties', + 'hasAllAdditionalProperties', + 'cmHandleWithDmiPlugin') + } + + +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidatorSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidatorSpec.groovy new file mode 100644 index 000000000..e1055bb21 --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/RestQueryParametersValidatorSpec.groovy @@ -0,0 +1,125 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 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.onap.cps.ncmp.api.impl.utils + +import org.onap.cps.ncmp.api.models.CmHandleQueryServiceParameters +import org.onap.cps.spi.exceptions.DataValidationException +import org.onap.cps.spi.model.ConditionProperties +import spock.lang.Specification + +class RestQueryParametersValidatorSpec extends Specification { + + + def 'CM Handle Query validation: empty query.'() { + given: 'a cm handle query' + def cmHandleQueryParameters = new CmHandleQueryServiceParameters() + when: 'validator is invoked' + RestQueryParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters, []) + then: 'data validation exception is not thrown' + noExceptionThrown() + } + + def 'CM Handle Query validation: normal query.'() { + given: 'a cm handle query' + def cmHandleQueryParameters = new CmHandleQueryServiceParameters() + def condition = new ConditionProperties() + condition.conditionName = 'validConditionName' + condition.conditionParameters = [['key':'value']] + cmHandleQueryParameters.cmHandleQueryParameters = [condition] + when: 'validator is invoked' + RestQueryParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters, ['validConditionName']) + then: 'data validation exception is not thrown' + noExceptionThrown() + } + + def 'CM Handle Query validation: #scenario.'() { + given: 'a cm handle query' + def cmHandleQueryParameters = new CmHandleQueryServiceParameters() + def condition = new ConditionProperties() + condition.conditionName = conditionName + condition.conditionParameters = conditionParameters + cmHandleQueryParameters.cmHandleQueryParameters = [condition] + when: 'validator is invoked' + RestQueryParametersValidator.validateCmHandleQueryParameters(cmHandleQueryParameters, ['validConditionName']) + then: 'a data validation exception is thrown' + def thrown = thrown(DataValidationException) + and: 'the exception details contain the correct significant term ' + thrown.details.contains(expectedWordInDetails) + where: + scenario | conditionName | conditionParameters || expectedWordInDetails + 'unknown condition name' | 'unknownCondition' | [['key':'value']] || 'conditionName' + 'no condition name' | '' | [['key':'value']] || 'conditionName' + 'empty properties' | 'validConditionName' | [[ : ]] || 'conditionsParameter' + 'empty conditions' | 'validConditionName' | [[:]] || 'conditionsParameter' + 'too many properties' | 'validConditionName' | [[key1:'value1', key2:'value2']] || 'conditionsParameter' + 'empty key' | 'validConditionName' | [['':'wrong']] || 'conditionsParameter' + } + + def 'CM Handle Query validation: validate module name condition properties - valid query.'() { + given: 'a condition property' + def conditionProperty = [moduleName: 'value'] + when: 'validator is invoked' + RestQueryParametersValidator.validateModuleNameConditionProperties(conditionProperty) + then: 'data validation exception is not thrown' + noExceptionThrown() + } + + def 'CM Handle Query validation: validate module name condition properties - #scenario.'() { + when: 'validator is invoked' + RestQueryParametersValidator.validateModuleNameConditionProperties(conditionProperty) + then: 'a data validation exception is thrown' + thrown(DataValidationException) + where: + scenario | conditionProperty + 'invalid value' | [moduleName: ''] + 'invalid name' | [wrongName: 'value'] + } + + def 'Validate CmHandle where an exception is thrown due to #scenario.'() { + when: 'the validator is called on a cps path condition property' + RestQueryParametersValidator.validateCpsPathConditionProperties(conditionProperty) + then: 'a data validation exception is thrown' + def e = thrown(DataValidationException) + and: 'exception message matches the expected message' + e.details.contains(exceptionMessage) + where: + scenario | conditionProperty || exceptionMessage + 'more than one condition is supplied' | ['cpsPath':'some-path', 'cpsPath2':'some-path'] || 'Only one condition property is allowed for the CPS path query.' + 'cpsPath key not supplied' | ['wrong-key':'some-path'] || 'Wrong CPS path condition property. - expecting "cpsPath" as the condition property.' + 'cpsPath not supplied' | ['cpsPath':''] || 'Wrong CPS path. - please supply a valid CPS path.' + } + + def 'No conditions.'() { + expect: 'no conditions always returns true' + RestQueryParametersValidator.validateCpsPathConditionProperties([:]) == true + } + + def 'Validate CmHandle where #scenario.'() { + when: 'the validator is called on a cps path condition property' + def result = RestQueryParametersValidator.validateCpsPathConditionProperties(['cpsPath':cpsPath]) + then: 'the expected boolean value is returned' + result == expectedBoolean + where: + scenario | cpsPath || expectedBoolean + 'cpsPath is valid' | '/some/valid/path' || true + 'cpsPath attempts to query private properties' | "//additional-properties[@some-property='some-value']" || false + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/YangDataConverterSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/YangDataConverterSpec.groovy new file mode 100644 index 000000000..3ef3df5ef --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/utils/YangDataConverterSpec.groovy @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START======================================================== + * Copyright (C) 2022 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.onap.cps.ncmp.api.impl.utils + +import org.onap.cps.spi.model.DataNode +import spock.lang.Specification + +class YangDataConverterSpec extends Specification{ + + def 'Convert a cm handle data node with private properties.'() { + given: 'a datanode with some additional (dmi, private) properties' + def dataNodeAdditionalProperties = new DataNode(xpath:'/additional-properties[@name="dmiProp1"]', + leaves: ['name': 'dmiProp1', 'value': 'dmiValue1']) + def dataNode = new DataNode(childDataNodes:[dataNodeAdditionalProperties]) + when: 'the dataNode is converted' + def yangModelCmHandle = YangDataConverter.convertCmHandleToYangModel(dataNode,'sample-id') + then: 'the converted object has the correct id' + assert yangModelCmHandle.id == 'sample-id' + and: 'the additional (dmi, private) properties are included' + assert yangModelCmHandle.dmiProperties[0].name == 'dmiProp1' + assert yangModelCmHandle.dmiProperties[0].value == 'dmiValue1' + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImplSpec.groovy index 752b9f3ec..2800f5c24 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/CmHandleQueriesImplSpec.groovy @@ -37,10 +37,13 @@ class CmHandleQueriesImplSpec extends Specification { @Shared def static sampleDataNodes = [new DataNode()] + def dataNodeWithPrivateField = '//additional-properties[@name=\"Contact3\" and @value=\"newemailforstore3@bookstore.com\"]/ancestor::cm-handles' + def static pnfDemo = createDataNode('PNFDemo') def static pnfDemo2 = createDataNode('PNFDemo2') def static pnfDemo3 = createDataNode('PNFDemo3') def static pnfDemo4 = createDataNode('PNFDemo4') + def static pnfDemo5 = createDataNode('PNFDemo5') def static pnfDemoCmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo') def static pnfDemo2CmHandle = new NcmpServiceCmHandle(cmHandleId: 'PNFDemo2') @@ -69,6 +72,22 @@ class CmHandleQueriesImplSpec extends Specification { returnedCmHandlesWithData.keySet().size() == 0 } + def 'Query CmHandles using empty private properties query pair.'() { + when: 'a query on CmHandle private properties is executed using an empty map' + def returnedCmHandlesWithData = objectUnderTest.queryCmHandleAdditionalProperties([:]) + then: 'no cm handles are returned' + returnedCmHandlesWithData.keySet().size() == 0 + } + + def 'Query CmHandles by a private field\'s value.'() { + given: 'a data node exists with a certain additional-property' + cpsDataPersistenceService.queryDataNodes(_, _, dataNodeWithPrivateField, _) >> [pnfDemo5] + when: 'a query on CmHandle private properties is executed using a map' + def returnedCmHandlesWithData = objectUnderTest.queryCmHandleAdditionalProperties(['Contact3': 'newemailforstore3@bookstore.com']) + then: 'one cm handle is returned' + returnedCmHandlesWithData.keySet().size() == 1 + } + def 'Combine two query results where #scenario.'() { when: 'two query results in the form of a map of NcmpServiceCmHandles are combined into a single query result' def result = objectUnderTest.combineCmHandleQueries(firstQuery, secondQuery) @@ -85,7 +104,7 @@ class CmHandleQueriesImplSpec extends Specification { 'both queries are null' | null | null || null } - def 'Get Cm Handles By State'() { + def 'Get CmHandles by it\'s state.'() { given: 'a cm handle state to query' def cmHandleState = CmHandleState.ADVISED and: 'the persistence service returns a list of data nodes' @@ -97,6 +116,22 @@ class CmHandleQueriesImplSpec extends Specification { assert result == sampleDataNodes } + def 'Check the state of a cmHandle when #scenario.'() { + given: 'a cm handle state to compare' + def cmHandleState = state + and: 'the persistence service returns a list of data nodes' + cpsDataPersistenceService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', + '/dmi-registry/cm-handles[@id=\'some-cm-handle\']/state', OMIT_DESCENDANTS) >> new DataNode(leaves: ['cm-handle-state': 'READY']) + when: 'cm handles are compared by state' + def result = objectUnderTest.cmHandleHasState('some-cm-handle', cmHandleState) + then: 'the returned result matches the expected result from the persistence service' + result == expectedResult + where: + scenario | state || expectedResult + 'the provided state matches' | CmHandleState.READY || true + 'the provided state does not match'| CmHandleState.DELETED || false + } + def 'Get Cm Handles state by Cm-Handle Id'() { given: 'a cm handle state to query' def cmHandleState = CmHandleState.READY diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy new file mode 100644 index 000000000..f7c41ecdf --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/kafka/MessagingBaseSpec.groovy @@ -0,0 +1,71 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2022 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.onap.cps.ncmp.api.kafka + +import org.apache.kafka.common.serialization.StringDeserializer +import org.apache.kafka.common.serialization.StringSerializer +import org.springframework.kafka.core.DefaultKafkaProducerFactory +import org.springframework.kafka.core.KafkaTemplate +import org.springframework.kafka.support.serializer.JsonSerializer +import org.springframework.test.context.DynamicPropertyRegistry +import org.springframework.test.context.DynamicPropertySource +import org.testcontainers.containers.KafkaContainer +import org.testcontainers.utility.DockerImageName +import spock.lang.Specification + +class MessagingBaseSpec extends Specification { + + static { + Runtime.getRuntime().addShutdownHook(new Thread(kafkaTestContainer::stop)) + } + + def setupSpec() { + kafkaTestContainer.start() + } + + static kafkaTestContainer = new KafkaContainer(DockerImageName.parse('registry.nordix.org/onaptest/confluentinc/cp-kafka:6.2.1').asCompatibleSubstituteFor('confluentinc/cp-kafka')) + + def producerConfigProperties() { + return [('bootstrap.servers'): kafkaTestContainer.getBootstrapServers().split(',')[0], + ('retries') : 0, + ('batch-size') : 16384, + ('linger.ms') : 1, + ('buffer.memory') : 33554432, + ('key.serializer') : StringSerializer, + ('value.serializer') : JsonSerializer] + } + + def consumerConfigProperties(consumerGroupId) { + return [('bootstrap.servers') : kafkaTestContainer.getBootstrapServers().split(',')[0], + ('key.deserializer') : StringDeserializer, + ('value.deserializer'): StringDeserializer, + ('auto.offset.reset') : 'earliest', + ('group.id') : consumerGroupId + ] + } + + def kafkaTemplate = new KafkaTemplate<>(new DefaultKafkaProducerFactory(producerConfigProperties())) + + @DynamicPropertySource + static void registerKafkaProperties(DynamicPropertyRegistry dynamicPropertyRegistry) { + dynamicPropertyRegistry.add('spring.kafka.bootstrap-servers', kafkaTestContainer::getBootstrapServers) + } +} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/DmiServiceUrlBuilderSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/DmiServiceUrlBuilderSpec.groovy deleted file mode 100644 index 09f455031..000000000 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/DmiServiceUrlBuilderSpec.groovy +++ /dev/null @@ -1,83 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2022 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.onap.cps.ncmp.api.utils - -import org.onap.cps.spi.utils.CpsValidator - -import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING - -import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle -import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration -import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder -import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle -import spock.lang.Shared -import spock.lang.Specification - -class DmiServiceUrlBuilderSpec extends Specification { - - @Shared - YangModelCmHandle yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle('dmiServiceName', - 'dmiDataServiceName', 'dmiModuleServiceName', new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')) - - NcmpConfiguration.DmiProperties dmiProperties = new NcmpConfiguration.DmiProperties() - - def mockCpsValidator = Mock(CpsValidator) - - def objectUnderTest = new DmiServiceUrlBuilder(dmiProperties, mockCpsValidator) - - def 'Create the dmi service url with #scenario.'() { - given: 'uri variables' - dmiProperties.dmiBasePath = 'dmi' - def uriVars = objectUnderTest.populateUriVariables(yangModelCmHandle, - "cmHandle", PASSTHROUGH_RUNNING) - and: 'query params' - def uriQueries = objectUnderTest.populateQueryParams(resourceId, - 'optionsParamInQuery', topic) - when: 'a dmi datastore service url is generated' - def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars) - then: 'service url is generated as expected' - assert dmiServiceUrl == expectedDmiServiceUrl - where: 'the following parameters are used' - scenario | topic | resourceId || expectedDmiServiceUrl - 'With valid resourceId' | 'topicParamInQuery' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery' - 'With Empty resourceId' | 'topicParamInQuery' | '' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?options=optionsParamInQuery&topic=topicParamInQuery' - 'With Empty dmi base path' | 'topicParamInQuery' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery&topic=topicParamInQuery' - 'With Empty topicParamInQuery' | '' | 'resourceId' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running?resourceIdentifier=resourceId&options=optionsParamInQuery' - } - - def 'Populate dmi data store url #scenario.'() { - given: 'uri variables are created' - dmiProperties.dmiBasePath = dmiBasePath - def uriVars = objectUnderTest.populateUriVariables(yangModelCmHandle, - "cmHandle", PASSTHROUGH_RUNNING) - and: 'null query params' - def uriQueries = objectUnderTest.populateQueryParams(null, - null, null) - when: 'a dmi datastore service url is generated' - def dmiServiceUrl = objectUnderTest.getDmiDatastoreUrl(uriQueries, uriVars) - then: 'the created dmi service url matches the expected' - assert dmiServiceUrl == expectedDmiServiceUrl - where: 'the following parameters are used' - scenario | decription | dmiBasePath || expectedDmiServiceUrl - 'with base path / ' | 'Invalid base path as it starts with /' | '/dmi' || 'dmiServiceName//dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running' - 'without base path / ' | 'Valid path as it does not starts with /' | 'dmi' || 'dmiServiceName/dmi/v1/ch/cmHandle/data/ds/ncmp-datastore:passthrough-running' - } -} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/MessagingSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/MessagingSpec.groovy deleted file mode 100644 index 712c2b8f1..000000000 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/MessagingSpec.groovy +++ /dev/null @@ -1,71 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (c) 2022 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.onap.cps.ncmp.api.utils - -import org.apache.kafka.common.serialization.StringDeserializer -import org.apache.kafka.common.serialization.StringSerializer -import org.springframework.kafka.core.DefaultKafkaProducerFactory -import org.springframework.kafka.core.KafkaTemplate -import org.springframework.kafka.support.serializer.JsonSerializer -import org.springframework.test.context.DynamicPropertyRegistry -import org.springframework.test.context.DynamicPropertySource -import org.testcontainers.containers.KafkaContainer -import org.testcontainers.utility.DockerImageName -import spock.lang.Specification - -class MessagingSpec extends Specification { - - static { - Runtime.getRuntime().addShutdownHook(new Thread(kafkaTestContainer::stop)) - } - - def setupSpec() { - kafkaTestContainer.start() - } - - static kafkaTestContainer = new KafkaContainer(DockerImageName.parse('registry.nordix.org/onaptest/confluentinc/cp-kafka:6.2.1').asCompatibleSubstituteFor('confluentinc/cp-kafka')) - - def producerConfigProperties() { - return [('bootstrap.servers'): kafkaTestContainer.getBootstrapServers().split(',')[0], - ('retries') : 0, - ('batch-size') : 16384, - ('linger.ms') : 1, - ('buffer.memory') : 33554432, - ('key.serializer') : StringSerializer, - ('value.serializer') : JsonSerializer] - } - - def consumerConfigProperties(consumerGroupId) { - return [('bootstrap.servers') : kafkaTestContainer.getBootstrapServers().split(',')[0], - ('key.deserializer') : StringDeserializer, - ('value.deserializer'): StringDeserializer, - ('auto.offset.reset') : 'earliest', - ('group.id') : consumerGroupId - ] - } - - def kafkaTemplate = new KafkaTemplate<>(new DefaultKafkaProducerFactory(producerConfigProperties())) - - @DynamicPropertySource - static void registerKafkaProperties(DynamicPropertyRegistry dynamicPropertyRegistry) { - dynamicPropertyRegistry.add('spring.kafka.bootstrap-servers', kafkaTestContainer::getBootstrapServers) - } -} diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/YangDataConverterSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/YangDataConverterSpec.groovy deleted file mode 100644 index 20d384fa5..000000000 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/utils/YangDataConverterSpec.groovy +++ /dev/null @@ -1,42 +0,0 @@ -/* - * ============LICENSE_START======================================================== - * Copyright (C) 2022 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.onap.cps.ncmp.api.utils - -import org.onap.cps.ncmp.api.impl.utils.YangDataConverter -import org.onap.cps.spi.model.DataNode -import spock.lang.Specification - -class YangDataConverterSpec extends Specification{ - - def 'Convert a cm handle data node with private properties.'() { - given: 'a datanode with some additional (dmi, private) properties' - def dataNodeAdditionalProperties = new DataNode(xpath:'/additional-properties[@name="dmiProp1"]', - leaves: ['name': 'dmiProp1', 'value': 'dmiValue1']) - def dataNode = new DataNode(childDataNodes:[dataNodeAdditionalProperties]) - when: 'the dataNode is converted' - def yangModelCmHandle = YangDataConverter.convertCmHandleToYangModel(dataNode,'sample-id') - then: 'the converted object has the correct id' - assert yangModelCmHandle.id == 'sample-id' - and: 'the additional (dmi, private) properties are included' - assert yangModelCmHandle.dmiProperties[0].name == 'dmiProp1' - assert yangModelCmHandle.dmiProperties[0].value == 'dmiValue1' - } -} -- cgit 1.2.3-korg