Refactor existing model sync code into separate package
[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.operations.YangModelCmHandleRetriever
27 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
28 import org.onap.cps.ncmp.api.models.DmiPluginRegistration
29 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle
30 import org.onap.cps.spi.exceptions.DataValidationException
31 import org.onap.cps.ncmp.api.inventory.sync.ModuleSyncService
32 import spock.lang.Shared
33
34 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
35 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
36 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
37 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ
38 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
39
40 import org.onap.cps.utils.JsonObjectMapper
41 import com.fasterxml.jackson.core.JsonProcessingException
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 mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
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, mockYangModelCmHandleRetriever, mockModuleSyncService)
74
75     def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
76
77     def dataNode = new DataNode(leaves: ['dmi-service-name': 'testDmiService'])
78
79     def 'Write resource data for pass-through running from DMI using POST #scenario cm handle properties.'() {
80         given: 'cpsDataService returns valid datanode'
81             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
82                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
83         when: 'get 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 'Write resource data for pass-through running from DMI using POST "not found" response (from DMI).'() {
105         given: 'cpsDataService returns valid dataNode'
106             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
107                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
108         and: 'DMI returns a response with 404 status code'
109             mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle',
110                 'testResourceId', CREATE,
111                 '{some-json}', 'application/json')
112                 >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) }
113         when: 'write resource data is called'
114             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
115                 'testResourceId', CREATE,
116                 '{some-json}', 'application/json')
117         then: 'exception is thrown'
118             def exceptionThrown = thrown(HttpClientRequestException.class)
119         and: 'http status (not found) error code: 404'
120             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
121     }
122
123     def 'Get resource data for pass-through operational from DMI.'() {
124         given: 'get data node is called'
125             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
126                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
127         and: 'get resource data from DMI is called'
128             mockDmiDataOperations.getResourceDataFromDmi(
129                     'testCmHandle',
130                     'testResourceId',
131                     OPTIONS_PARAM,
132                     PASSTHROUGH_OPERATIONAL,
133                     NO_REQUEST_ID,
134                     NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
135         when: 'get resource data operational for cm-handle is called'
136             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
137                     'testResourceId',
138                     OPTIONS_PARAM,
139                     NO_TOPIC,
140                     NO_REQUEST_ID)
141         then: 'DMI returns a json response'
142             response == 'dmi-response'
143     }
144
145     def 'Get resource data for pass-through operational from DMI with invalid name.'() {\
146         when: 'get resource data operational for cm-handle is called'
147             objectUnderTest.getResourceDataOperationalForCmHandle('invalid test cm handle',
148                 'testResourceId',
149                 OPTIONS_PARAM,
150                 NO_TOPIC,
151                 NO_REQUEST_ID)
152         then: 'A data validation Exception is thrown'
153             thrown(DataValidationException)
154     }
155
156     def 'Get resource data for pass-through operational from DMI with Json Processing Exception.'() {
157         given: 'cps data service returns valid data node'
158             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
159                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
160         and: 'objectMapper not able to parse object'
161             spiedJsonObjectMapper.asJsonString(_) >> { throw new JsonProcessingException('testException') }
162         and: 'DMI returns NOK response'
163             mockDmiDataOperations.getResourceDataFromDmi(*_)
164                 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
165         when: 'get resource data is called'
166             objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
167                     'testResourceId',
168                     OPTIONS_PARAM,
169                     NO_TOPIC,
170                     NO_REQUEST_ID)
171         then: 'exception is thrown with the expected response code and details'
172             def exceptionThrown = thrown(HttpClientRequestException.class)
173             exceptionThrown.details.contains('NOK-json')
174             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
175     }
176
177     def 'Get resource data for pass-through operational from DMI return NOK response.'() {
178         given: 'cps data service returns valid data node'
179             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
180                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
181         and: 'DMI returns NOK response'
182             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
183                     'testResourceId',
184                     OPTIONS_PARAM,
185                     PASSTHROUGH_OPERATIONAL,
186                     NO_REQUEST_ID,
187                     NO_TOPIC)
188                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
189         when: 'get resource data is called'
190             objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
191                     'testResourceId',
192                     OPTIONS_PARAM,
193                     NO_TOPIC,
194                     NO_REQUEST_ID)
195         then: 'exception is thrown'
196             def exceptionThrown = thrown(HttpClientRequestException.class)
197         and: 'details contain the original response'
198             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
199             exceptionThrown.details.contains('NOK-json')
200     }
201
202     def 'Get resource data for pass-through running from DMI.'() {
203         given: 'cpsDataService returns valid data node'
204             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
205                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
206         and: 'DMI returns valid response and data'
207             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
208                     'testResourceId',
209                     OPTIONS_PARAM,
210                     PASSTHROUGH_RUNNING,
211                     NO_REQUEST_ID,
212                     NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
213         when: 'get resource data is called'
214             def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
215                     'testResourceId',
216                     OPTIONS_PARAM,
217                     NO_TOPIC,
218                     NO_REQUEST_ID)
219         then: 'get resource data returns expected response'
220             response == '{dmi-response}'
221     }
222
223     def 'Get resource data for pass-through running from DMI with invalid name.'() {
224         when: 'get resource data operational for cm-handle is called'
225             objectUnderTest.getResourceDataPassThroughRunningForCmHandle('invalid test cm handle',
226                 'testResourceId',
227                 OPTIONS_PARAM,
228                 NO_TOPIC,
229                 NO_REQUEST_ID)
230         then: 'A data validation Exception is thrown'
231             thrown(DataValidationException)
232     }
233
234     def 'Get resource data for pass-through running from DMI return NOK response.'() {
235         given: 'cpsDataService returns valid dataNode'
236             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
237                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
238         and: 'DMI returns NOK response'
239             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
240                     'testResourceId',
241                     OPTIONS_PARAM,
242                     PASSTHROUGH_RUNNING,
243                     NO_REQUEST_ID,
244                     NO_TOPIC)
245                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
246         when: 'get resource data is called'
247             objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
248                     'testResourceId',
249                     OPTIONS_PARAM,
250                     NO_TOPIC,
251                     NO_REQUEST_ID)
252         then: 'exception is thrown'
253             def exceptionThrown = thrown(HttpClientRequestException.class)
254         and: 'details contain the original response'
255             exceptionThrown.details.contains('NOK-json')
256             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
257     }
258
259     def 'Getting Yang Resources.'() {
260         when: 'yang resources is called'
261             objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
262         then: 'CPS module services is invoked for the correct dataspace and cm handle'
263             1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
264     }
265
266     def 'Getting Yang Resources with an invalid #scenario.'() {
267         when: 'yang resources is called'
268             objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces')
269         then: 'a data validation exception is thrown'
270             thrown(DataValidationException)
271         and: 'CPS module services is not invoked'
272             0 * mockCpsModuleService.getYangResourcesModuleReferences(_, _)
273     }
274
275     def 'Get cm handle identifiers for the given module names.'() {
276         when: 'execute a cm handle search for the given module names'
277             objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
278         then: 'get anchor identifiers is invoked  with the expected parameters'
279             1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
280     }
281
282     def 'Get a cm handle.'() {
283         given: 'the system returns a yang modelled cm handle'
284             def dmiServiceName = 'some service name'
285             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
286             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
287             def yangModelCmHandle = new YangModelCmHandle(id:'Some-Cm-Handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties, publicProperties: publicProperties)
288             1 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties('Some-Cm-Handle') >> yangModelCmHandle
289         when: 'getting cm handle details for a given cm handle id from ncmp service'
290             def result = objectUnderTest.getNcmpServiceCmHandle('Some-Cm-Handle')
291         then: 'the result returns the correct data'
292             result.cmHandleId == 'Some-Cm-Handle'
293             result.dmiProperties ==[ Book:'Romance Novel' ]
294             result.publicProperties == [ "Public Book":'Public Romance Novel' ]
295
296     }
297
298     def 'Get a cm handle with an invalid id.'() {
299         when: 'getting cm handle details for a given cm handle id with an invalid name'
300             objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces')
301         then: 'an exception is thrown'
302             thrown(DataValidationException)
303         and: 'the yang model cm handle retriever is not invoked'
304             0 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties(_)
305     }
306
307     def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
308         given: 'cpsDataService returns valid datanode'
309             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
310                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
311         when: 'get resource data is called'
312             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
313                 'testResourceId', UPDATE,
314                 '{some-json}', 'application/json')
315         then: 'DMI called with correct data'
316             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
317                 UPDATE, '{some-json}', 'application/json')
318                 >> { new ResponseEntity<>(HttpStatus.OK) }
319     }
320
321     def 'Verify error message from handleResponse is correct for #scenario operation.'() {
322         given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
323             mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
324                 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
325         when: 'get resource data is called'
326             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
327                 'testCmHandle',
328                 'testResourceId',
329                 givenOperation,
330                 '{some-json}',
331                 'application/json')
332         then: 'an exception is thrown with the expected error message details with correct operation'
333             def exceptionThrown = thrown(HttpClientRequestException.class)
334             exceptionThrown.getMessage().contains(expectedResponseMessage)
335         where:
336             scenario | givenOperation || expectedResponseMessage
337             'CREATE' | CREATE         || 'Unable to create resource data.'
338             'READ'   | READ           || 'Unable to read resource data.'
339             'UPDATE' | UPDATE         || 'Unable to update resource data.'
340     }
341
342     def 'Verify modules and create anchor params'() {
343         given: 'dmi plugin registration return created cm handles'
344             def dmiPluginRegistration = new DmiPluginRegistration(dmiPlugin: 'service1', dmiModelPlugin: 'service1',
345                     dmiDataPlugin: 'service2')
346             dmiPluginRegistration.createdCmHandles = [ncmpServiceCmHandle]
347             mockDmiPluginRegistration.getCreatedCmHandles() >> [ncmpServiceCmHandle]
348         when: 'parse and create cm handle in dmi registration then sync module'
349             objectUnderTest.parseAndCreateCmHandlesInDmiRegistrationAndSyncModules(mockDmiPluginRegistration)
350         then: 'validate params for creating anchor and list elements'
351             1 * mockCpsDataService.saveListElements('NCMP-Admin', 'ncmp-dmi-registry',
352                     '/dmi-registry', '{"cm-handles":[{"id":"some-cm-handle-id",' +
353                     '"additional-properties":[],"public-properties":[]}]}', null)
354             1 * mockCpsAdminService.createAnchor('NFP-Operational', null,
355                     'some-cm-handle-id')
356     }
357 }