Radutamas/bael 1487 (#3569)

* Code for test article: Different Types of Bean Injection in Spring

* Adding jUnits for test article: Different Types of Bean Injection in Spring

* BAEL-1265: Adding jUnit for article

* BAEL-1265: Closing ExecutorService in jUnit

* BAEL-1487: Adding test for AsyncHtpClient tutorial
This commit is contained in:
tamasradu 2018-02-01 15:56:36 +02:00 committed by Grzegorz Piwowarek
parent 15f18bbb83
commit 72c9fea7e2
2 changed files with 227 additions and 1 deletions

View File

@ -116,6 +116,12 @@
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.asynchttpclient/async-http-client -->
<dependency>
<groupId>org.asynchttpclient</groupId>
<artifactId>async-http-client</artifactId>
<version>${async.http.client.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.beykery/neuroph/2.92 -->
<dependency>
<groupId>org.beykery</groupId>
@ -766,7 +772,7 @@
<serenity.jira.version>1.1.3-rc.5</serenity.jira.version>
<serenity.plugin.version>1.4.0</serenity.plugin.version>
<jUnitParams.version>1.1.0</jUnitParams.version>
<netty.version>4.1.15.Final</netty.version>
<netty.version>4.1.20.Final</netty.version>
<commons.collections.version>4.1</commons.collections.version>
<junit.version>4.12</junit.version>
<java-lsh.version>0.10</java-lsh.version>
@ -796,5 +802,6 @@
<kafka.version>1.0.0</kafka.version>
<smooks.version>1.7.0</smooks.version>
<docker.version>3.0.14</docker.version>
<async.http.client.version>2.2.0</async.http.client.version>
</properties>
</project>

View File

@ -0,0 +1,219 @@
package com.baeldung.asynchttpclient;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.AsyncHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.BoundRequestBuilder;
import org.asynchttpclient.Dsl;
import org.asynchttpclient.HttpResponseBodyPart;
import org.asynchttpclient.HttpResponseStatus;
import org.asynchttpclient.ListenableFuture;
import org.asynchttpclient.Request;
import org.asynchttpclient.Response;
import org.asynchttpclient.ws.WebSocket;
import org.asynchttpclient.ws.WebSocketListener;
import org.asynchttpclient.ws.WebSocketUpgradeHandler;
import org.junit.Before;
import org.junit.Test;
import io.netty.handler.codec.http.HttpHeaders;
public class AsyncHttpClientTestCase {
private static AsyncHttpClient HTTP_CLIENT;
@Before
public void setup() {
AsyncHttpClientConfig clientConfig = Dsl.config().setConnectTimeout(15000).setRequestTimeout(15000).build();
HTTP_CLIENT = Dsl.asyncHttpClient(clientConfig);
}
@Test
public void givenHttpClient_executeSyncGetRequest() {
BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com");
Future<Response> responseFuture = boundGetRequest.execute();
try {
Response response = responseFuture.get(5000, TimeUnit.MILLISECONDS);
assertNotNull(response);
assertEquals(200, response.getStatusCode());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void givenHttpClient_executeAsyncGetRequest() {
// execute an unbound GET request
Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build();
HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncCompletionHandler<Integer>() {
@Override
public Integer onCompleted(Response response) throws Exception {
int resposeStatusCode = response.getStatusCode();
assertEquals(200, resposeStatusCode);
return resposeStatusCode;
}
});
// execute a bound GET request
BoundRequestBuilder boundGetRequest = HTTP_CLIENT.prepareGet("http://www.baeldung.com");
boundGetRequest.execute(new AsyncCompletionHandler<Integer>() {
@Override
public Integer onCompleted(Response response) throws Exception {
int resposeStatusCode = response.getStatusCode();
assertEquals(200, resposeStatusCode);
return resposeStatusCode;
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void givenHttpClient_executeAsyncGetRequestWithAsyncHandler() {
// execute an unbound GET request
Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build();
HTTP_CLIENT.executeRequest(unboundGetRequest, new AsyncHandler<Integer>() {
int responseStatusCode = -1;
@Override
public State onStatusReceived(HttpResponseStatus responseStatus) throws Exception {
responseStatusCode = responseStatus.getStatusCode();
return State.CONTINUE;
}
@Override
public State onHeadersReceived(HttpHeaders headers) throws Exception {
return State.CONTINUE;
}
@Override
public State onBodyPartReceived(HttpResponseBodyPart bodyPart) throws Exception {
return State.CONTINUE;
}
@Override
public void onThrowable(Throwable t) {
}
@Override
public Integer onCompleted() throws Exception {
assertEquals(200, responseStatusCode);
return responseStatusCode;
}
});
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void givenHttpClient_executeAsyncGetRequestWithListanableFuture() {
// execute an unbound GET request
Request unboundGetRequest = Dsl.get("http://www.baeldung.com").build();
ListenableFuture<Response> listenableFuture = HTTP_CLIENT.executeRequest(unboundGetRequest);
listenableFuture.addListener(() -> {
Response response;
try {
response = listenableFuture.get(5000, TimeUnit.MILLISECONDS);
assertEquals(200, response.getStatusCode());
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
}, Executors.newCachedThreadPool());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void givenWebSocketClient_tryToConnect() {
WebSocketUpgradeHandler.Builder upgradeHandlerBuilder = new WebSocketUpgradeHandler.Builder();
WebSocketUpgradeHandler wsHandler = upgradeHandlerBuilder.addWebSocketListener(new WebSocketListener() {
@Override
public void onOpen(WebSocket websocket) {
// WebSocket connection opened
}
@Override
public void onClose(WebSocket websocket, int code, String reason) {
// WebSocket connection closed
}
@Override
public void onError(Throwable t) {
// WebSocket connection error
assertTrue(t.getMessage().contains("Request timeout"));
}
}).build();
WebSocket WEBSOCKET_CLIENT = null;
try {
WEBSOCKET_CLIENT = Dsl.asyncHttpClient()
.prepareGet("ws://localhost:5590/websocket")
.addHeader("header_name", "header_value")
.addQueryParam("key", "value")
.setRequestTimeout(5000)
.execute(wsHandler).get();
if (WEBSOCKET_CLIENT.isOpen()) {
WEBSOCKET_CLIENT.sendPingFrame();
WEBSOCKET_CLIENT.sendTextFrame("test message");
WEBSOCKET_CLIENT.sendBinaryFrame(new byte[] { 't', 'e', 's', 't' });
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
if (WEBSOCKET_CLIENT != null && WEBSOCKET_CLIENT.isOpen()) {
WEBSOCKET_CLIENT.sendCloseFrame(200, "OK");
}
}
}
}