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 org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
25 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
26 import org.spockframework.spring.SpringBean
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.test.context.SpringBootTest
29 import org.springframework.http.HttpEntity
30 import org.springframework.http.HttpHeaders
31 import org.springframework.http.HttpStatus
32 import org.springframework.http.ResponseEntity
33 import org.springframework.test.context.ContextConfiguration
34 import org.springframework.web.client.HttpServerErrorException
35 import org.springframework.web.client.RestTemplate
36 import spock.lang.Specification
37
38 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
39 import static org.onap.cps.ncmp.api.impl.operations.OperationType.PATCH
40 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
41
42 @SpringBootTest
43 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiRestClient])
44 class DmiRestClientSpec extends Specification {
45
46     @SpringBean
47     RestTemplate mockRestTemplate = Mock(RestTemplate)
48
49     @Autowired
50     DmiRestClient objectUnderTest
51     def resourceUrl = 'some url'
52
53     def mockResponseEntity = Mock(ResponseEntity)
54     def dmiProperties = new NcmpConfiguration.DmiProperties()
55
56     def setup() {
57         dmiProperties.authUsername = 'test user'
58         dmiProperties.authPassword = 'test pass'
59         dmiProperties.dmiBasePath = 'dmi'
60     }
61
62     def 'DMI POST operation with JSON.'() {
63         given: 'the rest template returns a valid response entity'
64             mockRestTemplate.postForEntity(resourceUrl, _ as HttpEntity, Object.class) >> mockResponseEntity
65         when: 'POST operation is invoked'
66             def result = objectUnderTest.postOperationWithJsonData(resourceUrl, 'json-data', READ)
67         then: 'the output of the method is equal to the output from the test template'
68             result == mockResponseEntity
69     }
70
71     def 'Failing DMI POST operation.'() {
72         given: 'the rest template returns a valid response entity'
73             def serverResponse = 'server response'.getBytes()
74             def httpServerErrorException = new HttpServerErrorException(HttpStatus.FORBIDDEN, 'status text', serverResponse, null)
75             mockRestTemplate.postForEntity(*_) >> { throw httpServerErrorException }
76         when: 'POST operation is invoked'
77             def result = objectUnderTest.postOperationWithJsonData('some url', 'some json', operation)
78         then: 'a Http Client Exception is thrown'
79             def thrown = thrown(HttpClientRequestException)
80         and: 'the exception has the relevant details from the error response'
81             assert thrown.httpStatus == 403
82             assert thrown.message == "Unable to ${operation} resource data."
83             assert thrown.details == 'server response'
84         where: 'the following operation is executed'
85             operation << [CREATE, READ, PATCH]
86     }
87
88     def 'Basic auth header #scenario'() {
89         when: 'Specific dmi properties are provided'
90             dmiProperties.dmiBasicAuthEnabled = authEnabled
91             objectUnderTest.dmiProperties = dmiProperties
92         then: 'http headers to conditionally have Authorization header'
93             assert (objectUnderTest.configureHttpHeaders(new HttpHeaders()).get('Authorization') != null) == isPresentInHttpHeader
94         where: 'the following configurations are used'
95             scenario        | authEnabled || isPresentInHttpHeader
96             'auth enabled'  | true        || true
97             'auth disabled' | false       || false
98     }
99
100 }