2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020, 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.rest;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
28 import jakarta.ws.rs.Consumes;
29 import jakarta.ws.rs.DELETE;
30 import jakarta.ws.rs.DefaultValue;
31 import jakarta.ws.rs.GET;
32 import jakarta.ws.rs.HttpMethod;
33 import jakarta.ws.rs.POST;
34 import jakarta.ws.rs.PUT;
35 import jakarta.ws.rs.Path;
36 import jakarta.ws.rs.PathParam;
37 import jakarta.ws.rs.Produces;
38 import jakarta.ws.rs.QueryParam;
39 import jakarta.ws.rs.core.MediaType;
40 import java.lang.annotation.Documented;
41 import java.lang.annotation.ElementType;
42 import java.lang.annotation.Retention;
43 import java.lang.annotation.RetentionPolicy;
44 import java.lang.annotation.Target;
45 import org.apache.commons.lang3.tuple.Pair;
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
50 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
51 import org.onap.policy.common.utils.network.NetworkUtil;
54 public class RestTest {
55 private static final String MERGE_PATCH_PLUS_JSON = "application/merge-patch+json";
57 private static final String NAME_PARAM = "Bob";
58 private static final String AGE_PARAM = "10";
59 private static final String PAYLOAD = "At last! ";
60 private static final String RETURN_STRING = "Hello There ";
61 private static final String EXPECT_STRING = RETURN_STRING + NAME_PARAM + " aged " + AGE_PARAM;
63 private static final String LOCALHOST = "localhost";
64 private static final String BASE = "base";
66 private static int port;
67 private static String baseUri;
68 private static String getUri;
69 private static String deleteUri;
70 private static String putUri;
71 private static String putUriBlank;
72 private static String postUri;
73 private static String postUriBlank;
74 private static String patchUri;
75 private static String patchUriBlank;
77 private static HttpServletServer server;
80 * Sets server endpoint for the tests.
83 public static void setUp() throws Exception {
85 port = NetworkUtil.allocPort();
86 baseUri = "http://" + LOCALHOST + ":" + port + "/" + BASE + "/";
87 getUri = baseUri + "RestTest/GetHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
88 deleteUri = baseUri + "RestTest/DeleteHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
89 putUri = baseUri + "RestTest/PutHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
90 putUriBlank = baseUri + "RestTest/PutBlank";
91 postUri = baseUri + "RestTest/PostHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
92 postUriBlank = baseUri + "RestTest/PostBlank";
93 patchUri = baseUri + "RestTest/PatchHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
94 patchUriBlank = baseUri + "RestTest/PatchBlank";
96 server = HttpServletServerFactoryInstance.getServerFactory()
97 .build("RestTest", LOCALHOST, port, "/" + BASE, false, true);
98 server.addServletClass("/*", RestTest.class.getName());
99 server.waitedStart(5000);
104 * Tear down server endpoint for the tests.
106 * @throws Exception if there is a problem
109 public static void tearDown() throws Exception {
110 HttpServletServerFactoryInstance.getServerFactory().destroy();
113 @Test(expected = NullPointerException.class)
114 public void testGetUrlNull() {
115 RestManager mgr = new RestManager();
116 mgr.get(null, "user", null, null);
119 @Test(expected = NullPointerException.class)
120 public void testPutUrlNull() {
121 RestManager mgr = new RestManager();
122 mgr.put(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
125 @Test(expected = NullPointerException.class)
126 public void testPostUrlNull() {
127 RestManager mgr = new RestManager();
128 mgr.post(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
131 @Test(expected = NullPointerException.class)
132 public void testDeleteUrlNull() {
133 RestManager mgr = new RestManager();
134 mgr.delete(null, "user", null, null, null, null);
137 @Test(expected = NullPointerException.class)
138 public void testPatchUrlNull() {
139 RestManager mgr = new RestManager();
140 mgr.patch(null, "user", null, null, PAYLOAD);
144 public void testUsernameNull() {
145 RestManager mgr = new RestManager();
147 Pair<Integer, String> result = mgr.get(getUri, null, null, null);
148 checkResult(result, "GOT: " + EXPECT_STRING);
150 result = mgr.delete(deleteUri, null, null, null, null, null);
151 checkResult(result, "DELETE: " + EXPECT_STRING);
153 result = mgr.delete(deleteUri, null, null, null);
154 checkResult(result, "DELETE: " + EXPECT_STRING);
156 result = mgr.put(putUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
157 checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);
159 result = mgr.put(putUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
160 checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);
162 result = mgr.post(postUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
163 checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);
165 result = mgr.post(postUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
166 checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);
168 result = mgr.patch(patchUri, null, null, null, PAYLOAD);
169 checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);
171 result = mgr.patch(patchUriBlank, null, null, null, PAYLOAD);
172 checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);
176 private void checkResult(Pair<Integer, String> result, String expectedText) {
177 assertEquals((Integer) 200, result.getLeft());
178 assertNotNull(result.getRight());
179 assertTrue(result.getRight().length() > 0);
180 assertEquals(expectedText, result.getRight());
184 public void testUsernameEmpty() {
185 RestManager mgr = new RestManager();
187 Pair<Integer, String> result = mgr.get(getUri, "", null, null);
188 checkResult(result, "GOT: " + EXPECT_STRING);
190 result = mgr.delete(deleteUri, "", null, null, null, null);
191 checkResult(result, "DELETE: " + EXPECT_STRING);
193 result = mgr.put(putUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
194 checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);
196 result = mgr.put(putUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
197 checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);
199 result = mgr.post(postUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
200 checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);
202 result = mgr.post(postUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
203 checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);
205 result = mgr.patch(patchUri, "", null, null, PAYLOAD);
206 checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);
208 result = mgr.patch(patchUriBlank, "", null, null, PAYLOAD);
209 checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);
214 public void testGoodUrl() {
215 RestManager mgr = new RestManager();
217 Pair<Integer, String> result = mgr.get(getUri, "user", null, null);
218 checkResult(result, "GOT: " + EXPECT_STRING);
220 result = mgr.delete(deleteUri, "user", null, null, null, null);
221 checkResult(result, "DELETE: " + EXPECT_STRING);
223 result = mgr.put(putUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
224 checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);
226 result = mgr.put(putUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
227 checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);
229 result = mgr.post(postUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
230 checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);
232 result = mgr.post(postUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
233 checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);
235 result = mgr.patch(patchUri, "user", null, null, PAYLOAD);
236 checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);
238 result = mgr.patch(patchUriBlank, "user", null, null, PAYLOAD);
239 checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);
244 public void testNoUrlParamUrl() {
245 RestManager mgr = new RestManager();
247 Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/", null, null, null);
249 assertEquals((Integer) 404, result.getLeft());
251 result = mgr.delete(baseUri + "RestTest/DeleteHello/", null, null, null, null, null);
252 assertEquals((Integer) 404, result.getLeft());
254 result = mgr.put(baseUri + "RestTest/PutHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
255 assertEquals((Integer) 404, result.getLeft());
257 result = mgr.post(baseUri + "RestTest/PostHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
258 assertEquals((Integer) 404, result.getLeft());
260 result = mgr.patch(baseUri + "RestTest/PatchHello/", null, null, null, PAYLOAD);
261 assertEquals((Integer) 404, result.getLeft());
266 public void testNoQueryParamUrl() {
267 RestManager mgr = new RestManager();
269 Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/" + NAME_PARAM, null, null, null);
270 checkResult(result, "GOT: " + RETURN_STRING + NAME_PARAM + " aged 90");
272 result = mgr.delete(baseUri + "RestTest/DeleteHello/" + NAME_PARAM, null, null, null, null, null);
273 checkResult(result, "DELETE: " + RETURN_STRING + NAME_PARAM + " aged 90");
275 result = mgr.put(baseUri + "RestTest/PutHello/" + NAME_PARAM, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
276 checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90");
278 result = mgr.post(baseUri + "RestTest/PostHello/" + NAME_PARAM, null, null,
279 null, MediaType.TEXT_PLAIN, PAYLOAD);
280 checkResult(result, "POST: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90");
282 result = mgr.patch(baseUri + "RestTest/PatchHello/" + NAME_PARAM, null, null,
284 checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90");
289 public void testBadUrl() {
290 RestManager mgr = new RestManager();
292 Pair<Integer, String> result = mgr.get(baseUri + "NonExistant/URL/", null, null, null);
294 assertEquals((Integer) 404, result.getLeft());
296 result = mgr.delete(baseUri + "NonExistant/URL/", null, null, null, null, null);
297 assertEquals((Integer) 404, result.getLeft());
299 result = mgr.put(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
300 assertEquals((Integer) 404, result.getLeft());
302 result = mgr.post(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
303 assertEquals((Integer) 404, result.getLeft());
305 result = mgr.patch(baseUri + "NonExistant/URL/", null, null, null, PAYLOAD);
306 assertEquals((Integer) 404, result.getLeft());
311 public void testWrongUrl() {
312 RestManager mgr = new RestManager();
314 Pair<Integer, String> result = mgr.get(deleteUri, null, null, null);
316 assertEquals((Integer) 405, result.getLeft());
318 result = mgr.delete(getUri, null, null, null, null, null);
319 assertEquals((Integer) 405, result.getLeft());
321 result = mgr.delete(getUri, null, null, null);
322 assertEquals((Integer) 405, result.getLeft());
324 result = mgr.put(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
325 assertEquals((Integer) 405, result.getLeft());
327 result = mgr.post(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
328 assertEquals((Integer) 405, result.getLeft());
330 result = mgr.patch(getUri, null, null, null, PAYLOAD);
331 assertEquals((Integer) 405, result.getLeft());
336 @Path("/GetHello/{name}")
337 @Produces(MediaType.TEXT_PLAIN)
338 public String getIt(@PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
339 return "GOT: " + RETURN_STRING + name + " aged " + age;
343 @Path("/DeleteHello/{name}")
344 @Produces(MediaType.TEXT_PLAIN)
345 public String deleteIt(@PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
346 return "DELETE: " + RETURN_STRING + name + " aged " + age;
350 @Path("/PutHello/{name}")
351 @Consumes(MediaType.TEXT_PLAIN)
352 @Produces(MediaType.TEXT_PLAIN)
353 public String putBlank(
355 @PathParam("name") String name,
356 @DefaultValue("90") @QueryParam("age") String age) {
358 return "PUT: " + payload + RETURN_STRING + name + " aged " + age;
363 @Produces(MediaType.TEXT_PLAIN)
364 public String putIt(String payload) {
365 return "PUT: " + payload + RETURN_STRING;
369 @Path("/PostHello/{name}")
370 @Consumes(MediaType.TEXT_PLAIN)
371 @Produces(MediaType.TEXT_PLAIN)
372 public String postIt(
374 @PathParam("name") String name,
375 @DefaultValue("90") @QueryParam("age") String age) {
377 return "POST: " + payload + RETURN_STRING + name + " aged " + age;
382 @Produces(MediaType.TEXT_PLAIN)
383 public String postBlank(String payload) {
384 return "POST: " + payload + RETURN_STRING;
387 @Target({ElementType.METHOD})
388 @Retention(RetentionPolicy.RUNTIME)
391 public static @interface Patch {
395 @Path("/PatchHello/{name}")
396 @Consumes(MERGE_PATCH_PLUS_JSON)
397 @Produces(MERGE_PATCH_PLUS_JSON)
398 public String patchIt(
400 @PathParam("name") String name,
401 @DefaultValue("90") @QueryParam("age") String age) {
403 return "PATCH: " + payload + RETURN_STRING + name + " aged " + age;
408 @Produces(MERGE_PATCH_PLUS_JSON)
409 public String patchBlank(String payload) {
410 return "PATCH: " + payload + RETURN_STRING;