Merge "Migrate CPS to Spring-boot 3.0"
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / impl / client / DmiRestClientSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2023 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.client
23
24 import com.fasterxml.jackson.databind.JsonNode
25 import com.fasterxml.jackson.databind.ObjectMapper
26 import com.fasterxml.jackson.databind.node.ObjectNode
27 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
28 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
29 import org.onap.cps.ncmp.api.impl.trustlevel.dmiavailability.DmiPluginStatus
30 import org.onap.cps.ncmp.utils.TestUtils
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.HttpEntity
35 import org.springframework.http.HttpHeaders
36 import org.springframework.http.HttpStatus
37 import org.springframework.http.ResponseEntity
38 import org.springframework.test.context.ContextConfiguration
39 import org.springframework.web.client.HttpServerErrorException
40 import org.springframework.web.client.RestTemplate
41 import spock.lang.Specification
42
43 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
44 import static org.onap.cps.ncmp.api.impl.operations.OperationType.PATCH
45 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
46
47 @SpringBootTest
48 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiRestClient, ObjectMapper])
49 class DmiRestClientSpec extends Specification {
50
51     @SpringBean
52     RestTemplate mockRestTemplate = Mock(RestTemplate)
53
54     @Autowired
55     DmiRestClient objectUnderTest
56
57     @Autowired
58     ObjectMapper objectMapper
59
60     def resourceUrl = 'some url'
61     def mockResponseEntity = Mock(ResponseEntity)
62     def dmiProperties = new NcmpConfiguration.DmiProperties()
63
64     def setup() {
65         dmiProperties.authUsername = 'test user'
66         dmiProperties.authPassword = 'test pass'
67         dmiProperties.dmiBasePath = 'dmi'
68     }
69
70     def 'DMI POST operation with JSON.'() {
71         given: 'the rest template returns a valid response entity'
72             mockRestTemplate.postForEntity(resourceUrl, _ as HttpEntity, Object.class) >> mockResponseEntity
73         when: 'POST operation is invoked'
74             def result = objectUnderTest.postOperationWithJsonData(resourceUrl, 'json-data', READ)
75         then: 'the output of the method is equal to the output from the test template'
76             result == mockResponseEntity
77     }
78
79     def 'Failing DMI POST operation.'() {
80         given: 'the rest template returns a valid response entity'
81             def serverResponse = 'server response'.getBytes()
82             def httpServerErrorException = new HttpServerErrorException(HttpStatus.FORBIDDEN, 'status text', serverResponse, null)
83             mockRestTemplate.postForEntity(*_) >> { throw httpServerErrorException }
84         when: 'POST operation is invoked'
85             def result = objectUnderTest.postOperationWithJsonData('some url', 'some json', operation)
86         then: 'a Http Client Exception is thrown'
87             def thrown = thrown(HttpClientRequestException)
88         and: 'the exception has the relevant details from the error response'
89             assert thrown.httpStatus == 403
90             assert thrown.message == "Unable to ${operation} resource data."
91             assert thrown.details == 'server response'
92         where: 'the following operation is executed'
93             operation << [CREATE, READ, PATCH]
94     }
95
96     def 'Get dmi plugin health status #scenario'() {
97         given: 'a health check response data as jsonNode'
98             def dmiPluginHealthCheckResponseJsonData = TestUtils.getResourceFileContent('dmiPluginHealthCheckResponse.json')
99             def jsonNode = objectMapper.readValue(dmiPluginHealthCheckResponseJsonData, JsonNode.class)
100             ((ObjectNode) jsonNode).put('status', dmiAliveness);
101         and: 'the rest template return a valid json node'
102             mockRestTemplate.getForObject(*_) >> {jsonNode}
103         when: 'get aliveness of the dmi plugin'
104             def result = objectUnderTest.getDmiPluginStatus(resourceUrl)
105         then: 'return value is equal to result of rest template call'
106             result == expectedResult
107         where: 'the following dmi aliveness are being used'
108             scenario             | dmiAliveness || expectedResult
109             'dmi plugin is UP'   | 'UP'         || DmiPluginStatus.UP
110             'dmi plugin is DOWN' | 'DOWN'       || DmiPluginStatus.DOWN
111     }
112
113     def 'Failing to get dmi plugin health status #scenario'() {
114         given: 'the rest template return null'
115             mockRestTemplate.getForObject(*_) >> {getResponse}
116         when: 'get aliveness of the dmi plugin'
117             def result = objectUnderTest.getDmiPluginStatus(resourceUrl)
118         then: 'return value is equal to result of rest template call'
119             result == expectedResult
120         where: 'the following dmi responses are being used'
121             scenario                        | getResponse                  || expectedResult
122             'get response is null'          | null                         || DmiPluginStatus.DOWN
123             'get response throws exception' | {throw new Exception()}      || DmiPluginStatus.DOWN
124     }
125
126     def 'Basic auth header #scenario'() {
127         when: 'Specific dmi properties are provided'
128             dmiProperties.dmiBasicAuthEnabled = authEnabled
129             objectUnderTest.dmiProperties = dmiProperties
130         then: 'http headers to conditionally have Authorization header'
131             assert (objectUnderTest.configureHttpHeaders(new HttpHeaders()).get('Authorization') != null) == isPresentInHttpHeader
132         where: 'the following configurations are used'
133             scenario        | authEnabled || isPresentInHttpHeader
134             'auth enabled'  | true        || true
135             'auth disabled' | false       || false
136     }
137
138 }