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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.aaisimulator.utils;
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;
32 * @author Waqas Ikram (waqas.ikram@est.tech)
37 public class TestRestTemplateService {
40 private TestRestTemplate restTemplate;
43 private UserCredentials userCredentials;
46 public <T> ResponseEntity<T> invokeHttpGet(final String url, final Class<T> clazz) {
47 return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), clazz);
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);
55 public <T> ResponseEntity<T> invokeHttpDelete(final String url, final Class<T> clazz) {
56 final HttpEntity<?> request = new HttpEntity<>(getHttpHeaders());
57 return restTemplate.exchange(url, HttpMethod.DELETE, request, clazz);
60 public <T> ResponseEntity<T> invokeHttpPost(final String url, final Object obj, final Class<T> clazz) {
61 final HttpEntity<?> httpEntity = getHttpEntity(obj);
62 return restTemplate.exchange(url, HttpMethod.POST, httpEntity, clazz);
65 public <T> ResponseEntity<T> invokeHttpPost(final HttpHeaders headers, final String url, final Object obj,
66 final Class<T> clazz) {
67 final HttpEntity<Object> entity = new HttpEntity<>(obj, headers);
68 return restTemplate.exchange(url, HttpMethod.POST, entity, clazz);
71 private HttpEntity<?> getHttpEntity(final Object obj) {
72 return new HttpEntity<>(obj, getHttpHeaders());
75 public HttpHeaders getHttpHeaders() {
76 return TestUtils.getHttpHeaders(userCredentials.getUsers().iterator().next().getUsername());