Additional validation for names/identifiers
[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 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.InvalidTopicException
26 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever
27 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
28 import spock.lang.Shared
29
30 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
31 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
32 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
33 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ
34 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
35
36 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
37 import org.onap.cps.utils.JsonObjectMapper
38 import com.fasterxml.jackson.core.JsonProcessingException
39 import com.fasterxml.jackson.databind.ObjectMapper
40 import org.onap.cps.api.CpsAdminService
41 import org.onap.cps.api.CpsDataService
42 import org.onap.cps.api.CpsModuleService
43 import org.onap.cps.ncmp.api.impl.exception.ServerNcmpException
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 POST "not found" response (from DMI).'() {
88         given: 'cpsDataService returns valid dataNode'
89             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
90                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
91         and: 'DMI returns a response with 404 status code'
92             mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle',
93                 'testResourceId', CREATE,
94                 '{some-json}', 'application/json')
95                 >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) }
96         when: 'write resource data is called'
97             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
98                 'testResourceId', CREATE,
99                 '{some-json}', 'application/json')
100         then: 'exception is thrown'
101             def exceptionThrown = thrown(ServerNcmpException.class)
102         and: 'details contains (not found) error code: 404'
103             exceptionThrown.details.contains('404')
104     }
105
106     def 'Get resource data for pass-through operational from DMI.'() {
107         given: 'get data node is called'
108             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
109                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
110         and: 'get resource data from DMI is called'
111             mockDmiDataOperations.getResourceDataFromDmi(
112                     'testCmHandle',
113                     'testResourceId',
114                     OPTIONS_PARAM,
115                     'testAcceptParam',
116                     PASSTHROUGH_OPERATIONAL,
117                     NO_REQUEST_ID,
118                     NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
119         when: 'get resource data operational for cm-handle is called'
120             def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
121                     'testResourceId',
122                     'testAcceptParam',
123                     OPTIONS_PARAM,
124                     NO_TOPIC)
125         then: 'DMI returns a json response'
126             response == 'dmi-response'
127     }
128
129     def 'Get resource data for pass-through operational from DMI with Json Processing Exception.'() {
130         given: 'cps data service returns valid data node'
131             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
132                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
133         and: 'objectMapper not able to parse object'
134             spiedJsonObjectMapper.asJsonString(_) >> { throw new JsonProcessingException('testException') }
135         and: 'DMI returns NOK response'
136             mockDmiDataOperations.getResourceDataFromDmi(*_)
137                 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
138         when: 'get resource data is called'
139             objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
140                     'testResourceId',
141                     'testAcceptParam',
142                     OPTIONS_PARAM,
143                     NO_TOPIC)
144         then: 'exception is thrown with the expected details'
145             def exceptionThrown = thrown(ServerNcmpException.class)
146             exceptionThrown.details == 'DMI status code: 404, DMI response body: NOK-json'
147     }
148
149     def 'Get resource data for pass-through operational from DMI return NOK response.'() {
150         given: 'cps data service returns valid data node'
151             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
152                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
153         and: 'DMI returns NOK response'
154             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
155                     'testResourceId',
156                     OPTIONS_PARAM,
157                     'testAcceptParam',
158                     PASSTHROUGH_OPERATIONAL,
159                     NO_REQUEST_ID,
160                     NO_TOPIC)
161                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
162         when: 'get resource data is called'
163             objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
164                     'testResourceId',
165                     'testAcceptParam',
166                     OPTIONS_PARAM,
167                     NO_TOPIC)
168         then: 'exception is thrown'
169             def exceptionThrown = thrown(ServerNcmpException.class)
170         and: 'details contains the original response'
171             exceptionThrown.details.contains('NOK-json')
172     }
173
174     def 'Get resource data for pass-through running from DMI.'() {
175         given: 'cpsDataService returns valid data node'
176             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
177                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
178         and: 'DMI returns valid response and data'
179             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
180                     'testResourceId',
181                     OPTIONS_PARAM,
182                     'testAcceptParam',
183                     PASSTHROUGH_RUNNING,
184                     NO_REQUEST_ID,
185                     NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
186         when: 'get resource data is called'
187             def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
188                     'testResourceId',
189                     'testAcceptParam',
190                     OPTIONS_PARAM,
191                     NO_TOPIC)
192         then: 'get resource data returns expected response'
193             response == '{dmi-response}'
194     }
195
196     def 'Get resource data for pass-through running from DMI return NOK response.'() {
197         given: 'cpsDataService returns valid dataNode'
198             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
199                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
200         and: 'DMI returns NOK response'
201             mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
202                     'testResourceId',
203                     OPTIONS_PARAM,
204                     'testAcceptParam',
205                     PASSTHROUGH_RUNNING,
206                     NO_REQUEST_ID,
207                     NO_TOPIC)
208                     >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
209         when: 'get resource data is called'
210             objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
211                     'testResourceId',
212                     'testAcceptParam',
213                     OPTIONS_PARAM,
214                     NO_TOPIC)
215         then: 'exception is thrown'
216             def exceptionThrown = thrown(ServerNcmpException.class)
217         and: 'details contains the original response'
218             exceptionThrown.details.contains('NOK-json')
219     }
220
221     def 'DMI Operational data request with #scenario'() {
222         given: 'cps data service returns valid data node'
223             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
224                     cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
225         and: 'dmi data operation returns valid response and data'
226             mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, NO_REQUEST_ID, NO_TOPIC)
227                     >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
228         when: 'get resource data is called data operational with blank topic'
229             def responseData = objectUnderTest.getResourceDataOperationalForCmHandle('', '',
230                     '', '', emptyTopic)
231         then: 'a invalid topic exception is thrown'
232             thrown(InvalidTopicException)
233         where: 'the following parameters are used'
234             scenario                               | emptyTopic
235             'no topic value in url'                | ''
236             'empty topic value in url'             | '\"\"'
237             'blank topic value in url'             | ' '
238             'invalid non-empty topic value in url' | '1_5_*_#'
239     }
240
241     def 'Get resource data for data operational from DMI with valid topic i.e. async request.'() {
242         given: 'cps data service returns valid data node'
243             mockCpsDataService.getDataNode(*_) >> dataNode
244         and: 'dmi data operation returns valid response and data'
245             mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, _, 'my-topic-name')
246                     >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
247         when: 'get resource data is called for data operational with valid topic'
248             def responseData = objectUnderTest.getResourceDataOperationalForCmHandle('', '', '', '', 'my-topic-name')
249         then: 'non empty request id is generated'
250             assert responseData.body.requestId.length() > 0
251     }
252
253     def 'Get resource data for pass through running from DMI with valid topic async request.'() {
254         given: 'cps data service returns valid data node'
255             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
256                     cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
257         and: 'dmi data operation returns valid response and data'
258             mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, _, 'my-topic-name')
259                     >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
260         when: 'get resource data is called for data operational with valid topic'
261             def responseData = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('',
262                     '', '', OPTIONS_PARAM, 'my-topic-name')
263         then: 'non empty request id is generated'
264             assert responseData.body.requestId.length() > 0
265     }
266
267     def 'DMI pass through running data request with #scenario'() {
268         given: 'cps data service returns valid data node'
269             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
270                     cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
271         and: 'dmi data operation returns valid response and data'
272             mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, NO_REQUEST_ID, NO_TOPIC)
273                     >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
274         when: 'get resource data is called for data operational with valid topic'
275             def responseData = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('',
276                     '', '', '', emptyTopic)
277         then: 'a invalid topic exception is thrown'
278             thrown(InvalidTopicException)
279         where: 'the following parameters are used'
280             scenario                               | emptyTopic
281             'no topic value in url'                | ''
282             'empty topic value in url'             | '\"\"'
283             'blank topic value in url'             | ' '
284             'invalid non-empty topic value in url' | '1_5_*_#'
285     }
286
287     def 'Getting Yang Resources.'() {
288         when: 'yang resources is called'
289             objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
290         then: 'CPS module services is invoked for the correct dataspace and cm handle'
291             1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
292     }
293
294     def 'Get cm handle identifiers for the given module names.'() {
295         when: 'execute a cm handle search for the given module names'
296             objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
297         then: 'get anchor identifiers is invoked  with the expected parameters'
298             1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
299     }
300
301     def 'Get a cm handle.'() {
302         given: 'the system returns a yang modelled cm handle'
303             def dmiServiceName = 'some service name'
304             def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
305             def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
306             def yangModelCmHandle = new YangModelCmHandle(id:'Some-Cm-Handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties, publicProperties: publicProperties)
307             1 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties('Some-Cm-Handle') >> yangModelCmHandle
308         when: 'getting cm handle details for a given cm handle id from ncmp service'
309             def result = objectUnderTest.getNcmpServiceCmHandle('Some-Cm-Handle')
310         then: 'the result returns the correct data'
311             result.cmHandleID == 'Some-Cm-Handle'
312             result.dmiProperties ==[ Book:'Romance Novel' ]
313             result.publicProperties == [ "Public Book":'Public Romance Novel' ]
314
315     }
316
317     def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
318         given: 'cpsDataService returns valid datanode'
319             mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
320                 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
321         when: 'get resource data is called'
322             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
323                 'testResourceId', UPDATE,
324                 '{some-json}', 'application/json')
325         then: 'DMI called with correct data'
326             1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
327                 UPDATE, '{some-json}', 'application/json')
328                 >> { new ResponseEntity<>(HttpStatus.OK) }
329     }
330
331     def 'Verify error message from handleResponse is correct for #scenario operation.'() {
332         given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
333             mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
334                 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
335         when: 'get resource data is called'
336             objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
337                 'testCmHandle',
338                 'testResourceId',
339                 givenOperation,
340                 '{some-json}',
341                 'application/json')
342         then: 'an exception is thrown with the expected error message details with correct operation'
343             def exceptionThrown = thrown(ServerNcmpException.class)
344             exceptionThrown.getMessage().contains(expectedResponseMessage)
345         where:
346             scenario | givenOperation || expectedResponseMessage
347             'CREATE' | CREATE         || 'Not able to create resource data.'
348             'READ'   | READ           || 'Not able to read resource data.'
349             'UPDATE' | UPDATE         || 'Not able to update resource data.'
350     }
351 }