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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.client
24 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
25 import static org.onap.cps.ncmp.api.impl.operations.OperationType.PATCH
26 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
27 import static org.springframework.http.HttpStatus.SERVICE_UNAVAILABLE
28 import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
29 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA
31 import com.fasterxml.jackson.databind.JsonNode
32 import com.fasterxml.jackson.databind.ObjectMapper
33 import com.fasterxml.jackson.databind.node.ObjectNode
34 import org.onap.cps.ncmp.api.impl.config.DmiWebClientConfiguration
35 import org.onap.cps.ncmp.api.impl.exception.InvalidDmiResourceUrlException
36 import org.onap.cps.ncmp.api.impl.exception.DmiClientRequestException
37 import org.onap.cps.ncmp.utils.TestUtils
38 import org.onap.cps.utils.JsonObjectMapper
39 import org.spockframework.spring.SpringBean
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.boot.test.context.SpringBootTest
42 import org.springframework.http.HttpHeaders
43 import org.springframework.http.ResponseEntity
44 import org.springframework.test.context.ContextConfiguration
45 import org.springframework.web.client.HttpServerErrorException
46 import org.springframework.web.reactive.function.client.WebClient
47 import reactor.core.publisher.Mono
48 import spock.lang.Specification
49 import org.springframework.web.reactive.function.client.WebClientResponseException
50 import org.onap.cps.ncmp.api.impl.config.DmiProperties
53 @ContextConfiguration(classes = [DmiProperties, DmiRestClient, ObjectMapper])
54 class DmiRestClientSpec extends Specification {
56 static final NO_AUTH_HEADER = null
57 static final BASIC_AUTH_HEADER = 'Basic c29tZS11c2VyOnNvbWUtcGFzc3dvcmQ='
58 static final BEARER_AUTH_HEADER = 'Bearer my-bearer-token'
61 DmiProperties dmiProperties
64 DmiRestClient objectUnderTest
67 WebClient mockWebClient = Mock(WebClient);
70 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
72 def mockRequestBodyUriSpec = Mock(WebClient.RequestBodyUriSpec)
73 def mockResponseSpec = Mock(WebClient.ResponseSpec)
74 def mockResponseEntity = Mock(ResponseEntity)
77 mockRequestBodyUriSpec.uri(_) >> mockRequestBodyUriSpec
78 mockRequestBodyUriSpec.headers(_) >> mockRequestBodyUriSpec
79 mockRequestBodyUriSpec.retrieve() >> mockResponseSpec
82 def 'DMI POST operation with JSON.'() {
83 given: 'the web client returns a valid response entity for the expected parameters'
84 mockWebClient.post() >> mockRequestBodyUriSpec
85 mockRequestBodyUriSpec.body(_) >> mockRequestBodyUriSpec
86 def monoSpec = Mono.just(mockResponseEntity)
87 mockResponseSpec.bodyToMono(Object.class) >> monoSpec
88 monoSpec.block() >> mockResponseEntity
89 when: 'POST operation is invoked'
90 def response = objectUnderTest.postOperationWithJsonData('/my/url', 'some json', READ, null)
91 then: 'the output of the method is equal to the output from the test template'
92 assert response.statusCode.value() == 200
93 assert response.hasBody()
96 def 'Failing DMI POST operation for server error'() {
97 given: 'the web client throws an exception'
98 mockWebClient.post() >> { throw new HttpServerErrorException(SERVICE_UNAVAILABLE, null, null, null) }
99 when: 'POST operation is invoked'
100 objectUnderTest.postOperationWithJsonData('/some', 'some json', READ, null)
101 then: 'a http client exception is thrown'
102 def thrown = thrown(DmiClientRequestException)
103 and: 'the exception has the relevant details from the error response'
104 assert thrown.ncmpResponseStatus.code == '102'
105 assert thrown.httpStatusCode == 503
108 def 'Failing DMI POST operation due to invalid dmi resource url.'() {
109 when: 'POST operation is invoked with invalid dmi resource url'
110 objectUnderTest.postOperationWithJsonData('/invalid dmi url', null, null, null)
111 then: 'invalid dmi resource url exception is thrown'
112 def thrown = thrown(InvalidDmiResourceUrlException)
113 and: 'the exception has the relevant details from the error response'
114 assert thrown.httpStatus == 400
115 assert thrown.message == 'Invalid dmi resource url: /invalid dmi url'
116 where: 'the following operations are executed'
117 operation << [CREATE, READ, PATCH]
120 def 'Dmi service sends client error response when #scenario'() {
121 given: 'the web client unable to return response entity but error'
122 mockWebClient.post() >> mockRequestBodyUriSpec
123 mockRequestBodyUriSpec.body(_) >> mockRequestBodyUriSpec
124 def monoSpec = Mono.error(new WebClientResponseException('message', httpStatusCode, null, null, null, null))
125 mockResponseSpec.bodyToMono(Object.class) >> monoSpec
126 when: 'POST operation is invoked'
127 objectUnderTest.postOperationWithJsonData('/my/url', 'some json', READ, null)
128 then: 'a http client exception is thrown'
129 def thrown = thrown(DmiClientRequestException)
130 and: 'the exception has the relevant details from the error response'
131 assert thrown.ncmpResponseStatus.code == expectedNcmpResponseStatusCode
132 assert thrown.httpStatusCode == httpStatusCode
133 where: 'the following errors occur'
134 scenario | httpStatusCode | expectedNcmpResponseStatusCode
135 'dmi request timeout' | 408 | DMI_SERVICE_NOT_RESPONDING.code
136 'other error code' | 500 | UNABLE_TO_READ_RESOURCE_DATA.code
139 def 'Dmi trust level is determined by spring boot health status'() {
140 given: 'a health check response'
141 def dmiPluginHealthCheckResponseJsonData = TestUtils.getResourceFileContent('dmiPluginHealthCheckResponse.json')
142 def jsonNode = jsonObjectMapper.convertJsonString(dmiPluginHealthCheckResponseJsonData, JsonNode.class)
143 ((ObjectNode) jsonNode).put('status', 'my status')
144 def monoResponse = Mono.just(jsonNode)
145 mockWebClient.get() >> mockRequestBodyUriSpec
146 mockResponseSpec.bodyToMono(_) >> monoResponse
147 monoResponse.block() >> jsonNode
148 when: 'get trust level of the dmi plugin'
149 def result = objectUnderTest.getDmiHealthStatus('some/url')
150 then: 'the status value from the json is return'
151 assert result == 'my status'
154 def 'Failing to get dmi plugin health status #scenario'() {
155 given: 'rest template with #scenario'
156 mockWebClient.get() >> healthStatusResponse
157 when: 'attempt to get health status of the dmi plugin'
158 def result = objectUnderTest.getDmiHealthStatus('some url')
159 then: 'result will be empty'
161 where: 'the following responses are used'
162 scenario | healthStatusResponse
164 'exception' | {throw new Exception()}
167 def 'DMI auth header #scenario'() {
168 when: 'Specific dmi properties are provided'
169 dmiProperties.dmiBasicAuthEnabled = authEnabled
170 then: 'http headers to conditionally have Authorization header'
171 def authHeaderValues = objectUnderTest.configureHttpHeaders(new HttpHeaders(), ncmpAuthHeader).getOrEmpty('Authorization')
172 def outputAuthHeader = (authHeaderValues == null ? null : authHeaderValues[0])
173 assert outputAuthHeader == expectedAuthHeader
174 where: 'the following configurations are used'
175 scenario | authEnabled | ncmpAuthHeader || expectedAuthHeader
176 'DMI basic auth enabled, no NCMP bearer token' | true | NO_AUTH_HEADER || BASIC_AUTH_HEADER
177 'DMI basic auth enabled, with NCMP bearer token' | true | BEARER_AUTH_HEADER || BASIC_AUTH_HEADER
178 'DMI basic auth disabled, no NCMP bearer token' | false | NO_AUTH_HEADER || NO_AUTH_HEADER
179 'DMI basic auth disabled, with NCMP bearer token' | false | BEARER_AUTH_HEADER || BEARER_AUTH_HEADER
180 'DMI basic auth disabled, with NCMP basic auth' | false | BASIC_AUTH_HEADER || NO_AUTH_HEADER