Merge "Add module name to cps core output"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / NetworkCmProxyDataServiceImplSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 Nordix Foundation
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2021-2022 Bell Canada
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  SPDX-License-Identifier: Apache-2.0
20  *  ============LICENSE_END=========================================================
21  */
22
23 package org.onap.cps.ncmp.api.impl
24
25 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
27 import org.onap.cps.ncmp.api.inventory.CmHandleState
28 import org.onap.cps.ncmp.api.inventory.CompositeState
29 import org.onap.cps.ncmp.api.inventory.InventoryPersistence
30 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
31 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
32 import org.onap.cps.spi.exceptions.DataValidationException
33 import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService
34 import spock.lang.Shared
35
36 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
37 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
38 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
39 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
40
41 import org.onap.cps.utils.JsonObjectMapper
42 import com.fasterxml.jackson.databind.ObjectMapper
43 import org.onap.cps.api.CpsAdminService
44 import org.onap.cps.api.CpsDataService
45 import org.onap.cps.api.CpsModuleService
46 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
47 import org.onap.cps.spi.FetchDescendantsOption
48 import org.onap.cps.spi.model.DataNode
49 import org.springframework.http.HttpStatus
50 import org.springframework.http.ResponseEntity
51 import spock.lang.Specification
52
53 class NetworkCmProxyDataServiceImplSpec extends Specification {
54
55     def mockCpsDataService = Mock(CpsDataService)
56     def mockCpsModuleService = Mock(CpsModuleService)
57     def mockCpsAdminService = Mock(CpsAdminService)
58     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
59     def mockDmiDataOperations = Mock(DmiDataOperations)
60     def nullNetworkCmProxyDataServicePropertyHandler = null
61     def mockInventoryPersistence = Mock(InventoryPersistence)
62     def mockModuleSyncService = Mock(ModuleSyncService)
63     def mockDmiPluginRegistration = Mock(DmiPluginRegistration)
64
65     def NO_TOPIC = null
66     def NO_REQUEST_ID = null
67     @Shared
68     def OPTIONS_PARAM = '(a=1,b=2)'
69     @Shared
70     def ncmpServiceCmHandle = new NcmpServiceCmHandle(cmHandleId: 'some-cm-handle-id')
71
72     def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations,
73         mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockInventoryPersistence, mockModuleSyncService)
74
75     def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
76
77     def dataNode = new DataNode(leaves: ['id': 'some-cm-handle', 'dmi-service-name': 'testDmiService'])
78
79     def 'Write resource data for pass-through running from DMI using POST.'() {
80         given: 'cpsDataService returns valid datanode'
81             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
82                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
83         when: 'write resource data is called'
84             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
85                 'testResourceId', CREATE,
86                 '{some-json}', 'application/json')
87         then: 'DMI called with correct data'
88             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
89                 CREATE, '{some-json}', 'application/json')
90                 >> { new ResponseEntity<>(HttpStatus.CREATED) }
91     }
92
93     def 'Write resource data for pass-through running from DMI using an invalid id.'() {
94         when: 'write resource data is called'
95             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('invalid cm handle name',
96                 'testResourceId', CREATE,
97                 '{some-json}', 'application/json')
98         then: 'exception is thrown'
99             thrown(DataValidationException.class)
100         and: 'DMI is not invoked'
101             0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _)
102     }
103
104     def 'Get resource data for pass-through operational from DMI.'() {
105         given: 'get data node is called'
106             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
107                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
108         and: 'get resource data from DMI is called'
109             mockDmiDataOperations.getResourceDataFromDmi(
110                 'testCmHandle',
111                 'testResourceId',
112                 OPTIONS_PARAM,
113                 PASSTHROUGH_OPERATIONAL,
114                 NO_REQUEST_ID,
115                 NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
116         when: 'get resource data operational for cm-handle is called'
117             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
118                 'testResourceId',
119                 OPTIONS_PARAM,
120                 NO_TOPIC,
121                 NO_REQUEST_ID)
122         then: 'DMI returns a json response'
123             response == 'dmi-response'
124     }
125
126     def 'Get resource data for pass-through running from DMI.'() {
127         given: 'cpsDataService returns valid data node'
128             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
129                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
130         and: 'DMI returns valid response and data'
131             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
132                 'testResourceId',
133                 OPTIONS_PARAM,
134                 PASSTHROUGH_RUNNING,
135                 NO_REQUEST_ID,
136                 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
137         when: 'get resource data is called'
138             def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
139                 'testResourceId',
140                 OPTIONS_PARAM,
141                 NO_TOPIC,
142                 NO_REQUEST_ID)
143         then: 'get resource data returns expected response'
144             response == '{dmi-response}'
145     }
146
147     def 'Getting Yang Resources.'() {
148         when: 'yang resources is called'
149             objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
150         then: 'CPS module services is invoked for the correct dataspace and cm handle'
151             1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
152     }
153
154     def 'Getting Yang Resources with an invalid #scenario.'() {
155         when: 'yang resources is called'
156             objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces')
157         then: 'a data validation exception is thrown'
158             thrown(DataValidationException)
159         and: 'CPS module services is not invoked'
160             0 * mockCpsModuleService.getYangResourcesModuleReferences(*_)
161     }
162
163     def 'Get cm handle identifiers for the given module names.'() {
164         when: 'execute a cm handle search for the given module names'
165             objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
166         then: 'get anchor identifiers is invoked  with the expected parameters'
167             1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
168     }
169
170     def 'Get a cm handle.'() {
171         given: 'the system returns a yang modelled cm handle'
172             def dmiServiceName = 'some service name'
173             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
174             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
175             def compositeState = new CompositeState(cmHandleState: 'ADVISED')
176             def yangModelCmHandle = new YangModelCmHandle(id: 'some-cm-handle', dmiServiceName: dmiServiceName,
177                 dmiProperties: dmiProperties, publicProperties: publicProperties, compositeState: compositeState)
178             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
179         when: 'getting cm handle details for a given cm handle id from ncmp service'
180             def result = objectUnderTest.getNcmpServiceCmHandle('some-cm-handle')
181         then: 'the result returns the correct data'
182             result.cmHandleId == 'some-cm-handle'
183             result.dmiProperties ==[ Book:'Romance Novel' ]
184             result.publicProperties == [ "Public Book":'Public Romance Novel' ]
185             result.compositeState.cmHandleState == CmHandleState.ADVISED
186     }
187
188     def 'Get a cm handle with an invalid id.'() {
189         when: 'getting cm handle details for a given cm handle id with an invalid name'
190             objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces')
191         then: 'an exception is thrown'
192             thrown(DataValidationException)
193         and: 'the yang model cm handle retriever is not invoked'
194             0 * mockInventoryPersistence.getYangModelCmHandle(*_)
195     }
196
197     def 'Get cm handle public properties'() {
198         given: 'a yang modelled cm handle'
199             def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
200             def publicProperties = [new YangModelCmHandle.Property('public prop', 'some public prop')]
201             def yangModelCmHandle = new YangModelCmHandle(id:'some-cm-handle', dmiServiceName: 'some service name', dmiProperties: dmiProperties, publicProperties: publicProperties)
202         and: 'the system returns this yang modelled cm handle'
203             1 * mockInventoryPersistence.getYangModelCmHandle('some-cm-handle') >> yangModelCmHandle
204         when: 'getting cm handle public properties for a given cm handle id from ncmp service'
205             def result = objectUnderTest.getCmHandlePublicProperties('some-cm-handle')
206         then: 'the result returns the correct data'
207             result == [ 'public prop' : 'some public prop' ]
208     }
209
210     def 'Get cm handle public properties with an invalid id.'() {
211         when: 'getting cm handle details for a given cm handle id with an invalid name'
212             objectUnderTest.getCmHandlePublicProperties('invalid cm handle with spaces')
213         then: 'an exception is thrown'
214             thrown(DataValidationException)
215         and: 'the yang model cm handle retriever is not invoked'
216             0 * mockInventoryPersistence.getYangModelCmHandle(*_)
217     }
218
219     def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
220         given: 'cpsDataService returns valid datanode'
221             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
222                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
223         when: 'get resource data is called'
224             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
225                 'testResourceId', UPDATE,
226                 '{some-json}', 'application/json')
227         then: 'DMI called with correct data'
228             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
229                 UPDATE, '{some-json}', 'application/json')
230                 >> { new ResponseEntity<>(HttpStatus.OK) }
231     }
232
233     def 'Verify modules and create anchor params'() {
234         given: 'dmi plugin registration return created cm handles'
235             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1',
236                 dmiDataPlugin: 'service2')
237             dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
238             mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle]
239         when: 'parse and create cm handle in dmi registration then sync module'
240             objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration)
241         then: 'validate params for creating anchor and list elements'
242             1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
243                 '/dmi-registry', '{"cm-handles":[{"id":"some-cm-handle-id",' +
244                 '"additional-properties":[],"public-properties":[]}]}', null)
245             1 * mockCpsAdminService.createAnchor('NFP-Operational', null,
246                 'some-cm-handle-id')
247     }
248 }