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