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
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=========================================================
21 package org.onap.policy.clamp.controlloop.runtime.util.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
26 import javax.ws.rs.client.Client;
27 import javax.ws.rs.client.ClientBuilder;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.client.WebTarget;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import org.glassfish.jersey.client.ClientProperties;
34 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
35 import org.onap.policy.common.gson.GsonMessageBodyHandler;
36 import org.onap.policy.common.utils.network.NetworkUtil;
39 * Class to perform Rest unit tests.
42 public class CommonRestController {
44 public static final String SELF = NetworkUtil.getHostname();
45 public static final String CONTEXT_PATH = "onap/controlloop";
46 public static final String ENDPOINT_PREFIX = CONTEXT_PATH + "/v2/";
47 public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/";
49 private static String httpPrefix;
52 * Verifies that an endpoint appears within the swagger response.
54 * @param endpoint the endpoint of interest
55 * @throws Exception if an error occurs
57 protected void testSwagger(final String endpoint) throws Exception {
58 final Invocation.Builder invocationBuilder = sendRequest("api-docs");
59 final String resp = invocationBuilder.get(String.class);
61 assertThat(resp).contains(endpoint);
65 * Sends a request to an endpoint.
67 * @param endpoint the target endpoint
68 * @return a request builder
69 * @throws Exception if an error occurs
71 protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
72 return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, true);
76 * Sends a request to an actuator endpoint.
78 * @param endpoint the target endpoint
79 * @return a request builder
80 * @throws Exception if an error occurs
82 protected Invocation.Builder sendActRequest(final String endpoint) throws Exception {
83 return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true);
87 * Sends a request to an Rest Api endpoint, without any authorization header.
89 * @param endpoint the target endpoint
90 * @return a request builder
91 * @throws Exception if an error occurs
93 protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
94 return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, false);
98 * Sends a request to an actuator endpoint, without any authorization header.
100 * @param endpoint the target endpoint
101 * @return a request builder
102 * @throws Exception if an error occurs
104 protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception {
105 return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false);
109 * Sends a request to a fully qualified endpoint.
111 * @param fullyQualifiedEndpoint the fully qualified target endpoint
112 * @param includeAuth if authorization header should be included
113 * @return a request builder
114 * @throws Exception if an error occurs
116 protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
118 final Client client = ClientBuilder.newBuilder().build();
120 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
121 client.register(GsonMessageBodyHandler.class);
124 client.register(HttpAuthenticationFeature.basic("runtimeUser", "zb!XztG34"));
127 final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
129 return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
133 * Assert that POST call is Unauthorized.
135 * @param endPoint the endpoint
136 * @param entity the entity ofthe body
137 * @throws Exception if an error occurs
139 protected void assertUnauthorizedPost(final String endPoint, final Entity<?> entity) throws Exception {
140 Response rawresp = sendNoAuthRequest(endPoint).post(entity);
141 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
145 * Assert that PUT call is Unauthorized.
147 * @param endPoint the endpoint
148 * @param entity the entity ofthe body
149 * @throws Exception if an error occurs
151 protected void assertUnauthorizedPut(final String endPoint, final Entity<?> entity) throws Exception {
152 Response rawresp = sendNoAuthRequest(endPoint).put(entity);
153 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
157 * Assert that GET call is Unauthorized.
159 * @param endPoint the endpoint
160 * @throws Exception if an error occurs
162 protected void assertUnauthorizedGet(final String endPoint) throws Exception {
163 Response rawresp = sendNoAuthRequest(endPoint).buildGet().invoke();
164 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
168 * Assert that GET call to actuator endpoint is Unauthorized.
170 * @param endPoint the endpoint
171 * @throws Exception if an error occurs
173 protected void assertUnauthorizedActGet(final String endPoint) throws Exception {
174 Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke();
175 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
179 * Assert that DELETE call is Unauthorized.
181 * @param endPoint the endpoint
182 * @throws Exception if an error occurs
184 protected void assertUnauthorizedDelete(final String endPoint) throws Exception {
185 Response rawresp = sendNoAuthRequest(endPoint).delete();
186 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
192 * @param port the port
194 protected void setHttpPrefix(int port) {
195 httpPrefix = "http://" + SELF + ":" + port + "/";
198 protected String getHttpPrefix() {