2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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 com.fasterxml.jackson.databind.ObjectMapper
25 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
26 import org.onap.cps.spi.model.ModuleReference
27 import org.onap.cps.utils.JsonObjectMapper
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.test.context.SpringBootTest
31 import org.springframework.http.HttpStatus
32 import org.springframework.http.ResponseEntity
33 import org.springframework.test.context.ContextConfiguration
34 import spock.lang.Shared
37 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations])
38 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
41 def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
44 DmiModelOperations objectUnderTest
47 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
49 def 'Retrieving module references.'() {
50 given: 'a persistence cm handle'
51 mockPersistenceCmHandleRetrieval([])
52 and: 'a positive response from dmi service when it is called with the expected parameters'
53 def moduleReferencesAsLisOfMaps = [[moduleName:'mod1',revision:'A'],[moduleName:'mod2',revision:'X']]
54 def responseFromDmi = new ResponseEntity([schemas:moduleReferencesAsLisOfMaps], HttpStatus.OK)
55 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
56 '{"cmHandleProperties":{}}', [:]) >> responseFromDmi
57 when: 'get module references is called'
58 def result = objectUnderTest.getModuleReferences(persistenceCmHandle)
59 then: 'the result consists of expected module references'
60 assert result == [new ModuleReference(moduleName:'mod1',revision:'A'), new ModuleReference(moduleName:'mod2',revision:'X')]
63 def 'Retrieving module references edge case: #scenario.'() {
64 given: 'a persistence cm handle'
65 mockPersistenceCmHandleRetrieval([])
66 and: 'any response from dmi service when it is called with the expected parameters'
67 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
68 def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
69 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
70 when: 'get module references is called'
71 def result = objectUnderTest.getModuleReferences(persistenceCmHandle)
72 then: 'the result is empty'
74 where: 'the dmi response body has the following content'
76 'no modules' | [schemas:[]]
77 'modules null' | [schemas:null]
78 'no schema' | [something:'else']
82 def 'Retrieving module references, additional property handling: #scenario.'() {
83 given: 'a persistence cm handle'
84 mockPersistenceCmHandleRetrieval(additionalPropertiesObject)
85 and: 'a positive response from dmi service when it is called with tha expected parameters'
86 def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
87 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
88 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', [:]) >> responseFromDmi
89 when: 'a get module references is called'
90 def result = objectUnderTest.getModuleReferences(persistenceCmHandle)
91 then: 'the result is the response from dmi service'
93 where: 'the following additional properties are used'
94 scenario | additionalPropertiesObject || expectedAdditionalPropertiesInRequest
95 'with properties' | [sampleAdditionalProperty] || '{"prop1":"val1"}'
96 'with null properties' | null || '{}'
97 'without properties' | [] || '{}'
100 def 'Retrieving yang resources.'() {
101 given: 'a persistence cm handle'
102 mockPersistenceCmHandleRetrieval([])
103 and: 'a positive response from dmi service when it is called with tha expected parameters'
104 def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
105 [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
106 def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
107 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
108 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', [:]) >> responseFromDmi
109 when: 'get new yang resources from dmi service'
110 def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, newModuleReferences)
111 then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
112 assert result.size() == 2
113 assert result.get('mod1') == 'some yang source'
114 assert result.get('mod2') == 'other yang source'
117 def 'Retrieving yang resources, edge case: scenario.'() {
118 given: 'a persistence cm handle'
119 mockPersistenceCmHandleRetrieval([])
120 and: 'a positive response from dmi service when it is called with tha expected parameters'
121 // TODO (toine): production code ignores any error code from DMI, this should be improved in future
122 def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
123 mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
124 when: 'get new yang resources from dmi service'
125 def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, newModuleReferences)
126 then: 'the result is empty'
128 where: 'the dmi response body has the following content'
129 scenario | responseFromDmiBody
134 def 'Retrieving yang resources, additional property handling #scenario.'() {
135 given: 'a persistence cm handle'
136 mockPersistenceCmHandleRetrieval(additionalPropertiesObject)
137 and: 'a positive response from dmi service when it is called with the expected parameters'
138 def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
139 mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
140 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}',
141 [:]) >> responseFromDmi
142 when: 'get new yang resources from dmi service'
143 def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, unknownModuleReferences)
144 then: 'the result is the response from dmi service'
145 assert result == [mod1:'some yang source']
146 where: 'the following additional properties are used'
147 scenario | additionalPropertiesObject | unknownModuleReferences || expectedAdditionalPropertiesInRequest | expectedModuleReferencesInRequest
148 'with module references and properties' | [sampleAdditionalProperty] | newModuleReferences || '{"prop1":"val1"}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
149 'without module references' | [sampleAdditionalProperty] | [] || '{"prop1":"val1"}' | ''
150 'without properties' | [] | newModuleReferences || '{}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
153 def 'Retrieving yang resources from dmi with additional properties null.'() {
154 given: 'a persistence cm handle'
155 mockPersistenceCmHandleRetrieval(null)
156 when: 'a get new yang resources from dmi is called'
157 objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, [])
158 then: 'a null pointer is thrown (we might need to address this later)'
159 thrown(NullPointerException)
162 def 'Retrieving module references with Json processing exception.'() {
163 given: 'a persistence cm handle'
164 mockPersistenceCmHandleRetrieval([])
165 and: 'a Json processing exception occurs'
166 spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))}
167 when: 'a dmi operation is executed'
168 objectUnderTest.getModuleReferences(persistenceCmHandle)
169 then: 'an ncmp exception is thrown'
170 def exceptionThrown = thrown(JsonProcessingException)
171 and: 'the message indicates a parsing error'
172 exceptionThrown.message.toLowerCase().contains('parsing error')