1+ package com .baeldung .cxf .jaxrs .implementation ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import org .junit .AfterClass ;
6+ import org .junit .BeforeClass ;
7+ import org .junit .Test ;
8+
9+ import java .io .IOException ;
10+ import java .io .InputStream ;
11+ import java .io .InputStreamReader ;
12+ import java .net .URL ;
13+
14+ import javax .xml .bind .JAXB ;
15+
16+ import org .apache .http .HttpResponse ;
17+ import org .apache .http .client .methods .HttpDelete ;
18+ import org .apache .http .client .methods .HttpPost ;
19+ import org .apache .http .client .methods .HttpPut ;
20+ import org .apache .http .entity .InputStreamEntity ;
21+ import org .apache .http .impl .client .CloseableHttpClient ;
22+ import org .apache .http .impl .client .HttpClients ;
23+
24+ public class ServiceTest {
25+ private static final String BASE_URL = "http://localhost:8080/baeldung/courses/" ;
26+ private static CloseableHttpClient client ;
27+
28+ @ BeforeClass
29+ public static void createClient () {
30+ client = HttpClients .createDefault ();
31+ }
32+
33+ @ AfterClass
34+ public static void closeClient () throws IOException {
35+ client .close ();
36+ }
37+
38+ @ Test
39+ public void whenPutCourse_thenCorrect () throws IOException {
40+ HttpPut httpPut = new HttpPut (BASE_URL + "3" );
41+ InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("course.xml" );
42+ httpPut .setEntity (new InputStreamEntity (resourceStream ));
43+ httpPut .setHeader ("Content-Type" , "text/xml" );
44+
45+ HttpResponse response = client .execute (httpPut );
46+ assertEquals (200 , response .getStatusLine ().getStatusCode ());
47+
48+ Course course = getCourse (3 );
49+ assertEquals (3 , course .getId ());
50+ assertEquals ("Apache CXF Support for RESTful" , course .getName ());
51+ }
52+
53+ @ Test
54+ public void whenPostStudent_thenCorrect () throws IOException {
55+ HttpPost httpPost = new HttpPost (BASE_URL + "2/students" );
56+ InputStream resourceStream = this .getClass ().getClassLoader ().getResourceAsStream ("student.xml" );
57+ httpPost .setEntity (new InputStreamEntity (resourceStream ));
58+ httpPost .setHeader ("Content-Type" , "text/xml" );
59+
60+ HttpResponse response = client .execute (httpPost );
61+ assertEquals (200 , response .getStatusLine ().getStatusCode ());
62+
63+ Student student = getStudent (2 , 0 );
64+ assertEquals (3 , student .getId ());
65+ assertEquals ("Student C" , student .getName ());
66+ }
67+
68+ @ Test
69+ public void whenDeleteStudent_thenCorrect () throws IOException {
70+ HttpDelete httpDelete = new HttpDelete (BASE_URL + "1/students/0" );
71+ HttpResponse response = client .execute (httpDelete );
72+ assertEquals (200 , response .getStatusLine ().getStatusCode ());
73+
74+ Course course = getCourse (1 );
75+ assertEquals (1 , course .getStudents ().size ());
76+ assertEquals (2 , course .getStudents ().get (0 ).getId ());
77+ assertEquals ("Student B" , course .getStudents ().get (0 ).getName ());
78+ }
79+
80+ private Course getCourse (int courseOrder ) throws IOException {
81+ URL url = new URL (BASE_URL + courseOrder );
82+ InputStream input = url .openStream ();
83+ Course course = JAXB .unmarshal (new InputStreamReader (input ), Course .class );
84+ return course ;
85+ }
86+
87+ private Student getStudent (int courseOrder , int studentOrder ) throws IOException {
88+ URL url = new URL (BASE_URL + courseOrder + "/students/" + studentOrder );
89+ InputStream input = url .openStream ();
90+ Student student = JAXB .unmarshal (new InputStreamReader (input ), Student .class );
91+ return student ;
92+ }
93+ }
0 commit comments