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.impl.dmi.DmiOperationsBaseSpec
27 import org.onap.cps.ncmp.impl.dmi.DmiProperties
28 import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters
29 import org.onap.cps.spi.model.ModuleReference
30 import org.onap.cps.utils.JsonObjectMapper
31 import org.spockframework.spring.SpringBean
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.test.context.SpringBootTest
34 import org.springframework.http.HttpStatus
35 import org.springframework.http.ResponseEntity
36 import org.springframework.test.context.ContextConfiguration
37 import spock.lang.Shared
39 import static org.onap.cps.ncmp.api.data.models.OperationType.READ
40 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.MODEL
43 @ContextConfiguration(classes = [DmiProperties, DmiModelOperations])
44 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
46 def expectedModulesUrlTemplateWithVariables = new UrlTemplateParameters('myServiceName/dmi/v1/ch/{cmHandleId}/modules', ['cmHandleId': cmHandleId])
47 def expectedModuleResourcesUrlTemplateWithVariables = new UrlTemplateParameters('myServiceName/dmi/v1/ch/{cmHandleId}/moduleResources', ['cmHandleId': cmHandleId])
50 def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
53 DmiModelOperations objectUnderTest
56 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
58 def NO_AUTH_HEADER = null
60 def 'Retrieving module references.'() {
62 mockYangModelCmHandleRetrieval([])
63 and: 'a positive response from DMI service when it is called with the expected parameters'
64 def moduleReferencesAsLisOfMaps = [[moduleName: 'mod1', revision: 'A'], [moduleName: 'mod2', revision: 'X']]
65 def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK)
66 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModulesUrlTemplateWithVariables, '{"cmHandleProperties":{},"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
67 when: 'get module references is called'
68 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
69 then: 'the result consists of expected module references'
70 assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')]
73 def 'Retrieving module references edge case: #scenario.'() {
75 mockYangModelCmHandleRetrieval([])
76 and: 'any response from DMI service when it is called with the expected parameters'
77 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
78 def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
79 mockDmiRestClient.synchronousPostOperationWithJsonData(*_) >> responseFromDmi
80 when: 'get module references is called'
81 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
82 then: 'the result is empty'
84 where: 'the DMI response body has the following content'
86 'no modules' | [schemas:[]]
87 'modules null' | [schemas:null]
88 'no schema' | [something:'else']
92 def 'Retrieving module references, DMI property handling: #scenario.'() {
94 mockYangModelCmHandleRetrieval(dmiProperties)
95 and: 'a positive response from DMI service when it is called with tha expected parameters'
96 def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
97 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModulesUrlTemplateWithVariables,
98 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + ',"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
99 when: 'a get module references is called'
100 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
101 then: 'the result is the response from DMI service'
103 where: 'the following DMI properties are used'
104 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
105 'with properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
106 'without properties' | [] || '{}'
109 def 'Retrieving yang resources.'() {
111 mockYangModelCmHandleRetrieval([])
112 and: 'a positive response from DMI service when it is called with the expected parameters'
113 def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
114 [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
115 def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
116 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModuleResourcesUrlTemplateWithVariables,
117 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', READ, NO_AUTH_HEADER) >> responseFromDmi
118 when: 'get new yang resources from DMI service'
119 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
120 then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
121 assert result.size() == 2
122 assert result.get('mod1') == 'some yang source'
123 assert result.get('mod2') == 'other yang source'
126 def 'Retrieving yang resources, edge case: scenario.'() {
128 mockYangModelCmHandleRetrieval([])
129 and: 'a positive response from DMI service when it is called with tha expected parameters'
130 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
131 def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
132 mockDmiRestClient.synchronousPostOperationWithJsonData(*_) >> responseFromDmi
133 when: 'get new yang resources from DMI service'
134 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
135 then: 'the result is empty'
137 where: 'the DMI response body has the following content'
138 scenario | responseFromDmiBody
143 def 'Retrieving yang resources, DMI property handling #scenario.'() {
145 mockYangModelCmHandleRetrieval(dmiProperties)
146 and: 'a positive response from DMI service when it is called with the expected moduleSetTag, modules and properties'
147 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
148 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModuleResourcesUrlTemplateWithVariables,
149 '{"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}',
150 READ, NO_AUTH_HEADER) >> responseFromDmi
151 when: 'get new yang resources from DMI service'
152 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
153 then: 'the result is the response from DMI service'
154 assert result == [mod1:'some yang source']
155 where: 'the following DMI properties are used'
156 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
157 'with module references and properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
158 'without properties' | [] || '{}'
161 def 'Retrieving yang resources #scenario'() {
163 mockYangModelCmHandleRetrieval([], moduleSetTag)
164 and: 'a positive response from DMI service when it is called with the expected moduleSetTag'
165 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
166 mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModuleResourcesUrlTemplateWithVariables,
167 '{' + expectedModuleSetTagInRequest + '"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":{}}', READ, NO_AUTH_HEADER) >> responseFromDmi
168 when: 'get new yang resources from DMI service'
169 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
170 then: 'the result is the response from DMI service'
171 assert result == [mod1:'some yang source']
172 where: 'the following Module Set Tags are used'
173 scenario | moduleSetTag || expectedModuleSetTagInRequest
174 'Without module set tag' | '' || ''
175 'With module set tag' | 'moduleSetTag1' || '"moduleSetTag":"moduleSetTag1",'
176 'Special characters in module set tag' | 'module:set#tag$2' || '"moduleSetTag":"module:set#tag$2",'
179 def 'Retrieving yang resources from DMI with no module references.'() {
181 mockYangModelCmHandleRetrieval([])
182 when: 'a get new yang resources from DMI is called with no module references'
183 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [])
184 then: 'no resources are returned'
186 and: 'no request is sent to DMI'
187 0 * mockDmiRestClient.synchronousPostOperationWithJsonData(*_)
190 def 'Retrieving yang resources from DMI with null DMI properties.'() {
192 mockYangModelCmHandleRetrieval(null)
193 when: 'a get new yang resources from DMI is called'
194 objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('mod1', 'A')])
195 then: 'a null pointer is thrown (we might need to address this later)'
196 thrown(NullPointerException)
199 def 'Retrieving module references with Json processing exception.'() {
201 mockYangModelCmHandleRetrieval([])
202 and: 'a Json processing exception occurs'
203 spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))}
204 when: 'a DMI operation is executed'
205 objectUnderTest.getModuleReferences(yangModelCmHandle)
206 then: 'an ncmp exception is thrown'
207 def exceptionThrown = thrown(JsonProcessingException)
208 and: 'the message indicates a parsing error'
209 exceptionThrown.message.toLowerCase().contains('parsing error')