diff --git a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java index ce9cba1fa34..bf2fcfe82af 100644 --- a/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java +++ b/examples/helloworld-cdi2-se/src/main/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldResource.java @@ -15,11 +15,15 @@ import javax.inject.Inject; import javax.inject.Singleton; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; +import javax.ws.rs.Consumes; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.SecurityContext; import static java.util.Optional.ofNullable; @@ -40,16 +44,28 @@ public class HelloWorldResource { @Path("{name}") @Produces("text/plain") public String getHello(@PathParam("name") String name, @Context SecurityContext sc) { + return hello(sc, name); + } + + @POST + @Produces(MediaType.TEXT_PLAIN) + @Consumes(MediaType.TEXT_PLAIN) + public String postHello(@NotNull @Size(min=1) String name, @Context SecurityContext sc) { + return hello(sc, name); + } + + private String hello(SecurityContext sc, String name) { final StringBuilder sb = new StringBuilder(this.helloBean.hello(name)); ofNullable(sc.getUserPrincipal()) - .map(Principal::getName) - .ifPresent(p -> { - sb.append("("); - sb.append(p); - sb.append(")"); - }); + .map(Principal::getName) + .ifPresent(p -> { + sb.append("("); + sb.append(p); + sb.append(")"); + }); return sb.toString(); } + } diff --git a/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldTest.java b/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldTest.java index 0bba28a7162..bc50c9c1531 100644 --- a/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldTest.java +++ b/examples/helloworld-cdi2-se/src/test/java/org/glassfish/jersey/examples/helloworld/cdi2se/HelloWorldTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0, which is available at @@ -14,6 +14,10 @@ import org.glassfish.jersey.test.JerseyTest; import org.junit.Test; + +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.Response; + import static org.junit.Assert.assertEquals; /** @@ -31,4 +35,17 @@ public void testHello() throws InterruptedException { String response = target().path(App.ROOT_HELLO_PATH).path("James").request().get(String.class); assertEquals("Hello James", response); } + + @Test + public void testHelloValid() throws InterruptedException { + String response = target().path(App.ROOT_HELLO_PATH).request().post(Entity.text("James")).readEntity(String.class); + assertEquals("Hello James", response); + } + + @Test + public void testHelloInvalid() throws InterruptedException { + Response response = target().path(App.ROOT_HELLO_PATH).request().post(Entity.text("")); + + assertEquals(400, response.getStatus()); + } }