Skip to content

Commit 4305efd

Browse files
committed
Merge pull request #142 from dagguh/master
Arquillian test for resource-validation
2 parents f61f065 + 318aff5 commit 4305efd

File tree

2 files changed

+178
-94
lines changed

2 files changed

+178
-94
lines changed
Lines changed: 95 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,95 @@
1-
/*
2-
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3-
*
4-
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5-
*
6-
* The contents of this file are subject to the terms of either the GNU
7-
* General Public License Version 2 only ("GPL") or the Common Development
8-
* and Distribution License("CDDL") (collectively, the "License"). You
9-
* may not use this file except in compliance with the License. You can
10-
* obtain a copy of the License at
11-
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12-
* or packager/legal/LICENSE.txt. See the License for the specific
13-
* language governing permissions and limitations under the License.
14-
*
15-
* When distributing the software, include this License Header Notice in each
16-
* file and include the License file at packager/legal/LICENSE.txt.
17-
*
18-
* GPL Classpath Exception:
19-
* Oracle designates this particular file as subject to the "Classpath"
20-
* exception as provided by Oracle in the GPL Version 2 section of the License
21-
* file that accompanied this code.
22-
*
23-
* Modifications:
24-
* If applicable, add the following below the License Header, with the fields
25-
* enclosed by brackets [] replaced by your own identifying information:
26-
* "Portions Copyright [year] [name of copyright owner]"
27-
*
28-
* Contributor(s):
29-
* If you wish your version of this file to be governed by only the CDDL or
30-
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31-
* elects to include this software in this distribution under the [CDDL or GPL
32-
* Version 2] license." If you don't indicate a single choice of license, a
33-
* recipient has the option to distribute your version of this file under
34-
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35-
* its licensees as provided above. However, if you add GPL Version 2 code
36-
* and therefore, elected the GPL Version 2 license, then the option applies
37-
* only if the new code is made subject to such option by the copyright
38-
* holder.
39-
*/
40-
package org.javaee7.jaxrs.resource.validation;
41-
42-
import javax.validation.constraints.NotNull;
43-
import javax.validation.constraints.Size;
44-
import javax.xml.bind.annotation.XmlRootElement;
45-
46-
/**
47-
* @author Arun Gupta
48-
*/
49-
@XmlRootElement
50-
public class Name {
51-
52-
@NotNull
53-
@Size(min = 1)
54-
private String firstName;
55-
56-
@NotNull
57-
@Size(min = 1)
58-
private String lastName;
59-
60-
@Email
61-
private String email;
62-
63-
public Name() { }
64-
65-
public Name(String firstName, String lastName, String email) {
66-
this.firstName = firstName;
67-
this.lastName = lastName;
68-
this.email = email;
69-
}
70-
71-
public String getFirstName() {
72-
return firstName;
73-
}
74-
75-
public void setFirstName(String firstName) {
76-
this.firstName = firstName;
77-
}
78-
79-
public String getLastName() {
80-
return lastName;
81-
}
82-
83-
public void setLastName(String lastName) {
84-
this.lastName = lastName;
85-
}
86-
87-
public String getEmail() {
88-
return email;
89-
}
90-
91-
public void setEmail(String email) {
92-
this.email = email;
93-
}
94-
}
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.jaxrs.resource.validation;
41+
42+
import javax.validation.constraints.NotNull;
43+
import javax.validation.constraints.Size;
44+
import javax.xml.bind.annotation.XmlRootElement;
45+
46+
/**
47+
* @author Arun Gupta
48+
*/
49+
@XmlRootElement
50+
public class Name {
51+
52+
@NotNull
53+
@Size(min = 1)
54+
private String firstName;
55+
56+
@NotNull
57+
@Size(min = 1)
58+
private String lastName;
59+
60+
@Email
61+
private String email;
62+
63+
public Name() {
64+
}
65+
66+
public Name(String firstName, String lastName, String email) {
67+
this.firstName = firstName;
68+
this.lastName = lastName;
69+
this.email = email;
70+
}
71+
72+
public String getFirstName() {
73+
return firstName;
74+
}
75+
76+
public void setFirstName(String firstName) {
77+
this.firstName = firstName;
78+
}
79+
80+
public String getLastName() {
81+
return lastName;
82+
}
83+
84+
public void setLastName(String lastName) {
85+
this.lastName = lastName;
86+
}
87+
88+
public String getEmail() {
89+
return email;
90+
}
91+
92+
public void setEmail(String email) {
93+
this.email = email;
94+
}
95+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package org.javaee7.jaxrs.resource.validation;
2+
3+
import java.net.URL;
4+
import javax.json.Json;
5+
import javax.json.JsonObject;
6+
import javax.ws.rs.client.Client;
7+
import javax.ws.rs.client.ClientBuilder;
8+
import javax.ws.rs.client.Entity;
9+
import javax.ws.rs.client.WebTarget;
10+
import javax.ws.rs.core.Response;
11+
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;
24+
25+
@RunWith(Arquillian.class)
26+
public class NameAddResourceTest {
27+
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+
}
82+
83+
}

0 commit comments

Comments
 (0)