Changing putOperationWithJson to postOperationWithJson
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplModelSyncSpec.groovy
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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 com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.api.CpsAdminService
25 import org.onap.cps.api.CpsModuleService
26 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
27 import org.onap.cps.ncmp.api.models.PersistenceCmHandle
28 import org.onap.cps.spi.model.ModuleReference
29 import spock.lang.Specification
30
31 class NetworkCmProxyDataServiceImplModelSyncSpec extends Specification {
32
33     def mockCpsModuleService = Mock(CpsModuleService)
34     def mockCpsAdminService = Mock(CpsAdminService)
35     def mockDmiModelOperations = Mock(DmiModelOperations)
36
37     def objectUnderTest = new NetworkCmProxyDataServiceImpl(null, mockDmiModelOperations,
38         mockCpsModuleService, null, null, mockCpsAdminService, new ObjectMapper())
39
40     def expectedDataspaceName = 'NFP-Operational'
41
42     def 'Sync model for a (new) cm handle with #scenario'() {
43         given: 'persistence cm handle is given'
44             def cmHandleForModelSync = new PersistenceCmHandle(id:'some cm handle', dmiServiceName: 'some service name')
45         and: 'additional properties are set as required'
46             if (additionalProperties!=null) {
47                 cmHandleForModelSync.asAdditionalProperties(additionalProperties)
48             }
49         and: 'dmi operations returns some module references'
50             def moduleReferences =  [ new ModuleReference(moduleName:'module1',revision:'1'),
51                                                             new ModuleReference(moduleName:'module2',revision:'2') ]
52             mockDmiModelOperations.getModuleReferences(cmHandleForModelSync) >> moduleReferences
53         and: 'CPS-Core returns list of existing module resources'
54             mockCpsModuleService.getYangResourceModuleReferences(expectedDataspaceName) >> existingModuleResourcesInCps
55         and: 'DMI-Plugin returns resource(s) for "new" module(s)'
56             mockDmiModelOperations.getNewYangResourcesFromDmi(cmHandleForModelSync, [new ModuleReference('module1', '1')]) >> yangResourceToContentMap
57         when: 'module sync is triggered'
58             objectUnderTest.syncModulesAndCreateAnchor(cmHandleForModelSync)
59         then: 'the CPS module service is called once with the correct parameters'
60             1 * mockCpsModuleService.createSchemaSetFromModules(expectedDataspaceName, cmHandleForModelSync.getId(), yangResourceToContentMap, expectedKnownModules)
61         and: 'admin service create anchor method has been called with correct parameters'
62             1 * mockCpsAdminService.createAnchor(expectedDataspaceName, cmHandleForModelSync.getId(), cmHandleForModelSync.getId())
63         where: 'the following parameters are used'
64             scenario                        | additionalProperties | existingModuleResourcesInCps                                               | yangResourceToContentMap      || expectedKnownModules                                                       | expectedJsonForAdditionalProperties
65             'one unknown module'            | ['name1': 'value1']  | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | [module1: 'some yang source'] || [new ModuleReference('module2', '2')]                                      | '{"name1":"value1"}'
66             'no add. properties'            | [:]                  | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | [module1: 'some yang source'] || [new ModuleReference('module2', '2')]                                      | '{}'
67             'additional properties is null' | null                 | [new ModuleReference('module2', '2'), new ModuleReference('module3', '3')] | [module1: 'some yang source'] || [new ModuleReference('module2', '2')]                                      | '{}'
68             'no unknown module'             | [:]                  | [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')] | [:]                           || [new ModuleReference('module1', '1'), new ModuleReference('module2', '2')] | '{}'
69     }
70 }