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.NcmpConfiguration
27 import org.onap.cps.ncmp.api.impl.executor.TaskExecutor
28 import org.onap.cps.ncmp.api.impl.utils.DmiServiceNameOrganizer
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.impl.operations.OperationType.READ
42 @ContextConfiguration(classes = [NcmpConfiguration.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 = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules"
62 def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK)
63 mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"cmHandleProperties":{}}', READ, NO_AUTH_HEADER)
65 when: 'get module references is called'
66 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
67 then: 'the result consists of expected module references'
68 assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')]
71 def 'Retrieving module references edge case: #scenario.'() {
73 mockYangModelCmHandleRetrieval([])
74 and: 'any response from DMI service when it is called with the expected parameters'
75 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
76 def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
77 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
78 when: 'get module references is called'
79 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
80 then: 'the result is empty'
82 where: 'the DMI response body has the following content'
84 'no modules' | [schemas:[]]
85 'modules null' | [schemas:null]
86 'no schema' | [something:'else']
90 def 'Retrieving module references, DMI property handling: #scenario.'() {
92 mockYangModelCmHandleRetrieval(dmiProperties)
93 and: 'a positive response from DMI service when it is called with tha expected parameters'
94 def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
95 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
96 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', READ, NO_AUTH_HEADER) >> responseFromDmi
97 when: 'a get module references is called'
98 def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
99 then: 'the result is the response from DMI service'
101 where: 'the following DMI properties are used'
102 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
103 'with properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
104 'without properties' | [] || '{}'
107 def 'Retrieving yang resources.'() {
109 mockYangModelCmHandleRetrieval([])
110 and: 'a positive response from DMI service when it is called with the expected parameters'
111 def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
112 [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
113 def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
114 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
115 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', READ, NO_AUTH_HEADER) >> responseFromDmi
116 when: 'get new yang resources from DMI service'
117 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
118 then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
119 assert result.size() == 2
120 assert result.get('mod1') == 'some yang source'
121 assert result.get('mod2') == 'other yang source'
124 def 'Retrieving yang resources, edge case: scenario.'() {
126 mockYangModelCmHandleRetrieval([])
127 and: 'a positive response from DMI service when it is called with tha expected parameters'
128 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
129 def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
130 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
131 when: 'get new yang resources from DMI service'
132 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
133 then: 'the result is empty'
135 where: 'the DMI response body has the following content'
136 scenario | responseFromDmiBody
141 def 'Retrieving yang resources, DMI property handling #scenario.'() {
143 mockYangModelCmHandleRetrieval(dmiProperties)
144 and: 'a positive response from DMI service when it is called with the expected parameters'
145 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
146 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
147 '{"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}',
148 READ, NO_AUTH_HEADER) >> responseFromDmi
149 when: 'get new yang resources from DMI service'
150 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
151 then: 'the result is the response from DMI service'
152 assert result == [mod1:'some yang source']
153 where: 'the following DMI properties are used'
154 scenario | dmiProperties || expectedAdditionalPropertiesInRequest
155 'with module references and properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
156 'without properties' | [] || '{}'
159 def 'Retrieving yang resources from DMI with no module references.'() {
161 mockYangModelCmHandleRetrieval([])
162 when: 'a get new yang resources from DMI is called with no module references'
163 def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [])
164 then: 'no resources are returned'
166 and: 'no request is sent to DMI'
167 0 * mockDmiRestClient.postOperationWithJsonData(*_)
170 def 'Retrieving yang resources from DMI with null DMI properties.'() {
172 mockYangModelCmHandleRetrieval(null)
173 when: 'a get new yang resources from DMI is called'
174 objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('mod1', 'A')])
175 then: 'a null pointer is thrown (we might need to address this later)'
176 thrown(NullPointerException)
179 def 'Retrieving module references with Json processing exception.'() {
181 mockYangModelCmHandleRetrieval([])
182 and: 'a Json processing exception occurs'
183 spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))}
184 when: 'a DMI operation is executed'
185 objectUnderTest.getModuleReferences(yangModelCmHandle)
186 then: 'an ncmp exception is thrown'
187 def exceptionThrown = thrown(JsonProcessingException)
188 and: 'the message indicates a parsing error'
189 exceptionThrown.message.toLowerCase().contains('parsing error')