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
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(
115 PASSTHROUGH_OPERATIONAL,
117 NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
118 when: 'get resource data operational for cm-handle is called'
119 def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
123 then: 'DMI returns a json response'
124 response == 'dmi-response'
127 def 'Get resource data for pass-through operational from DMI with Json Processing Exception.'() {
128 given: 'cps data service returns valid data node'
129 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
130 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
131 and: 'objectMapper not able to parse object'
132 spiedJsonObjectMapper.asJsonString(_) >> { throw new JsonProcessingException('testException') }
133 and: 'DMI returns NOK response'
134 mockDmiDataOperations.getResourceDataFromDmi(*_)
135 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
136 when: 'get resource data is called'
137 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
141 then: 'exception is thrown with the expected response code and details'
142 def exceptionThrown = thrown(HttpClientRequestException.class)
143 exceptionThrown.details.contains('NOK-json')
144 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
147 def 'Get resource data for pass-through operational from DMI return NOK response.'() {
148 given: 'cps data service returns valid data node'
149 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
150 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
151 and: 'DMI returns NOK response'
152 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
155 PASSTHROUGH_OPERATIONAL,
158 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
159 when: 'get resource data is called'
160 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
164 then: 'exception is thrown'
165 def exceptionThrown = thrown(HttpClientRequestException.class)
166 and: 'details contain the original response'
167 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
168 exceptionThrown.details.contains('NOK-json')
171 def 'Get resource data for pass-through running from DMI.'() {
172 given: 'cpsDataService returns valid data node'
173 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
174 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
175 and: 'DMI returns valid response and data'
176 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
181 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
182 when: 'get resource data is called'
183 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
187 then: 'get resource data returns expected response'
188 response == '{dmi-response}'
191 def 'Get resource data for pass-through running from DMI return NOK response.'() {
192 given: 'cpsDataService returns valid dataNode'
193 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
194 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
195 and: 'DMI returns NOK response'
196 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
202 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
203 when: 'get resource data is called'
204 objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
208 then: 'exception is thrown'
209 def exceptionThrown = thrown(HttpClientRequestException.class)
210 and: 'details contain the original response'
211 exceptionThrown.details.contains('NOK-json')
212 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
215 def 'DMI Operational data request with #scenario'() {
216 given: 'cps data service returns valid data node'
217 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
218 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
219 and: 'dmi data operation returns valid response and data'
220 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, NO_REQUEST_ID, NO_TOPIC)
221 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
222 when: 'get resource data is called data operational with blank topic'
223 def responseData = objectUnderTest.getResourceDataOperationalForCmHandle('', '',
225 then: 'a invalid topic exception is thrown'
226 thrown(InvalidTopicException)
227 where: 'the following parameters are used'
228 scenario | emptyTopic
229 'no topic value in url' | ''
230 'empty topic value in url' | '\"\"'
231 'blank topic value in url' | ' '
232 'invalid non-empty topic value in url' | '1_5_*_#'
235 def 'Get resource data for data operational from DMI with valid topic i.e. async request.'() {
236 given: 'cps data service returns valid data node'
237 mockCpsDataService.getDataNode(*_) >> dataNode
238 and: 'dmi data operation returns valid response and data'
239 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, 'my-topic-name')
240 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
241 when: 'get resource data is called for data operational with valid topic'
242 def responseData = objectUnderTest.getResourceDataOperationalForCmHandle('', '', '', 'my-topic-name')
243 then: 'non empty request id is generated'
244 assert responseData.body.requestId.length() > 0
247 def 'Get resource data for pass through running from DMI with valid topic async request.'() {
248 given: 'cps data service returns valid data node'
249 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
250 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
251 and: 'dmi data operation returns valid response and data'
252 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, _, 'my-topic-name')
253 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
254 when: 'get resource data is called for data operational with valid topic'
255 def responseData = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('',
256 '', OPTIONS_PARAM, 'my-topic-name')
257 then: 'non empty request id is generated'
258 assert responseData.body.requestId.length() > 0
261 def 'DMI pass through running data request with #scenario'() {
262 given: 'cps data service returns valid data node'
263 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
264 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
265 and: 'dmi data operation returns valid response and data'
266 mockDmiDataOperations.getResourceDataFromDmi(_, _, _, _, NO_REQUEST_ID, NO_TOPIC)
267 >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
268 when: 'get resource data is called for data operational with valid topic'
269 def responseData = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('',
271 then: 'a invalid topic exception is thrown'
272 thrown(InvalidTopicException)
273 where: 'the following parameters are used'
274 scenario | emptyTopic
275 'no topic value in url' | ''
276 'empty topic value in url' | '\"\"'
277 'blank topic value in url' | ' '
278 'invalid non-empty topic value in url' | '1_5_*_#'
281 def 'Getting Yang Resources.'() {
282 when: 'yang resources is called'
283 objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
284 then: 'CPS module services is invoked for the correct dataspace and cm handle'
285 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
288 def 'Get cm handle identifiers for the given module names.'() {
289 when: 'execute a cm handle search for the given module names'
290 objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
291 then: 'get anchor identifiers is invoked with the expected parameters'
292 1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
295 def 'Get a cm handle.'() {
296 given: 'the system returns a yang modelled cm handle'
297 def dmiServiceName = 'some service name'
298 def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
299 def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
300 def yangModelCmHandle = new YangModelCmHandle(id:'Some-Cm-Handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties, publicProperties: publicProperties)
301 1 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties('Some-Cm-Handle') >> yangModelCmHandle
302 when: 'getting cm handle details for a given cm handle id from ncmp service'
303 def result = objectUnderTest.getNcmpServiceCmHandle('Some-Cm-Handle')
304 then: 'the result returns the correct data'
305 result.cmHandleID == 'Some-Cm-Handle'
306 result.dmiProperties ==[ Book:'Romance Novel' ]
307 result.publicProperties == [ "Public Book":'Public Romance Novel' ]
311 def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
312 given: 'cpsDataService returns valid datanode'
313 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
314 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
315 when: 'get resource data is called'
316 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
317 'testResourceId', UPDATE,
318 '{some-json}', 'application/json')
319 then: 'DMI called with correct data'
320 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
321 UPDATE, '{some-json}', 'application/json')
322 >> { new ResponseEntity<>(HttpStatus.OK) }
325 def 'Verify error message from handleResponse is correct for #scenario operation.'() {
326 given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
327 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
328 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
329 when: 'get resource data is called'
330 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
336 then: 'an exception is thrown with the expected error message details with correct operation'
337 def exceptionThrown = thrown(HttpClientRequestException.class)
338 exceptionThrown.getMessage().contains(expectedResponseMessage)
340 scenario | givenOperation || expectedResponseMessage
341 'CREATE' | CREATE || 'Unable to create resource data.'
342 'READ' | READ || 'Unable to read resource data.'
343 'UPDATE' | UPDATE || 'Unable to update resource data.'