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