2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.operations
23 import com.fasterxml.jackson.core.JsonProcessingException
24 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
25 import org.onap.cps.ncmp.api.impl.exception.NcmpException
26 import org.onap.cps.spi.model.ModuleReference
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.test.context.SpringBootTest
29 import org.springframework.http.HttpStatus
30 import org.springframework.http.ResponseEntity
31 import org.springframework.test.context.ContextConfiguration
32 import spock.lang.Shared
35 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations])
36 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
39 def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
42 DmiModelOperations objectUnderTest
44 def 'Retrieving module references.'() {
45 given: 'a persistence cm handle'
46 mockPersistenceCmHandleRetrieval([])
47 and: 'a positive response from dmi service when it is called with the expected parameters'
48 def moduleReferencesAsLisOfMaps = [[moduleName:'mod1',revision:'A'],[moduleName:'mod2',revision:'X']]
49 def responseFromDmi = new ResponseEntity([schemas:moduleReferencesAsLisOfMaps], HttpStatus.OK)
50 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
51 '{"cmHandleProperties":{}}', [:]) >> responseFromDmi
52 when: 'get module references is called'
53 def result = objectUnderTest.getModuleReferences(persistenceCmHandle)
54 then: 'the result consists of expected module references'
55 assert result == [new ModuleReference(moduleName:'mod1',revision:'A'), new ModuleReference(moduleName:'mod2',revision:'X')]
58 def 'Retrieving module references edge case: #scenario.'() {
59 given: 'a persistence cm handle'
60 mockPersistenceCmHandleRetrieval([])
61 and: 'any response from dmi service when it is called with the expected parameters'
62 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
63 def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
64 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
65 when: 'get module references is called'
66 def result = objectUnderTest.getModuleReferences(persistenceCmHandle)
67 then: 'the result is empty'
69 where: 'the dmi response body has the following content'
71 'no modules' | [schemas:[]]
72 'modules null' | [schemas:null]
73 'no schema' | [something:'else']
77 def 'Retrieving module references, additional property handling: #scenario.'() {
78 given: 'a persistence cm handle'
79 mockPersistenceCmHandleRetrieval(additionalPropertiesObject)
80 and: 'a positive response from dmi service when it is called with tha expected parameters'
81 def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
82 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
83 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', [:]) >> responseFromDmi
84 when: 'a get module references is called'
85 def result = objectUnderTest.getModuleReferences(persistenceCmHandle)
86 then: 'the result is the response from dmi service'
88 where: 'the following additional properties are used'
89 scenario | additionalPropertiesObject || expectedAdditionalPropertiesInRequest
90 'with properties' | [sampleAdditionalProperty] || '{"prop1":"val1"}'
91 'with null properties' | null || '{}'
92 'without properties' | [] || '{}'
95 def 'Retrieving yang resources.'() {
96 given: 'a persistence cm handle'
97 mockPersistenceCmHandleRetrieval([])
98 and: 'a positive response from dmi service when it is called with tha expected parameters'
99 def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
100 [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
101 def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
102 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
103 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', [:]) >> responseFromDmi
104 when: 'get new yang resources from dmi service'
105 def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, newModuleReferences)
106 then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
107 assert result.size() == 2
108 assert result.get('mod1') == 'some yang source'
109 assert result.get('mod2') == 'other yang source'
112 def 'Retrieving yang resources, edge case: scenario.'() {
113 given: 'a persistence cm handle'
114 mockPersistenceCmHandleRetrieval([])
115 and: 'a positive response from dmi service when it is called with tha expected parameters'
116 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
117 def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
118 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
119 when: 'get new yang resources from dmi service'
120 def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, newModuleReferences)
121 then: 'the result is empty'
123 where: 'the dmi response body has the following content'
124 scenario | responseFromDmiBody
129 def 'Retrieving yang resources, additional property handling #scenario.'() {
130 given: 'a persistence cm handle'
131 mockPersistenceCmHandleRetrieval(additionalPropertiesObject)
132 and: 'a positive response from dmi service when it is called with the expected parameters'
133 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
134 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
135 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}',
136 [:]) >> responseFromDmi
137 when: 'get new yang resources from dmi service'
138 def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, unknownModuleReferences)
139 then: 'the result is the response from dmi service'
140 assert result == [mod1:'some yang source']
141 where: 'the following additional properties are used'
142 scenario | additionalPropertiesObject | unknownModuleReferences || expectedAdditionalPropertiesInRequest | expectedModuleReferencesInRequest
143 'with module references and properties' | [sampleAdditionalProperty] | newModuleReferences || '{"prop1":"val1"}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
144 'without module references' | [sampleAdditionalProperty] | [] || '{"prop1":"val1"}' | ''
145 'without properties' | [] | newModuleReferences || '{}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
148 def 'Retrieving yang resources from dmi with additional properties null.'() {
149 given: 'a persistence cm handle'
150 mockPersistenceCmHandleRetrieval(null)
151 when: 'a get new yang resources from dmi is called'
152 objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, [])
153 then: 'a null pointer is thrown (we might need to address this later)'
154 thrown(NullPointerException)
157 def 'Retrieving module references with Json processing exception.'() {
158 given: 'a persistence cm handle'
159 mockPersistenceCmHandleRetrieval([])
160 and: 'a Json processing exception occurs'
161 spyObjectMapper.writeValueAsString(_) >> {throw (new JsonProcessingException(''))}
162 when: 'a dmi operation is executed'
163 objectUnderTest.getModuleReferences(persistenceCmHandle)
164 then: 'an ncmp exception is thrown'
165 def exceptionThrown = thrown(NcmpException)
166 and: 'the message indicates a parsing error'
167 exceptionThrown.message.toLowerCase().contains('parsing error')