2 * ============LICENSE_START=======================================================
3 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property.
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.models.sim.dmaap.rest;
24 import java.io.FileOutputStream;
25 import java.nio.charset.StandardCharsets;
26 import javax.ws.rs.client.Client;
27 import javax.ws.rs.client.ClientBuilder;
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.client.WebTarget;
30 import javax.ws.rs.core.MediaType;
32 import org.glassfish.jersey.client.ClientProperties;
33 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
34 import org.onap.policy.common.gson.GsonMessageBodyHandler;
35 import org.onap.policy.common.utils.network.NetworkUtil;
36 import org.onap.policy.sim.dmaap.parameters.CommonTestData;
39 * Common base class for rest server tests.
41 public class CommonRestServer {
42 public static final String NOT_ALIVE = "not alive";
43 public static final String ALIVE = "alive";
44 public static final String SELF = "self";
45 public static final String NAME = "DMaaP Simulator";
46 public static final String ENDPOINT_PREFIX = "events/";
48 protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
51 private static int port;
53 protected static String httpPrefix;
56 * Allocates a port for the server, writes a config file.
58 * @throws Exception if an error occurs
60 public static void reconfigure() throws Exception {
61 port = NetworkUtil.allocPort();
63 httpPrefix = "http://localhost:" + port + "/";
65 String json = new CommonTestData().getParameterGroupAsString(port);
66 makeConfigFile(CONFIG_FILE, json);
68 HttpServletServerFactoryInstance.getServerFactory().destroy();
72 * Makes a parameter configuration file.
73 * @param fileName name of the config file to be created
74 * @param json json to be written to the file
76 * @throws Exception if an error occurs
78 protected static void makeConfigFile(String fileName, String json) throws Exception {
79 File file = new File(fileName);
82 try (FileOutputStream output = new FileOutputStream(file)) {
83 output.write(json.getBytes(StandardCharsets.UTF_8));
88 * Sends a request to an endpoint.
90 * @param endpoint the target endpoint
91 * @return a request builder
92 * @throws Exception if an error occurs
94 protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
95 return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint);
99 * Sends a request to a fully qualified endpoint.
101 * @param fullyQualifiedEndpoint the fully qualified target endpoint
102 * @return a request builder
104 protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint) {
105 final Client client = ClientBuilder.newBuilder().build();
107 client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
108 client.register(GsonMessageBodyHandler.class);
110 final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
112 return webTarget.request(MediaType.APPLICATION_JSON);