2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019-2020 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.assertTrue;
27 import java.lang.annotation.Documented;
28 import java.lang.annotation.ElementType;
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 import java.lang.annotation.Target;
32 import javax.ws.rs.Consumes;
33 import javax.ws.rs.DELETE;
34 import javax.ws.rs.DefaultValue;
35 import javax.ws.rs.GET;
36 import javax.ws.rs.HttpMethod;
37 import javax.ws.rs.POST;
38 import javax.ws.rs.PUT;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.PathParam;
41 import javax.ws.rs.Produces;
42 import javax.ws.rs.QueryParam;
43 import javax.ws.rs.core.MediaType;
44 import org.apache.commons.lang3.tuple.Pair;
45 import org.junit.AfterClass;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
49 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
50 import org.onap.policy.common.utils.network.NetworkUtil;
53 public class RestTest {
54 private static final String MERGE_PATCH_PLUS_JSON = "application/merge-patch+json";
56 private static final String NAME_PARAM = "Bob";
57 private static final String AGE_PARAM = "10";
58 private static final String PAYLOAD = "At last! ";
59 private static final String RETURN_STRING = "Hello There ";
60 private static final String EXPECT_STRING = RETURN_STRING + NAME_PARAM + " aged " + AGE_PARAM;
62 private static final String LOCALHOST = "localhost";
63 private static final String BASE = "base";
65 private static int port;
66 private static String baseUri;
67 private static String getUri;
68 private static String deleteUri;
69 private static String putUri;
70 private static String putUriBlank;
71 private static String postUri;
72 private static String postUriBlank;
73 private static String patchUri;
74 private static String patchUriBlank;
76 private static HttpServletServer server;
79 * Sets server endpoint for the tests.
82 public static void setUp() throws Exception {
84 port = NetworkUtil.allocPort();
85 baseUri = "http://" + LOCALHOST + ":" + port + "/" + BASE + "/";
86 getUri = baseUri + "RestTest/GetHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
87 deleteUri = baseUri + "RestTest/DeleteHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
88 putUri = baseUri + "RestTest/PutHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
89 putUriBlank = baseUri + "RestTest/PutBlank";
90 postUri = baseUri + "RestTest/PostHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
91 postUriBlank = baseUri + "RestTest/PostBlank";
92 patchUri = baseUri + "RestTest/PatchHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
93 patchUriBlank = baseUri + "RestTest/PatchBlank";
95 server = HttpServletServerFactoryInstance.getServerFactory()
96 .build("RestTest", LOCALHOST, port, "/" + BASE, false, true);
97 server.addServletClass("/*", RestTest.class.getName());
98 server.waitedStart(5000);
103 * Tear down server endpoint for the tests.
105 * @throws Exception if there is a problem
108 public static void tearDown() throws Exception {
109 HttpServletServerFactoryInstance.getServerFactory().destroy();
112 @Test(expected = NullPointerException.class)
113 public void testGetUrlNull() {
114 RestManager mgr = new RestManager();
115 mgr.get(null, "user", null, null);
118 @Test(expected = NullPointerException.class)
119 public void testPutUrlNull() {
120 RestManager mgr = new RestManager();
121 mgr.put(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
124 @Test(expected = NullPointerException.class)
125 public void testPostUrlNull() {
126 RestManager mgr = new RestManager();
127 mgr.post(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
130 @Test(expected = NullPointerException.class)
131 public void testDeleteUrlNull() {
132 RestManager mgr = new RestManager();
133 mgr.delete(null, "user", null, null, null, null);
136 @Test(expected = NullPointerException.class)
137 public void testPatchUrlNull() {
138 RestManager mgr = new RestManager();
139 mgr.patch(null, "user", null, null, PAYLOAD);
143 public void testUsernameNull() {
144 RestManager mgr = new RestManager();
146 Pair<Integer, String> result = mgr.get(getUri, null, null, null);
148 assertEquals((Integer) 200, result.getLeft());
149 assertTrue(result.getRight() != null);
150 assertTrue(result.getRight().length() > 0);
151 assertEquals("GOT: " + EXPECT_STRING, result.getRight());
153 result = mgr.delete(deleteUri, null, null, null, null, null);
154 assertEquals((Integer) 200, result.getLeft());
155 assertTrue(result.getRight() != null);
156 assertTrue(result.getRight().length() > 0);
157 assertEquals("DELETE: " + EXPECT_STRING, result.getRight());
159 result = mgr.delete(deleteUri, null, null, null);
160 assertEquals((Integer) 200, result.getLeft());
161 assertTrue(result.getRight() != null);
162 assertTrue(result.getRight().length() > 0);
163 assertEquals("DELETE: " + EXPECT_STRING, result.getRight());
165 result = mgr.put(putUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
166 assertEquals((Integer) 200, result.getLeft());
167 assertTrue(result.getRight() != null);
168 assertTrue(result.getRight().length() > 0);
169 assertEquals("PUT: " + PAYLOAD + EXPECT_STRING, result.getRight());
171 result = mgr.put(putUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
172 assertEquals((Integer) 200, result.getLeft());
173 assertTrue(result.getRight() != null);
174 assertTrue(result.getRight().length() > 0);
175 assertEquals("PUT: " + PAYLOAD + RETURN_STRING, result.getRight());
177 result = mgr.post(postUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
178 assertEquals((Integer) 200, result.getLeft());
179 assertTrue(result.getRight() != null);
180 assertTrue(result.getRight().length() > 0);
181 assertEquals("POST: " + PAYLOAD + EXPECT_STRING, result.getRight());
183 result = mgr.post(postUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
184 assertEquals((Integer) 200, result.getLeft());
185 assertTrue(result.getRight() != null);
186 assertTrue(result.getRight().length() > 0);
187 assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.getRight());
189 result = mgr.patch(patchUri, null, null, null, PAYLOAD);
190 assertEquals((Integer) 200, result.getLeft());
191 assertTrue(result.getRight() != null);
192 assertTrue(result.getRight().length() > 0);
193 assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.getRight());
195 result = mgr.patch(patchUriBlank, null, null, null, PAYLOAD);
196 assertEquals((Integer) 200, result.getLeft());
197 assertTrue(result.getRight() != null);
198 assertTrue(result.getRight().length() > 0);
199 assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.getRight());
204 public void testUsernameEmpty() {
205 RestManager mgr = new RestManager();
207 Pair<Integer, String> result = mgr.get(getUri, "", null, null);
209 assertEquals((Integer) 200, result.getLeft());
210 assertTrue(result.getRight() != null);
211 assertTrue(result.getRight().length() > 0);
212 assertEquals("GOT: " + EXPECT_STRING, result.getRight());
214 result = mgr.delete(deleteUri, "", null, null, null, null);
215 assertEquals((Integer) 200, result.getLeft());
216 assertTrue(result.getRight() != null);
217 assertTrue(result.getRight().length() > 0);
218 assertEquals("DELETE: " + EXPECT_STRING, result.getRight());
220 result = mgr.put(putUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
221 assertEquals((Integer) 200, result.getLeft());
222 assertTrue(result.getRight() != null);
223 assertTrue(result.getRight().length() > 0);
224 assertEquals("PUT: " + PAYLOAD + EXPECT_STRING, result.getRight());
226 result = mgr.put(putUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
227 assertEquals((Integer) 200, result.getLeft());
228 assertTrue(result.getRight() != null);
229 assertTrue(result.getRight().length() > 0);
230 assertEquals("PUT: " + PAYLOAD + RETURN_STRING, result.getRight());
232 result = mgr.post(postUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
233 assertEquals((Integer) 200, result.getLeft());
234 assertTrue(result.getRight() != null);
235 assertTrue(result.getRight().length() > 0);
236 assertEquals("POST: " + PAYLOAD + EXPECT_STRING, result.getRight());
238 result = mgr.post(postUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
239 assertEquals((Integer) 200, result.getLeft());
240 assertTrue(result.getRight() != null);
241 assertTrue(result.getRight().length() > 0);
242 assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.getRight());
244 result = mgr.patch(patchUri, "", null, null, PAYLOAD);
245 assertEquals((Integer) 200, result.getLeft());
246 assertTrue(result.getRight() != null);
247 assertTrue(result.getRight().length() > 0);
248 assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.getRight());
250 result = mgr.patch(patchUriBlank, "", null, null, PAYLOAD);
251 assertEquals((Integer) 200, result.getLeft());
252 assertTrue(result.getRight() != null);
253 assertTrue(result.getRight().length() > 0);
254 assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.getRight());
259 public void testGoodUrl() {
260 RestManager mgr = new RestManager();
262 Pair<Integer, String> result = mgr.get(getUri, "user", null, null);
264 assertEquals((Integer) 200, result.getLeft());
265 assertTrue(result.getRight() != null);
266 assertTrue(result.getRight().length() > 0);
267 assertEquals("GOT: " + EXPECT_STRING, result.getRight());
269 result = mgr.delete(deleteUri, "user", null, null, null, null);
270 assertEquals((Integer) 200, result.getLeft());
271 assertTrue(result.getRight() != null);
272 assertTrue(result.getRight().length() > 0);
273 assertEquals("DELETE: " + EXPECT_STRING, result.getRight());
275 result = mgr.put(putUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
276 assertEquals((Integer) 200, result.getLeft());
277 assertTrue(result.getRight() != null);
278 assertTrue(result.getRight().length() > 0);
279 assertEquals("PUT: " + PAYLOAD + EXPECT_STRING, result.getRight());
281 result = mgr.put(putUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
282 assertEquals((Integer) 200, result.getLeft());
283 assertTrue(result.getRight() != null);
284 assertTrue(result.getRight().length() > 0);
285 assertEquals("PUT: " + PAYLOAD + RETURN_STRING, result.getRight());
287 result = mgr.post(postUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
288 assertEquals((Integer) 200, result.getLeft());
289 assertTrue(result.getRight() != null);
290 assertTrue(result.getRight().length() > 0);
291 assertEquals("POST: " + PAYLOAD + EXPECT_STRING, result.getRight());
293 result = mgr.post(postUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
294 assertEquals((Integer) 200, result.getLeft());
295 assertTrue(result.getRight() != null);
296 assertTrue(result.getRight().length() > 0);
297 assertEquals("POST: " + PAYLOAD + RETURN_STRING, result.getRight());
299 result = mgr.patch(patchUri, "user", null, null, PAYLOAD);
300 assertEquals((Integer) 200, result.getLeft());
301 assertTrue(result.getRight() != null);
302 assertTrue(result.getRight().length() > 0);
303 assertEquals("PATCH: " + PAYLOAD + EXPECT_STRING, result.getRight());
305 result = mgr.patch(patchUriBlank, "user", null, null, PAYLOAD);
306 assertEquals((Integer) 200, result.getLeft());
307 assertTrue(result.getRight() != null);
308 assertTrue(result.getRight().length() > 0);
309 assertEquals("PATCH: " + PAYLOAD + RETURN_STRING, result.getRight());
314 public void testNoUrlParamUrl() {
315 RestManager mgr = new RestManager();
317 Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/", null, null, null);
319 assertEquals((Integer) 404, result.getLeft());
321 result = mgr.delete(baseUri + "RestTest/DeleteHello/", null, null, null, null, null);
322 assertEquals((Integer) 404, result.getLeft());
324 result = mgr.put(baseUri + "RestTest/PutHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
325 assertEquals((Integer) 404, result.getLeft());
327 result = mgr.post(baseUri + "RestTest/PostHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
328 assertEquals((Integer) 404, result.getLeft());
330 result = mgr.patch(baseUri + "RestTest/PatchHello/", null, null, null, PAYLOAD);
331 assertEquals((Integer) 404, result.getLeft());
336 public void testNoQueryParamUrl() {
337 RestManager mgr = new RestManager();
339 Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/" + NAME_PARAM, null, null, null);
341 assertEquals((Integer) 200, result.getLeft());
342 assertTrue(result.getRight() != null);
343 assertTrue(result.getRight().length() > 0);
344 assertEquals("GOT: " + RETURN_STRING + NAME_PARAM + " aged 90", result.getRight());
346 result = mgr.delete(baseUri + "RestTest/DeleteHello/" + NAME_PARAM, null, null, null, null, null);
347 assertEquals((Integer) 200, result.getLeft());
348 assertTrue(result.getRight() != null);
349 assertTrue(result.getRight().length() > 0);
350 assertEquals("DELETE: " + RETURN_STRING + NAME_PARAM + " aged 90", result.getRight());
352 result = mgr.put(baseUri + "RestTest/PutHello/" + NAME_PARAM, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
353 assertEquals((Integer) 200, result.getLeft());
354 assertTrue(result.getRight() != null);
355 assertTrue(result.getRight().length() > 0);
356 assertEquals("PUT: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.getRight());
358 result = mgr.post(baseUri + "RestTest/PostHello/" + NAME_PARAM, null, null,
359 null, MediaType.TEXT_PLAIN, PAYLOAD);
360 assertEquals((Integer) 200, result.getLeft());
361 assertTrue(result.getRight() != null);
362 assertTrue(result.getRight().length() > 0);
363 assertEquals("POST: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.getRight());
365 result = mgr.patch(baseUri + "RestTest/PatchHello/" + NAME_PARAM, null, null,
367 assertEquals((Integer) 200, result.getLeft());
368 assertTrue(result.getRight() != null);
369 assertTrue(result.getRight().length() > 0);
370 assertEquals("PATCH: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90", result.getRight());
375 public void testBadUrl() {
376 RestManager mgr = new RestManager();
378 Pair<Integer, String> result = mgr.get(baseUri + "NonExistant/URL/", null, null, null);
380 assertEquals((Integer) 404, result.getLeft());
382 result = mgr.delete(baseUri + "NonExistant/URL/", null, null, null, null, null);
383 assertEquals((Integer) 404, result.getLeft());
385 result = mgr.put(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
386 assertEquals((Integer) 404, result.getLeft());
388 result = mgr.post(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
389 assertEquals((Integer) 404, result.getLeft());
391 result = mgr.patch(baseUri + "NonExistant/URL/", null, null, null, PAYLOAD);
392 assertEquals((Integer) 404, result.getLeft());
397 public void testWrongUrl() {
398 RestManager mgr = new RestManager();
400 Pair<Integer, String> result = mgr.get(deleteUri, null, null, null);
402 assertEquals((Integer) 405, result.getLeft());
404 result = mgr.delete(getUri, null, null, null, null, null);
405 assertEquals((Integer) 405, result.getLeft());
407 result = mgr.delete(getUri, null, null, null);
408 assertEquals((Integer) 405, result.getLeft());
410 result = mgr.put(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
411 assertEquals((Integer) 405, result.getLeft());
413 result = mgr.post(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
414 assertEquals((Integer) 405, result.getLeft());
416 result = mgr.patch(getUri, null, null, null, PAYLOAD);
417 assertEquals((Integer) 405, result.getLeft());
422 @Path("/GetHello/{name}")
423 @Produces(MediaType.TEXT_PLAIN)
424 public String getIt(@PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
425 return "GOT: " + RETURN_STRING + name + " aged " + age;
429 @Path("/DeleteHello/{name}")
430 @Produces(MediaType.TEXT_PLAIN)
431 public String deleteIt(@PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
432 return "DELETE: " + RETURN_STRING + name + " aged " + age;
436 @Path("/PutHello/{name}")
437 @Consumes(MediaType.TEXT_PLAIN)
438 @Produces(MediaType.TEXT_PLAIN)
439 public String putBlank(
441 @PathParam("name") String name,
442 @DefaultValue("90") @QueryParam("age") String age) {
444 return "PUT: " + payload + RETURN_STRING + name + " aged " + age;
449 @Produces(MediaType.TEXT_PLAIN)
450 public String putIt(String payload) {
451 return "PUT: " + payload + RETURN_STRING;
455 @Path("/PostHello/{name}")
456 @Consumes(MediaType.TEXT_PLAIN)
457 @Produces(MediaType.TEXT_PLAIN)
458 public String postIt(
460 @PathParam("name") String name,
461 @DefaultValue("90") @QueryParam("age") String age) {
463 return "POST: " + payload + RETURN_STRING + name + " aged " + age;
468 @Produces(MediaType.TEXT_PLAIN)
469 public String postBlank(String payload) {
470 return "POST: " + payload + RETURN_STRING;
473 @Target({ElementType.METHOD})
474 @Retention(RetentionPolicy.RUNTIME)
477 public static @interface Patch {
481 @Path("/PatchHello/{name}")
482 @Consumes(MERGE_PATCH_PLUS_JSON)
483 @Produces(MERGE_PATCH_PLUS_JSON)
484 public String patchIt(
486 @PathParam("name") String name,
487 @DefaultValue("90") @QueryParam("age") String age) {
489 return "PATCH: " + payload + RETURN_STRING + name + " aged " + age;
494 @Produces(MERGE_PATCH_PLUS_JSON)
495 public String patchBlank(String payload) {
496 return "PATCH: " + payload + RETURN_STRING;