196a1cd360a0d61e385cd8a231ad04beb2d78ac3
[cps.git] /
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.impl.inventory.sync
23
24 import com.fasterxml.jackson.core.JsonProcessingException
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import org.onap.cps.ncmp.impl.dmi.DmiOperationsBaseSpec
27 import org.onap.cps.ncmp.impl.dmi.DmiProperties
28 import org.onap.cps.ncmp.impl.dmi.UrlTemplateParameters
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.data.models.OperationType.READ
40 import static org.onap.cps.ncmp.impl.models.RequiredDmiService.MODEL
41
42 @SpringBootTest
43 @ContextConfiguration(classes = [DmiProperties, DmiModelOperations])
44 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
45
46     def expectedModulesUrlTemplateWithVariables = new UrlTemplateParameters('myServiceName/dmi/v1/ch/{cmHandleId}/modules', ['cmHandleId': cmHandleId])
47     def expectedModuleResourcesUrlTemplateWithVariables = new UrlTemplateParameters('myServiceName/dmi/v1/ch/{cmHandleId}/moduleResources', ['cmHandleId': cmHandleId])
48
49     @Shared
50     def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
51
52     @Autowired
53     DmiModelOperations objectUnderTest
54
55     @SpringBean
56     JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
57
58     def NO_AUTH_HEADER = null
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 responseFromDmi = new ResponseEntity([schemas: moduleReferencesAsLisOfMaps], HttpStatus.OK)
66             mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModulesUrlTemplateWithVariables, '{"cmHandleProperties":{},"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
67         when: 'get module references is called'
68             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
69         then: 'the result consists of expected module references'
70             assert result == [new ModuleReference(moduleName: 'mod1', revision: 'A'), new ModuleReference(moduleName: 'mod2', revision: 'X')]
71     }
72
73     def 'Retrieving module references edge case: #scenario.'() {
74         given: 'a cm handle'
75             mockYangModelCmHandleRetrieval([])
76         and: 'any response from DMI service when it is called with the expected parameters'
77             // TODO (toine): production code ignores any error code from DMI, this should be improved in future
78             def responseFromDmi = new ResponseEntity(bodyAsMap, HttpStatus.NO_CONTENT)
79             mockDmiRestClient.synchronousPostOperationWithJsonData(*_) >> responseFromDmi
80         when: 'get module references is called'
81             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
82         then: 'the result is empty'
83             assert result == []
84         where: 'the DMI response body has the following content'
85             scenario       | bodyAsMap
86             'no modules'   | [schemas:[]]
87             'modules null' | [schemas:null]
88             'no schema'    | [something:'else']
89             'no body'      | null
90     }
91
92     def 'Retrieving module references, DMI property handling:  #scenario.'() {
93         given: 'a cm handle'
94             mockYangModelCmHandleRetrieval(dmiProperties)
95         and: 'a positive response from DMI service when it is called with tha expected parameters'
96             def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
97             mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModulesUrlTemplateWithVariables,
98                     '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + ',"moduleSetTag":""}', READ, NO_AUTH_HEADER) >> responseFromDmi
99         when: 'a get module references is called'
100             def result = objectUnderTest.getModuleReferences(yangModelCmHandle)
101         then: 'the result is the response from DMI service'
102             assert result == []
103         where: 'the following DMI properties are used'
104             scenario             | dmiProperties               || expectedAdditionalPropertiesInRequest
105             'with properties'    | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
106             'without properties' | []                          || '{}'
107     }
108
109     def 'Retrieving yang resources.'() {
110         given: 'a cm handle'
111             mockYangModelCmHandleRetrieval([])
112         and: 'a positive response from DMI service when it is called with the expected parameters'
113             def responseFromDmi = new ResponseEntity([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source'],
114                                                       [moduleName: 'mod2', revision: 'C', yangSource: 'other yang source']], HttpStatus.OK)
115             def expectedModuleReferencesInRequest = '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
116             mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModuleResourcesUrlTemplateWithVariables,
117                     '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":{}}', READ, NO_AUTH_HEADER) >> responseFromDmi
118         when: 'get new yang resources from DMI service'
119             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
120         then: 'the result has the 2 expected yang (re)sources (order is not guaranteed)'
121             assert result.size() == 2
122             assert result.get('mod1') == 'some yang source'
123             assert result.get('mod2') == 'other yang source'
124     }
125
126     def 'Retrieving yang resources, edge case: scenario.'() {
127         given: 'a cm handle'
128             mockYangModelCmHandleRetrieval([])
129         and: 'a positive response from DMI service when it is called with tha expected parameters'
130             // TODO (toine): production code ignores any error code from DMI, this should be improved in future
131             def responseFromDmi = new ResponseEntity(responseFromDmiBody, HttpStatus.NO_CONTENT)
132             mockDmiRestClient.synchronousPostOperationWithJsonData(*_) >> responseFromDmi
133         when: 'get new yang resources from DMI service'
134             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
135         then: 'the result is empty'
136             assert result == [:]
137         where: 'the DMI response body has the following content'
138             scenario      | responseFromDmiBody
139             'empty array' | []
140             'null array'  | null
141     }
142
143     def 'Retrieving yang resources, DMI property handling #scenario.'() {
144         given: 'a cm handle'
145             mockYangModelCmHandleRetrieval(dmiProperties)
146         and: 'a positive response from DMI service when it is called with the expected moduleSetTag, modules and properties'
147             def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
148             mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModuleResourcesUrlTemplateWithVariables,
149                     '{"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}',
150                     READ, NO_AUTH_HEADER) >> responseFromDmi
151         when: 'get new yang resources from DMI service'
152             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
153         then: 'the result is the response from DMI service'
154             assert result == [mod1:'some yang source']
155         where: 'the following DMI properties are used'
156             scenario                                | dmiProperties               || expectedAdditionalPropertiesInRequest
157             'with module references and properties' | [yangModelCmHandleProperty] || '{"prop1":"val1"}'
158             'without properties'                    | []                          || '{}'
159     }
160
161     def 'Retrieving yang resources  #scenario'() {
162         given: 'a cm handle'
163             mockYangModelCmHandleRetrieval([], moduleSetTag)
164         and: 'a positive response from DMI service when it is called with the expected moduleSetTag'
165             def responseFromDmi = new ResponseEntity<>([[moduleName: 'mod1', revision: 'A', yangSource: 'some yang source']], HttpStatus.OK)
166             mockDmiRestClient.synchronousPostOperationWithJsonData(MODEL, expectedModuleResourcesUrlTemplateWithVariables,
167                 '{' + expectedModuleSetTagInRequest + '"data":{"modules":[{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}]},"cmHandleProperties":{}}', READ, NO_AUTH_HEADER) >> responseFromDmi
168         when: 'get new yang resources from DMI service'
169             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, newModuleReferences)
170         then: 'the result is the response from DMI service'
171             assert result == [mod1:'some yang source']
172         where: 'the following Module Set Tags are used'
173             scenario                               | moduleSetTag       || expectedModuleSetTagInRequest
174             'Without module set tag'               | ''                 || ''
175             'With module set tag'                  | 'moduleSetTag1'    || '"moduleSetTag":"moduleSetTag1",'
176             'Special characters in module set tag' | 'module:set#tag$2' || '"moduleSetTag":"module:set#tag$2",'
177     }
178
179     def 'Retrieving yang resources from DMI with no module references.'() {
180         given: 'a cm handle'
181             mockYangModelCmHandleRetrieval([])
182         when: 'a get new yang resources from DMI is called with no module references'
183             def result = objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [])
184         then: 'no resources are returned'
185             assert result == [:]
186         and: 'no request is sent to DMI'
187             0 * mockDmiRestClient.synchronousPostOperationWithJsonData(*_)
188     }
189
190     def 'Retrieving yang resources from DMI with null DMI properties.'() {
191         given: 'a cm handle'
192             mockYangModelCmHandleRetrieval(null)
193         when: 'a get new yang resources from DMI is called'
194             objectUnderTest.getNewYangResourcesFromDmi(yangModelCmHandle, [new ModuleReference('mod1', 'A')])
195         then: 'a null pointer is thrown (we might need to address this later)'
196             thrown(NullPointerException)
197     }
198
199     def 'Retrieving module references with Json processing exception.'() {
200         given: 'a cm handle'
201             mockYangModelCmHandleRetrieval([])
202         and: 'a Json processing exception occurs'
203             spiedJsonObjectMapper.asJsonString(_) >> {throw (new JsonProcessingException('parsing error'))}
204         when: 'a DMI operation is executed'
205             objectUnderTest.getModuleReferences(yangModelCmHandle)
206         then: 'an ncmp exception is thrown'
207             def exceptionThrown = thrown(JsonProcessingException)
208         and: 'the message indicates a parsing error'
209             exceptionThrown.message.toLowerCase().contains('parsing error')
210     }
211 }