|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.tests.e2e.server; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import javax.ws.rs.GET; |
| 23 | +import javax.ws.rs.Path; |
| 24 | +import javax.ws.rs.QueryParam; |
| 25 | +import javax.ws.rs.core.Application; |
| 26 | +import javax.ws.rs.core.Response; |
| 27 | + |
| 28 | +import org.glassfish.jersey.server.ResourceConfig; |
| 29 | +import org.glassfish.jersey.test.JerseyTest; |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertEquals; |
| 33 | + |
| 34 | +public class OptionalParamConverterTest extends JerseyTest { |
| 35 | + |
| 36 | + private static final String PARAM_NAME = "paramName"; |
| 37 | + |
| 38 | + @Path("/OptionalResource") |
| 39 | + public static class OptionalResource { |
| 40 | + |
| 41 | + @GET |
| 42 | + @Path("/fromString") |
| 43 | + public Response fromString(@QueryParam(PARAM_NAME) Optional<String> data) { |
| 44 | + return Response.ok(data.orElse("")).build(); |
| 45 | + } |
| 46 | + |
| 47 | + @GET |
| 48 | + @Path("/fromInteger") |
| 49 | + public Response fromInteger(@QueryParam(PARAM_NAME) Optional<Integer> data) { |
| 50 | + return Response.ok(data.orElse(0)).build(); |
| 51 | + } |
| 52 | + |
| 53 | + @GET |
| 54 | + @Path("/fromList") |
| 55 | + public Response fromList(@QueryParam(PARAM_NAME) List<Optional<Integer>> data) { |
| 56 | + StringBuilder builder = new StringBuilder(""); |
| 57 | + for (Optional<Integer> val : data) { |
| 58 | + builder.append(val.orElse(0)); |
| 59 | + } |
| 60 | + return Response.ok(builder.toString()).build(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected Application configure() { |
| 66 | + return new ResourceConfig(OptionalResource.class); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void fromOptionalStr() { |
| 71 | + Response empty = target("/OptionalResource/fromString").request().get(); |
| 72 | + Response notEmpty = target("/OptionalResource/fromString").queryParam(PARAM_NAME, "anyValue").request().get(); |
| 73 | + assertEquals(200, empty.getStatus()); |
| 74 | + assertEquals("", empty.readEntity(String.class)); |
| 75 | + assertEquals(200, notEmpty.getStatus()); |
| 76 | + assertEquals("anyValue", notEmpty.readEntity(String.class)); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void fromOptionalInt() { |
| 81 | + Response empty = target("/OptionalResource/fromInteger").request().get(); |
| 82 | + Response notEmpty = target("/OptionalResource/fromInteger").queryParam(PARAM_NAME, 1).request().get(); |
| 83 | + assertEquals(200, empty.getStatus()); |
| 84 | + assertEquals(Integer.valueOf(0), empty.readEntity(Integer.class)); |
| 85 | + assertEquals(200, notEmpty.getStatus()); |
| 86 | + assertEquals(Integer.valueOf(1), notEmpty.readEntity(Integer.class)); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void fromOptionalList() { |
| 91 | + Response empty = target("/OptionalResource/fromList").request().get(); |
| 92 | + Response notEmpty = target("/OptionalResource/fromList").queryParam(PARAM_NAME, 1) |
| 93 | + .queryParam(PARAM_NAME, 2).request().get(); |
| 94 | + assertEquals(200, empty.getStatus()); |
| 95 | + assertEquals("", empty.readEntity(String.class)); |
| 96 | + assertEquals(200, notEmpty.getStatus()); |
| 97 | + assertEquals("12", notEmpty.readEntity(String.class)); |
| 98 | + } |
| 99 | +} |
0 commit comments