aboutsummaryrefslogtreecommitdiffstats
path: root/models-interactions/model-impl/rest/src/test/java/org/onap/policy/rest/RestTest.java
blob: e5a8874298f93aee3a5551063e800eb9399f3317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * ============LICENSE_START=======================================================
 * rest
 * ================================================================================
 * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.rest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
import org.onap.policy.common.utils.network.NetworkUtil;

@Path("RestTest")
public class RestTest {
    private static final String MERGE_PATCH_PLUS_JSON = "application/merge-patch+json";

    private static final String NAME_PARAM = "Bob";
    private static final String AGE_PARAM = "10";
    private static final String PAYLOAD = "At last! ";
    private static final String RETURN_STRING = "Hello There ";
    private static final String EXPECT_STRING = RETURN_STRING + NAME_PARAM + " aged " + AGE_PARAM;

    private static final String LOCALHOST = "localhost";
    private static final String BASE = "base";

    private static int port;
    private static String baseUri;
    private static String getUri;
    private static String deleteUri;
    private static String putUri;
    private static String putUriBlank;
    private static String postUri;
    private static String postUriBlank;
    private static String patchUri;
    private static String patchUriBlank;

    private static HttpServletServer server;

    /**
     * Sets server endpoint for the tests.
     */
    @BeforeClass
    public static void setUp() throws Exception {

        port = NetworkUtil.allocPort();
        baseUri = "http://" + LOCALHOST + ":" + port + "/" + BASE + "/";
        getUri = baseUri + "RestTest/GetHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
        deleteUri = baseUri + "RestTest/DeleteHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
        putUri = baseUri + "RestTest/PutHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
        putUriBlank = baseUri + "RestTest/PutBlank";
        postUri = baseUri + "RestTest/PostHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
        postUriBlank = baseUri + "RestTest/PostBlank";
        patchUri = baseUri + "RestTest/PatchHello/" + NAME_PARAM + "?age=" + AGE_PARAM;
        patchUriBlank = baseUri + "RestTest/PatchBlank";

        server = HttpServletServerFactoryInstance.getServerFactory()
            .build("RestTest", LOCALHOST, port, "/" + BASE, false, true);
        server.addServletClass("/*", RestTest.class.getName());
        server.waitedStart(5000);

    }

    /**
     * Tear down server endpoint for the tests.
     *
     * @throws Exception if there is a problem
     */
    @AfterClass
    public static void tearDown() throws Exception {
        HttpServletServerFactoryInstance.getServerFactory().destroy();
    }

    @Test(expected = NullPointerException.class)
    public void testGetUrlNull() {
        RestManager mgr = new RestManager();
        mgr.get(null, "user", null, null);
    }

    @Test(expected = NullPointerException.class)
    public void testPutUrlNull() {
        RestManager mgr = new RestManager();
        mgr.put(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
    }

    @Test(expected = NullPointerException.class)
    public void testPostUrlNull() {
        RestManager mgr = new RestManager();
        mgr.post(null, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
    }

    @Test(expected = NullPointerException.class)
    public void testDeleteUrlNull() {
        RestManager mgr = new RestManager();
        mgr.delete(null, "user", null, null, null, null);
    }

    @Test(expected = NullPointerException.class)
    public void testPatchUrlNull() {
        RestManager mgr = new RestManager();
        mgr.patch(null, "user", null, null, PAYLOAD);
    }

    @Test
    public void testUsernameNull() {
        RestManager mgr = new RestManager();

        Pair<Integer, String> result = mgr.get(getUri, null, null, null);
        checkResult(result, "GOT: " + EXPECT_STRING);

        result = mgr.delete(deleteUri, null, null, null, null, null);
        checkResult(result, "DELETE: " + EXPECT_STRING);

        result = mgr.delete(deleteUri, null, null, null);
        checkResult(result, "DELETE: " + EXPECT_STRING);

        result = mgr.put(putUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);

        result = mgr.put(putUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);

        result = mgr.post(postUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);

        result = mgr.post(postUriBlank, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);

        result = mgr.patch(patchUri, null, null, null, PAYLOAD);
        checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);

        result = mgr.patch(patchUriBlank, null, null, null, PAYLOAD);
        checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);

    }

    private void checkResult(Pair<Integer, String> result, String expectedText) {
        assertEquals((Integer) 200, result.getLeft());
        assertNotNull(result.getRight());
        assertTrue(result.getRight().length() > 0);
        assertEquals(expectedText, result.getRight());
    }

    @Test
    public void testUsernameEmpty() {
        RestManager mgr = new RestManager();

        Pair<Integer, String> result = mgr.get(getUri, "", null, null);
        checkResult(result, "GOT: " + EXPECT_STRING);

        result = mgr.delete(deleteUri, "", null, null, null, null);
        checkResult(result, "DELETE: " + EXPECT_STRING);

        result = mgr.put(putUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);

        result = mgr.put(putUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);

        result = mgr.post(postUri, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);

        result = mgr.post(postUriBlank, "", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);

        result = mgr.patch(patchUri, "", null, null, PAYLOAD);
        checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);

        result = mgr.patch(patchUriBlank, "", null, null, PAYLOAD);
        checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);

    }

    @Test
    public void testGoodUrl() {
        RestManager mgr = new RestManager();

        Pair<Integer, String> result = mgr.get(getUri, "user", null, null);
        checkResult(result, "GOT: " + EXPECT_STRING);

        result = mgr.delete(deleteUri, "user", null, null, null, null);
        checkResult(result, "DELETE: " + EXPECT_STRING);

        result = mgr.put(putUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "PUT: " + PAYLOAD + EXPECT_STRING);

        result = mgr.put(putUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING);

        result = mgr.post(postUri, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "POST: " + PAYLOAD + EXPECT_STRING);

        result = mgr.post(postUriBlank, "user", null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "POST: " + PAYLOAD + RETURN_STRING);

        result = mgr.patch(patchUri, "user", null, null, PAYLOAD);
        checkResult(result, "PATCH: " + PAYLOAD + EXPECT_STRING);

        result = mgr.patch(patchUriBlank, "user", null, null, PAYLOAD);
        checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING);

    }

    @Test
    public void testNoUrlParamUrl() {
        RestManager mgr = new RestManager();

        Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/", null, null, null);

        assertEquals((Integer) 404, result.getLeft());

        result = mgr.delete(baseUri + "RestTest/DeleteHello/", null, null, null, null, null);
        assertEquals((Integer) 404, result.getLeft());

        result = mgr.put(baseUri + "RestTest/PutHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        assertEquals((Integer) 404, result.getLeft());

        result = mgr.post(baseUri + "RestTest/PostHello/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        assertEquals((Integer) 404, result.getLeft());

        result = mgr.patch(baseUri + "RestTest/PatchHello/", null, null, null, PAYLOAD);
        assertEquals((Integer) 404, result.getLeft());

    }

    @Test
    public void testNoQueryParamUrl() {
        RestManager mgr = new RestManager();

        Pair<Integer, String> result = mgr.get(baseUri + "RestTest/GetHello/" + NAME_PARAM, null, null, null);
        checkResult(result, "GOT: " + RETURN_STRING + NAME_PARAM + " aged 90");

        result = mgr.delete(baseUri + "RestTest/DeleteHello/" + NAME_PARAM, null, null, null, null, null);
        checkResult(result, "DELETE: " + RETURN_STRING + NAME_PARAM + " aged 90");

        result = mgr.put(baseUri + "RestTest/PutHello/" + NAME_PARAM, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "PUT: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90");

        result = mgr.post(baseUri + "RestTest/PostHello/" + NAME_PARAM, null, null,
            null, MediaType.TEXT_PLAIN, PAYLOAD);
        checkResult(result, "POST: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90");

        result = mgr.patch(baseUri + "RestTest/PatchHello/" + NAME_PARAM, null, null,
            null, PAYLOAD);
        checkResult(result, "PATCH: " + PAYLOAD + RETURN_STRING + NAME_PARAM + " aged 90");

    }

    @Test
    public void testBadUrl() {
        RestManager mgr = new RestManager();

        Pair<Integer, String> result = mgr.get(baseUri + "NonExistant/URL/", null, null, null);

        assertEquals((Integer) 404, result.getLeft());

        result = mgr.delete(baseUri + "NonExistant/URL/", null, null, null, null, null);
        assertEquals((Integer) 404, result.getLeft());

        result = mgr.put(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        assertEquals((Integer) 404, result.getLeft());

        result = mgr.post(baseUri + "NonExistant/URL/", null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        assertEquals((Integer) 404, result.getLeft());

        result = mgr.patch(baseUri + "NonExistant/URL/", null, null, null, PAYLOAD);
        assertEquals((Integer) 404, result.getLeft());

    }

    @Test
    public void testWrongUrl() {
        RestManager mgr = new RestManager();

        Pair<Integer, String> result = mgr.get(deleteUri, null, null, null);

        assertEquals((Integer) 405, result.getLeft());

        result = mgr.delete(getUri, null, null, null, null, null);
        assertEquals((Integer) 405, result.getLeft());

        result = mgr.delete(getUri, null, null, null);
        assertEquals((Integer) 405, result.getLeft());

        result = mgr.put(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        assertEquals((Integer) 405, result.getLeft());

        result = mgr.post(getUri, null, null, null, MediaType.TEXT_PLAIN, PAYLOAD);
        assertEquals((Integer) 405, result.getLeft());

        result = mgr.patch(getUri, null, null, null, PAYLOAD);
        assertEquals((Integer) 405, result.getLeft());

    }

    @GET
    @Path("/GetHello/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt(@PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
        return "GOT: " + RETURN_STRING + name + " aged " + age;
    }

    @DELETE
    @Path("/DeleteHello/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public String deleteIt(@PathParam("name") String name, @DefaultValue("90") @QueryParam("age") String age) {
        return "DELETE: " + RETURN_STRING + name + " aged " + age;
    }

    @PUT
    @Path("/PutHello/{name}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    public String putBlank(
        String payload,
        @PathParam("name") String name,
        @DefaultValue("90") @QueryParam("age") String age) {

        return "PUT: " + payload + RETURN_STRING + name + " aged " + age;
    }

    @PUT
    @Path("/PutBlank")
    @Produces(MediaType.TEXT_PLAIN)
    public String putIt(String payload) {
        return "PUT: " + payload + RETURN_STRING;
    }

    @POST
    @Path("/PostHello/{name}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    public String postIt(
        String payload,
        @PathParam("name") String name,
        @DefaultValue("90") @QueryParam("age") String age) {

        return "POST: " + payload + RETURN_STRING + name + " aged " + age;
    }

    @POST
    @Path("/PostBlank")
    @Produces(MediaType.TEXT_PLAIN)
    public String postBlank(String payload) {
        return "POST: " + payload + RETURN_STRING;
    }

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @HttpMethod("PATCH")
    @Documented
    public static @interface Patch {
    }

    @Patch
    @Path("/PatchHello/{name}")
    @Consumes(MERGE_PATCH_PLUS_JSON)
    @Produces(MERGE_PATCH_PLUS_JSON)
    public String patchIt(
        String payload,
        @PathParam("name") String name,
        @DefaultValue("90") @QueryParam("age") String age) {

        return "PATCH: " + payload + RETURN_STRING + name + " aged " + age;
    }

    @Patch
    @Path("/PatchBlank")
    @Produces(MERGE_PATCH_PLUS_JSON)
    public String patchBlank(String payload) {
        return "PATCH: " + payload + RETURN_STRING;
    }
}