[cps] Fix getResourceDataForPassthroughOperational endpoint
[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-2022 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.utils.DmiServiceUrlBuilder
28 import org.onap.cps.spi.model.ModuleReference
29 import org.onap.cps.utils.JsonObjectMapper
30 import org.spockframework.spring.SpringBean
31 import org.springframework.beans.factory.annotation.Autowired
32 import org.springframework.boot.test.context.SpringBootTest
33 import org.springframework.http.HttpStatus
34 import org.springframework.http.ResponseEntity
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.web.util.UriComponentsBuilder
37 import spock.lang.Shared
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 'Retrieving module references.'() {
53         given: 'a cm handle'
54             mockYangModelCmHandleRetrieval([])
55         and: 'a positive response from DMI service when it is called with the expected parameters'
56             def moduleReferencesAsLisOfMaps = [[moduleName: 'mod1', revision: 'A'], [moduleName: 'mod2', revision: 'X']]
57             def expectedUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules"
58             def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK)
59             mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"cmHandleProperties":{}}')
60                     >> responseFromDmi
61         when: 'get module references is called'
62             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
63         then: 'the result consists of expected module references'
64             assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')]
65     }
66
67     def 'Retrieving module references edge case: #scenario.'() {
68         given: 'a cm handle'
69             mockYangModelCmHandleRetrieval([])
70         and: 'any response from DMI service when it is called with the expected parameters'
71             // TODO (toine): production code ignores any error code from DMI, this should be improved in future
72             def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
73             mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
74         when: 'get module references is called'
75             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
76         then: 'the result is empty'
77             assert result == []
78         where: 'the DMI response body has the following content'
79             scenario       | bodyAsMap
80             'no modules'   | [schemas:[]]
81             'modules null' | [schemas:null]
82             'no schema'    | [something:'else']
83             'no body'      | null
84     }
85
86     def 'Retrieving module references, DMI property handling:  #scenario.'() {
87         given: 'a cm handle'
88             mockYangModelCmHandleRetrieval(dmiProperties)
89         and: 'a positive response from DMI service when it is called with tha expected parameters'
90             def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
91             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
92                 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}') >> responseFromDmi
93         when: 'a get module references is called'
94             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
95         then: 'the result is the response from DMI service'
96             assert result == []
97         where: 'the following DMI properties are used'
98             scenario               | dmiProperties       || expectedAdditionalPropertiesInRequest
99             'with properties'      | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
100             'without properties'   | []                  || '{}'
101     }
102
103     def 'Retrieving yang resources.'() {
104         given: 'a cm handle'
105             mockYangModelCmHandleRetrieval([])
106         and: 'a positive response from DMI service when it is called with the expected parameters'
107             def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
108                                                       [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
109             def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
110             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
111                 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}') >> responseFromDmi
112         when: 'get new yang resources from DMI service'
113             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
114         then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
115             assert result.size() == 2
116             assert result.get('mod1') == 'some yang source'
117             assert result.get('mod2') == 'other yang source'
118     }
119
120     def 'Retrieving yang resources, edge case: scenario.'() {
121         given: 'a cm handle'
122             mockYangModelCmHandleRetrieval([])
123         and: 'a positive response from DMI service when it is called with tha expected parameters'
124             // TODO (toine): production code ignores any error code from DMI, this should be improved in future
125             def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
126             mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
127         when: 'get new yang resources from DMI service'
128             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
129         then: 'the result is empty'
130             assert result == [:]
131         where: 'the DMI response body has the following content'
132             scenario      | responseFromDmiBody
133             'empty array' | []
134             'null array'  | null
135     }
136
137     def 'Retrieving yang resources, DMI property handling #scenario.'() {
138         given: 'a cm handle'
139             mockYangModelCmHandleRetrieval(dmiProperties)
140         and: 'a positive response from DMI service when it is called with the expected parameters'
141             def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
142             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
143             '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}') >> responseFromDmi
144         when: 'get new yang resources from DMI service'
145             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, unknownModuleReferences)
146         then: 'the result is the response from DMI service'
147             assert result == [mod1:'some yang source']
148         where: 'the following DMI properties are used'
149             scenario                                | dmiProperties       | unknownModuleReferences || expectedAdditionalPropertiesInRequest | expectedModuleReferencesInRequest
150             'with module references and properties' | [yangModelCmHandleProperty] | newModuleReferences || '{"prop1":"val1"}' | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
151             'without module references'             | [yangModelCmHandleProperty] | []                  || '{"prop1":"val1"}' | ''
152             'without properties'                    | []                  | newModuleReferences     || '{}'                                  | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
153     }
154
155     def 'Retrieving yang resources from DMI with null DMI properties.'() {
156         given: 'a cm handle'
157             mockYangModelCmHandleRetrieval(null)
158         when: 'a get new yang resources from DMI is called'
159             objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [])
160         then: 'a null pointer is thrown (we might need to address this later)'
161             thrown(NullPointerException)
162     }
163
164     def 'Retrieving module references with Json processing exception.'() {
165         given: 'a cm handle'
166             mockYangModelCmHandleRetrieval([])
167         and: 'a Json processing exception occurs'
168             spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))}
169         when: 'a DMI operation is executed'
170             objectUnderTest.getModuleReferences(yangModelCmHandle)
171         then: 'an ncmp exception is thrown'
172             def exceptionThrown = thrown(JsonProcessingException)
173         and: 'the message indicates a parsing error'
174             exceptionThrown.message.toLowerCase().contains('parsing error')
175     }
176
177 }