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
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 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.config.NcmpConfiguration.DmiProperties;
29 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
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
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
48 @ContextConfiguration(classes = [DmiProperties, DmiRestClient, ObjectMapper])
49 class DmiRestClientSpec extends Specification {
52 RestTemplate mockRestTemplate = Mock(RestTemplate)
55 NcmpConfiguration.DmiProperties dmiProperties
58 DmiRestClient objectUnderTest
61 ObjectMapper objectMapper
63 def responseFromRestTemplate = Mock(ResponseEntity)
65 def 'DMI POST operation with JSON.'() {
66 given: 'the rest template returns a valid response entity for the expected parameters'
67 mockRestTemplate.postForEntity('my url', _ as HttpEntity, Object.class) >> responseFromRestTemplate
68 when: 'POST operation is invoked'
69 def result = objectUnderTest.postOperationWithJsonData('my url', 'some json', READ)
70 then: 'the output of the method is equal to the output from the test template'
71 result == responseFromRestTemplate
74 def 'Failing DMI POST operation.'() {
75 given: 'the rest template returns a valid response entity'
76 def serverResponse = 'server response'.getBytes()
77 def httpServerErrorException = new HttpServerErrorException(HttpStatus.FORBIDDEN, 'status text', serverResponse, null)
78 mockRestTemplate.postForEntity(*_) >> { throw httpServerErrorException }
79 when: 'POST operation is invoked'
80 def result = objectUnderTest.postOperationWithJsonData('some url', 'some json', operation)
81 then: 'a Http Client Exception is thrown'
82 def thrown = thrown(HttpClientRequestException)
83 and: 'the exception has the relevant details from the error response'
84 assert thrown.httpStatus == 403
85 assert thrown.message == "Unable to ${operation} resource data."
86 assert thrown.details == 'server response'
87 where: 'the following operation is executed'
88 operation << [CREATE, READ, PATCH]
91 def 'Dmi trust level is determined by spring boot health status'() {
92 given: 'a health check response'
93 def dmiPluginHealthCheckResponseJsonData = TestUtils.getResourceFileContent('dmiPluginHealthCheckResponse.json')
94 def jsonNode = objectMapper.readValue(dmiPluginHealthCheckResponseJsonData, JsonNode.class)
95 ((ObjectNode) jsonNode).put('status', 'my status')
96 mockRestTemplate.getForObject(*_) >> {jsonNode}
97 when: 'get trust level of the dmi plugin'
98 def result = objectUnderTest.getDmiHealthStatus('some url')
99 then: 'the correct trust level is returned'
100 assert result == 'my status'
103 def 'Failing to get dmi plugin health status #scenario'() {
104 given: 'rest template with #scenario'
105 mockRestTemplate.getForObject(*_) >> healthStatusResponse
106 when: 'attempt to get health status of the dmi plugin'
107 def result = objectUnderTest.getDmiHealthStatus('some url')
108 then: 'result will be EMPTY_STRING "" '
110 where: 'the following values are used'
111 scenario | healthStatusResponse
113 'exception' | {throw new Exception()}
116 def 'Basic auth header #scenario'() {
117 when: 'Specific dmi properties are provided'
118 dmiProperties.dmiBasicAuthEnabled = authEnabled
119 then: 'http headers to conditionally have Authorization header'
120 assert (objectUnderTest.configureHttpHeaders(new HttpHeaders()).get('Authorization') != null) == isPresentInHttpHeader
121 where: 'the following configurations are used'
122 scenario | authEnabled || isPresentInHttpHeader
123 'auth enabled' | true || true
124 'auth disabled' | false || false