Fix ANTLR version issue
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncServiceSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.inventory.sync
22
23 import org.onap.cps.api.CpsModuleService
24 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
25 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
26 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
27 import org.onap.cps.spi.model.ModuleReference
28 import spock.lang.Specification
29
30 class ModuleSyncServiceSpec extends Specification {
31
32
33     def mockCpsModuleService = Mock(CpsModuleService)
34     def mockDmiModelOperations = Mock(DmiModelOperations)
35
36     def objectUnderTest = new ModuleSyncService(mockDmiModelOperations, mockCpsModuleService)
37
38     def expectedDataspaceName = 'NFP-Operational'
39
40     def 'Sync model for a (new) cm handle with #scenario'() {
41         given: 'a cm handle'
42             def ncmpServiceCmHandle = new NcmpServiceCmHandle()
43             def dmiServiceName = 'some service name'
44             ncmpServiceCmHandle.cmHandleId = 'cmHandleId-1'
45             def yangModelCmHandle = YangModelCmHandle.toYangModelCmHandle(dmiServiceName, '' , '', ncmpServiceCmHandle)
46         and: 'DMI operations returns some module references'
47             def moduleReferences =  [ new ModuleReference(moduleName:'module1',revision:'1'),
48                                                             new ModuleReference(moduleName:'module2',revision:'2') ]
49             mockDmiModelOperations.getModuleReferences(yangModelCmHandle) >> moduleReferences
50         and: 'CPS-Core returns list of existing module resources'
51             mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> toModuleReference(existingModuleResourcesInCps)
52         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
53             mockDmiModelOperations.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('module1', '1')]) >> yangResourceToContentMap
54         when: 'module sync is triggered'
55             mockCpsModuleService.identifyNewModuleReferences(moduleReferences) >> toModuleReference(identifiedNewModuleReferences)
56             def result = objectUnderTest.syncAndCreateSchemaSet(yangModelCmHandle)
57         then: 'the resulting schema set name is the same as the cm handle id'
58             assert result == 'cmHandleId-1'
59         where: 'the following parameters are used'
60             scenario             | existingModuleResourcesInCps           | identifiedNewModuleReferences | yangResourceToContentMap
61             'one new module'     | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']
62             'no add. properties' | [['module2' : '2'], ['module3' : '3']] | [['module1' : '1']]           | [module1: 'some yang source']
63             'no new module'      | [['module1' : '1'], ['module2' : '2']] | []                            | [:]
64     }
65
66     def toModuleReference(moduleReferenceAsMap) {
67         def moduleReferences = [].withDefault { [:] }
68         moduleReferenceAsMap.forEach(property ->
69             property.forEach((moduleName, revision) -> {
70                 moduleReferences.add(new ModuleReference('moduleName' : moduleName, 'revision' : revision))
71             }))
72         return moduleReferences
73     }
74
75 }