2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2024 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.impl.inventory.sync
24 import com.fasterxml.jackson.core.JsonProcessingException
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import org.onap.cps.ncmp.api.impl.config.DmiProperties
27 import org.onap.cps.ncmp.impl.DmiOperationsBaseSpec
28 import org.onap.cps.spi.model.ModuleReference
29 import org.onap.cps.utils.JsonObjectMapper
30 import org.spockframework.spring.SpringBean
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.boot.test.context.SpringBootTest
33 import org.springframework.http.HttpStatus
34 import org.springframework.http.ResponseEntity
35 import org.springframework.test.context.ContextConfiguration
36 import spock.lang.Shared
38 import static org.onap.cps.ncmp.api.data.models.OperationType.READ
39 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.MODEL
42 @ContextConfiguration(classes = [DmiProperties, DmiModelOperations])
43 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
46 def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
49 DmiModelOperations objectUnderTest
52 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
54 def NO_AUTH_HEADER = null
56 def 'Retrieving module references.'() {
58 mockYangModelCmHandleRetrieval([])
59 and: 'a positive response from DMI service when it is called with the expected parameters'
60 def moduleReferencesAsLisOfMaps = [[moduleName: 'mod1', revision: 'A'], [moduleName: 'mod2', revision: 'X']]
61 def expectedUrl = "${DmiOperationsBaseSpec.dmiServiceName}/dmi/v1/ch/${DmiOperationsBaseSpec.cmHandleId}/modules"
62 def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK)
63 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedUrl, '{"cmHandleProperties":{},"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
64 when: 'get module references is called'
65 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
66 then: 'the result consists of expected module references'
67 assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')]
70 def 'Retrieving module references edge case: #scenario.'() {
72 mockYangModelCmHandleRetrieval([])
73 and: 'any response from DMI service when it is called with the expected parameters'
74 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
75 def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
76 mockDmiRestClient.synchronousPostOperationWithJsonData(*_) >> responseFromDmi
77 when: 'get module references is called'
78 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
79 then: 'the result is empty'
81 where: 'the DMI response body has the following content'
83 'no modules' | [schemas:[]]
84 'modules null' | [schemas:null]
85 'no schema' | [something:'else']
89 def 'Retrieving module references, DMI property handling: #scenario.'() {
91 mockYangModelCmHandleRetrieval(dmiProperties)
92 and: 'a positive response from DMI service when it is called with tha expected parameters'
93 def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
94 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, "${DmiOperationsBaseSpec.dmiServiceName}/dmi/v1/ch/${DmiOperationsBaseSpec.cmHandleId}/modules",
95 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + ',"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
96 when: 'a get module references is called'
97 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
98 then: 'the result is the response from DMI service'
100 where: 'the following DMI properties are used'
101 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
102 'with properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
103 'without properties' | [] || '{}'
106 def 'Retrieving yang resources.'() {
108 mockYangModelCmHandleRetrieval([])
109 and: 'a positive response from DMI service when it is called with the expected parameters'
110 def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
111 [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
112 def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
113 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, "${DmiOperationsBaseSpec.dmiServiceName}/dmi/v1/ch/${DmiOperationsBaseSpec.cmHandleId}/moduleResources",
114 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', READ, NO_AUTH_HEADER) >> responseFromDmi
115 when: 'get new yang resources from DMI service'
116 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
117 then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
118 assert result.size() == 2
119 assert result.get('mod1') == 'some yang source'
120 assert result.get('mod2') == 'other yang source'
123 def 'Retrieving yang resources, edge case: scenario.'() {
125 mockYangModelCmHandleRetrieval([])
126 and: 'a positive response from DMI service when it is called with tha expected parameters'
127 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
128 def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
129 mockDmiRestClient.synchronousPostOperationWithJsonData(*_) >> responseFromDmi
130 when: 'get new yang resources from DMI service'
131 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
132 then: 'the result is empty'
134 where: 'the DMI response body has the following content'
135 scenario | responseFromDmiBody
140 def 'Retrieving yang resources, DMI property handling #scenario.'() {
142 mockYangModelCmHandleRetrieval(dmiProperties)
143 and: 'a positive response from DMI service when it is called with the expected moduleSetTag, modules and properties'
144 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
145 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, "${DmiOperationsBaseSpec.dmiServiceName}/dmi/v1/ch/${DmiOperationsBaseSpec.cmHandleId}/moduleResources",
146 '{"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}',
147 READ, NO_AUTH_HEADER) >> responseFromDmi
148 when: 'get new yang resources from DMI service'
149 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
150 then: 'the result is the response from DMI service'
151 assert result == [mod1:'some yang source']
152 where: 'the following DMI properties are used'
153 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
154 'with module references and properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
155 'without properties' | [] || '{}'
158 def 'Retrieving yang resources #scenario'() {
160 mockYangModelCmHandleRetrieval([], moduleSetTag)
161 and: 'a positive response from DMI service when it is called with the expected moduleSetTag'
162 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
163 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, "${DmiOperationsBaseSpec.dmiServiceName}/dmi/v1/ch/${DmiOperationsBaseSpec.cmHandleId}/moduleResources",
164 '{' + expectedModuleSetTagInRequest + '"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":{}}',
165 READ, NO_AUTH_HEADER) >> responseFromDmi
166 when: 'get new yang resources from DMI service'
167 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
168 then: 'the result is the response from DMI service'
169 assert result == [mod1:'some yang source']
170 where: 'the following Module Set Tags are used'
171 scenario | moduleSetTag || expectedModuleSetTagInRequest
172 'Without module set tag' | '' || ''
173 'With module set tag' | 'moduleSetTag1' || '"moduleSetTag":"moduleSetTag1",'
176 def 'Retrieving yang resources from DMI with no module references.'() {
178 mockYangModelCmHandleRetrieval([])
179 when: 'a get new yang resources from DMI is called with no module references'
180 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [])
181 then: 'no resources are returned'
183 and: 'no request is sent to DMI'
184 0 * mockDmiRestClient.synchronousPostOperationWithJsonData(*_)
187 def 'Retrieving yang resources from DMI with null DMI properties.'() {
189 mockYangModelCmHandleRetrieval(null)
190 when: 'a get new yang resources from DMI is called'
191 objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('mod1', 'A')])
192 then: 'a null pointer is thrown (we might need to address this later)'
193 thrown(NullPointerException)
196 def 'Retrieving module references with Json processing exception.'() {
198 mockYangModelCmHandleRetrieval([])
199 and: 'a Json processing exception occurs'
200 spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))}
201 when: 'a DMI operation is executed'
202 objectUnderTest.getModuleReferences(yangModelCmHandle)
203 then: 'an ncmp exception is thrown'
204 def exceptionThrown = thrown(JsonProcessingException)
205 and: 'the message indicates a parsing error'
206 exceptionThrown.message.toLowerCase().contains('parsing error')