Refactoring/ Adding Tests for Validation
[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.spi.exceptions.DataValidationException
29 import spock.lang.Shared
30
31 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
32 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
33 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
34 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ
35 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
36
37 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
38 import org.onap.cps.utils.JsonObjectMapper
39 import com.fasterxml.jackson.core.JsonProcessingException
40 import com.fasterxml.jackson.databind.ObjectMapper
41 import org.onap.cps.api.CpsAdminService
42 import org.onap.cps.api.CpsDataService
43 import org.onap.cps.api.CpsModuleService
44 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
45 import org.onap.cps.spi.FetchDescendantsOption
46 import org.onap.cps.spi.model.DataNode
47 import org.springframework.http.HttpStatus
48 import org.springframework.http.ResponseEntity
49 import spock.lang.Specification
50
51 class NetworkCmProxyDataServiceImplSpec extends Specification {
52
53     def mockCpsDataService = Mock(CpsDataService)
54     def mockCpsModuleService = Mock(CpsModuleService)
55     def mockCpsAdminService = Mock(CpsAdminService)
56     def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
57     def mockDmiModelOperations = Mock(DmiModelOperations)
58     def mockDmiDataOperations = Mock(DmiDataOperations)
59     def nullNetworkCmProxyDataServicePropertyHandler = null
60     def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
61     def NO_TOPIC = null
62     def NO_REQUEST_ID = null
63     @Shared
64     def OPTIONS_PARAM = '(a=1,b=2)'
65
66     def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
67         mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever)
68
69     def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
70
71     def dataNode = new DataNode(leaves: ['dmi-service-name': 'testDmiService'])
72
73     def 'Write resource data for pass-through running from DMI using POST #scenario cm handle properties.'() {
74         given: 'cpsDataService returns valid datanode'
75             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
76                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
77         when: 'get resource data is called'
78             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
79                 'testResourceId', CREATE,
80                 '{some-json}', 'application/json')
81         then: 'DMI called with correct data'
82             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
83                 CREATE, '{some-json}', 'application/json')
84                 >> { new ResponseEntity<>(HttpStatus.CREATED) }
85     }
86
87     def 'Write resource data for pass-through running from DMI using an invalid id.'() {
88         when: 'write resource data is called'
89             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('invalid cm handle name',
90                 'testResourceId', CREATE,
91                 '{some-json}', 'application/json')
92         then: 'exception is thrown'
93             thrown(DataValidationException.class)
94         and: 'DMI is not invoked'
95             0 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(_, _, _, _, _)
96     }
97
98     def 'Write resource data for pass-through running from DMI using POST "not found" response (from DMI).'() {
99         given: 'cpsDataService returns valid dataNode'
100             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
101                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
102         and: 'DMI returns a response with 404 status code'
103             mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle',
104                 'testResourceId', CREATE,
105                 '{some-json}', 'application/json')
106                 >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) }
107         when: 'write resource data is called'
108             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
109                 'testResourceId', CREATE,
110                 '{some-json}', 'application/json')
111         then: 'exception is thrown'
112             def exceptionThrown = thrown(HttpClientRequestException.class)
113         and: 'http status (not found) error code: 404'
114             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
115     }
116
117     def 'Get resource data for pass-through operational from DMI.'() {
118         given: 'get data node is called'
119             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
120                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
121         and: 'get resource data from DMI is called'
122             mockDmiDataOperations.getResourceDataFromDmi(
123                     'testCmHandle',
124                     'testResourceId',
125                     OPTIONS_PARAM,
126                     PASSTHROUGH_OPERATIONAL,
127                     NO_REQUEST_ID,
128                     NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
129         when: 'get resource data operational for cm-handle is called'
130             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
131                     'testResourceId',
132                     OPTIONS_PARAM,
133                     NO_TOPIC,
134                     NO_REQUEST_ID)
135         then: 'DMI returns a json response'
136             response == 'dmi-response'
137     }
138
139     def 'Get resource data for pass-through operational from DMI with invalid name.'() {\
140         when: 'get resource data operational for cm-handle is called'
141             objectUnderTest.getResourceDataOperationalForCmHandle('invalid test cm handle',
142                 'testResourceId',
143                 OPTIONS_PARAM,
144                 NO_TOPIC,
145                 NO_REQUEST_ID)
146         then: 'A data validation Exception is thrown'
147             thrown(DataValidationException)
148     }
149
150     def 'Get resource data for pass-through operational from DMI with Json Processing Exception.'() {
151         given: 'cps data service returns valid data node'
152             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
153                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
154         and: 'objectMapper not able to parse object'
155             spiedJsonObjectMapper.asJsonString(_) >> { throw new JsonProcessingException('testException') }
156         and: 'DMI returns NOK response'
157             mockDmiDataOperations.getResourceDataFromDmi(*_)
158                 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
159         when: 'get resource data is called'
160             objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
161                     'testResourceId',
162                     OPTIONS_PARAM,
163                     NO_TOPIC,
164                     NO_REQUEST_ID)
165         then: 'exception is thrown with the expected response code and details'
166             def exceptionThrown = thrown(HttpClientRequestException.class)
167             exceptionThrown.details.contains('NOK-json')
168             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
169     }
170
171     def 'Get resource data for pass-through operational from DMI return NOK response.'() {
172         given: 'cps data service returns valid data node'
173             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
174                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
175         and: 'DMI returns NOK response'
176             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
177                     'testResourceId',
178                     OPTIONS_PARAM,
179                     PASSTHROUGH_OPERATIONAL,
180                     NO_REQUEST_ID,
181                     NO_TOPIC)
182                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
183         when: 'get resource data is called'
184             objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
185                     'testResourceId',
186                     OPTIONS_PARAM,
187                     NO_TOPIC,
188                     NO_REQUEST_ID)
189         then: 'exception is thrown'
190             def exceptionThrown = thrown(HttpClientRequestException.class)
191         and: 'details contain the original response'
192             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
193             exceptionThrown.details.contains('NOK-json')
194     }
195
196     def 'Get resource data for pass-through running from DMI.'() {
197         given: 'cpsDataService returns valid data node'
198             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
199                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
200         and: 'DMI returns valid response and data'
201             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
202                     'testResourceId',
203                     OPTIONS_PARAM,
204                     PASSTHROUGH_RUNNING,
205                     NO_REQUEST_ID,
206                     NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
207         when: 'get resource data is called'
208             def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
209                     'testResourceId',
210                     OPTIONS_PARAM,
211                     NO_TOPIC,
212                     NO_REQUEST_ID)
213         then: 'get resource data returns expected response'
214             response == '{dmi-response}'
215     }
216
217     def 'Get resource data for pass-through running from DMI with invalid name.'() {
218         when: 'get resource data operational for cm-handle is called'
219             objectUnderTest.getResourceDataPassThroughRunningForCmHandle('invalid test cm handle',
220                 'testResourceId',
221                 OPTIONS_PARAM,
222                 NO_TOPIC,
223                 NO_REQUEST_ID)
224         then: 'A data validation Exception is thrown'
225             thrown(DataValidationException)
226     }
227
228     def 'Get resource data for pass-through running from DMI return NOK response.'() {
229         given: 'cpsDataService returns valid dataNode'
230             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
231                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
232         and: 'DMI returns NOK response'
233             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
234                     'testResourceId',
235                     OPTIONS_PARAM,
236                     PASSTHROUGH_RUNNING,
237                     NO_REQUEST_ID,
238                     NO_TOPIC)
239                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
240         when: 'get resource data is called'
241             objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
242                     'testResourceId',
243                     OPTIONS_PARAM,
244                     NO_TOPIC,
245                     NO_REQUEST_ID)
246         then: 'exception is thrown'
247             def exceptionThrown = thrown(HttpClientRequestException.class)
248         and: 'details contain the original response'
249             exceptionThrown.details.contains('NOK-json')
250             exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
251     }
252
253     def 'Getting Yang Resources.'() {
254         when: 'yang resources is called'
255             objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
256         then: 'CPS module services is invoked for the correct dataspace and cm handle'
257             1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
258     }
259
260     def 'Getting Yang Resources with an invalid #scenario.'() {
261         when: 'yang resources is called'
262             objectUnderTest.getYangResourcesModuleReferences('invalid cm handle with spaces')
263         then: 'a data validation exception is thrown'
264             thrown(DataValidationException)
265         and: 'CPS module services is not invoked'
266             0 * mockCpsModuleService.getYangResourcesModuleReferences(_, _)
267     }
268
269     def 'Get cm handle identifiers for the given module names.'() {
270         when: 'execute a cm handle search for the given module names'
271             objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
272         then: 'get anchor identifiers is invoked  with the expected parameters'
273             1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
274     }
275
276     def 'Get a cm handle.'() {
277         given: 'the system returns a yang modelled cm handle'
278             def dmiServiceName = 'some service name'
279             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
280             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
281             def yangModelCmHandle = new YangModelCmHandle(id:'Some-Cm-Handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties, publicProperties: publicProperties)
282             1 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties('Some-Cm-Handle') >> yangModelCmHandle
283         when: 'getting cm handle details for a given cm handle id from ncmp service'
284             def result = objectUnderTest.getNcmpServiceCmHandle('Some-Cm-Handle')
285         then: 'the result returns the correct data'
286             result.cmHandleId == 'Some-Cm-Handle'
287             result.dmiProperties ==[ Book:'Romance Novel' ]
288             result.publicProperties == [ "Public Book":'Public Romance Novel' ]
289
290     }
291
292     def 'Get a cm handle with an invalid id.'() {
293         when: 'getting cm handle details for a given cm handle id with an invalid name'
294             objectUnderTest.getNcmpServiceCmHandle('invalid cm handle with spaces')
295         then: 'an exception is thrown'
296             thrown(DataValidationException)
297         and: 'the yang model cm handle retriever is not invoked'
298             0 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties(_)
299     }
300
301     def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
302         given: 'cpsDataService returns valid datanode'
303             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
304                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
305         when: 'get resource data is called'
306             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
307                 'testResourceId', UPDATE,
308                 '{some-json}', 'application/json')
309         then: 'DMI called with correct data'
310             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
311                 UPDATE, '{some-json}', 'application/json')
312                 >> { new ResponseEntity<>(HttpStatus.OK) }
313     }
314
315     def 'Verify error message from handleResponse is correct for #scenario operation.'() {
316         given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
317             mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
318                 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
319         when: 'get resource data is called'
320             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
321                 'testCmHandle',
322                 'testResourceId',
323                 givenOperation,
324                 '{some-json}',
325                 'application/json')
326         then: 'an exception is thrown with the expected error message details with correct operation'
327             def exceptionThrown = thrown(HttpClientRequestException.class)
328             exceptionThrown.getMessage().contains(expectedResponseMessage)
329         where:
330             scenario | givenOperation || expectedResponseMessage
331             'CREATE' | CREATE         || 'Unable to create resource data.'
332             'READ'   | READ           || 'Unable to read resource data.'
333             'UPDATE' | UPDATE         || 'Unable to update resource data.'
334     }
335 }