Refactor JerseyApiLiveTest
This commit is contained in:
parent
2b2dc447bf
commit
b5d525ad7c
|
@ -1,10 +1,9 @@
|
||||||
package com.baeldung.server;
|
package com.baeldung.server;
|
||||||
|
|
||||||
import java.io.IOException;
|
import com.baeldung.server.model.Employee;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.apache.http.client.ClientProtocolException;
|
|
||||||
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpGet;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.client.methods.HttpUriRequest;
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
@ -12,15 +11,14 @@ import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.server.model.Employee;
|
import java.io.IOException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
public class JerseyApiLiveTest {
|
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/jersey-api/resources/employees";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws ClientProtocolException, IOException {
|
public void givenGetAllEmployees_whenCorrectRequest_thenResponseCodeSuccess() throws IOException {
|
||||||
final HttpUriRequest request = new HttpGet(SERVICE_URL);
|
final HttpUriRequest request = new HttpGet(SERVICE_URL);
|
||||||
|
|
||||||
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
@ -29,7 +27,7 @@ public class JerseyApiLiveTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGetEmployee_whenEmployeeExists_thenResponseCodeSuccess() throws ClientProtocolException, IOException {
|
public void givenGetEmployee_whenEmployeeExists_thenResponseCodeSuccess() throws IOException {
|
||||||
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
||||||
|
|
||||||
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
@ -38,7 +36,7 @@ public class JerseyApiLiveTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGetEmployee_whenEmployeeDoesNotExist_thenResponseCodeNotFound() throws ClientProtocolException, IOException {
|
public void givenGetEmployee_whenEmployeeDoesNotExist_thenResponseCodeNotFound() throws IOException {
|
||||||
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1000");
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1000");
|
||||||
|
|
||||||
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
final HttpResponse httpResponse = HttpClientBuilder.create().build().execute(request);
|
||||||
|
@ -47,7 +45,7 @@ public class JerseyApiLiveTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenGetEmployee_whenJsonRequested_thenCorrectDataRetrieved() throws ClientProtocolException, IOException {
|
public void givenGetEmployee_whenJsonRequested_thenCorrectDataRetrieved() throws IOException {
|
||||||
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
final HttpUriRequest request = new HttpGet(SERVICE_URL + "/1");
|
||||||
|
|
||||||
request.setHeader("Accept", "application/json");
|
request.setHeader("Accept", "application/json");
|
||||||
|
@ -59,7 +57,7 @@ public class JerseyApiLiveTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAddEmployee_whenJsonRequestSent_thenResponseCodeCreated() throws ClientProtocolException, IOException {
|
public void givenAddEmployee_whenJsonRequestSent_thenResponseCodeCreated() throws IOException {
|
||||||
final HttpPost request = new HttpPost(SERVICE_URL);
|
final HttpPost request = new HttpPost(SERVICE_URL);
|
||||||
|
|
||||||
Employee emp = new Employee(5, "Johny");
|
Employee emp = new Employee(5, "Johny");
|
||||||
|
@ -74,7 +72,7 @@ public class JerseyApiLiveTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAddEmployee_whenRequestForExistingObjectSent_thenResponseCodeConflict() throws ClientProtocolException, IOException {
|
public void givenAddEmployee_whenRequestForExistingObjectSent_thenResponseCodeConflict() throws IOException {
|
||||||
final HttpPost request = new HttpPost(SERVICE_URL);
|
final HttpPost request = new HttpPost(SERVICE_URL);
|
||||||
|
|
||||||
Employee emp = new Employee(1, "Johny");
|
Employee emp = new Employee(1, "Johny");
|
||||||
|
|
Loading…
Reference in New Issue