Fix sample rapp generator to support helm artifact alone rApps
[nonrtric/plt/rappmanager.git] / participants / participant-impl-dme / src / test / java / org / oransc / participant / dme / utils / CommonActuatorController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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
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.oransc.participant.dme.utils;
22
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24
25 import jakarta.ws.rs.client.Client;
26 import jakarta.ws.rs.client.ClientBuilder;
27 import jakarta.ws.rs.client.Invocation;
28 import jakarta.ws.rs.client.WebTarget;
29 import jakarta.ws.rs.core.MediaType;
30 import jakarta.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 public class CommonActuatorController {
37
38     public static final String SELF = NetworkUtil.getHostname();
39     public static final String CONTEXT_PATH = "nonrtric/dmeparticipant/";
40
41     private static String httpPrefix;
42
43     protected Invocation.Builder sendActRequest(final String endpoint) {
44         return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, true);
45     }
46
47     protected Invocation.Builder sendNoAuthActRequest(final String endpoint) {
48         return sendFqeRequest(httpPrefix + CONTEXT_PATH + endpoint, false);
49     }
50
51     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, boolean includeAuth) {
52         final Client client = ClientBuilder.newBuilder().build();
53
54         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
55         client.register(GsonMessageBodyHandler.class);
56
57         if (includeAuth) {
58             client.register(HttpAuthenticationFeature.basic("participantUser", "zb!XztG34"));
59         }
60
61         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
62
63         return webTarget.request(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN);
64     }
65
66     protected void assertUnauthorizedActGet(final String endPoint) {
67         Response rawresp = sendNoAuthActRequest(endPoint).buildGet().invoke();
68         assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), rawresp.getStatus());
69     }
70
71     protected void setHttpPrefix(int port) {
72         httpPrefix = "http://" + SELF + ":" + port + "/";
73     }
74
75 }