From e5254a7b007e4a0dc59003d94f43e688c25cf7d1 Mon Sep 17 00:00:00 2001 From: lukegleeson Date: Fri, 26 Aug 2022 10:55:04 +0100 Subject: CPS Validator Changes Changed CPS Validator from static to non-static Moved CPSValidator implementation to rest interface layer to prevent duplicated usage Created CPSValidator interface in persistence layer Removed unnecessary tests Refactored misplaced tests Copyright fixes Copyright Check Script output formatting fix Issue-ID: CPS-1228 Signed-off-by: lukegleeson Change-Id: I4c99a51870e7d3b8e416bc1f909aeed5dc3baa42 --- ...rkCmProxyDataServiceImplRegistrationSpec.groovy | 3 +- .../impl/NetworkCmProxyDataServiceImplSpec.groovy | 53 +++------------------- .../impl/operations/DmiOperationsBaseSpec.groovy | 7 ++- .../inventory/InventoryPersistenceImplSpec.groovy | 40 +++++++--------- .../ncmp/api/utils/DmiServiceUrlBuilderSpec.groovy | 7 ++- 5 files changed, 34 insertions(+), 76 deletions(-) (limited to 'cps-ncmp-service/src/test') diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy index 3a0f3c73b2..e6c79f89a1 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplRegistrationSpec.groovy @@ -1,5 +1,5 @@ /* - * ============LICENSE_START======================================================= + * ============LICENSE_START======================================================= * Copyright (C) 2021-2022 Nordix Foundation * Modifications Copyright (C) 2022 Bell Canada * ================================================================================ @@ -222,7 +222,6 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { where: scenario | cmHandleId | exception || expectedError | expectedErrorText 'cm-handle already exist' | 'cmhandle' | new AlreadyDefinedExceptionBatch(["path[@id='${cmHandleId}']".toString()]) || CM_HANDLE_ALREADY_EXIST | 'cm-handle already exists' - 'cm-handle has invalid name' | 'cm handle with space' | new DataValidationException("", "") || CM_HANDLE_INVALID_ID | 'cm-handle has an invalid character(s) in id' 'unknown exception while registering cm-handle' | 'cmhandle' | new RuntimeException('Failed') || UNKNOWN_ERROR | 'Failed' } 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 91eb218eaf..58ca06bee4 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,10 +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.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 spock.lang.Shared @@ -55,6 +57,10 @@ 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 { @@ -105,17 +111,6 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { >> { new ResponseEntity<>(HttpStatus.CREATED) } } - def 'Write resource data for pass-through running from DMI using an invalid id.'() { - when: 'write resource data is called' - objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('invalid cm handle name', - 'testResourceId', CREATE, - '{some-json}', 'application/json') - then: 'exception is thrown' - thrown(DataValidationException.class) - and: 'DMI is not invoked' - 0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _) - } - def 'Get resource data for pass-through operational from DMI.'() { given: 'get data node is called' mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', @@ -166,15 +161,6 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { 1 * mockInventoryPersistence.getYangResourcesModuleReferences('some-cm-handle') } - def 'Getting Yang Resources with an invalid #scenario.'() { - when: 'yang resources is called' - objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces') - then: 'a data validation exception is thrown' - thrown(DataValidationException) - and: 'CPS module services is not invoked' - 0 * mockInventoryPersistence.getYangResourcesModuleReferences(*_) - } - def 'Get a cm handle.'() { given: 'the system returns a yang modelled cm handle' def dmiServiceName = 'some service name' @@ -203,15 +189,6 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { } - def 'Get a cm handle with an invalid id.'() { - when: 'getting cm handle details for a given cm handle id with an invalid name' - objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces') - then: 'an exception is thrown' - thrown(DataValidationException) - and: 'the yang model cm handle retriever is not invoked' - 0 * mockInventoryPersistence.getYangModelCmHandle(*_) - } - def 'Get cm handle public properties'() { given: 'a yang modelled cm handle' def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')] @@ -225,15 +202,6 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { result == [ 'public prop' : 'some public prop' ] } - def 'Get cm handle public properties with an invalid id.'() { - when: 'getting cm handle public properties for a given cm handle id with an invalid name' - objectUnderTest.getCmHandlePublicProperties('invalid cm handle with spaces') - then: 'an exception is thrown' - thrown(DataValidationException) - and: 'the yang model cm handle retriever is not invoked' - 0 * mockInventoryPersistence.getYangModelCmHandle(*_) - } - def 'Get cm handle composite state'() { given: 'a yang modelled cm handle' def compositeState = new CompositeState(cmHandleState: CmHandleState.ADVISED, @@ -252,15 +220,6 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { result == compositeState } - def 'Get cm handle composite state with an invalid id.'() { - when: 'getting cm handle composite state for a given cm handle id with an invalid name' - objectUnderTest.getCmHandleCompositeState('invalid cm handle with spaces') - then: 'an exception is thrown' - thrown(DataValidationException) - and: 'the yang model cm handle retriever is not invoked' - 0 * mockInventoryPersistence.getYangModelCmHandle(_) - } - def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() { given: 'cpsDataService returns valid datanode' mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy index 3a82ee3400..c4d0020a6c 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/operations/DmiOperationsBaseSpec.groovy @@ -15,7 +15,7 @@ * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= + * ============LICENSE_END========================================================= */ package org.onap.cps.ncmp.api.impl.operations @@ -28,6 +28,7 @@ import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder import org.onap.cps.ncmp.api.inventory.CmHandleState import org.onap.cps.ncmp.api.inventory.CompositeState import org.onap.cps.ncmp.api.inventory.InventoryPersistence +import org.onap.cps.spi.utils.CpsValidator import org.spockframework.spring.SpringBean import spock.lang.Shared import spock.lang.Specification @@ -43,11 +44,13 @@ abstract class DmiOperationsBaseSpec extends Specification { @SpringBean InventoryPersistence mockInventoryPersistence = Mock() + def mockCpsValidator = Mock(CpsValidator) + @SpringBean ObjectMapper spyObjectMapper = Spy() @SpringBean - DmiServiceUrlBuilder dmiServiceUrlBuilder = new DmiServiceUrlBuilder(new NcmpConfiguration.DmiProperties()) + DmiServiceUrlBuilder dmiServiceUrlBuilder = new DmiServiceUrlBuilder(new NcmpConfiguration.DmiProperties(), mockCpsValidator) def yangModelCmHandle = new YangModelCmHandle() def static dmiServiceName = 'some service name' diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImplSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImplSpec.groovy index 0d459fd0fa..c713aad3c4 100644 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImplSpec.groovy +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/InventoryPersistenceImplSpec.groovy @@ -29,11 +29,11 @@ import org.onap.cps.spi.CascadeDeleteAllowed import org.onap.cps.spi.CpsDataPersistenceService import org.onap.cps.spi.CpsAdminPersistenceService import org.onap.cps.spi.FetchDescendantsOption -import org.onap.cps.spi.exceptions.DataValidationException import org.onap.cps.spi.model.DataNode import org.onap.cps.spi.model.ModuleDefinition import org.onap.cps.spi.model.ModuleReference import org.onap.cps.utils.JsonObjectMapper +import org.onap.cps.spi.utils.CpsValidator import spock.lang.Shared import spock.lang.Specification @@ -56,8 +56,10 @@ class InventoryPersistenceImplSpec extends Specification { def mockCpsAdminPersistenceService = Mock(CpsAdminPersistenceService) + def mockCpsValidator = Mock(CpsValidator) + def objectUnderTest = new InventoryPersistenceImpl(spiedJsonObjectMapper, mockCpsDataService, mockCpsModuleService, - mockCpsDataPersistenceService, mockCpsAdminPersistenceService) + mockCpsDataPersistenceService, mockCpsAdminPersistenceService, mockCpsValidator) def formattedDateAndTime = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ") .format(OffsetDateTime.of(2022, 12, 31, 20, 30, 40, 1, ZoneOffset.UTC)) @@ -95,34 +97,29 @@ class InventoryPersistenceImplSpec extends Specification { result.publicProperties == expectedPublicProperties and: 'the state details are returned' result.compositeState.cmHandleState == expectedCompositeState + and: 'the CM Handle ID is validated' + 1 * mockCpsValidator.validateNameCharacters(cmHandleId) where: 'the following parameters are used' scenario | childDataNodes || expectedDmiProperties || expectedPublicProperties || expectedCompositeState 'no properties' | [] || [] || [] || null - 'DMI and public properties' | childDataNodesForCmHandleWithAllProperties || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")] || null + 'DMI and public properties' | childDataNodesForCmHandleWithAllProperties || [new YangModelCmHandle.Property("name1", "value1")] || [new YangModelCmHandle.Property("name2", "value2")] || null 'just DMI properties' | childDataNodesForCmHandleWithDMIProperties || [new YangModelCmHandle.Property("name1", "value1")] || [] || null 'just public properties' | childDataNodesForCmHandleWithPublicProperties || [] || [new YangModelCmHandle.Property("name2", "value2")] || null 'with state details' | childDataNodesForCmHandleWithState || [] || [] || CmHandleState.ADVISED } - def "Retrieve CmHandle using datanode with invalid CmHandle id."() { - when: 'retrieving the yang modelled cm handle with an invalid id' - def result = objectUnderTest.getYangModelCmHandle('cm handle id with spaces') - then: 'a data validation exception is thrown' - thrown(DataValidationException) - and: 'the result is not returned' - result == null - } - - def "Handling missing service names as null CPS-1043."() { + def "Handling missing service names as null."() { given: 'the cps data service returns a data node from the DMI registry with empty child and leaf attributes' def dataNode = new DataNode(childDataNodes:[], leaves: [:]) mockCpsDataPersistenceService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry', xpath, INCLUDE_ALL_DESCENDANTS) >> dataNode when: 'retrieving the yang modelled cm handle' def result = objectUnderTest.getYangModelCmHandle(cmHandleId) - then: 'the service names ae returned as null' + then: 'the service names are returned as null' result.dmiServiceName == null result.dmiDataServiceName == null result.dmiModelServiceName == null + and: 'the CM Handle ID is validated' + 1 * mockCpsValidator.validateNameCharacters(cmHandleId) } def 'Get a Cm Handle Composite State'() { @@ -136,6 +133,8 @@ class InventoryPersistenceImplSpec extends Specification { def result = objectUnderTest.getCmHandleState(cmHandleId) then: 'result has returned the correct cm handle state' result.cmHandleState == CmHandleState.ADVISED + and: 'the CM Handle ID is validated' + 1 * mockCpsValidator.validateNameCharacters(cmHandleId) } def 'Update Cm Handle with #scenario State'() { @@ -187,6 +186,8 @@ class InventoryPersistenceImplSpec extends Specification { def result = objectUnderTest.getYangResourcesModuleReferences('some-cmHandle-Id') then: 'the returned result is a collection of module definitions' assert result == moduleReferences + and: 'the CM Handle ID is validated' + 1 * mockCpsValidator.validateNameCharacters('some-cmHandle-Id') } def 'Save Cmhandle'() { @@ -230,15 +231,8 @@ class InventoryPersistenceImplSpec extends Specification { objectUnderTest.deleteSchemaSetWithCascade('validSchemaSetName') then: 'the module service to delete schemaSet is invoked once' 1 * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'validSchemaSetName', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) - } - - def 'Delete schema set with an invalid schema set name'() { - when: 'the method to delete schema set is called with an invalid schema set name' - objectUnderTest.deleteSchemaSetWithCascade('invalid SchemaSet name') - then: 'a data validation exception is thrown' - thrown(DataValidationException) - and: 'the module service to delete schemaSet is not called' - 0 * mockCpsModuleService.deleteSchemaSet('NFP-Operational', 'sampleSchemaSetName', CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED) + and: 'the CM Handle ID is validated' + 1 * mockCpsValidator.validateNameCharacters('validSchemaSetName') } def 'Get data node via xPath'() { 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 index 2d993973eb..09f4550319 100644 --- 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 @@ -15,11 +15,12 @@ * limitations under the License. * * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= + * ============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 @@ -38,7 +39,9 @@ class DmiServiceUrlBuilderSpec extends Specification { NcmpConfiguration.DmiProperties dmiProperties = new NcmpConfiguration.DmiProperties() - def objectUnderTest = new DmiServiceUrlBuilder(dmiProperties) + def mockCpsValidator = Mock(CpsValidator) + + def objectUnderTest = new DmiServiceUrlBuilder(dmiProperties, mockCpsValidator) def 'Create the dmi service url with #scenario.'() { given: 'uri variables' -- cgit 1.2.3-korg