JAX-RS Client [BAEL-595] (#992)
* WatchService vs. Apache Commons IO Mnitoring * Indentation fixed * Indentation fixed * JAX-RS API using Jersey [BAEL-558] * JAX-RS API using Jersey [BAEL-558] * Modifications made to remove xml * applicationContext.xml removed * All try catch moved to ExceptionMapper * fixes * review comments incorporated * module renamed * JAX-RS client [BAEL-595] * jersey-core dependency removed * assert changed to assertEquals * messagebody readers and writers removed * pom dependency corrected and other minor changes
This commit is contained in:
		
							parent
							
								
									6c2ed878c0
								
							
						
					
					
						commit
						4fe878d6d4
					
				| @ -4,7 +4,7 @@ | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>com.baeldung</groupId> | ||||
|     <artifactId>jersey-api</artifactId> | ||||
|     <artifactId>spring-jersey</artifactId> | ||||
|     <version>0.1-SNAPSHOT</version> | ||||
|     <packaging>war</packaging> | ||||
| 
 | ||||
| @ -23,6 +23,7 @@ | ||||
|     </properties> | ||||
| 
 | ||||
|     <build> | ||||
|         <finalName>spring-jersey</finalName> | ||||
|         <plugins> | ||||
|             <plugin> | ||||
|                 <groupId>org.apache.maven.plugins</groupId> | ||||
| @ -75,11 +76,6 @@ | ||||
| 
 | ||||
|         <!-- core library --> | ||||
| 
 | ||||
|         <dependency> | ||||
|             <groupId>org.glassfish.jersey.core</groupId> | ||||
|             <artifactId>jersey-server</artifactId> | ||||
|             <version>${jersey.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.glassfish.jersey.containers</groupId> | ||||
|             <artifactId>jersey-container-servlet</artifactId> | ||||
| @ -90,6 +86,12 @@ | ||||
|             <artifactId>jersey-media-json-jackson</artifactId> | ||||
|             <version>${jersey.version}</version> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.glassfish.jersey.core</groupId> | ||||
|             <artifactId>jersey-client</artifactId> | ||||
|             <version>${jersey.version}</version> | ||||
|         </dependency> | ||||
| 
 | ||||
|         <!-- servlet api --> | ||||
|         <dependency> | ||||
|             <groupId>javax.servlet</groupId> | ||||
| @ -97,8 +99,8 @@ | ||||
|             <version>${servlet-api-version}</version> | ||||
|             <scope>provided</scope> | ||||
|         </dependency> | ||||
|         <!-- optional library --> | ||||
| 
 | ||||
|         <!-- optional library --> | ||||
|         <dependency> | ||||
|             <groupId>org.glassfish.jersey.ext</groupId> | ||||
|             <artifactId>jersey-spring3</artifactId> | ||||
|  | ||||
| @ -0,0 +1,31 @@ | ||||
| package com.baeldung.client.rest; | ||||
| 
 | ||||
| import javax.ws.rs.client.Client; | ||||
| import javax.ws.rs.client.ClientBuilder; | ||||
| import javax.ws.rs.client.Entity; | ||||
| import javax.ws.rs.core.MediaType; | ||||
| import javax.ws.rs.core.Response; | ||||
| 
 | ||||
| import com.baeldung.server.model.Employee; | ||||
| 
 | ||||
| public class RestClient { | ||||
| 
 | ||||
|     private static final String REST_URI = "http://localhost:8082/spring-jersey/resources/employees"; | ||||
|     private Client client = ClientBuilder.newClient(); | ||||
| 
 | ||||
|     public Response createJsonEmployee(Employee emp) { | ||||
|         return client.target(REST_URI).request(MediaType.APPLICATION_JSON).post(Entity.entity(emp, MediaType.APPLICATION_JSON)); | ||||
|     } | ||||
| 
 | ||||
|     public Employee getJsonEmployee(int id) { | ||||
|         return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_JSON).get(Employee.class); | ||||
|     } | ||||
| 
 | ||||
|     public Response createXmlEmployee(Employee emp) { | ||||
|         return client.target(REST_URI).request(MediaType.APPLICATION_XML).post(Entity.entity(emp, MediaType.APPLICATION_XML)); | ||||
|     } | ||||
| 
 | ||||
|     public Employee getXmlEmployee(int id) { | ||||
|         return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_XML).get(Employee.class); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,53 @@ | ||||
| package com.baeldung.client; | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import javax.ws.rs.core.Response; | ||||
| 
 | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.baeldung.client.rest.RestClient; | ||||
| import com.baeldung.server.model.Employee; | ||||
| 
 | ||||
| public class JerseyClientLiveTest { | ||||
| 
 | ||||
|     public static final int HTTP_CREATED = 201; | ||||
| 
 | ||||
|     private RestClient client = new RestClient(); | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenCorrectObject_whenCorrectJsonRequest_thenResponseCodeCreated() { | ||||
|         Employee emp = new Employee(6, "Johny"); | ||||
| 
 | ||||
|         Response response = client.createJsonEmployee(emp); | ||||
| 
 | ||||
|         assertEquals(response.getStatus(), HTTP_CREATED); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenCorrectObject_whenCorrectXmlRequest_thenResponseCodeCreated() { | ||||
|         Employee emp = new Employee(7, "Jacky"); | ||||
| 
 | ||||
|         Response response = client.createXmlEmployee(emp); | ||||
| 
 | ||||
|         assertEquals(response.getStatus(), HTTP_CREATED); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenCorrectId_whenCorrectJsonRequest_thenCorrectEmployeeRetrieved() { | ||||
|         int employeeId = 1; | ||||
| 
 | ||||
|         Employee emp = client.getJsonEmployee(employeeId); | ||||
| 
 | ||||
|         assertEquals(emp.getFirstName(), "Jane"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenCorrectId_whenCorrectXmlRequest_thenCorrectEmployeeRetrieved() { | ||||
|         int employeeId = 1; | ||||
| 
 | ||||
|         Employee emp = client.getXmlEmployee(employeeId); | ||||
| 
 | ||||
|         assertEquals(emp.getFirstName(), "Jane"); | ||||
|     } | ||||
| } | ||||
| @ -1,5 +1,8 @@ | ||||
| package com.baeldung.server; | ||||
| 
 | ||||
| 
 | ||||
| import static org.junit.Assert.assertEquals; | ||||
| 
 | ||||
| import com.baeldung.server.model.Employee; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
| import org.apache.http.HttpResponse; | ||||
| @ -15,7 +18,7 @@ import java.io.IOException; | ||||
| 
 | ||||
| public class JerseyApiLiveTest { | ||||
| 
 | ||||
|     private static final String SERVICE_URL = "http://localhost:8082/jersey-api/resources/employees"; | ||||
|     private static final String SERVICE_URL = "http://localhost:8082/spring-jersey/resources/employees"; | ||||
| 
 | ||||
|     @Test | ||||
|     public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws IOException { | ||||
| @ -23,7 +26,7 @@ public class JerseyApiLiveTest { | ||||
| 
 | ||||
|         final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); | ||||
| 
 | ||||
|         assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK); | ||||
|         assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| @ -32,7 +35,7 @@ public class JerseyApiLiveTest { | ||||
| 
 | ||||
|         final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); | ||||
| 
 | ||||
|         assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK); | ||||
|         assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| @ -41,7 +44,7 @@ public class JerseyApiLiveTest { | ||||
| 
 | ||||
|         final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); | ||||
| 
 | ||||
|         assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND); | ||||
|         assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_NOT_FOUND); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| @ -53,7 +56,7 @@ public class JerseyApiLiveTest { | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         Employee emp = mapper.readValue(httpResponse.getEntity().getContent(), Employee.class); | ||||
| 
 | ||||
|         assert(emp.getFirstName().equals("Jane")); | ||||
|         assertEquals(emp.getFirstName(), "Jane"); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| @ -68,7 +71,7 @@ public class JerseyApiLiveTest { | ||||
|         request.setEntity(input); | ||||
|         final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); | ||||
| 
 | ||||
|         assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED); | ||||
|         assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CREATED); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
| @ -83,7 +86,7 @@ public class JerseyApiLiveTest { | ||||
|         request.setEntity(input); | ||||
|         final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request); | ||||
| 
 | ||||
|         assert(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT); | ||||
|         assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CONFLICT); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user