d3fc17cc077f414dcecfb2bbd0769a7b7ac48459
[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  *  ================================================================================
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.operations
22
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.ncmp.api.impl.utils.DmiServiceUrlBuilder
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 org.springframework.web.util.UriComponentsBuilder
36 import spock.lang.Shared
37
38 @SpringBootTest
39 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations])
40 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
41
42     @Shared
43     def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
44
45     @Autowired
46     DmiModelOperations objectUnderTest
47
48     @SpringBean
49     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
50
51     def 'Retrieving module references.'() {
52         given: 'a cm handle'
53             mockYangModelCmHandleRetrieval([])
54         and: 'a positive response from DMI service when it is called with the expected parameters'
55             def moduleReferencesAsLisOfMaps = [[moduleName: 'mod1', revision: 'A'], [moduleName: 'mod2', revision: 'X']]
56             def expectedUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules"
57             def responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK)
58             mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"cmHandleProperties":{}}', [:])
59                     >> responseFromDmi
60         when: 'get module references is called'
61             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
62         then: 'the result consists of expected module references'
63             assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')]
64     }
65
66     def 'Retrieving module references edge case: #scenario.'() {
67         given: 'a cm handle'
68             mockYangModelCmHandleRetrieval([])
69         and: 'any response from DMI service when it is called with the expected parameters'
70             // TODO (toine): production code ignores any error code from DMI, this should be improved in future
71             def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
72             mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
73         when: 'get module references is called'
74             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
75         then: 'the result is empty'
76             assert result == []
77         where: 'the DMI response body has the following content'
78             scenario       | bodyAsMap
79             'no modules'   | [schemas:[]]
80             'modules null' | [schemas:null]
81             'no schema'    | [something:'else']
82             'no body'      | null
83     }
84
85     def 'Retrieving module references, DMI property handling:  #scenario.'() {
86         given: 'a cm handle'
87             mockYangModelCmHandleRetrieval(dmiProperties)
88         and: 'a positive response from DMI service when it is called with tha expected parameters'
89             def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
90             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
91                 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', [:]) >> responseFromDmi
92         when: 'a get module references is called'
93             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
94         then: 'the result is the response from DMI service'
95             assert result == []
96         where: 'the following DMI properties are used'
97             scenario               | dmiProperties       || expectedAdditionalPropertiesInRequest
98             'with properties'      | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
99             'without properties'   | []                  || '{}'
100     }
101
102     def 'Retrieving yang resources.'() {
103         given: 'a cm handle'
104             mockYangModelCmHandleRetrieval([])
105         and: 'a positive response from DMI service when it is called with the expected parameters'
106             def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
107                                                       [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
108             def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
109             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
110                 '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', [:]) >> responseFromDmi
111         when: 'get new yang resources from DMI service'
112             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
113         then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
114             assert result.size() == 2
115             assert result.get('mod1') == 'some yang source'
116             assert result.get('mod2') == 'other yang source'
117     }
118
119     def 'Retrieving yang resources, edge case: scenario.'() {
120         given: 'a cm handle'
121             mockYangModelCmHandleRetrieval([])
122         and: 'a positive response from DMI service when it is called with tha expected parameters'
123             // TODO (toine): production code ignores any error code from DMI, this should be improved in future
124             def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
125             mockDmiRestClient.postOperationWithJsonData(*_) >> responseFromDmi
126         when: 'get new yang resources from DMI service'
127             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
128         then: 'the result is empty'
129             assert result == [:]
130         where: 'the DMI response body has the following content'
131             scenario      | responseFromDmiBody
132             'empty array' | []
133             'null array'  | null
134     }
135
136     def 'Retrieving yang resources, DMI property handling #scenario.'() {
137         given: 'a cm handle'
138             mockYangModelCmHandleRetrieval(dmiProperties)
139         and: 'a positive response from DMI service when it is called with the expected parameters'
140             def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
141             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
142             '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}',
143             [:]) >> 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 }