2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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.acm.participant.kubernetes.utils;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import jakarta.ws.rs.client.ClientBuilder;
26 import jakarta.ws.rs.client.Invocation;
27 import jakarta.ws.rs.core.MediaType;
28 import jakarta.ws.rs.core.Response;
29 import org.glassfish.jersey.client.ClientProperties;
30 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
31 import org.onap.policy.common.gson.GsonMessageBodyHandler;
32 import org.onap.policy.common.utils.network.NetworkUtil;
35 * Class to perform Rest unit tests.
38 public class CommonActuatorController {
40 public static final String SELF = NetworkUtil.getHostname();
41 public static final String CONTEXT_PATH = "onap/policy/clamp/acm/k8sparticipant/";
43 private static String httpPrefix;
46 * Sends a request to an actuator endpoint.
48 * @param endpoint the target endpoint
49 * @return a request builder
51 protected Invocation.Builder sendActRequest(final String endpoint) {
52 return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true);
56 * Sends a request to an actuator endpoint, without any authorization header.
58 * @param endpoint the target endpoint
59 * @return a request builder
61 protected Invocation.Builder sendNoAuthActRequest(final String endpoint) {
62 return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false);
66 * Sends a request to a fully qualified endpoint.
68 * @param fullyQualifiedEndpoint the fully qualified target endpoint
69 * @param includeAuth if authorization header should be included
70 * @return a request builder
72 protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) {
73 final var client = ClientBuilder.newBuilder().build();
75 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
76 client.register(GsonMessageBodyHandler.class);
79 client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34"));
82 final var webTarget = client.target(fullyQualifiedEndpoint);
84 return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
88 * Assert that GET call to actuator endpoint is Unauthorized.
90 * @param endPoint the endpoint
92 protected void assertUnauthorizedActGet(final String endPoint) {
93 var rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke();
94 assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
100 * @param port the port
102 protected void setHttpPrefix(int port) {
103 httpPrefix = "http://" + SELF + ":" + port + "/";