d9d12711fb7a19f14f0f8f6d501ff82672e5bb16
[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 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 org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
25 import org.onap.cps.ncmp.api.impl.exception.NcmpException
26 import org.onap.cps.spi.model.ModuleReference
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.test.context.SpringBootTest
29 import org.springframework.http.HttpStatus
30 import org.springframework.http.ResponseEntity
31 import org.springframework.test.context.ContextConfiguration
32 import spock.lang.Shared
33
34 @SpringBootTest
35 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiModelOperations])
36 class DmiModelOperationsSpec extends DmiOperationsBaseSpec {
37
38     @Shared
39     def newModuleReferences = [new ModuleReference('mod1','A'), new ModuleReference('mod2','X')]
40
41     @Autowired
42     DmiModelOperations objectUnderTest
43
44     def 'Module references for a persistence cm handle #scenario.'() {
45         given: 'a persistence cm handle for #cmHandleId'
46             mockPersistenceCmHandleRetrieval(additionalPropertiesObject)
47         and: 'a positive response from dmi service when it is called with tha expected parameters'
48             def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
49             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/modules",
50                 '{"cmHandleProperties":' + expectedAdditionalPropertiesInRequest + '}', [:]) >> responseFromDmi
51         when: 'a get module references is called'
52             def result = objectUnderTest.getModuleReferences(persistenceCmHandle)
53         then: 'the result is the response from dmi service'
54             assert result == responseFromDmi
55         where:
56             scenario               | additionalPropertiesObject || expectedAdditionalPropertiesInRequest
57             'with properties'      | [sampleAdditionalProperty] || '{"prop1":"val1"}'
58             'with null properties' | null                       || "{}"
59             'without properties'   | []                         || "{}"
60     }
61
62     def 'New yang resources from dmi using persistence cm handle #scenario.'() {
63         given: 'a persistence cm handle for #cmHandleId'
64             mockPersistenceCmHandleRetrieval(additionalPropertiesObject)
65         and: 'a positive response from dmi service when it is called with tha expected parameters'
66             def responseFromDmi = new ResponseEntity<String>(HttpStatus.OK)
67             mockDmiRestClient.postOperationWithJsonData("${dmiServiceName}/dmi/v1/ch/${cmHandleId}/moduleResources",
68             '{"data":{"modules":[' + expectedModuleReferencesInRequest + ']},"cmHandleProperties":'+expectedAdditionalPropertiesInRequest+'}',
69             [:]) >> responseFromDmi
70         when: 'get new yang resources from dmi service'
71             def result = objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, unknownModuleReferences)
72         then: 'the result is the response from dmi service'
73             assert result == responseFromDmi
74         where:
75             scenario                                | additionalPropertiesObject | unknownModuleReferences || expectedAdditionalPropertiesInRequest | expectedModuleReferencesInRequest
76             'with module references and properties' | [sampleAdditionalProperty] | newModuleReferences     || '{"prop1":"val1"}'                    | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
77             'without module references'             | [sampleAdditionalProperty] | []                      || '{"prop1":"val1"}'                    | ''
78             'without properties'                    | []                         | newModuleReferences     || '{}'                                  | '{"name":"mod1","revision":"A"},{"name":"mod2","revision":"X"}'
79     }
80
81     def 'New yang resources from dmi with additional properties null'() {
82         given: 'a persistence cm handle for #cmHandleId'
83             mockPersistenceCmHandleRetrieval(null)
84         when: 'a get new yang resources from dmi is called'
85             objectUnderTest.getNewYangResourcesFromDmi(persistenceCmHandle, [])
86         then: 'a null pointer is thrown (we might need to address this later)'
87             thrown(NullPointerException)
88     }
89
90     def 'Json Processing Exception'() {
91         given: 'a persistence cm handle for #cmHandleId'
92             mockPersistenceCmHandleRetrieval([])
93         and: 'a Json processing exception occurs'
94             spyObjectMapper.writeValueAsString(_) >> {throw (new JsonProcessingException(''))}
95         when: 'a dmi operation is executed'
96             objectUnderTest.getModuleReferences(persistenceCmHandle)
97         then: 'an ncmp exception is thrown'
98             def exceptionThrown = thrown(NcmpException)
99         and: 'the message indicates a parsing error'
100             exceptionThrown.message.toLowerCase().contains("parsing error")
101     }
102
103 }