1- /*
2- * To change this license header, choose License Headers in Project Properties.
3- * To change this template file, choose Tools | Templates
4- * and open the template in the editor.
5- */
61package org .javaee7 .jaxrs .jsonp ;
72
83import java .net .MalformedURLException ;
94import java .net .URI ;
105import java .net .URL ;
116
127import javax .json .Json ;
8+ import javax .json .JsonArray ;
139import javax .json .JsonObject ;
1410import javax .ws .rs .client .Client ;
1511import javax .ws .rs .client .ClientBuilder ;
2319import org .jboss .shrinkwrap .api .ShrinkWrap ;
2420import org .jboss .shrinkwrap .api .spec .WebArchive ;
2521import org .junit .After ;
22+ import static org .junit .Assert .assertEquals ;
23+ import static org .junit .Assert .assertFalse ;
24+ import static org .junit .Assert .assertNotNull ;
25+ import static org .junit .Assert .assertTrue ;
2626import org .junit .Before ;
2727import org .junit .Test ;
2828import org .junit .runner .RunWith ;
3434public class MyResourceTest {
3535
3636 Client client ;
37- WebTarget target ;
37+ WebTarget targetObject ;
38+ WebTarget targetArray ;
3839
3940 @ ArquillianResource
4041 URL base ;
4142
4243 @ Before
4344 public void setUp () throws MalformedURLException {
4445 client = ClientBuilder .newClient ();
45- target = client .target (URI .create (new URL (base , "webresources/endpoint" ).toExternalForm ()));
46+ targetObject = client .target (URI .create (new URL (base , "webresources/object" ).toExternalForm ()));
47+ targetArray = client .target (URI .create (new URL (base , "webresources/array" ).toExternalForm ()));
4648 }
4749
4850 @ After
@@ -55,11 +57,12 @@ public static WebArchive createDeployment() {
5557 return ShrinkWrap .create (WebArchive .class )
5658 .addClasses (
5759 MyApplication .class ,
58- MyResource .class );
60+ MyObjectResource .class ,
61+ MyArrayResource .class );
5962 }
6063
6164 /**
62- * Test of echoObject method, of class MyResource .
65+ * Test of echoObject method, of class MyObjectResource .
6366 */
6467 @ Test
6568 public void testEchoObject () {
@@ -68,10 +71,35 @@ public void testEchoObject() {
6871 .add ("banana" , "yellow" )
6972 .build ();
7073
71- JsonObject response = target
74+ JsonObject json = targetObject
7275 .request ()
7376 .post (Entity .entity (jsonObject , MediaType .APPLICATION_JSON ), JsonObject .class );
74- System .out .println (response );
77+ assertNotNull (json );
78+ assertFalse (json .isEmpty ());
79+ assertTrue (json .containsKey ("apple" ));
80+ assertEquals ("red" , json .getString ("apple" ));
81+ assertTrue (json .containsKey ("banana" ));
82+ assertEquals ("yellow" , json .getString ("banana" ));
83+ }
84+
85+ @ Test
86+ public void testEchoArray () {
87+ JsonArray jsonArray = Json .createArrayBuilder ()
88+ .add (Json .createObjectBuilder ()
89+ .add ("apple" , "red" ))
90+ .add (Json .createObjectBuilder ()
91+ .add ("banana" , "yellow" ))
92+ .build ();
93+
94+ JsonArray json = targetArray
95+ .request ()
96+ .post (Entity .entity (jsonArray , MediaType .APPLICATION_JSON ), JsonArray .class );
97+ assertNotNull (json );
98+ assertEquals (2 , json .size ());
99+ assertTrue (json .getJsonObject (0 ).containsKey ("apple" ));
100+ assertEquals ("red" , json .getJsonObject (0 ).getString ("apple" ));
101+ assertTrue (json .getJsonObject (1 ).containsKey ("banana" ));
102+ assertEquals ("yellow" , json .getJsonObject (1 ).getString ("banana" ));
75103 }
76104
77105}
0 commit comments