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