2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 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 java.security.SecureRandom;
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.TrustManager;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.client.WebTarget;
33 import javax.ws.rs.core.Response;
34 import org.glassfish.jersey.client.ClientProperties;
35 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
36 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
37 import org.onap.policy.common.gson.GsonMessageBodyHandler;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.coder.StandardYamlCoder;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46 * Util class to perform REST unit tests.
49 public class CommonTestRestController {
51 protected static final String APP_JSON = "application/json";
52 protected static final String APP_YAML = "application/yaml";
54 protected static final StandardCoder standardCoder = new StandardCoder();
55 protected static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
58 protected Response createResource(String endpoint, String resourceName, int apiPort)
61 ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
62 String mediaType = getMediaType(resourceName);
63 mediaType = mediaType == null ? APP_JSON : mediaType;
65 final Invocation.Builder invocationBuilder;
66 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
67 Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
68 return invocationBuilder.post(entity);
71 protected Response readResource(String endpoint, String mediaType, int apiPort) throws Exception {
73 final Invocation.Builder invocationBuilder;
74 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
75 return invocationBuilder.get();
78 protected Response deleteResource(String endpoint, String mediaType, int apiPort) throws Exception {
80 final Invocation.Builder invocationBuilder;
81 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
82 return invocationBuilder.delete();
85 protected Response updateResource(String endpoint, String resourceName, String mediaType, int apiPort)
88 ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
90 final Invocation.Builder invocationBuilder;
91 invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
92 Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
93 return invocationBuilder.put(entity);
96 protected ToscaServiceTemplate decodeJson(String resourceName) throws CoderException {
97 return standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
100 protected ToscaServiceTemplate decodeYaml(String resourceName) throws CoderException {
101 return standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
104 protected Invocation.Builder sendHttpsRequest(final String endpoint, String mediaType, int apiPort)
107 final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
109 final SSLContext sc = SSLContext.getInstance("TLSv1.2");
110 sc.init(null, noopTrustManager, new SecureRandom());
111 final ClientBuilder clientBuilder =
112 ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
113 final Client client = clientBuilder.build();
114 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
115 client.register(feature);
117 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
118 if (APP_JSON.equalsIgnoreCase(mediaType)) {
119 client.register(GsonMessageBodyHandler.class);
120 } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
121 client.register(YamlMessageBodyHandler.class);
124 final WebTarget webTarget = client.target("https://localhost:" + apiPort + "/policy/api/v1/" + endpoint);
126 final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
128 if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
129 throw new IllegalStateException("cannot connect to port " + apiPort);
131 return invocationBuilder;
134 private ToscaServiceTemplate getRawServiceTemplate(String resourceName) throws CoderException {
135 ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
136 if (APP_JSON.equals(getMediaType(resourceName))) {
137 rawServiceTemplate = decodeJson(resourceName);
138 } else if (APP_YAML.equals(getMediaType(resourceName))) {
139 rawServiceTemplate = decodeYaml(resourceName);
141 return rawServiceTemplate;
144 private String getMediaType(String resourceName) {
145 if (resourceName.endsWith(".json")) {
147 } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {