11package com .baeldung .cxf .jaxrs .implementation ;
22
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .io .IOException ;
6+ import java .io .InputStream ;
7+ import java .io .InputStreamReader ;
8+ import java .net .URL ;
9+
10+ import javax .xml .bind .JAXB ;
11+
312import org .apache .http .HttpResponse ;
413import org .apache .http .client .methods .HttpDelete ;
514import org .apache .http .client .methods .HttpPost ;
1120import org .junit .BeforeClass ;
1221import org .junit .Test ;
1322
14- import javax .xml .bind .JAXB ;
15- import java .io .IOException ;
16- import java .io .InputStream ;
17- import java .io .InputStreamReader ;
18- import java .net .URL ;
19-
20- import static org .junit .Assert .assertEquals ;
21-
22- public class ServiceTest {
23+ public class ServiceLiveTest {
2324 private static final String BASE_URL = "http://localhost:8080/baeldung/courses/" ;
2425 private static CloseableHttpClient client ;
25-
26+
2627 @ BeforeClass
2728 public static void createClient () {
2829 client = HttpClients .createDefault ();
2930 }
30-
31+
3132 @ AfterClass
3233 public static void closeClient () throws IOException {
3334 client .close ();
3435 }
35-
36+
3637 @ Test
3738 public void whenUpdateNonExistentCourse_thenReceiveNotFoundResponse () throws IOException {
38- HttpPut httpPut = new HttpPut (BASE_URL + "3" );
39- InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("non_existent_course.xml" );
39+ final HttpPut httpPut = new HttpPut (BASE_URL + "3" );
40+ final InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("non_existent_course.xml" );
4041 httpPut .setEntity (new InputStreamEntity (resourceStream ));
4142 httpPut .setHeader ("Content-Type" , "text/xml" );
42-
43- HttpResponse response = client .execute (httpPut );
43+
44+ final HttpResponse response = client .execute (httpPut );
4445 assertEquals (404 , response .getStatusLine ().getStatusCode ());
4546 }
46-
47+
4748 @ Test
4849 public void whenUpdateUnchangedCourse_thenReceiveNotModifiedResponse () throws IOException {
49- HttpPut httpPut = new HttpPut (BASE_URL + "1" );
50- InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("unchanged_course.xml" );
50+ final HttpPut httpPut = new HttpPut (BASE_URL + "1" );
51+ final InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("unchanged_course.xml" );
5152 httpPut .setEntity (new InputStreamEntity (resourceStream ));
5253 httpPut .setHeader ("Content-Type" , "text/xml" );
53-
54- HttpResponse response = client .execute (httpPut );
54+
55+ final HttpResponse response = client .execute (httpPut );
5556 assertEquals (304 , response .getStatusLine ().getStatusCode ());
5657 }
57-
58+
5859 @ Test
5960 public void whenUpdateValidCourse_thenReceiveOKResponse () throws IOException {
60- HttpPut httpPut = new HttpPut (BASE_URL + "2" );
61- InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("changed_course.xml" );
61+ final HttpPut httpPut = new HttpPut (BASE_URL + "2" );
62+ final InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("changed_course.xml" );
6263 httpPut .setEntity (new InputStreamEntity (resourceStream ));
6364 httpPut .setHeader ("Content-Type" , "text/xml" );
64-
65- HttpResponse response = client .execute (httpPut );
65+
66+ final HttpResponse response = client .execute (httpPut );
6667 assertEquals (200 , response .getStatusLine ().getStatusCode ());
67-
68- Course course = getCourse (2 );
68+
69+ final Course course = getCourse (2 );
6970 assertEquals (2 , course .getId ());
7071 assertEquals ("Apache CXF Support for RESTful" , course .getName ());
7172 }
72-
73+
7374 @ Test
7475 public void whenCreateConflictStudent_thenReceiveConflictResponse () throws IOException {
75- HttpPost httpPost = new HttpPost (BASE_URL + "1/students" );
76- InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("conflict_student.xml" );
76+ final HttpPost httpPost = new HttpPost (BASE_URL + "1/students" );
77+ final InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("conflict_student.xml" );
7778 httpPost .setEntity (new InputStreamEntity (resourceStream ));
7879 httpPost .setHeader ("Content-Type" , "text/xml" );
79-
80- HttpResponse response = client .execute (httpPost );
80+
81+ final HttpResponse response = client .execute (httpPost );
8182 assertEquals (409 , response .getStatusLine ().getStatusCode ());
8283 }
8384
8485 @ Test
8586 public void whenCreateValidStudent_thenReceiveOKResponse () throws IOException {
86- HttpPost httpPost = new HttpPost (BASE_URL + "2/students" );
87- InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("created_student.xml" );
87+ final HttpPost httpPost = new HttpPost (BASE_URL + "2/students" );
88+ final InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("created_student.xml" );
8889 httpPost .setEntity (new InputStreamEntity (resourceStream ));
8990 httpPost .setHeader ("Content-Type" , "text/xml" );
90-
91- HttpResponse response = client .execute (httpPost );
91+
92+ final HttpResponse response = client .execute (httpPost );
9293 assertEquals (200 , response .getStatusLine ().getStatusCode ());
93-
94- Student student = getStudent (2 , 3 );
94+
95+ final Student student = getStudent (2 , 3 );
9596 assertEquals (3 , student .getId ());
9697 assertEquals ("Student C" , student .getName ());
9798 }
98-
99+
99100 @ Test
100101 public void whenDeleteInvalidStudent_thenReceiveNotFoundResponse () throws IOException {
101- HttpDelete httpDelete = new HttpDelete (BASE_URL + "1/students/3" );
102- HttpResponse response = client .execute (httpDelete );
102+ final HttpDelete httpDelete = new HttpDelete (BASE_URL + "1/students/3" );
103+ final HttpResponse response = client .execute (httpDelete );
103104 assertEquals (404 , response .getStatusLine ().getStatusCode ());
104- }
105-
105+ }
106+
106107 @ Test
107108 public void whenDeleteValidStudent_thenReceiveOKResponse () throws IOException {
108- HttpDelete httpDelete = new HttpDelete (BASE_URL + "1/students/1" );
109- HttpResponse response = client .execute (httpDelete );
109+ final HttpDelete httpDelete = new HttpDelete (BASE_URL + "1/students/1" );
110+ final HttpResponse response = client .execute (httpDelete );
110111 assertEquals (200 , response .getStatusLine ().getStatusCode ());
111-
112- Course course = getCourse (1 );
112+
113+ final Course course = getCourse (1 );
113114 assertEquals (1 , course .getStudents ().size ());
114115 assertEquals (2 , course .getStudents ().get (0 ).getId ());
115116 assertEquals ("Student B" , course .getStudents ().get (0 ).getName ());
116117 }
117118
118119 private Course getCourse (int courseOrder ) throws IOException {
119- URL url = new URL (BASE_URL + courseOrder );
120- InputStream input = url .openStream ();
120+ final URL url = new URL (BASE_URL + courseOrder );
121+ final InputStream input = url .openStream ();
121122 return JAXB .unmarshal (new InputStreamReader (input ), Course .class );
122123 }
123124
124125 private Student getStudent (int courseOrder , int studentOrder ) throws IOException {
125- URL url = new URL (BASE_URL + courseOrder + "/students/" + studentOrder );
126- InputStream input = url .openStream ();
126+ final URL url = new URL (BASE_URL + courseOrder + "/students/" + studentOrder );
127+ final InputStream input = url .openStream ();
127128 return JAXB .unmarshal (new InputStreamReader (input ), Student .class );
128129 }
129130}
0 commit comments