689977e3c724dc842a956afd7f8c5845bde68e0a
[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.acm.participant.policy.main.utils;
22
23 import static org.junit.Assert.assertEquals;
24
25 import javax.ws.rs.client.Client;
26 import javax.ws.rs.client.ClientBuilder;
27 import javax.ws.rs.client.Invocation;
28 import javax.ws.rs.client.WebTarget;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.glassfish.jersey.client.ClientProperties;
32 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
33 import org.onap.policy.common.gson.GsonMessageBodyHandler;
34 import org.onap.policy.common.utils.network.NetworkUtil;
35
36 /**
37  * Class to perform Rest unit tests.
38  *
39  */
40 public class CommonActuatorController {
41
42     public static final String SELF = NetworkUtil.getHostname();
43     public static final String CONTEXT_PATH = "onap/policy/clamp/acm/policyparticipant";
44     public static final String ACTUATOR_ENDPOINT = CONTEXT_PATH + "/actuator/";
45
46     private static String httpPrefix;
47
48     /**
49      * Sends a request to an actuator endpoint.
50      *
51      * @param endpoint the target endpoint
52      * @return a request builder
53      * @throws Exception if an error occurs
54      */
55     protected Invocation.Builder sendActRequest(final String endpoint) throws Exception {
56         return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, true);
57     }
58
59     /**
60      * Sends a request to an actuator endpoint, without any authorization header.
61      *
62      * @param endpoint the target endpoint
63      * @return a request builder
64      * @throws Exception if an error occurs
65      */
66     protected Invocation.Builder sendNoAuthActRequest(final String endpoint) throws Exception {
67         return sendFqeRequest(httpPrefix + ACTUATOR_ENDPOINT + endpoint, false);
68     }
69
70     /**
71      * Sends a request to a fully qualified endpoint.
72      *
73      * @param fullyQualifiedEndpoint the fully qualified target endpoint
74      * @param includeAuth if authorization header should be included
75      * @return a request builder
76      * @throws Exception if an error occurs
77      */
78     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth)
79             throws Exception {
80         final Client client = ClientBuilder.newBuilder().build();
81
82         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
83         client.register(GsonMessageBodyHandler.class);
84
85         if (includeAuth) {
86             client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34"));
87         }
88
89         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
90
91         return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
92     }
93
94     /**
95      * Assert that GET call to actuator endpoint is Unauthorized.
96      *
97      * @param endPoint the endpoint
98      * @throws Exception if an error occurs
99      */
100     protected void assertUnauthorizedActGet(final String endPoint) throws Exception {
101         Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke();
102         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
103     }
104
105     /**
106      * Set Up httpPrefix.
107      *
108      * @param port the port
109      */
110     protected void setHttpPrefix(int port) {
111         httpPrefix = "http://" + SELF + ":" + port + "/";
112     }
113
114 }