Add bearer token to NCMP passthrough operations (CPS-2126 #2)
[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.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
38
39 import static  org.onap.cps.ncmp.api.impl.operations.OperationType.READ
40
41 @SpringBootTest
42 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations])
43 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
44
45     @Shared
46     def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
47
48     @Autowired
49     DmiModelOperations objectUnderTest
50
51     @SpringBean
52     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
53
54     def NO_AUTH_HEADER = null
55
56     def 'Retrieving module references.'() {
57         given: 'a cm handle'
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)
64                     >> responseFromDmi
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')]
69     }
70
71     def 'Retrieving module references edge case: #scenario.'() {
72         given: 'a cm handle'
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'
81             assert result == []
82         where: 'the DMI response body has the following content'
83             scenario       | bodyAsMap
84             'no modules'   | [schemas:[]]
85             'modules null' | [schemas:null]
86             'no schema'    | [something:'else']
87             'no body'      | null
88     }
89
90     def 'Retrieving module references, DMI property handling:  #scenario.'() {
91         given: 'a cm handle'
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'
100             assert result == []
101         where: 'the following DMI properties are used'
102             scenario             | dmiProperties               || expectedAdditionalPropertiesInRequest
103             'with properties'    | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
104             'without properties' | []                          || '{}'
105     }
106
107     def 'Retrieving yang resources.'() {
108         given: 'a cm handle'
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'
122     }
123
124     def 'Retrieving yang resources, edge case: scenario.'() {
125         given: 'a cm handle'
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'
134             assert result == [:]
135         where: 'the DMI response body has the following content'
136             scenario      | responseFromDmiBody
137             'empty array' | []
138             'null array'  | null
139     }
140
141     def 'Retrieving yang resources, DMI property handling #scenario.'() {
142         given: 'a cm handle'
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'                    | []                          || '{}'
157     }
158
159     def 'Retrieving yang resources from DMI with no module references.'() {
160         given: 'a cm handle'
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'
165             assert result == [:]
166         and: 'no request is sent to DMI'
167             0 * mockDmiRestClient.postOperationWithJsonData(*_)
168     }
169
170     def 'Retrieving yang resources from DMI with null DMI properties.'() {
171         given: 'a cm handle'
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)
177     }
178
179     def 'Retrieving module references with Json processing exception.'() {
180         given: 'a cm handle'
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')
190     }
191
192 }