2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.api.main.rest.utils;
25 import static org.junit.Assert.assertTrue;
27 import jakarta.ws.rs.client.Client;
28 import jakarta.ws.rs.client.ClientBuilder;
29 import jakarta.ws.rs.client.Entity;
30 import jakarta.ws.rs.client.Invocation;
31 import jakarta.ws.rs.client.WebTarget;
32 import jakarta.ws.rs.core.Response;
33 import java.security.SecureRandom;
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.TrustManager;
36 import org.glassfish.jersey.client.ClientProperties;
37 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
38 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
39 import org.onap.policy.common.gson.GsonMessageBodyHandler;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.coder.StandardYamlCoder;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
48 * Util class to perform REST unit tests.
50 public class CommonTestRestController {
52 protected static final String APP_JSON = "application/json";
53 protected static final String APP_YAML = "application/yaml";
55 protected static final StandardCoder standardCoder = new StandardCoder();
56 protected static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
58 protected static final String HTTPS_PREFIX = "https://localhost:";
59 protected static final String CONTEXT_PATH = "/policy/api/v1/";
61 protected void testSwagger(final int apiPort) throws Exception {
62 final Invocation.Builder invocationBuilder = sendHttpsRequest("v3/api-docs", APP_JSON, apiPort);
63 final String resp = invocationBuilder.get(String.class);
64 assertTrue((resp).contains("{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Policy Framework Lifecycle API\""));
67 protected Response createResource(String endpoint, String resourceName, int apiPort)
70 ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
71 String mediaType = getMediaType(resourceName);
72 mediaType = mediaType == null ? APP_JSON : mediaType;
74 final Invocation.Builder invocationBuilder;
75 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
76 Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
77 return invocationBuilder.post(entity);
80 protected Response readResource(String endpoint, String mediaType, int apiPort) throws Exception {
82 final Invocation.Builder invocationBuilder;
83 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
84 return invocationBuilder.get();
87 protected Response deleteResource(String endpoint, String mediaType, int apiPort) throws Exception {
89 final Invocation.Builder invocationBuilder;
90 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
91 return invocationBuilder.delete();
94 protected Response updateResource(String endpoint, String resourceName, String mediaType, int apiPort)
97 ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
99 final Invocation.Builder invocationBuilder;
100 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
101 Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
102 return invocationBuilder.put(entity);
105 protected ToscaServiceTemplate decodeJson(String resourceName) throws CoderException {
106 return standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
109 protected ToscaServiceTemplate decodeYaml(String resourceName) throws CoderException {
110 return standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
113 protected Invocation.Builder sendHttpsRequest(
114 final String endpoint, String mediaType, int apiPort) throws Exception {
116 final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
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);
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);
133 final WebTarget webTarget = client.target(HTTPS_PREFIX + apiPort + CONTEXT_PATH + endpoint);
135 final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
137 if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
138 throw new IllegalStateException("cannot connect to port " + apiPort);
140 return invocationBuilder;
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);
150 return rawServiceTemplate;
153 private String getMediaType(String resourceName) {
154 if (resourceName.endsWith(".json")) {
156 } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {