99import javax .ws .rs .client .WebTarget ;
1010import javax .ws .rs .core .Response ;
1111import javax .ws .rs .core .Response .Status ;
12+ import static javax .ws .rs .core .Response .Status .Family .*;
1213import org .jboss .arquillian .container .test .api .Deployment ;
1314import org .jboss .arquillian .junit .Arquillian ;
1415import org .jboss .arquillian .test .api .ArquillianResource ;
1819import org .jboss .shrinkwrap .api .spec .WebArchive ;
1920import org .junit .Test ;
2021import static org .junit .Assert .*;
22+ import org .junit .Before ;
2123import org .junit .runner .RunWith ;
2224
2325@ RunWith (Arquillian .class )
@@ -30,42 +32,52 @@ public static WebArchive createDeployment() {
3032 }
3133 @ ArquillianResource
3234 private URL base ;
35+ private WebTarget target ;
3336
34- @ Test
35- public void shouldPassNameValidation () throws Exception {
37+ @ Before
38+ public void setUp () throws Exception {
3639 Client client = ClientBuilder .newClient ();
3740 client .register (JsonObjectProvider .class , JsonStructureProvider .class );
38- WebTarget target = client
39- .target (new URL (base , "webresources/nameadd" ).toExternalForm ());
41+ target = client .target (new URL (base , "webresources/nameadd" ).toExternalForm ());
42+
43+ }
44+
45+ @ Test
46+ public void shouldPassNameValidation () throws Exception {
4047 JsonObject name = Json .createObjectBuilder ()
4148 .add ("firstName" , "Sheldon" )
4249 .add ("lastName" , "Cooper" )
4350 .add ("email" , "random@example.com" )
4451 .build ();
52+ Response response = postName (name );
53+
54+ assertFamily (response , SUCCESSFUL );
55+ }
4556
46- Response r = target
57+ private Response postName (JsonObject name ) {
58+ return target
4759 .request ()
4860 .post (Entity .json (name ));
4961
50- assertEquals (r .getStatusInfo ().getFamily (), Status .Family .SUCCESSFUL );
5162 }
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+
5270 @ Test
5371 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 ());
5872 JsonObject name = Json .createObjectBuilder ()
5973 .add ("firstName" , "" )
6074 .add ("lastName" , "Cooper" )
6175 .add ("email" , "random@example.com" )
6276 .build ();
6377
64- Response r = target
65- .request ()
66- .post (Entity .json (name ));
78+ Response response = postName (name );
6779
68- assertEquals ( r . getStatusInfo (). getFamily (), Status . Family . CLIENT_ERROR );
80+ assertFamily ( response , CLIENT_ERROR );
6981 }
7082
7183}
0 commit comments