ccac0c63b7b9ecd1f5336152955f27253544367c
[policy/clamp.git] /
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.policy.clamp.controlloop.runtime.util.rest;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25
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;
37
38 /**
39  * Class to perform Rest unit tests.
40  *
41  */
42 public class CommonRestController {
43
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/";
48
49     private static String httpPrefix;
50
51     /**
52      * Verifies that an endpoint appears within the swagger response.
53      *
54      * @param endpoint the endpoint of interest
55      * @throws Exception if an error occurs
56      */
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);
60
61         assertThat(resp).contains(endpoint);
62     }
63
64     /**
65      * Sends a request to an endpoint.
66      *
67      * @param endpoint the target endpoint
68      * @return a request builder
69      * @throws Exception if an error occurs
70      */
71     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
72         return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, true);
73     }
74
75     /**
76      * Sends a request to an actuator endpoint.
77      *
78      * @param endpoint the target endpoint
79      * @return a request builder
80      * @throws Exception if an error occurs
81      */
82     protected Invocation.Builder sendActRequest(final String endpoint) throws Exception {
83         return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true);
84     }
85
86     /**
87      * Sends a request to an Rest Api endpoint, without any authorization header.
88      *
89      * @param endpoint the target endpoint
90      * @return a request builder
91      * @throws Exception if an error occurs
92      */
93     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
94         return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, false);
95     }
96
97     /**
98      * Sends a request to an actuator endpoint, without any authorization header.
99      *
100      * @param endpoint the target endpoint
101      * @return a request builder
102      * @throws Exception if an error occurs
103      */
104     protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception {
105         return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false);
106     }
107
108     /**
109      * Sends a request to a fully qualified endpoint.
110      *
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
115      */
116     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
117             throws Exception {
118         final Client client = ClientBuilder.newBuilder().build();
119
120         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
121         client.register(GsonMessageBodyHandler.class);
122
123         if (includeAuth) {
124             client.register(HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"));
125         }
126
127         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
128
129         return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
130     }
131
132     /**
133      * Assert that POST call is Unauthorized.
134      *
135      * @param endPoint the endpoint
136      * @param entity the entity ofthe body
137      * @throws Exception if an error occurs
138      */
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());
142     }
143
144     /**
145      * Assert that PUT call is Unauthorized.
146      *
147      * @param endPoint the endpoint
148      * @param entity the entity ofthe body
149      * @throws Exception if an error occurs
150      */
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());
154     }
155
156     /**
157      * Assert that GET call is Unauthorized.
158      *
159      * @param endPoint the endpoint
160      * @throws Exception if an error occurs
161      */
162     protected void assertUnauthorizedGet(final String endPoint) throws Exception {
163         Response rawresp = sendNoAuthRequest(endPoint).buildGet().invoke();
164         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
165     }
166
167     /**
168      * Assert that GET call to actuator endpoint is Unauthorized.
169      *
170      * @param endPoint the endpoint
171      * @throws Exception if an error occurs
172      */
173     protected void assertUnauthorizedActGet(final String endPoint) throws Exception {
174         Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke();
175         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
176     }
177
178     /**
179      * Assert that DELETE call is Unauthorized.
180      *
181      * @param endPoint the endpoint
182      * @throws Exception if an error occurs
183      */
184     protected void assertUnauthorizedDelete(final String endPoint) throws Exception {
185         Response rawresp = sendNoAuthRequest(endPoint).delete();
186         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
187     }
188
189     /**
190      * Set Up httpPrefix.
191      *
192      * @param port the port
193      */
194     protected void setHttpPrefix(int port) {
195         httpPrefix = "http://" + SELF + ":" + port + "/";
196     }
197
198     protected String getHttpPrefix() {
199         return httpPrefix;
200     }
201 }