diff --git a/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java b/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java index 80f48aef2..333aa28e9 100644 --- a/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java +++ b/apps/rest-showcase/src/test/java/it/org/apache/struts2/rest/example/PostOrderTest.java @@ -18,15 +18,16 @@ */ package it.org.apache.struts2.rest.example; +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import net.sourceforge.jwebunit.junit.WebTestCase; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; import java.io.IOException; -import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; - public class PostOrderTest extends WebTestCase { public void setUp() throws Exception { @@ -72,68 +73,50 @@ public class PostOrderTest extends WebTestCase { } public void testPostOrderInXml() throws IOException { - HttpClient client = new HttpClient(); - PostMethod method = null; - try { - method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.xml"); - method.setRequestEntity(new StringRequestEntity("\n" + - "Test3\n" + - "3342\n" + - "")); - client.executeMethod(method); - assertEquals(201, method.getStatusCode()); - assertTrue(method.getResponseHeader("Location").getValue().startsWith(ParameterUtils.getBaseUrl()+"/orders/")); - } finally { - method.releaseConnection(); - } + CloseableHttpClient client = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.xml"); + httpPost.setEntity(new StringEntity("\n" + + "Test3\n" + + "3342\n" + + "")); + CloseableHttpResponse response = client.execute(httpPost); + assertEquals(201, response.getStatusLine().getStatusCode()); + assertTrue(response.getHeaders("Location")[0].getValue().startsWith(ParameterUtils.getBaseUrl() + "/orders/")); + client.close(); } public void testPostOrderInXmlWithBadData() throws IOException { - HttpClient client = new HttpClient(); - PostMethod method = null; - try { - method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.xml"); - method.setRequestEntity(new StringRequestEntity("\n" + - "3342\n" + - "")); - client.executeMethod(method); - assertEquals(400, method.getStatusCode()); - String response = method.getResponseBodyAsString(); - assertTrue(response.contains("The client name is empty")); - assertNull(method.getResponseHeader("Location")); - } finally { - method.releaseConnection(); - } + CloseableHttpClient client = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.xml"); + httpPost.setEntity(new StringEntity("\n" + + "3342\n" + + "")); + CloseableHttpResponse response = client.execute(httpPost); + assertEquals(400, response.getStatusLine().getStatusCode()); + assertTrue(response.toString().contains("The client name is empty")); + assertNull(response.getHeaders("Location")); + client.close(); } public void testPostOrderInJson() throws IOException { - HttpClient client = new HttpClient(); - PostMethod method = null; - try { - method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.json"); - method.setRequestEntity(new StringRequestEntity("{\"amount\":33,\"clientName\":\"Test4\"}")); - client.executeMethod(method); - assertEquals(201, method.getStatusCode()); - assertTrue(method.getResponseHeader("Location").getValue().startsWith(ParameterUtils.getBaseUrl()+"/orders/")); - } finally { - method.releaseConnection(); - } + CloseableHttpClient client = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.json"); + httpPost.setEntity(new StringEntity("{\"amount\":33,\"clientName\":\"Test4\"}")); + CloseableHttpResponse response = client.execute(httpPost); + assertEquals(201, response.getStatusLine().getStatusCode()); + assertTrue(response.getHeaders("Location")[0].getValue().startsWith(ParameterUtils.getBaseUrl() + "/orders/")); + client.close(); } public void testPostOrderInJsonWithBadData() throws IOException { - HttpClient client = new HttpClient(); - PostMethod method = null; - try { - method = new PostMethod(ParameterUtils.getBaseUrl()+"/orders.json"); - method.setRequestEntity(new StringRequestEntity("{\"amount\":33}")); - client.executeMethod(method); - String response = method.getResponseBodyAsString(); - assertEquals(400, method.getStatusCode()); - - assertEquals("{\"actionErrors\":[],\"fieldErrors\":{\"clientName\":[\"The client name is empty\"]}}", response); - assertNull(method.getResponseHeader("Location")); - } finally { - method.releaseConnection(); - } + CloseableHttpClient client = HttpClients.createDefault(); + HttpPost httpPost = new HttpPost(ParameterUtils.getBaseUrl() + "/orders.json"); + httpPost.setEntity(new StringEntity("{\"amount\":33}")); + CloseableHttpResponse response = client.execute(httpPost); + assertEquals(400, response.getStatusLine().getStatusCode()); + assertTrue(response.toString() + .contains("{\"actionErrors\":[],\"fieldErrors\":{\"clientName\":[\"The client name is empty\"]}}")); + assertNull(response.getHeaders("Location")); + client.close(); } } \ No newline at end of file