Skip to content

Commit 7d689a2

Browse files
committed
Test case for successful Name valdiation.
Test case for failed Name validation, because of empty firstName.
1 parent f58ae20 commit 7d689a2

File tree

2 files changed

+123
-101
lines changed

2 files changed

+123
-101
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+
}

jaxrs/resource-validation/src/test/java/org/javaee7/jaxrs/resource/validation/NameAddResourceTest.java

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.javaee7.jaxrs.resource.validation;
22

33
import java.net.URL;
4+
import javax.json.Json;
5+
import javax.json.JsonObject;
46
import javax.ws.rs.client.Client;
57
import javax.ws.rs.client.ClientBuilder;
68
import javax.ws.rs.client.Entity;
@@ -21,30 +23,49 @@
2123
@RunWith(Arquillian.class)
2224
public class NameAddResourceTest {
2325

24-
@Deployment
26+
@Deployment(testable = false)
2527
public static WebArchive createDeployment() {
2628
return ShrinkWrap.create(WebArchive.class)
27-
.addClasses(MyApplication.class, NameAddResource.class, Name.class, Email.class, JsonObjectProvider.class, JsonStructureProvider.class);
29+
.addClasses(MyApplication.class, NameAddResource.class, Name.class, Email.class, EmailValidator.class);
2830
}
2931
@ArquillianResource
3032
private URL base;
3133

3234
@Test
3335
public void shouldPassNameValidation() throws Exception {
34-
if (true) {
35-
36-
return;
37-
}
3836
Client client = ClientBuilder.newClient();
3937
client.register(JsonObjectProvider.class, JsonStructureProvider.class);
4038
WebTarget target = client
4139
.target(new URL(base, "webresources/nameadd").toExternalForm());
40+
JsonObject name = Json.createObjectBuilder()
41+
.add("firstName", "Sheldon")
42+
.add("lastName", "Cooper")
43+
.add("email", "random@example.com")
44+
.build();
4245

4346
Response r = target
4447
.request()
45-
.post(Entity.json(new Name("Sheldon", "Cooper", "sheldon@cooper.com")));
48+
.post(Entity.json(name));
4649

4750
assertEquals(r.getStatusInfo().getFamily(), Status.Family.SUCCESSFUL);
4851
}
52+
@Test
53+
public void shouldFailAtFirstNameValidation() throws Exception {
54+
Client client = ClientBuilder.newClient();
55+
client.register(JsonObjectProvider.class, JsonStructureProvider.class);
56+
WebTarget target = client
57+
.target(new URL(base, "webresources/nameadd").toExternalForm());
58+
JsonObject name = Json.createObjectBuilder()
59+
.add("firstName", "")
60+
.add("lastName", "Cooper")
61+
.add("email", "random@example.com")
62+
.build();
63+
64+
Response r = target
65+
.request()
66+
.post(Entity.json(name));
67+
68+
assertEquals(r.getStatusInfo().getFamily(), Status.Family.CLIENT_ERROR);
69+
}
4970

5071
}

0 commit comments

Comments
 (0)