Merge "Implement ACK in DMI Plugin"
[cps/ncmp-dmi-plugin.git] / dmi-service / src / test / groovy / org / onap / cps / ncmp / dmi / service / client / SdncRestconfClientSpec.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.dmi.service.client
22
23 import org.onap.cps.ncmp.dmi.config.DmiConfiguration
24 import org.springframework.http.HttpEntity
25 import org.springframework.http.HttpHeaders
26 import org.springframework.http.HttpMethod
27 import org.springframework.http.ResponseEntity
28 import org.springframework.web.client.RestTemplate
29 import spock.lang.Specification
30
31 import static org.springframework.http.HttpMethod.GET
32 import static org.springframework.http.HttpMethod.POST
33 import static org.springframework.http.HttpMethod.DELETE
34 import static org.springframework.http.HttpMethod.PUT
35
36 class SdncRestconfClientSpec extends Specification {
37
38     def mockSdncProperties = Mock(DmiConfiguration.SdncProperties)
39     def mockRestTemplate = Mock(RestTemplate)
40     def objectUnderTest = new SdncRestconfClient(mockSdncProperties, mockRestTemplate)
41
42     def 'SDNC GET operation.'() {
43         given: 'a get resource url'
44             def getResourceUrl = '/getResourceUrl'
45         and: 'test configuration data'
46             setupTestConfigurationData()
47         and: 'the process returns a valid response entity'
48             def mockResponseEntity = Mock(ResponseEntity)
49             mockRestTemplate.exchange({ it.toString() == 'http://some-uri/getResourceUrl' },
50                     HttpMethod.GET, _ as HttpEntity, String.class) >> mockResponseEntity
51         when: 'the resource is fetched'
52             def result = objectUnderTest.getOperation(getResourceUrl)
53         then: 'the output of the method is equal to the output from the rest template service'
54             result == mockResponseEntity
55     }
56
57     def 'SDNC #scenario operation called.'() {
58         given: 'some request data'
59             def someRequestData = 'some request data'
60         and: 'a url for get module resources'
61             def getModuleResourceUrl = '/getModuleResourceUrl'
62         and: 'test configuration data'
63             setupTestConfigurationData()
64         and: 'the process returns a valid response entity'
65             def mockResponseEntity = Mock(ResponseEntity)
66         when: 'the resource is fetched'
67             def result = objectUnderTest.httpOperationWithJsonData(expectedHttpMethod, getModuleResourceUrl, someRequestData, new HttpHeaders())
68         then: 'the rest template is called with the correct uri and json in the body'
69             1 * mockRestTemplate.exchange({ it.toString() == 'http://some-uri/getModuleResourceUrl' },
70                     expectedHttpMethod, { it.body.contains(someRequestData) }, String.class) >> mockResponseEntity
71         and: 'the output of the method is the same as the output from the rest template service'
72             result == mockResponseEntity
73         where: 'the following values are used'
74             scenario || expectedHttpMethod
75             'POST'   || POST
76             'PUT'    || PUT
77             'GET'    || GET
78             'DELETE' || DELETE
79     }
80
81     def 'SDNC GET operation with headers.'() {
82         given: 'a get url'
83             def getResourceUrl = '/getResourceUrl'
84         and: 'test configuration data'
85             setupTestConfigurationData()
86         and: 'the process returns a valid response entity'
87             def mockResponseEntity = Mock(ResponseEntity)
88             mockRestTemplate.exchange({ it.toString() == 'http://some-uri/getResourceUrl' },
89                     HttpMethod.GET, _ as HttpEntity, String.class) >> mockResponseEntity
90         when: 'the resource is fetched with headers'
91             def result = objectUnderTest.getOperation(getResourceUrl, new HttpHeaders())
92         then: 'the output of the method is equal to the output from the rest template service'
93             result == mockResponseEntity
94     }
95
96     def setupTestConfigurationData() {
97         mockSdncProperties.baseUrl >> 'http://some-uri'
98         mockSdncProperties.authUsername >> 'some-username'
99         mockSdncProperties.authPassword >> 'some-password'
100         mockSdncProperties.topologyId >> 'some-topology-id'
101     }
102 }