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