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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.ncmp.api.impl
25 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
26 import org.onap.cps.ncmp.api.impl.exception.InvalidTopicException
27 import org.onap.cps.ncmp.api.impl.operations.YangModelCmHandleRetriever
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import spock.lang.Shared
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
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
51 class NetworkCmProxyDataServiceImplSpec extends Specification {
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)
62 def NO_REQUEST_ID = null
64 def OPTIONS_PARAM = '(a=1,b=2)'
66 def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
67 mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever)
69 def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
71 def dataNode = new DataNode(leaves: ['dmi-service-name': 'testDmiService'])
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) }
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(HttpClientRequestException.class)
102 and: 'http status (not found) error code: 404'
103 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
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(
116 PASSTHROUGH_OPERATIONAL,
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',
125 then: 'DMI returns a json response'
126 response == 'dmi-response'
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',
144 then: 'exception is thrown with the expected response code and details'
145 def exceptionThrown = thrown(HttpClientRequestException.class)
146 exceptionThrown.details.contains('NOK-json')
147 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
150 def 'Get resource data for pass-through operational from DMI return NOK response.'() {
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: 'DMI returns NOK response'
155 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
159 PASSTHROUGH_OPERATIONAL,
162 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
163 when: 'get resource data is called'
164 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
169 then: 'exception is thrown'
170 def exceptionThrown = thrown(HttpClientRequestException.class)
171 and: 'details contain the original response'
172 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
173 exceptionThrown.details.contains('NOK-json')
176 def 'Get resource data for pass-through running from DMI.'() {
177 given: 'cpsDataService returns valid data node'
178 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
179 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
180 and: 'DMI returns valid response and data'
181 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
187 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
188 when: 'get resource data is called'
189 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
194 then: 'get resource data returns expected response'
195 response == '{dmi-response}'
198 def 'Get resource data for pass-through running from DMI return NOK response.'() {
199 given: 'cpsDataService returns valid dataNode'
200 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
201 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
202 and: 'DMI returns NOK response'
203 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
210 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
211 when: 'get resource data is called'
212 objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
217 then: 'exception is thrown'
218 def exceptionThrown = thrown(HttpClientRequestException.class)
219 and: 'details contain the original response'
220 exceptionThrown.details.contains('NOK-json')
221 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
224 def 'DMI Operational data request with #scenario'() {
225 given: 'cps data service returns valid data node'
226 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
227 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
228 and: 'dmi data operation returns valid response and data'
229 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, NO_REQUEST_ID, NO_TOPIC)
230 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
231 when: 'get resource data is called data operational with blank topic'
232 def responseData = objectUnderTest.getResourceDataOperationalForCmHandle('', '',
234 then: 'a invalid topic exception is thrown'
235 thrown(InvalidTopicException)
236 where: 'the following parameters are used'
237 scenario | emptyTopic
238 'no topic value in url' | ''
239 'empty topic value in url' | '\"\"'
240 'blank topic value in url' | ' '
241 'invalid non-empty topic value in url' | '1_5_*_#'
244 def 'Get resource data for data operational from DMI with valid topic i.e. async request.'() {
245 given: 'cps data service returns valid data node'
246 mockCpsDataService.getDataNode(*_) >> dataNode
247 and: 'dmi data operation returns valid response and data'
248 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, _, 'my-topic-name')
249 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
250 when: 'get resource data is called for data operational with valid topic'
251 def responseData = objectUnderTest.getResourceDataOperationalForCmHandle('', '', '', '', 'my-topic-name')
252 then: 'non empty request id is generated'
253 assert responseData.body.requestId.length() > 0
256 def 'Get resource data for pass through running from DMI with valid topic async request.'() {
257 given: 'cps data service returns valid data node'
258 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
259 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
260 and: 'dmi data operation returns valid response and data'
261 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, _, 'my-topic-name')
262 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
263 when: 'get resource data is called for data operational with valid topic'
264 def responseData = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('',
265 '', '', OPTIONS_PARAM, 'my-topic-name')
266 then: 'non empty request id is generated'
267 assert responseData.body.requestId.length() > 0
270 def 'DMI pass through running data request with #scenario'() {
271 given: 'cps data service returns valid data node'
272 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
273 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
274 and: 'dmi data operation returns valid response and data'
275 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, NO_REQUEST_ID, NO_TOPIC)
276 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
277 when: 'get resource data is called for data operational with valid topic'
278 def responseData = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('',
279 '', '', '', emptyTopic)
280 then: 'a invalid topic exception is thrown'
281 thrown(InvalidTopicException)
282 where: 'the following parameters are used'
283 scenario | emptyTopic
284 'no topic value in url' | ''
285 'empty topic value in url' | '\"\"'
286 'blank topic value in url' | ' '
287 'invalid non-empty topic value in url' | '1_5_*_#'
290 def 'Getting Yang Resources.'() {
291 when: 'yang resources is called'
292 objectUnderTest.getYangResourcesModuleReferences('some cm handle')
293 then: 'CPS module services is invoked for the correct dataspace and cm handle'
294 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some cm handle')
297 def 'Get cm handle identifiers for the given module names.'() {
298 when: 'execute a cm handle search for the given module names'
299 objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
300 then: 'get anchor identifiers is invoked with the expected parameters'
301 1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
304 def 'Get a cm handle.'() {
305 given: 'the system returns a yang modelled cm handle'
306 def dmiServiceName = 'some service name'
307 def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
308 def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
309 def yangModelCmHandle = new YangModelCmHandle(id:'Some-Cm-Handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties, publicProperties: publicProperties)
310 1 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties('Some-Cm-Handle') >> yangModelCmHandle
311 when: 'getting cm handle details for a given cm handle id from ncmp service'
312 def result = objectUnderTest.getNcmpServiceCmHandle('Some-Cm-Handle')
313 then: 'the result returns the correct data'
314 result.cmHandleID == 'Some-Cm-Handle'
315 result.dmiProperties ==[ Book:'Romance Novel' ]
316 result.publicProperties == [ "Public Book":'Public Romance Novel' ]
320 def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
321 given: 'cpsDataService returns valid datanode'
322 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
323 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
324 when: 'get resource data is called'
325 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
326 'testResourceId', UPDATE,
327 '{some-json}', 'application/json')
328 then: 'DMI called with correct data'
329 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
330 UPDATE, '{some-json}', 'application/json')
331 >> { new ResponseEntity<>(HttpStatus.OK) }
334 def 'Verify error message from handleResponse is correct for #scenario operation.'() {
335 given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
336 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
337 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
338 when: 'get resource data is called'
339 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
345 then: 'an exception is thrown with the expected error message details with correct operation'
346 def exceptionThrown = thrown(HttpClientRequestException.class)
347 exceptionThrown.getMessage().contains(expectedResponseMessage)
349 scenario | givenOperation || expectedResponseMessage
350 'CREATE' | CREATE || 'Unable to create resource data.'
351 'READ' | READ || 'Unable to read resource data.'
352 'UPDATE' | UPDATE || 'Unable to update resource data.'