33import com .baeldung .client .ServicesInterface ;
44import com .baeldung .model .Movie ;
55import org .apache .commons .io .IOUtils ;
6+ import org .apache .http .impl .client .CloseableHttpClient ;
7+ import org .apache .http .impl .client .HttpClients ;
8+ import org .apache .http .impl .conn .PoolingHttpClientConnectionManager ;
69import org .codehaus .jackson .map .DeserializationConfig ;
710import org .codehaus .jackson .map .ObjectMapper ;
811import org .jboss .resteasy .client .jaxrs .ResteasyClient ;
912import org .jboss .resteasy .client .jaxrs .ResteasyClientBuilder ;
1013import org .jboss .resteasy .client .jaxrs .ResteasyWebTarget ;
14+ import org .jboss .resteasy .client .jaxrs .engines .ApacheHttpClient4Engine ;
1115import org .junit .Before ;
1216import org .junit .Test ;
1317import javax .naming .NamingException ;
2125
2226public class RestEasyClientTest {
2327
28+ public static final UriBuilder FULL_PATH = UriBuilder .fromPath ("http://127.0.0.1:8080/RestEasyTutorial/rest" );
2429 Movie transformerMovie = null ;
2530 Movie batmanMovie = null ;
2631 ObjectMapper jsonMapper = null ;
@@ -54,7 +59,7 @@ public void setup() throws ClassNotFoundException, IllegalAccessException, Insta
5459 public void testListAllMovies () {
5560
5661 ResteasyClient client = new ResteasyClientBuilder ().build ();
57- ResteasyWebTarget target = client .target (UriBuilder . fromPath ( "http://127.0.0.1:8080/RestEasyTutorial/rest" ) );
62+ ResteasyWebTarget target = client .target (FULL_PATH );
5863 ServicesInterface simple = target .proxy (ServicesInterface .class );
5964
6065 Response moviesResponse = simple .addMovie (transformerMovie );
@@ -72,7 +77,7 @@ public void testMovieByImdbId() {
7277 String transformerImdbId = "tt0418279" ;
7378
7479 ResteasyClient client = new ResteasyClientBuilder ().build ();
75- ResteasyWebTarget target = client .target (UriBuilder . fromPath ( "http://127.0.0.1:8080/RestEasyTutorial/rest" ) );
80+ ResteasyWebTarget target = client .target (FULL_PATH );
7681 ServicesInterface simple = target .proxy (ServicesInterface .class );
7782
7883 Response moviesResponse = simple .addMovie (transformerMovie );
@@ -86,7 +91,7 @@ public void testMovieByImdbId() {
8691 public void testAddMovie () {
8792
8893 ResteasyClient client = new ResteasyClientBuilder ().build ();
89- ResteasyWebTarget target = client .target (UriBuilder . fromPath ( "http://127.0.0.1:8080/RestEasyTutorial/rest" ) );
94+ ResteasyWebTarget target = client .target (FULL_PATH );
9095 ServicesInterface simple = target .proxy (ServicesInterface .class );
9196
9297 Response moviesResponse = simple .addMovie (batmanMovie );
@@ -98,14 +103,44 @@ public void testAddMovie() {
98103 }
99104
100105 moviesResponse .close ();
101- System .out .println ("Response Code: " + Response .Status .OK .getStatusCode ());
106+ System .out .println ("Response Code: " + moviesResponse .getStatus ());
107+ }
108+
109+ @ Test
110+ public void testAddMovieMultiConnection () {
111+
112+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager ();
113+ CloseableHttpClient httpClient = HttpClients .custom ()
114+ .setConnectionManager (cm )
115+ .build ();
116+ ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine (httpClient );
117+ ResteasyClient client = new ResteasyClientBuilder ().httpEngine (engine ).build ();
118+ ResteasyWebTarget target = client .target (FULL_PATH );
119+ ServicesInterface simple = target .proxy (ServicesInterface .class );
120+
121+ Response batmanResponse = simple .addMovie (batmanMovie );
122+ Response transformerResponse = simple .addMovie (transformerMovie );
123+
124+ if (batmanResponse .getStatus () != Response .Status .CREATED .getStatusCode ()) {
125+ System .out .println ("Batman Movie creation Failed : HTTP error code : " + batmanResponse .getStatus ());
126+ }
127+ if (batmanResponse .getStatus () != Response .Status .CREATED .getStatusCode ()) {
128+ System .out .println ("Batman Movie creation Failed : HTTP error code : " + batmanResponse .getStatus ());
129+ }
130+
131+ batmanResponse .close ();
132+ transformerResponse .close ();
133+ cm .close ();
134+
135+
136+
102137 }
103138
104139 @ Test
105140 public void testDeleteMovie () {
106141
107142 ResteasyClient client = new ResteasyClientBuilder ().build ();
108- ResteasyWebTarget target = client .target (UriBuilder . fromPath ( "http://127.0.0.1:8080/RestEasyTutorial/rest" ) );
143+ ResteasyWebTarget target = client .target (FULL_PATH );
109144 ServicesInterface simple = target .proxy (ServicesInterface .class );
110145
111146 Response moviesResponse = simple .addMovie (batmanMovie );
@@ -118,14 +153,14 @@ public void testDeleteMovie() {
118153 }
119154
120155 moviesResponse .close ();
121- System .out .println ("Response Code: " + Response . Status . OK . getStatusCode ());
156+ System .out .println ("Response Code: " + moviesResponse . getStatus ());
122157 }
123158
124159 @ Test
125160 public void testUpdateMovie () {
126161
127162 ResteasyClient client = new ResteasyClientBuilder ().build ();
128- ResteasyWebTarget target = client .target (UriBuilder . fromPath ( "http://127.0.0.1:8080/RestEasyTutorial/rest" ) );
163+ ResteasyWebTarget target = client .target (FULL_PATH );
129164 ServicesInterface simple = target .proxy (ServicesInterface .class );
130165
131166 Response moviesResponse = simple .addMovie (batmanMovie );
@@ -138,7 +173,7 @@ public void testUpdateMovie() {
138173 }
139174
140175 moviesResponse .close ();
141- System .out .println ("Response Code: " + Response . Status . OK . getStatusCode ());
176+ System .out .println ("Response Code: " + moviesResponse . getStatus ());
142177 }
143178
144179}
0 commit comments