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