2017-01-12 19:25:18 +05:30
|
|
|
package com.baeldung.server;
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
2017-01-15 06:50:07 +01:00
|
|
|
import com.baeldung.server.model.Employee;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2017-01-12 19:25:18 +05:30
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
import org.apache.http.HttpStatus;
|
|
|
|
import org.apache.http.client.methods.HttpGet;
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.client.methods.HttpUriRequest;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2017-01-15 06:50:07 +01:00
|
|
|
import java.io.IOException;
|
2017-01-12 19:25:18 +05:30
|
|
|
|
|
|
|
public class JerseyApiLiveTest {
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
private static final String SERVICE_URL = "http://localhost:8082/spring-jersey/resources/employees";
|
2017-01-12 19:25:18 +05:30
|
|
|
|
|
|
|
@Test
|
2017-01-15 06:50:07 +01:00
|
|
|
public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws IOException {
|
2017-01-12 19:25:18 +05:30
|
|
|
final HttpUriRequest request = new HttpGet(SERVICE_URL);
|
|
|
|
|
|
|
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK);
|
2017-01-12 19:25:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-01-15 06:50:07 +01:00
|
|
|
public void givenGetEmployee_whenEmployeeExists_thenResponseCodeSuccess() throws IOException {
|
2017-01-12 19:25:18 +05:30
|
|
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
|
|
|
|
|
|
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_OK);
|
2017-01-12 19:25:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-01-15 06:50:07 +01:00
|
|
|
public void givenGetEmployee_whenEmployeeDoesNotExist_thenResponseCodeNotFound() throws IOException {
|
2017-01-12 19:25:18 +05:30
|
|
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1000");
|
|
|
|
|
|
|
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_NOT_FOUND);
|
2017-01-12 19:25:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-01-15 06:50:07 +01:00
|
|
|
public void givenGetEmployee_whenJsonRequested_thenCorrectDataRetrieved() throws IOException {
|
2017-01-12 19:25:18 +05:30
|
|
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
|
|
|
|
|
|
|
request.setHeader("Accept", "application/json");
|
|
|
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
Employee emp = mapper.readValue(httpResponse.getEntity().getContent(), Employee.class);
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
assertEquals(emp.getFirstName(), "Jane");
|
2017-01-12 19:25:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-01-15 06:50:07 +01:00
|
|
|
public void givenAddEmployee_whenJsonRequestSent_thenResponseCodeCreated() throws IOException {
|
2017-01-12 19:25:18 +05:30
|
|
|
final HttpPost request = new HttpPost(SERVICE_URL);
|
|
|
|
|
|
|
|
Employee emp = new Employee(5, "Johny");
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
String empJson = mapper.writeValueAsString(emp);
|
|
|
|
StringEntity input = new StringEntity(empJson);
|
|
|
|
input.setContentType("application/json");
|
|
|
|
request.setEntity(input);
|
|
|
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CREATED);
|
2017-01-12 19:25:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-01-15 06:50:07 +01:00
|
|
|
public void givenAddEmployee_whenRequestForExistingObjectSent_thenResponseCodeConflict() throws IOException {
|
2017-01-12 19:25:18 +05:30
|
|
|
final HttpPost request = new HttpPost(SERVICE_URL);
|
|
|
|
|
|
|
|
Employee emp = new Employee(1, "Johny");
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
String empJson = mapper.writeValueAsString(emp);
|
|
|
|
StringEntity input = new StringEntity(empJson);
|
|
|
|
input.setContentType("application/json");
|
|
|
|
request.setEntity(input);
|
|
|
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
|
|
|
|
2017-01-31 13:17:23 +05:30
|
|
|
assertEquals(httpResponse.getStatusLine().getStatusCode(), HttpStatus.SC_CONFLICT);
|
2017-01-12 19:25:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|