018c056a4cf175ae19bac229b97e7fdd24a7ba37
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / aai-simulator / src / test / java / org / onap / so / aaisimulator / utils / TestRestTemplateService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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 package org.onap.so.aaisimulator.utils;
21
22 import org.onap.so.simulator.model.UserCredentials;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.boot.test.web.client.TestRestTemplate;
25 import org.springframework.http.HttpEntity;
26 import org.springframework.http.HttpHeaders;
27 import org.springframework.http.HttpMethod;
28 import org.springframework.http.ResponseEntity;
29 import org.springframework.stereotype.Service;
30
31 /**
32  * @author Waqas Ikram (waqas.ikram@est.tech)
33  *
34  */
35
36 @Service
37 public class TestRestTemplateService {
38
39     @Autowired
40     private TestRestTemplate restTemplate;
41
42     @Autowired
43     private UserCredentials userCredentials;
44
45
46     public <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
47         return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz);
48     }
49
50     public <T> ResponseEntity<T> invokeHttpPut(final String url, final Object obj, final Class<T> clazz) {
51         final HttpEntity<?> httpEntity = getHttpEntity(obj);
52         return restTemplate.exchange(url, HttpMethod.PUT, httpEntity, clazz);
53     }
54
55     public <T> ResponseEntity<T> invokeHttpPost(final String url, final Object obj, final Class<T> clazz) {
56         final HttpEntity<?> httpEntity = getHttpEntity(obj);
57         return restTemplate.exchange(url, HttpMethod.POST, httpEntity, clazz);
58     }
59
60     public <T> ResponseEntity<T> invokeHttpPost(final HttpHeaders headers, final String url, final Object obj,
61             final Class<T> clazz) {
62         final HttpEntity<Object> entity = new HttpEntity<>(obj, headers);
63         return restTemplate.exchange(url, HttpMethod.POST, entity, clazz);
64     }
65
66     private HttpEntity<?> getHttpEntity(final Object obj) {
67         return new HttpEntity<>(obj, getHttpHeaders());
68     }
69
70     public HttpHeaders getHttpHeaders() {
71         return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());
72     }
73
74 }