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 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
46 class NetworkCmProxyDataServiceImplSpec extends Specification {
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)
55 def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
56 mockCpsModuleService, mockCpsAdminService)
58 def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
60 def dataNode = new DataNode(leaves: ['dmi-service-name': 'testDmiService'])
63 def 'Write resource data for pass-through running from DMI using POST #scenario cm handle properties.'() {
64 given: 'cpsDataService returns valid datanode'
65 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
66 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
67 when: 'get resource data is called'
68 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
69 'testResourceId', CREATE,
70 '{some-json}', 'application/json')
71 then: 'DMI called with correct data'
72 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
73 CREATE, '{some-json}', 'application/json')
74 >> { new ResponseEntity<>(HttpStatus.CREATED) }
77 def 'Write resource data for pass-through running from DMI using POST "not found" response (from DMI).'() {
78 given: 'cpsDataService returns valid dataNode'
79 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
80 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
81 and: 'DMI returns a response with 404 status code'
82 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle',
83 'testResourceId', CREATE,
84 '{some-json}', 'application/json')
85 >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) }
86 when: 'write resource data is called'
87 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
88 'testResourceId', CREATE,
89 '{some-json}', 'application/json')
90 then: 'exception is thrown'
91 def exceptionThrown = thrown(ServerNcmpException.class)
92 and: 'details contains (not found) error code: 404'
93 exceptionThrown.details.contains('404')
96 def 'Get resource data for pass-through operational from DMI.'() {
97 given: 'get data node is called'
98 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
99 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
100 and: 'get resource data from DMI is called'
101 mockDmiDataOperations.getResourceDataFromDmi(
106 PASSTHROUGH_OPERATIONAL) >> new ResponseEntity<>('result-json', HttpStatus.OK)
107 when: 'get resource data operational for cm-handle is called'
108 def response = objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
112 then: 'DMI returns a json response'
113 response == 'result-json'
116 def 'Get resource data for pass-through operational from DMI with Json Processing Exception.'() {
117 given: 'cps data service returns valid data node'
118 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
119 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
120 and: 'objectMapper not able to parse object'
121 spiedJsonObjectMapper.asJsonString(_) >> { throw new JsonProcessingException('testException') }
122 and: 'DMI returns NOK response'
123 mockDmiDataOperations.getResourceDataFromDmi(*_)
124 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
125 when: 'get resource data is called'
126 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
130 then: 'exception is thrown with the expected details'
131 def exceptionThrown = thrown(ServerNcmpException.class)
132 exceptionThrown.details == 'DMI status code: 404, DMI response body: NOK-json'
135 def 'Get resource data for pass-through operational from DMI return NOK response.'() {
136 given: 'cps data service returns valid data node'
137 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
138 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
139 and: 'DMI returns NOK response'
140 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
144 PASSTHROUGH_OPERATIONAL)
145 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
146 when: 'get resource data is called'
147 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
151 then: 'exception is thrown'
152 def exceptionThrown = thrown(ServerNcmpException.class)
153 and: 'details contains the original response'
154 exceptionThrown.details.contains('NOK-json')
157 def 'Get resource data for pass-through running from DMI.'() {
158 given: 'cpsDataService returns valid data node'
159 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
160 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
161 and: 'DMI returns valid response and data'
162 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
166 PASSTHROUGH_RUNNING) >> new ResponseEntity<>('{result-json}', HttpStatus.OK)
167 when: 'get resource data is called'
168 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
172 then: 'get resource data returns expected response'
173 response == '{result-json}'
176 def 'Get resource data for pass-through running from DMI return NOK response.'() {
177 given: 'cpsDataService returns valid dataNode'
178 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
179 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
180 and: 'DMI returns NOK response'
181 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
186 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
187 when: 'get resource data is called'
188 objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
192 then: 'exception is thrown'
193 def exceptionThrown = thrown(ServerNcmpException.class)
194 and: 'details contains the original response'
195 exceptionThrown.details.contains('NOK-json')
198 def 'Getting Yang Resources.'() {
199 when: 'yang resources is called'
200 objectUnderTest.getYangResourcesModuleReferences('some cm handle')
201 then: 'CPS module services is invoked for the correct dataspace and cm handle'
202 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some cm handle')
205 def 'Get cm handle identifiers for the given module names.'() {
206 when: 'execute a cm handle search for the given module names'
207 objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
208 then: 'get anchor identifiers is invoked with the expected parameters'
209 1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
212 def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
213 given: 'cpsDataService returns valid datanode'
214 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
215 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
216 when: 'get resource data is called'
217 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
218 'testResourceId', UPDATE,
219 '{some-json}', 'application/json')
220 then: 'DMI called with correct data'
221 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
222 UPDATE, '{some-json}', 'application/json')
223 >> { new ResponseEntity<>(HttpStatus.OK) }
226 def 'Verify error message from handleResponse is correct for #scenario operation.'() {
227 given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
228 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
229 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
230 when: 'get resource data is called'
231 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
237 then: 'an exception is thrown with the expected error message detailsd with correct operation'
238 def exceptionThrown = thrown(ServerNcmpException.class)
239 exceptionThrown.getMessage().contains(expectedResponseMessage)
241 scenario | givenOperation || expectedResponseMessage
242 'CREATE' | CREATE || 'Not able to create resource data.'
243 'READ' | READ || 'Not able to read resource data.'
244 'UPDATE' | UPDATE || 'Not able to update resource data.'