Skip to content

Commit 0a4aa6c

Browse files
committed
Removed RESTEasy vendor lock.
Restored tab indentation coding style. Added test cases for failed last name size validation and at symbol email validation. Reduced duplication in test code. Made Name class immutable.
1 parent 318aff5 commit 0a4aa6c

File tree

4 files changed

+106
-82
lines changed

4 files changed

+106
-82
lines changed

jaxrs/resource-validation/src/main/java/org/javaee7/jaxrs/resource/validation/MyApplication.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
/**
4646
* @author Arun Gupta
4747
*/
48-
@ApplicationPath("webresources")
48+
@ApplicationPath(MyApplication.PATH)
4949
public class MyApplication extends Application {
50+
static final String PATH = "webresources";
5051
}

jaxrs/resource-validation/src/main/java/org/javaee7/jaxrs/resource/validation/Name.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,12 @@ public String getFirstName() {
7373
return firstName;
7474
}
7575

76-
public void setFirstName(String firstName) {
77-
this.firstName = firstName;
78-
}
79-
8076
public String getLastName() {
8177
return lastName;
8278
}
8379

84-
public void setLastName(String lastName) {
85-
this.lastName = lastName;
86-
}
87-
8880
public String getEmail() {
8981
return email;
9082
}
9183

92-
public void setEmail(String email) {
93-
this.email = email;
94-
}
9584
}

jaxrs/resource-validation/src/main/java/org/javaee7/jaxrs/resource/validation/NameAddResource.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@
4747
/**
4848
* @author Arun Gupta
4949
*/
50-
@Path("/nameadd")
50+
@Path(NameAddResource.PATH)
5151
public class NameAddResource {
5252

53+
static final String PATH = "/nameadd";
54+
5355
@POST
5456
@Consumes("application/json")
5557
public String addUser(@Valid Name name) {
56-
System.out.println("addUser");
5758
return name.getFirstName() + " " + name.getLastName() + " with email " + name.getEmail() + " added";
5859
}
59-
60+
6061
}
Lines changed: 100 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,116 @@
11
package org.javaee7.jaxrs.resource.validation;
22

3-
import java.net.URL;
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.arquillian.test.api.ArquillianResource;
6+
import org.jboss.shrinkwrap.api.ShrinkWrap;
7+
import org.jboss.shrinkwrap.api.spec.WebArchive;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.junit.runner.RunWith;
11+
412
import javax.json.Json;
513
import javax.json.JsonObject;
14+
import javax.json.JsonObjectBuilder;
615
import javax.ws.rs.client.Client;
716
import javax.ws.rs.client.ClientBuilder;
817
import javax.ws.rs.client.Entity;
918
import javax.ws.rs.client.WebTarget;
1019
import javax.ws.rs.core.Response;
1120
import javax.ws.rs.core.Response.Status;
12-
import static javax.ws.rs.core.Response.Status.Family.*;
13-
import org.jboss.arquillian.container.test.api.Deployment;
14-
import org.jboss.arquillian.junit.Arquillian;
15-
import org.jboss.arquillian.test.api.ArquillianResource;
16-
import org.jboss.resteasy.plugins.providers.jsonp.JsonObjectProvider;
17-
import org.jboss.resteasy.plugins.providers.jsonp.JsonStructureProvider;
18-
import org.jboss.shrinkwrap.api.ShrinkWrap;
19-
import org.jboss.shrinkwrap.api.spec.WebArchive;
20-
import org.junit.Test;
21-
import static org.junit.Assert.*;
22-
import org.junit.Before;
23-
import org.junit.runner.RunWith;
21+
import java.net.URI;
22+
import java.net.URL;
23+
24+
import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
25+
import static javax.ws.rs.core.Response.Status.OK;
26+
import static org.junit.Assert.assertEquals;
2427

2528
@RunWith(Arquillian.class)
2629
public class NameAddResourceTest {
2730

28-
@Deployment(testable = false)
29-
public static WebArchive createDeployment() {
30-
return ShrinkWrap.create(WebArchive.class)
31-
.addClasses(MyApplication.class, NameAddResource.class, Name.class, Email.class, EmailValidator.class);
32-
}
33-
@ArquillianResource
34-
private URL base;
35-
private WebTarget target;
36-
37-
@Before
38-
public void setUp() throws Exception {
39-
Client client = ClientBuilder.newClient();
40-
client.register(JsonObjectProvider.class, JsonStructureProvider.class);
41-
target = client.target(new URL(base, "webresources/nameadd").toExternalForm());
42-
43-
}
44-
45-
@Test
46-
public void shouldPassNameValidation() throws Exception {
47-
JsonObject name = Json.createObjectBuilder()
48-
.add("firstName", "Sheldon")
49-
.add("lastName", "Cooper")
50-
.add("email", "random@example.com")
51-
.build();
52-
Response response = postName(name);
53-
54-
assertFamily(response, SUCCESSFUL);
55-
}
56-
57-
private Response postName(JsonObject name) {
58-
return target
59-
.request()
60-
.post(Entity.json(name));
61-
62-
}
63-
64-
private void assertFamily(Response response, Status.Family family) {
65-
Response.StatusType statusInfo = response.getStatusInfo();
66-
Status.Family actualFamily = statusInfo.getFamily();
67-
assertEquals(actualFamily, family);
68-
}
69-
70-
@Test
71-
public void shouldFailAtFirstNameValidation() throws Exception {
72-
JsonObject name = Json.createObjectBuilder()
73-
.add("firstName", "")
74-
.add("lastName", "Cooper")
75-
.add("email", "random@example.com")
76-
.build();
77-
78-
Response response = postName(name);
79-
80-
assertFamily(response, CLIENT_ERROR);
81-
}
31+
@ArquillianResource
32+
private URL base;
33+
private WebTarget target;
34+
35+
@Deployment(testable = false)
36+
public static WebArchive createDeployment() {
37+
return ShrinkWrap.create(WebArchive.class)
38+
.addClasses(MyApplication.class, NameAddResource.class, Name.class, Email.class, EmailValidator.class);
39+
}
40+
41+
@Before
42+
public void setUp() throws Exception {
43+
Client client = ClientBuilder.newClient();
44+
String resourcePath = MyApplication.PATH + NameAddResource.PATH;
45+
URI resourceUri = new URL(base, resourcePath).toURI();
46+
target = client.target(resourceUri);
47+
}
48+
49+
@Test
50+
public void shouldPassNameValidation() throws Exception {
51+
JsonObject name = startValidName()
52+
.build();
53+
54+
Response response = postName(name);
55+
56+
assertStatus(response, OK);
57+
}
58+
59+
private JsonObjectBuilder startValidName() {
60+
return Json.createObjectBuilder()
61+
.add("firstName", "Sheldon")
62+
.add("lastName", "Cooper")
63+
.add("email", "random@example.com");
64+
}
65+
66+
private Response postName(JsonObject name) {
67+
Entity<JsonObject> nameEntity = Entity.json(name);
68+
return target
69+
.request()
70+
.post(nameEntity);
71+
72+
}
73+
74+
private void assertStatus(Response response, Status expectedStatus) {
75+
Response.StatusType actualStatus = response.getStatusInfo();
76+
assertEquals(actualStatus, expectedStatus);
77+
}
78+
79+
@Test
80+
public void shouldFailAtFirstNameSizeValidation() throws Exception {
81+
JsonObject name = startValidName()
82+
.add("firstName", "")
83+
.build();
84+
85+
Response response = postName(name);
86+
87+
assertFailedValidation(response);
88+
}
89+
90+
private void assertFailedValidation(Response response) {
91+
assertStatus(response, BAD_REQUEST);
92+
}
93+
94+
@Test
95+
public void shouldFailAtLastNameSizeValidation() throws Exception {
96+
JsonObject name = startValidName()
97+
.add("lastName", "")
98+
.build();
99+
100+
Response response = postName(name);
101+
102+
assertFailedValidation(response);
103+
}
104+
105+
@Test
106+
public void shouldFailAtEmailValidation() throws Exception {
107+
JsonObject name = startValidName()
108+
.add("email", "missing-at-symbol.com")
109+
.build();
110+
111+
Response response = postName(name);
112+
113+
assertFailedValidation(response);
114+
}
82115

83116
}

0 commit comments

Comments
 (0)