eebaa5281174a0137cca9344799769da28976ac8
[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 ENDPOINT_PREFIX = "onap/controlloop/v2/";
46
47     private static String httpPrefix;
48
49     /**
50      * Verifies that an endpoint appears within the swagger response.
51      *
52      * @param endpoint the endpoint of interest
53      * @throws Exception if an error occurs
54      */
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);
58
59         assertThat(resp).contains(endpoint);
60     }
61
62     /**
63      * Sends a request to an endpoint.
64      *
65      * @param endpoint the target endpoint
66      * @return a request builder
67      * @throws Exception if an error occurs
68      */
69     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
70         return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, true);
71     }
72
73     /**
74      * Sends a request to an endpoint, without any authorization header.
75      *
76      * @param endpoint the target endpoint
77      * @return a request builder
78      * @throws Exception if an error occurs
79      */
80     protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception {
81         return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint, false);
82     }
83
84     /**
85      * Sends a request to a fully qualified endpoint.
86      *
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
91      */
92     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
93             throws Exception {
94         final Client client = ClientBuilder.newBuilder().build();
95
96         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
97         client.register(GsonMessageBodyHandler.class);
98
99         if (includeAuth) {
100             client.register(HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"));
101         }
102
103         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
104
105         return webTarget.request(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
106     }
107
108     /**
109      * Assert that POST call is Unauthorized.
110      *
111      * @param endPoint the endpoint
112      * @param entity the entity ofthe body
113      * @throws Exception if an error occurs
114      */
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());
118     }
119
120     /**
121      * Assert that PUT call is Unauthorized.
122      *
123      * @param endPoint the endpoint
124      * @param entity the entity ofthe body
125      * @throws Exception if an error occurs
126      */
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());
130     }
131
132     /**
133      * Assert that GET call is Unauthorized.
134      *
135      * @param endPoint the endpoint
136      * @throws Exception if an error occurs
137      */
138     protected void assertUnauthorizedGet(final String endPoint) throws Exception {
139         Response rawresp = sendNoAuthRequest(endPoint).buildGet().invoke();
140         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
141     }
142
143     /**
144      * Assert that DELETE call is Unauthorized.
145      *
146      * @param endPoint the endpoint
147      * @throws Exception if an error occurs
148      */
149     protected void assertUnauthorizedDelete(final String endPoint) throws Exception {
150         Response rawresp = sendNoAuthRequest(endPoint).delete();
151         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
152     }
153
154     /**
155      * Set Up httpPrefix.
156      *
157      * @param port the port
158      */
159     protected void setHttpPrefix(int port) {
160         httpPrefix = "http://" + SELF + ":" + port + "/";
161     }
162 }