Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / utils / CommonTestRestController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2022-2023 Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest.utils;
24
25 import jakarta.ws.rs.client.Client;
26 import jakarta.ws.rs.client.ClientBuilder;
27 import jakarta.ws.rs.client.Entity;
28 import jakarta.ws.rs.client.Invocation;
29 import jakarta.ws.rs.client.WebTarget;
30 import jakarta.ws.rs.core.Response;
31 import java.security.SecureRandom;
32 import javax.net.ssl.SSLContext;
33 import javax.net.ssl.TrustManager;
34 import org.glassfish.jersey.client.ClientProperties;
35 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
36 import org.junit.jupiter.api.Assertions;
37 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
38 import org.onap.policy.common.gson.GsonMessageBodyHandler;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.coder.StandardYamlCoder;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.onap.policy.common.utils.resources.ResourceUtils;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45
46 /**
47  * Util class to perform REST unit tests.
48  */
49 public class CommonTestRestController {
50
51     protected static final String APP_JSON = "application/json";
52     protected static final String APP_YAML = "application/yaml";
53
54     protected static final StandardCoder standardCoder = new StandardCoder();
55     protected static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
56
57     protected static final String HTTPS_PREFIX = "https://localhost:";
58     protected static final String CONTEXT_PATH = "/policy/api/v1/";
59
60     protected void testSwagger(final int apiPort) throws Exception {
61         final Invocation.Builder invocationBuilder = sendHttpsRequest("v3/api-docs", APP_JSON, apiPort);
62         final String resp = invocationBuilder.get(String.class);
63         Assertions.assertTrue(
64             (resp).contains("{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Policy Framework Lifecycle API\""));
65     }
66
67     protected Response createResource(String endpoint, String resourceName, int apiPort)
68         throws Exception {
69
70         ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
71         String mediaType = getMediaType(resourceName);
72         mediaType = mediaType == null ? APP_JSON : mediaType;
73
74         final Invocation.Builder invocationBuilder;
75         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
76         Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
77         return invocationBuilder.post(entity);
78     }
79
80     protected Response readResource(String endpoint, String mediaType, int apiPort) throws Exception {
81
82         final Invocation.Builder invocationBuilder;
83         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
84         return invocationBuilder.get();
85     }
86
87     protected Response deleteResource(String endpoint, String mediaType, int apiPort) throws Exception {
88
89         final Invocation.Builder invocationBuilder;
90         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
91         return invocationBuilder.delete();
92     }
93
94     protected Response updateResource(String endpoint, String resourceName, String mediaType, int apiPort)
95         throws Exception {
96
97         ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
98
99         final Invocation.Builder invocationBuilder;
100         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
101         Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
102         return invocationBuilder.put(entity);
103     }
104
105     protected ToscaServiceTemplate decodeJson(String resourceName) throws CoderException {
106         return standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
107     }
108
109     protected ToscaServiceTemplate decodeYaml(String resourceName) throws CoderException {
110         return standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
111     }
112
113     protected Invocation.Builder sendHttpsRequest(
114         final String endpoint, String mediaType, int apiPort) throws Exception {
115
116         final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
117
118         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
119         sc.init(null, noopTrustManager, new SecureRandom());
120         final ClientBuilder clientBuilder =
121             ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
122         final Client client = clientBuilder.build();
123         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
124         client.register(feature);
125
126         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
127         if (APP_JSON.equalsIgnoreCase(mediaType)) {
128             client.register(GsonMessageBodyHandler.class);
129         } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
130             client.register(YamlMessageBodyHandler.class);
131         }
132
133         final WebTarget webTarget = client.target(HTTPS_PREFIX + apiPort + CONTEXT_PATH + endpoint);
134
135         final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
136
137         if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
138             throw new IllegalStateException("cannot connect to port " + apiPort);
139         }
140         return invocationBuilder;
141     }
142
143     private ToscaServiceTemplate getRawServiceTemplate(String resourceName) throws CoderException {
144         ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
145         if (APP_JSON.equals(getMediaType(resourceName))) {
146             rawServiceTemplate = decodeJson(resourceName);
147         } else if (APP_YAML.equals(getMediaType(resourceName))) {
148             rawServiceTemplate = decodeYaml(resourceName);
149         }
150         return rawServiceTemplate;
151     }
152
153     private String getMediaType(String resourceName) {
154         if (resourceName.endsWith(".json")) {
155             return APP_JSON;
156         } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
157             return APP_YAML;
158         }
159         return null;
160     }
161
162 }