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.NcmpException
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(NcmpException.class)
92 and: 'details contains (not found) error code: 404'
93 exceptionThrown.details.contains('404')
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(
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',
113 then: 'DMI returns a json response'
114 response == 'result-json'
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',
131 then: 'exception is thrown with the expected details'
132 def exceptionThrown = thrown(NcmpException.class)
133 exceptionThrown.details == 'DMI status code: 404, DMI response body: NOK-json'
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',
145 PASSTHROUGH_OPERATIONAL)
146 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
147 when: 'get resource data is called'
148 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
152 then: 'exception is thrown'
153 def exceptionThrown = thrown(NcmpException.class)
154 and: 'details contains the original response'
155 exceptionThrown.details.contains('NOK-json')
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',
167 PASSTHROUGH_RUNNING) >> new ResponseEntity<>('{result-json}', HttpStatus.OK)
168 when: 'get resource data is called'
169 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
173 then: 'get resource data returns expected response'
174 response == '{result-json}'
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',
187 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
188 when: 'get resource data is called'
189 objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
193 then: 'exception is thrown'
194 def exceptionThrown = thrown(NcmpException.class)
195 and: 'details contains the original response'
196 exceptionThrown.details.contains('NOK-json')
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')
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'])
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) }
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(
238 then: 'an exception is thrown with the expected error message detailsd with correct operation'
239 def exceptionThrown = thrown(NcmpException.class)
240 exceptionThrown.getMessage().contains(expectedResponseMessage)
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.'