Merge "Handle numRecords default setting when built as 0."
[policy/models.git] / models-sim / models-sim-dmaap / src / test / java / org / onap / policy / models / sim / dmaap / rest / CommonRestServer.java
1 /*
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
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.onap.policy.models.sim.dmaap.rest;
22
23 import java.io.File;
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;
31 import lombok.Getter;
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;
37
38 /**
39  * Common base class for rest server tests.
40  */
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/";
47
48     protected static final String CONFIG_FILE = "src/test/resources/parameters/TestConfigParams.json";
49
50     @Getter
51     private static int port;
52
53     protected static String httpPrefix;
54
55     /**
56      * Allocates a port for the server, writes a config file.
57      *
58      * @throws Exception if an error occurs
59      */
60     public static void reconfigure() throws Exception {
61         port = NetworkUtil.allocPort();
62
63         httpPrefix = "http://localhost:" + port + "/";
64
65         String json = new CommonTestData().getParameterGroupAsString(port);
66         makeConfigFile(CONFIG_FILE, json);
67
68         HttpServletServerFactoryInstance.getServerFactory().destroy();
69     }
70
71     /**
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
75      *
76      * @throws Exception if an error occurs
77      */
78     protected static void makeConfigFile(String fileName, String json) throws Exception {
79         File file = new File(fileName);
80         file.deleteOnExit();
81
82         try (FileOutputStream output = new FileOutputStream(file)) {
83             output.write(json.getBytes(StandardCharsets.UTF_8));
84         }
85     }
86
87     /**
88      * Sends a request to an endpoint.
89      *
90      * @param endpoint the target endpoint
91      * @return a request builder
92      * @throws Exception if an error occurs
93      */
94     protected Invocation.Builder sendRequest(final String endpoint) throws Exception {
95         return sendFqeRequest(httpPrefix + ENDPOINT_PREFIX + endpoint);
96     }
97
98     /**
99      * Sends a request to a fully qualified endpoint.
100      *
101      * @param fullyQualifiedEndpoint the fully qualified target endpoint
102      * @return a request builder
103      */
104     protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint) {
105         final Client client = ClientBuilder.newBuilder().build();
106
107         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
108         client.register(GsonMessageBodyHandler.class);
109
110         final WebTarget webTarget = client.target(fullyQualifiedEndpoint);
111
112         return webTarget.request(MediaType.APPLICATION_JSON);
113     }
114 }