Skip to content

Commit

Permalink
eclipse-ee4j#3753: added validation constraint to helloworld-cdi2-se
Browse files Browse the repository at this point in the history
Signed-off-by: pappy <pappy.stanescu@gmail.com>
  • Loading branch information
pa314159 committed Aug 28, 2019
1 parent 24f2d81 commit 02cfcb5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

/**
Expand All @@ -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());
}
}

0 comments on commit 02cfcb5

Please sign in to comment.