Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
/**
* @author Arun Gupta
*/
@ApplicationPath("webresources")
@ApplicationPath(MyApplication.PATH)
public class MyApplication extends Application {
static final String PATH = "webresources";
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,12 @@ public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@
/**
* @author Arun Gupta
*/
@Path("/nameadd")
@Path(NameAddResource.PATH)
public class NameAddResource {

static final String PATH = "/nameadd";

@POST
@Consumes("application/json")
public String addUser(@Valid Name name) {
System.out.println("addUser");
return name.getFirstName() + " " + name.getLastName() + " with email " + name.getEmail() + " added";
}

}
Original file line number Diff line number Diff line change
@@ -1,83 +1,148 @@
package org.javaee7.jaxrs.resource.validation;

import java.net.URL;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import static javax.ws.rs.core.Response.Status.Family.*;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.resteasy.plugins.providers.jsonp.JsonObjectProvider;
import org.jboss.resteasy.plugins.providers.jsonp.JsonStructureProvider;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import java.net.URI;
import java.net.URL;

import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
import static javax.ws.rs.core.Response.Status.OK;
import static org.junit.Assert.assertEquals;

@RunWith(Arquillian.class)
public class NameAddResourceTest {

@Deployment(testable = false)
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(MyApplication.class, NameAddResource.class, Name.class, Email.class, EmailValidator.class);
}
@ArquillianResource
private URL base;
private WebTarget target;

@Before
public void setUp() throws Exception {
Client client = ClientBuilder.newClient();
client.register(JsonObjectProvider.class, JsonStructureProvider.class);
target = client.target(new URL(base, "webresources/nameadd").toExternalForm());

}

@Test
public void shouldPassNameValidation() throws Exception {
JsonObject name = Json.createObjectBuilder()
.add("firstName", "Sheldon")
.add("lastName", "Cooper")
.add("email", "random@example.com")
.build();
Response response = postName(name);

assertFamily(response, SUCCESSFUL);
}

private Response postName(JsonObject name) {
return target
.request()
.post(Entity.json(name));

}

private void assertFamily(Response response, Status.Family family) {
Response.StatusType statusInfo = response.getStatusInfo();
Status.Family actualFamily = statusInfo.getFamily();
assertEquals(actualFamily, family);
}

@Test
public void shouldFailAtFirstNameValidation() throws Exception {
JsonObject name = Json.createObjectBuilder()
.add("firstName", "")
.add("lastName", "Cooper")
.add("email", "random@example.com")
.build();

Response response = postName(name);

assertFamily(response, CLIENT_ERROR);
}
@ArquillianResource
private URL base;
private WebTarget target;

@Deployment(testable = false)
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(MyApplication.class, NameAddResource.class, Name.class, Email.class, EmailValidator.class);
}

@Before
public void setUp() throws Exception {
Client client = ClientBuilder.newClient();
String resourcePath = MyApplication.PATH + NameAddResource.PATH;
URI resourceUri = new URL(base, resourcePath).toURI();
target = client.target(resourceUri);
}

@Test
public void shouldPassNameValidation() throws Exception {
JsonObject name = startValidName()
.build();

Response response = postName(name);

assertStatus(response, OK);
}

private JsonObjectBuilder startValidName() {
return Json.createObjectBuilder()
.add("firstName", "Sheldon")
.add("lastName", "Cooper")
.add("email", "random@example.com");
}

private Response postName(JsonObject name) {
Entity<JsonObject> nameEntity = Entity.json(name);
return target
.request()
.post(nameEntity);
}

private void assertStatus(Response response, Status expectedStatus) {
Response.StatusType actualStatus = response.getStatusInfo();
assertEquals(actualStatus, expectedStatus);
}

@Test
public void shouldFailAtFirstNameSizeValidation() throws Exception {
JsonObject name = startValidName()
.add("firstName", "")
.build();

Response response = postName(name);

assertFailedValidation(response);
}

private void assertFailedValidation(Response response) {
assertStatus(response, BAD_REQUEST);
}

@Test
public void shouldFailAtFirstNameNullValidation() throws Exception {
JsonObject name = startValidName()
.addNull("firstName")
.build();

Response response = postName(name);

assertFailedValidation(response);
}

@Test
public void shouldFailAtLastNameSizeValidation() throws Exception {
JsonObject name = startValidName()
.add("lastName", "")
.build();

Response response = postName(name);

assertFailedValidation(response);
}

@Test
public void shouldFailAtLastNameNullValidation() throws Exception {
JsonObject name = startValidName()
.addNull("lastName")
.build();

Response response = postName(name);

assertFailedValidation(response);
}

@Test
public void shouldFailAtEmailAtSymbolValidation() throws Exception {
JsonObject name = startValidName()
.add("email", "missing-at-symbol.com")
.build();

Response response = postName(name);

assertFailedValidation(response);
}

@Test
public void shouldFailAtEmailComDomainValidation() throws Exception {
JsonObject name = startValidName()
.add("email", "other-than-com@domain.pl")
.build();

Response response = postName(name);

assertFailedValidation(response);
}

}