From 006c7dca01d6d04ce122eadc06707a5b0b70d76c Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Wed, 6 Apr 2022 16:24:52 +0100 Subject: Refactor existing model sync code into separate package - Created a new class to seperate sync related operations of modules. - Kept newly created class into .../api/inventory/sync package. - Created a separate groovy test. - Fixed existing groovy test. Issue-ID: CPS-950 Signed-off-by: sourabh_sourabh Change-Id: Ie7cc36ea75daf43f1383757bcf2057c5cb4894b1 --- ...tworkCmProxyDataServiceImplModelSyncSpec.groovy | 87 ---------------------- ...rkCmProxyDataServiceImplRegistrationSpec.groovy | 6 +- .../impl/NetworkCmProxyDataServiceImplSpec.groovy | 30 +++++++- .../inventory/sync/ModuleSyncServiceSpec.groovy | 75 +++++++++++++++++++ 4 files changed, 105 insertions(+), 93 deletions(-) delete mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy create mode 100644 cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncServiceSpec.groovy (limited to 'cps-ncmp-service/src/test/groovy/org') diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy deleted file mode 100644 index 673230e74..000000000 --- a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/impl/NetworkCmProxyDataServiceImplModelSyncSpec.groovy +++ /dev/null @@ -1,87 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2021-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 - -import org.onap.cps.api.CpsAdminService -import org.onap.cps.api.CpsModuleService -import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations -import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations -import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever -import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle -import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle -import org.onap.cps.spi.model.ModuleReference -import org.onap.cps.utils.JsonObjectMapper -import spock.lang.Specification - -class NetworkCmProxyDataServiceImplModelSyncSpec extends Specification { - - def nullCpsDataService = null - def mockJsonObjectMapper = Mock(JsonObjectMapper) - def mockCpsModuleService = Mock(CpsModuleService) - def mockCpsAdminService = Mock(CpsAdminService) - def mockDmiModelOperations = Mock(DmiModelOperations) - def mockDmiDataOperations = Mock(DmiDataOperations) - def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever) - def nullNetworkCmProxyDataServicePropertyHandler = null - - def objectUnderTest = new NetworkCmProxyDataServiceImpl(nullCpsDataService, mockJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations, - mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler,mockYangModelCmHandleRetriever) - - def expectedDataspaceName = 'NFP-Operational' - - def 'Sync model for a (new) cm handle with #scenario'() { - given: 'a cm handle' - def ncmpServiceCmHandle = new NcmpServiceCmHandle() - def dmiServiceName = 'some service name' - ncmpServiceCmHandle.cmHandleId = 'cm-handle-id-1' - def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '' , '', ncmpServiceCmHandle) - and: 'DMI operations returns some module references' - def moduleReferences = [ new ModuleReference(moduleName:'module1',revision:'1'), - new ModuleReference(moduleName:'module2',revision:'2') ] - mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences - and: 'CPS-Core returns list of existing module resources' - mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps) - and: 'DMI-Plugin returns resource(s) for "new" module(s)' - mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> yangResourceToContentMap - when: 'module sync is triggered' - mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences) - objectUnderTest.syncModulesAndCreateAnchor(yangModelCmHandle) - then: 'the CPS module service is called once with the correct parameters' - 1 * mockCpsModuleService.createSchemaSetFromModules(expectedDataspaceName, yangModelCmHandle.getId(), yangResourceToContentMap, toModuleReference(expectedKnownModules)) - and: 'admin service create anchor method has been called with correct parameters' - 1 * mockCpsAdminService.createAnchor(expectedDataspaceName, yangModelCmHandle.getId(), yangModelCmHandle.getId()) - where: 'the following parameters are used' - scenario | existingModuleResourcesInCps | identifiedNewModuleReferences | yangResourceToContentMap || expectedKnownModules - 'one new module' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] || [['module2' : '2']] - 'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] || [['module2' : '2']] - 'no new module' | [['module1' : '1'], ['module2' : '2']] | [] | [:] || [['module1' : '1'], ['module2' : '2']] - } - - def toModuleReference(moduleReferenceAsMap) { - def moduleReferences = [].withDefault { [:] } - moduleReferenceAsMap.forEach(property -> - property.forEach((moduleName, revision) -> { - moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision)) - })) - return moduleReferences - } - -} 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 1f41c6b8c..5683d57e5 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 @@ -33,6 +33,7 @@ import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever import org.onap.cps.ncmp.api.models.CmHandleRegistrationResponse import org.onap.cps.ncmp.api.models.DmiPluginRegistration import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle +import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService import org.onap.cps.spi.exceptions.AlreadyDefinedException import org.onap.cps.spi.exceptions.DataNodeNotFoundException import org.onap.cps.spi.exceptions.DataValidationException @@ -64,6 +65,7 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { def mockDmiDataOperations = Mock(DmiDataOperations) def mockNetworkCmProxyDataServicePropertyHandler = Mock(NetworkCmProxyDataServicePropertyHandler) def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever) + def mockModuleSyncService = Mock(ModuleSyncService) def noTimestamp = null def objectUnderTest = getObjectUnderTestWithModelSyncDisabled() @@ -386,7 +388,7 @@ class NetworkCmProxyDataServiceImplRegistrationSpec extends Specification { } def getObjectUnderTest() { - return Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations, - mockCpsModuleService, mockCpsAdminService, mockNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever)) + return Spy(new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, + mockCpsModuleService, mockCpsAdminService, mockNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever, mockModuleSyncService)) } } 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 7ddbbb21d..7629500db 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 @@ -25,7 +25,10 @@ package org.onap.cps.ncmp.api.impl import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle +import org.onap.cps.ncmp.api.models.DmiPluginRegistration +import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle import org.onap.cps.spi.exceptions.DataValidationException +import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService import spock.lang.Shared import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL @@ -34,7 +37,6 @@ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE -import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations import org.onap.cps.utils.JsonObjectMapper import com.fasterxml.jackson.core.JsonProcessingException import com.fasterxml.jackson.databind.ObjectMapper @@ -54,17 +56,21 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { def mockCpsModuleService = Mock(CpsModuleService) def mockCpsAdminService = Mock(CpsAdminService) def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper())) - def mockDmiModelOperations = Mock(DmiModelOperations) def mockDmiDataOperations = Mock(DmiDataOperations) def nullNetworkCmProxyDataServicePropertyHandler = null def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever) + def mockModuleSyncService = Mock(ModuleSyncService) + def mockDmiPluginRegistration = Mock(DmiPluginRegistration) + def NO_TOPIC = null def NO_REQUEST_ID = null @Shared def OPTIONS_PARAM = '(a=1,b=2)' + @Shared + def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id') - def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations, - mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever) + def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, + mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever, mockModuleSyncService) def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']" @@ -332,4 +338,20 @@ class NetworkCmProxyDataServiceImplSpec extends Specification { 'READ' | READ || 'Unable to read resource data.' 'UPDATE' | UPDATE || 'Unable to update resource data.' } + + def 'Verify modules and create anchor params'() { + given: 'dmi plugin registration return created cm handles' + def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1', + dmiDataPlugin: 'service2') + dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle] + mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle] + when: 'parse and create cm handle in dmi registration then sync module' + objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration) + then: 'validate params for creating anchor and list elements' + 1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry', + '/dmi-registry', '{"cm-handles":[{"id":"some-cm-handle-id",' + + '"additional-properties":[],"public-properties":[]}]}', null) + 1 * mockCpsAdminService.createAnchor('NFP-Operational', null, + 'some-cm-handle-id') + } } diff --git a/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncServiceSpec.groovy b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncServiceSpec.groovy new file mode 100644 index 000000000..37fdbeeb2 --- /dev/null +++ b/cps-ncmp-service/src/test/groovy/org/onap/cps/ncmp/api/inventory/sync/ModuleSyncServiceSpec.groovy @@ -0,0 +1,75 @@ +/* + * ============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.sync + +import org.onap.cps.api.CpsModuleService +import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations +import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle +import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle +import org.onap.cps.spi.model.ModuleReference +import spock.lang.Specification + +class ModuleSyncServiceSpec extends Specification { + + + def mockCpsModuleService = Mock(CpsModuleService) + def mockDmiModelOperations = Mock(DmiModelOperations) + + def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService) + + def expectedDataspaceName = 'NFP-Operational' + + def 'Sync model for a (new) cm handle with #scenario'() { + given: 'a cm handle' + def ncmpServiceCmHandle = new NcmpServiceCmHandle() + def dmiServiceName = 'some service name' + ncmpServiceCmHandle.cmHandleId = 'cmHandleId-1' + def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '' , '', ncmpServiceCmHandle) + and: 'DMI operations returns some module references' + def moduleReferences = [ new ModuleReference(moduleName:'module1',revision:'1'), + new ModuleReference(moduleName:'module2',revision:'2') ] + mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences + and: 'CPS-Core returns list of existing module resources' + mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps) + and: 'DMI-Plugin returns resource(s) for "new" module(s)' + mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> yangResourceToContentMap + when: 'module sync is triggered' + mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences) + def result = objectUnderTest.syncAndCreateSchemaSet(yangModelCmHandle) + then: 'the resulting schema set name is the same as the cm handle id' + assert result == 'cmHandleId-1' + where: 'the following parameters are used' + scenario | existingModuleResourcesInCps | identifiedNewModuleReferences | yangResourceToContentMap + 'one new module' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] + 'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']] | [module1: 'some yang source'] + 'no new module' | [['module1' : '1'], ['module2' : '2']] | [] | [:] + } + + def toModuleReference(moduleReferenceAsMap) { + def moduleReferences = [].withDefault { [:] } + moduleReferenceAsMap.forEach(property -> + property.forEach((moduleName, revision) -> { + moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision)) + })) + return moduleReferences + } + +} -- cgit 1.2.3-korg