2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 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 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.HttpStatus
31 import org.springframework.http.ResponseEntity
32 import org.springframework.test.context.ContextConfiguration
33 import org.springframework.web.client.HttpServerErrorException
34 import org.springframework.web.client.RestTemplate
35 import spock.lang.Specification
37 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.READ
38 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.PATCH
39 import static org.onap.cps.ncmp.api.impl.operations.DmiRequestBody.OperationEnum.CREATE
43 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiRestClient])
44 class DmiRestClientSpec extends Specification {
47 RestTemplate mockRestTemplate = Mock(RestTemplate)
50 DmiRestClient objectUnderTest
51 def resourceUrl = 'some url'
53 def mockResponseEntity = Mock(ResponseEntity)
55 def 'DMI POST operation with JSON.'() {
56 given: 'the rest template returns a valid response entity'
57 mockRestTemplate.postForEntity(resourceUrl, _ as HttpEntity, Object.class) >> mockResponseEntity
58 when: 'POST operation is invoked'
59 def result = objectUnderTest.postOperationWithJsonData(resourceUrl, 'json-data', READ)
60 then: 'the output of the method is equal to the output from the test template'
61 result == mockResponseEntity
64 def 'Failing DMI POST operation.'() {
65 given: 'the rest template returns a valid response entity'
66 def serverResponse = 'server response'.getBytes()
67 def httpServerErrorException = new HttpServerErrorException(HttpStatus.FORBIDDEN, 'status text', serverResponse, null)
68 mockRestTemplate.postForEntity(*_) >> { throw httpServerErrorException }
69 when: 'POST operation is invoked'
70 def result = objectUnderTest.postOperationWithJsonData('some url', 'some json', operation)
71 then: 'a Http Client Exception is thrown'
72 def thrown = thrown(HttpClientRequestException)
73 and: 'the exception has the relevant details from the error response'
74 assert thrown.httpStatus == 403
75 assert thrown.message == "Unable to ${operation} resource data."
76 assert thrown.details == 'server response'
77 where: 'the following operation is executed'
78 operation << [CREATE, READ, PATCH]