de5e15e504e11eb841d8dced0b106ea155b0ca53
[cps.git] /
1 /*
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.operations
23
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
36
37 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
38
39 @SpringBootTest
40 @ContextConfiguration(classes = [DmiProperties, DmiModelOperations])
41 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
42
43     @Shared
44     def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
45
46     @Autowired
47     DmiModelOperations objectUnderTest
48
49     @SpringBean
50     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
51
52     def NO_AUTH_HEADER = null
53
54     def 'Retrieving module references.'() {
55         given: 'a cm handle'
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')]
66     }
67
68     def 'Retrieving module references edge case: #scenario.'() {
69         given: 'a cm handle'
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'
78             assert result == []
79         where: 'the DMI response body has the following content'
80             scenario       | bodyAsMap
81             'no modules'   | [schemas:[]]
82             'modules null' | [schemas:null]
83             'no schema'    | [something:'else']
84             'no body'      | null
85     }
86
87     def 'Retrieving module references, DMI property handling:  #scenario.'() {
88         given: 'a cm handle'
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'
97             assert result == []
98         where: 'the following DMI properties are used'
99             scenario             | dmiProperties               || expectedAdditionalPropertiesInRequest
100             'with properties'    | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
101             'without properties' | []                          || '{}'
102     }
103
104     def 'Retrieving yang resources.'() {
105         given: 'a cm handle'
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'
119     }
120
121     def 'Retrieving yang resources, edge case: scenario.'() {
122         given: 'a cm handle'
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'
131             assert result == [:]
132         where: 'the DMI response body has the following content'
133             scenario      | responseFromDmiBody
134             'empty array' | []
135             'null array'  | null
136     }
137
138     def 'Retrieving yang resources, DMI property handling #scenario.'() {
139         given: 'a cm handle'
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'                    | []                          || '{}'
154     }
155
156     def 'Retrieving yang resources  #scenario'() {
157         given: 'a cm handle'
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",'
172     }
173
174     def 'Retrieving yang resources from DMI with no module references.'() {
175         given: 'a cm handle'
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'
180             assert result == [:]
181         and: 'no request is sent to DMI'
182             0 * mockDmiRestClient.postOperationWithJsonData(*_)
183     }
184
185     def 'Retrieving yang resources from DMI with null DMI properties.'() {
186         given: 'a cm handle'
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)
192     }
193
194     def 'Retrieving module references with Json processing exception.'() {
195         given: 'a cm handle'
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')
205     }
206
207 }