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