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 ENDPOINT_PREFIX = "onap/controlloop/v2/";
47 private static String httpPrefix;
50 * Verifies that an endpoint appears within the swagger response.
52 * @param endpoint the endpoint of interest
53 * @throws Exception if an error occurs
55 protected void testSwagger(final String endpoint) throws Exception {
56 final Invocation.Builder invocationBuilder = sendRequest("api-docs");
57 final String resp = invocationBuilder.get(String.class);
59 assertThat(resp).contains(endpoint);
63 * Sends a request to an endpoint.
65 * @param endpoint the target endpoint
66 * @return a request builder
67 * @throws Exception if an error occurs
69 protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
70 return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, true);
74 * Sends a request to an endpoint, without any authorization header.
76 * @param endpoint the target endpoint
77 * @return a request builder
78 * @throws Exception if an error occurs
80 protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
81 return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, false);
85 * Sends a request to a fully qualified endpoint.
87 * @param fullyQualifiedEndpoint the fully qualified target endpoint
88 * @param includeAuth if authorization header should be included
89 * @return a request builder
90 * @throws Exception if an error occurs
92 protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
94 final Client client = ClientBuilder.newBuilder().build();
96 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
97 client.register(GsonMessageBodyHandler.class);
100 client.register(HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"));
103 final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
105 return webTarget.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
109 * Assert that POST call is Unauthorized.
111 * @param endPoint the endpoint
112 * @param entity the entity ofthe body
113 * @throws Exception if an error occurs
115 protected void assertUnauthorizedPost(final String endPoint, final Entity<?> entity) throws Exception {
116 Response rawresp = sendNoAuthRequest(endPoint).post(entity);
117 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
121 * Assert that PUT call is Unauthorized.
123 * @param endPoint the endpoint
124 * @param entity the entity ofthe body
125 * @throws Exception if an error occurs
127 protected void assertUnauthorizedPut(final String endPoint, final Entity<?> entity) throws Exception {
128 Response rawresp = sendNoAuthRequest(endPoint).put(entity);
129 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
133 * Assert that GET call is Unauthorized.
135 * @param endPoint the endpoint
136 * @throws Exception if an error occurs
138 protected void assertUnauthorizedGet(final String endPoint) throws Exception {
139 Response rawresp = sendNoAuthRequest(endPoint).buildGet().invoke();
140 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
144 * Assert that DELETE call is Unauthorized.
146 * @param endPoint the endpoint
147 * @throws Exception if an error occurs
149 protected void assertUnauthorizedDelete(final String endPoint) throws Exception {
150 Response rawresp = sendNoAuthRequest(endPoint).delete();
151 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
157 * @param port the port
159 protected void setHttpPrefix(int port) {
160 httpPrefix = "http://" + SELF + ":" + port + "/";