11package com .baeldung .server ;
22
3- import com .baeldung .client .ServicesInterface ;
4- import com .baeldung .model .Movie ;
3+ import java .io .InputStream ;
4+ import java .nio .charset .StandardCharsets ;
5+ import java .text .SimpleDateFormat ;
6+ import java .util .List ;
7+ import java .util .Locale ;
8+
9+ import javax .naming .NamingException ;
10+ import javax .ws .rs .core .Response ;
11+ import javax .ws .rs .core .UriBuilder ;
12+
513import org .apache .commons .io .IOUtils ;
614import org .apache .http .impl .client .CloseableHttpClient ;
715import org .apache .http .impl .client .HttpClients ;
1422import org .jboss .resteasy .client .jaxrs .engines .ApacheHttpClient4Engine ;
1523import org .junit .Before ;
1624import org .junit .Test ;
17- import javax .naming .NamingException ;
18- import javax .ws .rs .core .Response ;
19- import javax .ws .rs .core .UriBuilder ;
20- import java .io .InputStream ;
21- import java .nio .charset .StandardCharsets ;
22- import java .text .SimpleDateFormat ;
23- import java .util .List ;
24- import java .util .Locale ;
2525
26- public class RestEasyClientTest {
26+ import com .baeldung .client .ServicesInterface ;
27+ import com .baeldung .model .Movie ;
28+
29+ public class RestEasyClientLiveTest {
2730
28- public static final UriBuilder FULL_PATH = UriBuilder .fromPath ("http://127.0.0.1:8080 /RestEasyTutorial/rest" );
31+ public static final UriBuilder FULL_PATH = UriBuilder .fromPath ("http://127.0.0.1:8082 /RestEasyTutorial/rest" );
2932 Movie transformerMovie = null ;
3033 Movie batmanMovie = null ;
3134 ObjectMapper jsonMapper = null ;
@@ -35,64 +38,64 @@ public void setup() throws ClassNotFoundException, IllegalAccessException, Insta
3538
3639 jsonMapper = new ObjectMapper ().configure (DeserializationConfig .Feature .FAIL_ON_UNKNOWN_PROPERTIES , false );
3740 jsonMapper .configure (DeserializationConfig .Feature .ACCEPT_SINGLE_VALUE_AS_ARRAY , true );
38- SimpleDateFormat sdf = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz" , Locale .ENGLISH );
41+ final SimpleDateFormat sdf = new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz" , Locale .ENGLISH );
3942 jsonMapper .setDateFormat (sdf );
4043
41- try (InputStream inputStream = new RestEasyClientTest ().getClass ().getResourceAsStream ("./movies/transformer.json" )) {
42- String transformerMovieAsString = String .format (IOUtils .toString (inputStream , StandardCharsets .UTF_8 ));
44+ try (InputStream inputStream = new RestEasyClientLiveTest ().getClass ().getResourceAsStream ("./movies/transformer.json" )) {
45+ final String transformerMovieAsString = String .format (IOUtils .toString (inputStream , StandardCharsets .UTF_8 ));
4346 transformerMovie = jsonMapper .readValue (transformerMovieAsString , Movie .class );
44- } catch (Exception e ) {
47+ } catch (final Exception e ) {
4548 e .printStackTrace ();
4649 throw new RuntimeException ("Test is going to die ..." , e );
4750 }
4851
49- try (InputStream inputStream = new RestEasyClientTest ().getClass ().getResourceAsStream ("./movies/batman.json" )) {
50- String batmanMovieAsString = String .format (IOUtils .toString (inputStream , StandardCharsets .UTF_8 ));
52+ try (InputStream inputStream = new RestEasyClientLiveTest ().getClass ().getResourceAsStream ("./movies/batman.json" )) {
53+ final String batmanMovieAsString = String .format (IOUtils .toString (inputStream , StandardCharsets .UTF_8 ));
5154 batmanMovie = jsonMapper .readValue (batmanMovieAsString , Movie .class );
5255
53- } catch (Exception e ) {
56+ } catch (final Exception e ) {
5457 throw new RuntimeException ("Test is going to die ..." , e );
5558 }
5659 }
5760
5861 @ Test
5962 public void testListAllMovies () {
6063
61- ResteasyClient client = new ResteasyClientBuilder ().build ();
62- ResteasyWebTarget target = client .target (FULL_PATH );
63- ServicesInterface proxy = target .proxy (ServicesInterface .class );
64+ final ResteasyClient client = new ResteasyClientBuilder ().build ();
65+ final ResteasyWebTarget target = client .target (FULL_PATH );
66+ final ServicesInterface proxy = target .proxy (ServicesInterface .class );
6467
6568 Response moviesResponse = proxy .addMovie (transformerMovie );
6669 moviesResponse .close ();
6770 moviesResponse = proxy .addMovie (batmanMovie );
6871 moviesResponse .close ();
6972
70- List <Movie > movies = proxy .listMovies ();
73+ final List <Movie > movies = proxy .listMovies ();
7174 System .out .println (movies );
7275 }
7376
7477 @ Test
7578 public void testMovieByImdbId () {
7679
77- String transformerImdbId = "tt0418279" ;
80+ final String transformerImdbId = "tt0418279" ;
7881
79- ResteasyClient client = new ResteasyClientBuilder ().build ();
80- ResteasyWebTarget target = client .target (FULL_PATH );
81- ServicesInterface proxy = target .proxy (ServicesInterface .class );
82+ final ResteasyClient client = new ResteasyClientBuilder ().build ();
83+ final ResteasyWebTarget target = client .target (FULL_PATH );
84+ final ServicesInterface proxy = target .proxy (ServicesInterface .class );
8285
83- Response moviesResponse = proxy .addMovie (transformerMovie );
86+ final Response moviesResponse = proxy .addMovie (transformerMovie );
8487 moviesResponse .close ();
8588
86- Movie movies = proxy .movieByImdbId (transformerImdbId );
89+ final Movie movies = proxy .movieByImdbId (transformerImdbId );
8790 System .out .println (movies );
8891 }
8992
9093 @ Test
9194 public void testAddMovie () {
9295
93- ResteasyClient client = new ResteasyClientBuilder ().build ();
94- ResteasyWebTarget target = client .target (FULL_PATH );
95- ServicesInterface proxy = target .proxy (ServicesInterface .class );
96+ final ResteasyClient client = new ResteasyClientBuilder ().build ();
97+ final ResteasyWebTarget target = client .target (FULL_PATH );
98+ final ServicesInterface proxy = target .proxy (ServicesInterface .class );
9699
97100 Response moviesResponse = proxy .addMovie (batmanMovie );
98101 moviesResponse .close ();
@@ -109,17 +112,15 @@ public void testAddMovie() {
109112 @ Test
110113 public void testAddMovieMultiConnection () {
111114
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 proxy = target .proxy (ServicesInterface .class );
115+ final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager ();
116+ final CloseableHttpClient httpClient = HttpClients .custom ().setConnectionManager (cm ).build ();
117+ final ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine (httpClient );
118+ final ResteasyClient client = new ResteasyClientBuilder ().httpEngine (engine ).build ();
119+ final ResteasyWebTarget target = client .target (FULL_PATH );
120+ final ServicesInterface proxy = target .proxy (ServicesInterface .class );
120121
121- Response batmanResponse = proxy .addMovie (batmanMovie );
122- Response transformerResponse = proxy .addMovie (transformerMovie );
122+ final Response batmanResponse = proxy .addMovie (batmanMovie );
123+ final Response transformerResponse = proxy .addMovie (transformerMovie );
123124
124125 if (batmanResponse .getStatus () != Response .Status .CREATED .getStatusCode ()) {
125126 System .out .println ("Batman Movie creation Failed : HTTP error code : " + batmanResponse .getStatus ());
@@ -132,16 +133,14 @@ public void testAddMovieMultiConnection() {
132133 transformerResponse .close ();
133134 cm .close ();
134135
135-
136-
137136 }
138137
139138 @ Test
140139 public void testDeleteMovie () {
141140
142- ResteasyClient client = new ResteasyClientBuilder ().build ();
143- ResteasyWebTarget target = client .target (FULL_PATH );
144- ServicesInterface proxy = target .proxy (ServicesInterface .class );
141+ final ResteasyClient client = new ResteasyClientBuilder ().build ();
142+ final ResteasyWebTarget target = client .target (FULL_PATH );
143+ final ServicesInterface proxy = target .proxy (ServicesInterface .class );
145144
146145 Response moviesResponse = proxy .addMovie (batmanMovie );
147146 moviesResponse .close ();
@@ -159,9 +158,9 @@ public void testDeleteMovie() {
159158 @ Test
160159 public void testUpdateMovie () {
161160
162- ResteasyClient client = new ResteasyClientBuilder ().build ();
163- ResteasyWebTarget target = client .target (FULL_PATH );
164- ServicesInterface proxy = target .proxy (ServicesInterface .class );
161+ final ResteasyClient client = new ResteasyClientBuilder ().build ();
162+ final ResteasyWebTarget target = client .target (FULL_PATH );
163+ final ServicesInterface proxy = target .proxy (ServicesInterface .class );
165164
166165 Response moviesResponse = proxy .addMovie (batmanMovie );
167166 moviesResponse .close ();
0 commit comments