Create Endpoint For Get Cm Handles By Name
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplModelSyncSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl
22
23 import org.onap.cps.api.CpsAdminService
24 import org.onap.cps.api.CpsModuleService
25 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
26 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
27 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
30 import org.onap.cps.spi.model.ModuleReference
31 import org.onap.cps.utils.JsonObjectMapper
32 import spock.lang.Specification
33
34 class NetworkCmProxyDataServiceImplModelSyncSpec extends Specification {
35
36     def nullCpsDataService = null
37     def mockJsonObjectMapper = Mock(JsonObjectMapper)
38     def mockCpsModuleService = Mock(CpsModuleService)
39     def mockCpsAdminService = Mock(CpsAdminService)
40     def mockDmiModelOperations = Mock(DmiModelOperations)
41     def mockDmiDataOperations = Mock(DmiDataOperations)
42     def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
43     def nullNetworkCmProxyDataServicePropertyHandler = null
44
45     def objectUnderTest = new NetworkCmProxyDataServiceImpl(nullCpsDataService, mockJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
46             mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler,mockYangModelCmHandleRetriever)
47
48     def expectedDataspaceName = 'NFP-Operational'
49
50     def 'Sync model for a (new) cm handle with #scenario'() {
51         given: 'a cm handle'
52             def ncmpServiceCmHandle = new NcmpServiceCmHandle()
53             def dmiServiceName = 'some service name'
54             ncmpServiceCmHandle.cmHandleID = 'cm handle id 1'
55             def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '' , '', ncmpServiceCmHandle)
56         and: 'DMI operations returns some module references'
57             def moduleReferences =  [ new ModuleReference(moduleName:'module1',revision:'1'),
58                                                             new ModuleReference(moduleName:'module2',revision:'2') ]
59             mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
60         and: 'CPS-Core returns list of existing module resources'
61             mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
62         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
63             mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> yangResourceToContentMap
64         when: 'module sync is triggered'
65             mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
66             objectUnderTest.syncModulesAndCreateAnchor(yangModelCmHandle)
67         then: 'the CPS module service is called once with the correct parameters'
68             1 * mockCpsModuleService.createSchemaSetFromModules(expectedDataspaceName, yangModelCmHandle.getId(), yangResourceToContentMap, toModuleReference(expectedKnownModules))
69         and: 'admin service create anchor method has been called with correct parameters'
70             1 * mockCpsAdminService.createAnchor(expectedDataspaceName, yangModelCmHandle.getId(), yangModelCmHandle.getId())
71         where: 'the following parameters are used'
72             scenario             | existingModuleResourcesInCps           | identifiedNewModuleReferences | yangResourceToContentMap      || expectedKnownModules
73             'one new module'     | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source'] || [['module2' : '2']]
74             'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source'] || [['module2' : '2']]
75             'no new module'      | [['module1' : '1'], ['module2' : '2']] | []                            | [:]                           || [['module1' : '1'], ['module2' : '2']]
76     }
77
78     def toModuleReference(moduleReferenceAsMap) {
79         def moduleReferences = [].withDefault { [:] }
80         moduleReferenceAsMap.forEach(property ->
81             property.forEach((moduleName, revision) -> {
82                 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
83             }))
84         return moduleReferences
85     }
86
87 }