389086c7701ec71211e33712941be0f1d48cc286
[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 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.client
22
23 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
24 import org.spockframework.spring.SpringBean
25 import org.springframework.beans.factory.annotation.Autowired
26 import org.springframework.boot.test.context.SpringBootTest
27 import org.springframework.http.HttpEntity
28 import org.springframework.http.HttpHeaders
29 import org.springframework.http.HttpMethod
30 import org.springframework.http.ResponseEntity
31 import org.springframework.test.context.ContextConfiguration
32 import org.springframework.web.client.RestTemplate
33 import spock.lang.Specification
34
35 @SpringBootTest
36 @ContextConfiguration(classes = [NcmpConfiguration.DmiProperties, DmiRestClient])
37 class DmiRestClientSpec extends Specification {
38
39     @SpringBean
40     RestTemplate mockRestTemplate = Mock(RestTemplate)
41
42     @Autowired
43     DmiRestClient objectUnderTest
44     def resourceUrl = 'some url'
45
46     def 'DMI POST operation'() {
47         given: 'the rest template returns a valid response entity'
48             def mockResponseEntity = Mock(ResponseEntity)
49             mockRestTemplate.exchange(resourceUrl, HttpMethod.POST, _ as HttpEntity, Object.class) >> mockResponseEntity
50         when: 'POST operation is invoked'
51             def result = objectUnderTest.postOperation(resourceUrl, new HttpHeaders())
52         then: 'the output of the method is equal to the output from the rest template'
53             result == mockResponseEntity
54     }
55
56     def 'DMI POST operation with JSON.'() {
57         given: 'the rest template returns a valid response entity'
58             def mockResponseEntity = Mock(ResponseEntity)
59             mockRestTemplate.postForEntity(resourceUrl, _ as HttpEntity, Object.class) >> mockResponseEntity
60         when: 'POST operation is invoked'
61             def result = objectUnderTest.postOperationWithJsonData(resourceUrl, 'json-data', new HttpHeaders())
62         then: 'the output of the method is equal to the output from the test template'
63             result == mockResponseEntity
64     }
65
66 }