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.operations.YangModelCmHandleRetriever
27 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
28 import spock.lang.Shared
30 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_OPERATIONAL
31 import static org.onap.cps.ncmp.api.impl.operations.DmiOperations.DataStoreEnum.PASSTHROUGH_RUNNING
32 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
33 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ
34 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.UPDATE
36 import org.onap.cps.ncmp.api.impl.operations.DmiModelOperations
37 import org.onap.cps.utils.JsonObjectMapper
38 import com.fasterxml.jackson.core.JsonProcessingException
39 import com.fasterxml.jackson.databind.ObjectMapper
40 import org.onap.cps.api.CpsAdminService
41 import org.onap.cps.api.CpsDataService
42 import org.onap.cps.api.CpsModuleService
43 import org.onap.cps.ncmp.api.impl.operations.DmiDataOperations
44 import org.onap.cps.spi.FetchDescendantsOption
45 import org.onap.cps.spi.model.DataNode
46 import org.springframework.http.HttpStatus
47 import org.springframework.http.ResponseEntity
48 import spock.lang.Specification
50 class NetworkCmProxyDataServiceImplSpec extends Specification {
52 def mockCpsDataService = Mock(CpsDataService)
53 def mockCpsModuleService = Mock(CpsModuleService)
54 def mockCpsAdminService = Mock(CpsAdminService)
55 def spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
56 def mockDmiModelOperations = Mock(DmiModelOperations)
57 def mockDmiDataOperations = Mock(DmiDataOperations)
58 def nullNetworkCmProxyDataServicePropertyHandler = null
59 def mockYangModelCmHandleRetriever = Mock(YangModelCmHandleRetriever)
61 def NO_REQUEST_ID = null
63 def OPTIONS_PARAM = '(a=1,b=2)'
65 def objectUnderTest = new NetworkCmProxyDataServiceImpl(mockCpsDataService, spiedJsonObjectMapper, mockDmiDataOperations, mockDmiModelOperations,
66 mockCpsModuleService, mockCpsAdminService, nullNetworkCmProxyDataServicePropertyHandler, mockYangModelCmHandleRetriever)
68 def cmHandleXPath = "/dmi-registry/cm-handles[@id='testCmHandle']"
70 def dataNode = new DataNode(leaves: ['dmi-service-name': 'testDmiService'])
72 def 'Write resource data for pass-through running from DMI using POST #scenario cm handle properties.'() {
73 given: 'cpsDataService returns valid datanode'
74 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
75 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
76 when: 'get resource data is called'
77 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
78 'testResourceId', CREATE,
79 '{some-json}', 'application/json')
80 then: 'DMI called with correct data'
81 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
82 CREATE, '{some-json}', 'application/json')
83 >> { new ResponseEntity<>(HttpStatus.CREATED) }
86 def 'Write resource data for pass-through running from DMI using POST "not found" response (from DMI).'() {
87 given: 'cpsDataService returns valid dataNode'
88 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
89 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
90 and: 'DMI returns a response with 404 status code'
91 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle',
92 'testResourceId', CREATE,
93 '{some-json}', 'application/json')
94 >> { new ResponseEntity<>(HttpStatus.NOT_FOUND) }
95 when: 'write resource data is called'
96 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
97 'testResourceId', CREATE,
98 '{some-json}', 'application/json')
99 then: 'exception is thrown'
100 def exceptionThrown = thrown(HttpClientRequestException.class)
101 and: 'http status (not found) error code: 404'
102 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
105 def 'Get resource data for pass-through operational from DMI.'() {
106 given: 'get data node is called'
107 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
108 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
109 and: 'get resource data from DMI is called'
110 mockDmiDataOperations.getResourceDataFromDmi(
114 PASSTHROUGH_OPERATIONAL,
116 NO_TOPIC) >> new ResponseEntity<>('dmi-response', HttpStatus.OK)
117 when: 'get resource data operational for cm-handle is called'
118 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',
142 then: 'exception is thrown with the expected response code and details'
143 def exceptionThrown = thrown(HttpClientRequestException.class)
144 exceptionThrown.details.contains('NOK-json')
145 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
148 def 'Get resource data for pass-through operational from DMI return NOK response.'() {
149 given: 'cps data service returns valid data node'
150 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
151 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
152 and: 'DMI returns NOK response'
153 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
156 PASSTHROUGH_OPERATIONAL,
159 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
160 when: 'get resource data is called'
161 objectUnderTest.getResourceDataOperationalForCmHandle('testCmHandle',
166 then: 'exception is thrown'
167 def exceptionThrown = thrown(HttpClientRequestException.class)
168 and: 'details contain the original response'
169 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
170 exceptionThrown.details.contains('NOK-json')
173 def 'Get resource data for pass-through running from DMI.'() {
174 given: 'cpsDataService returns valid data node'
175 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
176 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
177 and: 'DMI returns valid response and data'
178 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
183 NO_TOPIC) >> new ResponseEntity<>('{dmi-response}', HttpStatus.OK)
184 when: 'get resource data is called'
185 def response = objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
190 then: 'get resource data returns expected response'
191 response == '{dmi-response}'
194 def 'Get resource data for pass-through running from DMI return NOK response.'() {
195 given: 'cpsDataService returns valid dataNode'
196 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
197 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
198 and: 'DMI returns NOK response'
199 mockDmiDataOperations.getResourceDataFromDmi('testCmHandle',
205 >> new ResponseEntity<>('NOK-json', HttpStatus.NOT_FOUND)
206 when: 'get resource data is called'
207 objectUnderTest.getResourceDataPassThroughRunningForCmHandle('testCmHandle',
212 then: 'exception is thrown'
213 def exceptionThrown = thrown(HttpClientRequestException.class)
214 and: 'details contain the original response'
215 exceptionThrown.details.contains('NOK-json')
216 exceptionThrown.httpStatus == HttpStatus.NOT_FOUND.value()
219 def 'Getting Yang Resources.'() {
220 when: 'yang resources is called'
221 objectUnderTest.getYangResourcesModuleReferences('some-cm-handle')
222 then: 'CPS module services is invoked for the correct dataspace and cm handle'
223 1 * mockCpsModuleService.getYangResourcesModuleReferences('NFP-Operational','some-cm-handle')
226 def 'Get cm handle identifiers for the given module names.'() {
227 when: 'execute a cm handle search for the given module names'
228 objectUnderTest.executeCmHandleHasAllModulesSearch(['some-module-name'])
229 then: 'get anchor identifiers is invoked with the expected parameters'
230 1 * mockCpsAdminService.queryAnchorNames('NFP-Operational', ['some-module-name'])
233 def 'Get a cm handle.'() {
234 given: 'the system returns a yang modelled cm handle'
235 def dmiServiceName = 'some service name'
236 def dmiProperties = [new YangModelCmHandle.Property('Book', 'Romance Novel')]
237 def publicProperties = [new YangModelCmHandle.Property('Public Book', 'Public Romance Novel')]
238 def yangModelCmHandle = new YangModelCmHandle(id:'Some-Cm-Handle', dmiServiceName: dmiServiceName, dmiProperties: dmiProperties, publicProperties: publicProperties)
239 1 * mockYangModelCmHandleRetriever.getDmiServiceNamesAndProperties('Some-Cm-Handle') >> yangModelCmHandle
240 when: 'getting cm handle details for a given cm handle id from ncmp service'
241 def result = objectUnderTest.getNcmpServiceCmHandle('Some-Cm-Handle')
242 then: 'the result returns the correct data'
243 result.cmHandleID == 'Some-Cm-Handle'
244 result.dmiProperties ==[ Book:'Romance Novel' ]
245 result.publicProperties == [ "Public Book":'Public Romance Novel' ]
249 def 'Update resource data for pass-through running from dmi using POST #scenario DMI properties.'() {
250 given: 'cpsDataService returns valid datanode'
251 mockCpsDataService.getDataNode('NCMP-Admin', 'ncmp-dmi-registry',
252 cmHandleXPath, FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS) >> dataNode
253 when: 'get resource data is called'
254 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle('testCmHandle',
255 'testResourceId', UPDATE,
256 '{some-json}', 'application/json')
257 then: 'DMI called with correct data'
258 1 * mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi('testCmHandle', 'testResourceId',
259 UPDATE, '{some-json}', 'application/json')
260 >> { new ResponseEntity<>(HttpStatus.OK) }
263 def 'Verify error message from handleResponse is correct for #scenario operation.'() {
264 given: 'writeResourceDataPassThroughRunningFromDmi fails to return OK HttpStatus'
265 mockDmiDataOperations.writeResourceDataPassThroughRunningFromDmi(*_)
266 >> new ResponseEntity<>(HttpStatus.NOT_FOUND)
267 when: 'get resource data is called'
268 objectUnderTest.writeResourceDataPassThroughRunningForCmHandle(
274 then: 'an exception is thrown with the expected error message details with correct operation'
275 def exceptionThrown = thrown(HttpClientRequestException.class)
276 exceptionThrown.getMessage().contains(expectedResponseMessage)
278 scenario | givenOperation || expectedResponseMessage
279 'CREATE' | CREATE || 'Unable to create resource data.'
280 'READ' | READ || 'Unable to read resource data.'
281 'UPDATE' | UPDATE || 'Unable to update resource data.'