Java 17 Upgrade
[policy/models.git] / models-interactions / model-impl / rest / src / test / java / org / onap / policy / rest / RestTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * rest
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.rest;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
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;
52
53 @Path("RestTest")
54 public class RestTest {
55     private static final String MERGE_PATCH_PLUS_JSON = "application/merge-patch+json";
56
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;
62
63     private static final String LOCALHOST = "localhost";
64     private static final String BASE = "base";
65
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;
76
77     private static HttpServletServer server;
78
79     /**
80      * Sets server endpoint for the tests.
81      */
82     @BeforeClass
83     public static void setUp() throws Exception {
84
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";
95
96         server = HttpServletServerFactoryInstance.getServerFactory()
97             .build("RestTest", LOCALHOST, port, "/" + BASE, false, true);
98         server.addServletClass("/*", RestTest.class.getName());
99         server.waitedStart(5000);
100
101     }
102
103     /**
104      * Tear down server endpoint for the tests.
105      *
106      * @throws Exception if there is a problem
107      */
108     @AfterClass
109     public static void tearDown() throws Exception {
110         HttpServletServerFactoryInstance.getServerFactory().destroy();
111     }
112
113     @Test(expected = NullPointerException.class)
114     public void testGetUrlNull() {
115         RestManager mgr = new RestManager();
116         mgr.get(null, "user", null, null);
117     }
118
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);
123     }
124
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);
129     }
130
131     @Test(expected = NullPointerException.class)
132     public void testDeleteUrlNull() {
133         RestManager mgr = new RestManager();
134         mgr.delete(null, "user", null, null, null, null);
135     }
136
137     @Test(expected = NullPointerException.class)
138     public void testPatchUrlNull() {
139         RestManager mgr = new RestManager();
140         mgr.patch(null, "user", null, null, PAYLOAD);
141     }
142
143     @Test
144     public void testUsernameNull() {
145         RestManager mgr = new RestManager();
146
147         Pair<Integer, String> result = mgr.get(getUri, null, null, null);
148         checkResult(result, "GOT: " + EXPECT_STRING);
149
150         result = mgr.delete(deleteUri, null, null, null, null, null);
151         checkResult(result, "DELETE: " + EXPECT_STRING);
152
153         result = mgr.delete(deleteUri, null, null, null);
154         checkResult(result, "DELETE: " + EXPECT_STRING);
155
156         result = mgr.put(putUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
157         checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);
158
159         result = mgr.put(putUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
160         checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);
161
162         result = mgr.post(postUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
163         checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);
164
165         result = mgr.post(postUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
166         checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);
167
168         result = mgr.patch(patchUri, null, null, null, PAYLOAD);
169         checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);
170
171         result = mgr.patch(patchUriBlank, null, null, null, PAYLOAD);
172         checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);
173
174     }
175
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());
181     }
182
183     @Test
184     public void testUsernameEmpty() {
185         RestManager mgr = new RestManager();
186
187         Pair<Integer, String> result = mgr.get(getUri, "", null, null);
188         checkResult(result, "GOT: " + EXPECT_STRING);
189
190         result = mgr.delete(deleteUri, "", null, null, null, null);
191         checkResult(result, "DELETE: " + EXPECT_STRING);
192
193         result = mgr.put(putUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
194         checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);
195
196         result = mgr.put(putUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
197         checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);
198
199         result = mgr.post(postUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
200         checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);
201
202         result = mgr.post(postUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
203         checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);
204
205         result = mgr.patch(patchUri, "", null, null, PAYLOAD);
206         checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);
207
208         result = mgr.patch(patchUriBlank, "", null, null, PAYLOAD);
209         checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);
210
211     }
212
213     @Test
214     public void testGoodUrl() {
215         RestManager mgr = new RestManager();
216
217         Pair<Integer, String> result = mgr.get(getUri, "user", null, null);
218         checkResult(result, "GOT: " + EXPECT_STRING);
219
220         result = mgr.delete(deleteUri, "user", null, null, null, null);
221         checkResult(result, "DELETE: " + EXPECT_STRING);
222
223         result = mgr.put(putUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
224         checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);
225
226         result = mgr.put(putUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
227         checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);
228
229         result = mgr.post(postUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
230         checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);
231
232         result = mgr.post(postUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
233         checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);
234
235         result = mgr.patch(patchUri, "user", null, null, PAYLOAD);
236         checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);
237
238         result = mgr.patch(patchUriBlank, "user", null, null, PAYLOAD);
239         checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);
240
241     }
242
243     @Test
244     public void testNoUrlParamUrl() {
245         RestManager mgr = new RestManager();
246
247         Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/", null, null, null);
248
249         assertEquals((Integer) 404, result.getLeft());
250
251         result = mgr.delete(baseUri + "RestTest/DeleteHello/", null, null, null, null, null);
252         assertEquals((Integer) 404, result.getLeft());
253
254         result = mgr.put(baseUri + "RestTest/PutHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
255         assertEquals((Integer) 404, result.getLeft());
256
257         result = mgr.post(baseUri + "RestTest/PostHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
258         assertEquals((Integer) 404, result.getLeft());
259
260         result = mgr.patch(baseUri + "RestTest/PatchHello/", null, null, null, PAYLOAD);
261         assertEquals((Integer) 404, result.getLeft());
262
263     }
264
265     @Test
266     public void testNoQueryParamUrl() {
267         RestManager mgr = new RestManager();
268
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");
271
272         result = mgr.delete(baseUri + "RestTest/DeleteHello/" + NAME_PARAM, null, null, null, null, null);
273         checkResult(result, "DELETE: " + RETURN_STRING + NAME_PARAM + " aged 90");
274
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");
277
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");
281
282         result = mgr.patch(baseUri + "RestTest/PatchHello/" + NAME_PARAM, null, null,
283             null, PAYLOAD);
284         checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90");
285
286     }
287
288     @Test
289     public void testBadUrl() {
290         RestManager mgr = new RestManager();
291
292         Pair<Integer, String> result = mgr.get(baseUri + "NonExistant/URL/", null, null, null);
293
294         assertEquals((Integer) 404, result.getLeft());
295
296         result = mgr.delete(baseUri + "NonExistant/URL/", null, null, null, null, null);
297         assertEquals((Integer) 404, result.getLeft());
298
299         result = mgr.put(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
300         assertEquals((Integer) 404, result.getLeft());
301
302         result = mgr.post(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
303         assertEquals((Integer) 404, result.getLeft());
304
305         result = mgr.patch(baseUri + "NonExistant/URL/", null, null, null, PAYLOAD);
306         assertEquals((Integer) 404, result.getLeft());
307
308     }
309
310     @Test
311     public void testWrongUrl() {
312         RestManager mgr = new RestManager();
313
314         Pair<Integer, String> result = mgr.get(deleteUri, null, null, null);
315
316         assertEquals((Integer) 405, result.getLeft());
317
318         result = mgr.delete(getUri, null, null, null, null, null);
319         assertEquals((Integer) 405, result.getLeft());
320
321         result = mgr.delete(getUri, null, null, null);
322         assertEquals((Integer) 405, result.getLeft());
323
324         result = mgr.put(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
325         assertEquals((Integer) 405, result.getLeft());
326
327         result = mgr.post(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
328         assertEquals((Integer) 405, result.getLeft());
329
330         result = mgr.patch(getUri, null, null, null, PAYLOAD);
331         assertEquals((Integer) 405, result.getLeft());
332
333     }
334
335     @GET
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;
340     }
341
342     @DELETE
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;
347     }
348
349     @PUT
350     @Path("/PutHello/{name}")
351     @Consumes(MediaType.TEXT_PLAIN)
352     @Produces(MediaType.TEXT_PLAIN)
353     public String putBlank(
354         String payload,
355         @PathParam("name") String name,
356         @DefaultValue("90") @QueryParam("age") String age) {
357
358         return "PUT: " + payload + RETURN_STRING + name + " aged " + age;
359     }
360
361     @PUT
362     @Path("/PutBlank")
363     @Produces(MediaType.TEXT_PLAIN)
364     public String putIt(String payload) {
365         return "PUT: " + payload + RETURN_STRING;
366     }
367
368     @POST
369     @Path("/PostHello/{name}")
370     @Consumes(MediaType.TEXT_PLAIN)
371     @Produces(MediaType.TEXT_PLAIN)
372     public String postIt(
373         String payload,
374         @PathParam("name") String name,
375         @DefaultValue("90") @QueryParam("age") String age) {
376
377         return "POST: " + payload + RETURN_STRING + name + " aged " + age;
378     }
379
380     @POST
381     @Path("/PostBlank")
382     @Produces(MediaType.TEXT_PLAIN)
383     public String postBlank(String payload) {
384         return "POST: " + payload + RETURN_STRING;
385     }
386
387     @Target({ElementType.METHOD})
388     @Retention(RetentionPolicy.RUNTIME)
389     @HttpMethod("PATCH")
390     @Documented
391     public static @interface Patch {
392     }
393
394     @Patch
395     @Path("/PatchHello/{name}")
396     @Consumes(MERGE_PATCH_PLUS_JSON)
397     @Produces(MERGE_PATCH_PLUS_JSON)
398     public String patchIt(
399         String payload,
400         @PathParam("name") String name,
401         @DefaultValue("90") @QueryParam("age") String age) {
402
403         return "PATCH: " + payload + RETURN_STRING + name + " aged " + age;
404     }
405
406     @Patch
407     @Path("/PatchBlank")
408     @Produces(MERGE_PATCH_PLUS_JSON)
409     public String patchBlank(String payload) {
410         return "PATCH: " + payload + RETURN_STRING;
411     }
412 }