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.api.impl.operations
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.spi.model.ModuleReference
28 import org.onap.cps.utils.JsonObjectMapper
29 import org.spockframework.spring.SpringBean
30 import org.springframework.beans.factory.annotation.Autowired
31 import org.springframework.boot.test.context.SpringBootTest
32 import org.springframework.http.HttpStatus
33 import org.springframework.http.ResponseEntity
34 import org.springframework.test.context.ContextConfiguration
35 import spock.lang.Shared
37 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
40 @ContextConfiguration(classes = [DmiProperties, DmiModelOperations])
41 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
44 def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
47 DmiModelOperations objectUnderTest
50 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
52 def NO_AUTH_HEADER = null
54 def 'Retrieving module references.'() {
56 mockYangModelCmHandleRetrieval([])
57 and: 'a positive response from DMI service when it is called with the expected parameters'
58 def moduleReferencesAsLisOfMaps = [[moduleName: 'mod1', revision: 'A'], [moduleName: 'mod2', revision: 'X']]
59 def expectedUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules"
60 def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK)
61 mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"cmHandleProperties":{},"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
62 when: 'get module references is called'
63 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
64 then: 'the result consists of expected module references'
65 assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')]
68 def 'Retrieving module references edge case: #scenario.'() {
70 mockYangModelCmHandleRetrieval([])
71 and: 'any response from DMI service when it is called with the expected parameters'
72 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
73 def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
74 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
75 when: 'get module references is called'
76 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
77 then: 'the result is empty'
79 where: 'the DMI response body has the following content'
81 'no modules' | [schemas:[]]
82 'modules null' | [schemas:null]
83 'no schema' | [something:'else']
87 def 'Retrieving module references, DMI property handling: #scenario.'() {
89 mockYangModelCmHandleRetrieval(dmiProperties)
90 and: 'a positive response from DMI service when it is called with tha expected parameters'
91 def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
92 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
93 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + ',"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
94 when: 'a get module references is called'
95 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
96 then: 'the result is the response from DMI service'
98 where: 'the following DMI properties are used'
99 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
100 'with properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
101 'without properties' | [] || '{}'
104 def 'Retrieving yang resources.'() {
106 mockYangModelCmHandleRetrieval([])
107 and: 'a positive response from DMI service when it is called with the expected parameters'
108 def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
109 [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
110 def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
111 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
112 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', READ, NO_AUTH_HEADER) >> responseFromDmi
113 when: 'get new yang resources from DMI service'
114 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
115 then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
116 assert result.size() == 2
117 assert result.get('mod1') == 'some yang source'
118 assert result.get('mod2') == 'other yang source'
121 def 'Retrieving yang resources, edge case: scenario.'() {
123 mockYangModelCmHandleRetrieval([])
124 and: 'a positive response from DMI service when it is called with tha expected parameters'
125 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
126 def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
127 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
128 when: 'get new yang resources from DMI service'
129 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
130 then: 'the result is empty'
132 where: 'the DMI response body has the following content'
133 scenario | responseFromDmiBody
138 def 'Retrieving yang resources, DMI property handling #scenario.'() {
140 mockYangModelCmHandleRetrieval(dmiProperties)
141 and: 'a positive response from DMI service when it is called with the expected moduleSetTag, modules and properties'
142 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
143 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
144 '{"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}',
145 READ, NO_AUTH_HEADER) >> responseFromDmi
146 when: 'get new yang resources from DMI service'
147 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
148 then: 'the result is the response from DMI service'
149 assert result == [mod1:'some yang source']
150 where: 'the following DMI properties are used'
151 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
152 'with module references and properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
153 'without properties' | [] || '{}'
156 def 'Retrieving yang resources #scenario'() {
158 mockYangModelCmHandleRetrieval([], moduleSetTag)
159 and: 'a positive response from DMI service when it is called with the expected moduleSetTag'
160 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
161 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
162 '{' + expectedModuleSetTagInRequest + '"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":{}}',
163 READ, NO_AUTH_HEADER) >> responseFromDmi
164 when: 'get new yang resources from DMI service'
165 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
166 then: 'the result is the response from DMI service'
167 assert result == [mod1:'some yang source']
168 where: 'the following Module Set Tags are used'
169 scenario | moduleSetTag || expectedModuleSetTagInRequest
170 'Without module set tag' | '' || ''
171 'With module set tag' | 'moduleSetTag1' || '"moduleSetTag":"moduleSetTag1",'
174 def 'Retrieving yang resources from DMI with no module references.'() {
176 mockYangModelCmHandleRetrieval([])
177 when: 'a get new yang resources from DMI is called with no module references'
178 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [])
179 then: 'no resources are returned'
181 and: 'no request is sent to DMI'
182 0 * mockDmiRestClient.postOperationWithJsonData(*_)
185 def 'Retrieving yang resources from DMI with null DMI properties.'() {
187 mockYangModelCmHandleRetrieval(null)
188 when: 'a get new yang resources from DMI is called'
189 objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('mod1', 'A')])
190 then: 'a null pointer is thrown (we might need to address this later)'
191 thrown(NullPointerException)
194 def 'Retrieving module references with Json processing exception.'() {
196 mockYangModelCmHandleRetrieval([])
197 and: 'a Json processing exception occurs'
198 spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))}
199 when: 'a DMI operation is executed'
200 objectUnderTest.getModuleReferences(yangModelCmHandle)
201 then: 'an ncmp exception is thrown'
202 def exceptionThrown = thrown(JsonProcessingException)
203 and: 'the message indicates a parsing error'
204 exceptionThrown.message.toLowerCase().contains('parsing error')