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 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
31 import com.fasterxml.jackson.core.JsonProcessingException
32 import com.fasterxml.jackson.databind.ObjectMapper
33 import org.onap.cps.api.CpsAdminService
34 import org.onap.cps.api.CpsDataService
35 import org.onap.cps.api.CpsModuleService
36 import org.onap.cps.ncmp.api.impl.exception.NcmpException
37 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
38 import org.onap.cps.spi.FetchDescendantsOption
39 import org.onap.cps.spi.model.DataNode
40 import org.springframework.http.HttpStatus
41 import org.springframework.http.ResponseEntity
42 import spock.lang.Specification
44 class NetworkCmProxyDataServiceImplSpec extends Specification {
46 def mockCpsDataService = Mock(CpsDataService)
47 def mockCpsModuleService = Mock(CpsModuleService)
48 def mockCpsAdminService = Mock(CpsAdminService)
49 def spyObjectMapper = Spy(ObjectMapper)
50 def mockDmiDataOperations = Mock(DmiDataOperations)
52 def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockDmiDataOperations, null,
53 mockCpsModuleService, mockCpsDataService, mockCpsAdminService, spyObjectMapper)
55 def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
58 def 'Write resource data for pass-through running from dmi using POST #scenario cm handle properties.'() {
60 def dataNode = getDataNode(includeCmHandleProperties)
61 and: 'cpsDataService returns valid datanode'
62 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
63 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
64 when: 'get resource data is called'
65 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
66 'testResourceId', CREATE,
67 '{some-json}', 'application/json')
68 then: 'dmi called with correct data'
69 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
70 CREATE, '{some-json}', 'application/json')
71 >> { new ResponseEntity<>(HttpStatus.CREATED) }
73 scenario | includeCmHandleProperties || expectedJsonForCmhandleProperties
74 'with' | true || '{"testName":"testValue"}'
75 'without' | false || '{}'
78 def 'Write resource data for pass-through running from dmi using POST "not found" response (from DMI).'() {
80 def dataNode = getDataNode(true)
81 and: 'cpsDataService returns valid dataNode'
82 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
83 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
84 and: 'dmi returns a response with 404 status code'
85 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle',
86 'testResourceId', CREATE,
87 '{some-json}', 'application/json')
88 >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) }
89 when: 'write resource data is called'
90 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
91 'testResourceId', CREATE,
92 '{some-json}', 'application/json')
93 then: 'exception is thrown'
94 def exceptionThrown = thrown(NcmpException.class)
95 and: 'details contains (not found) error code: 404'
96 exceptionThrown.details.contains('404')
100 def 'Get resource data for pass-through operational from dmi.'() {
102 def dataNode = getDataNode(true)
103 and: 'get data node is called'
104 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
105 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
106 and: 'get resource data from dmi is called'
107 mockDmiDataOperations.getResourceDataFromDmi(
112 PASSTHROUGH_OPERATIONAL) >> new ResponseEntity<>('result-json', HttpStatus.OK)
113 when: 'get resource data operational for cm-handle is called'
114 def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
118 then: 'dmi returns a json response'
119 response == 'result-json'
122 def 'Get resource data for pass-through operational from dmi with Json Processing Exception.'() {
124 def dataNode = getDataNode(true)
125 and: 'cps data service returns valid data node'
126 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
127 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
128 and: 'objectMapper not able to parse object'
129 def mockObjectMapper = Mock(ObjectMapper)
130 objectUnderTest.objectMapper = mockObjectMapper
131 mockObjectMapper.writeValueAsString(_) >> { throw new JsonProcessingException('testException') }
132 and: 'dmi returns NOK response'
133 mockDmiDataOperations.getResourceDataFromDmi(*_)
134 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
135 when: 'get resource data is called'
136 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
140 then: 'exception is thrown with the expected details'
141 def exceptionThrown = thrown(NcmpException.class)
142 exceptionThrown.details == 'DMI status code: 404, DMI response body: NOK-json'
145 def 'Get resource data for pass-through operational from dmi return NOK response.'() {
147 def dataNode = getDataNode(true)
148 and: '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',
156 PASSTHROUGH_OPERATIONAL)
157 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
158 when: 'get resource data is called'
159 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
163 then: 'exception is thrown'
164 def exceptionThrown = thrown(NcmpException.class)
165 and: 'details contains the original response'
166 exceptionThrown.details.contains('NOK-json')
169 def 'Get resource data for pass-through running from dmi.'() {
171 def dataNode = getDataNode(true)
172 and: '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',
180 PASSTHROUGH_RUNNING) >> new ResponseEntity<>('{result-json}', HttpStatus.OK)
181 when: 'get resource data is called'
182 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
186 then: 'get resource data returns expected response'
187 response == '{result-json}'
190 def 'Get resource data for pass-through running from dmi return NOK response.'() {
192 def dataNode = getDataNode(true)
193 and: 'cpsDataService returns valid dataNode'
194 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
195 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
196 and: 'dmi returns NOK response'
197 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(NcmpException.class)
210 and: 'details contains the original response'
211 exceptionThrown.details.contains('NOK-json')
214 def 'Getting Yang Resources.'() {
215 when: 'yang resources is called'
216 objectUnderTest.getYangResourcesModuleReferences('some cm handle')
217 then: 'CPS module services is invoked for the correct dataspace and cm handle'
218 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some cm handle')
221 def 'Get cm handle identifiers for the given module names.'() {
222 when: 'execute a cm handle search for the given module names'
223 objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
224 then: 'get anchor identifiers is invoked with the expected parameters'
225 1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
229 def 'Update resource data for pass-through running from dmi using POST #scenario cm handle properties.'() {
231 def dataNode = getDataNode(includeCmHandleProperties)
232 and: 'cpsDataService returns valid datanode'
233 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
234 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
235 when: 'get resource data is called'
236 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
237 'testResourceId', UPDATE,
238 '{some-json}', 'application/json')
239 then: 'dmi called with correct data'
240 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
241 UPDATE, '{some-json}', 'application/json')
242 >> { new ResponseEntity<>(HttpStatus.OK) }
244 scenario | includeCmHandleProperties || expectedJsonForCmhandleProperties
245 'with' | true || '{"testName":"testValue"}'
246 'without' | false || '{}'
249 def 'Verify error message from handleResponse is correct for #scenario operation.'() {
250 given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
251 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
252 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
253 when: 'get resource data is called'
254 def response = objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
260 then: 'an exception is thrown with the expected error message detailsd with correct operation'
261 def exceptionThrown = thrown(NcmpException.class)
262 exceptionThrown.getMessage().contains(expectedResponseMessage)
264 scenario | givenOperation || expectedResponseMessage
265 'CREATE' | CREATE || 'Not able to create resource data.'
266 'READ' | READ || 'Not able to read resource data.'
267 'UPDATE' | UPDATE || 'Not able to update resource data.'
270 def getDataNode(boolean includeCmHandleProperties) {
271 def dataNode = new DataNode()
272 dataNode.leaves = ['dmi-service-name': 'testDmiService']
273 if (includeCmHandleProperties) {
274 def cmHandlePropertyDataNode = new DataNode()
275 cmHandlePropertyDataNode.leaves = ['name': 'testName', 'value': 'testValue']
276 dataNode.childDataNodes = [cmHandlePropertyDataNode]