|
1 | 1 | package org.javaee7.jaxrs.resource.validation; |
2 | 2 |
|
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 | + |
4 | 12 | import javax.json.Json; |
5 | 13 | import javax.json.JsonObject; |
| 14 | +import javax.json.JsonObjectBuilder; |
6 | 15 | import javax.ws.rs.client.Client; |
7 | 16 | import javax.ws.rs.client.ClientBuilder; |
8 | 17 | import javax.ws.rs.client.Entity; |
9 | 18 | import javax.ws.rs.client.WebTarget; |
10 | 19 | import javax.ws.rs.core.Response; |
11 | 20 | 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; |
24 | 27 |
|
25 | 28 | @RunWith(Arquillian.class) |
26 | 29 | public class NameAddResourceTest { |
27 | 30 |
|
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 | + } |
82 | 115 |
|
83 | 116 | } |
0 commit comments