Search in sources :

Example 1 with RequestCallback

use of com.tvd12.ezyhttp.client.callback.RequestCallback in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method executeJsonTest.

@Test
public void executeJsonTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<TestResponse> wrap = new EzyWrap<>();
    sut.execute(request, new RequestCallback<ResponseEntity>() {

        @Override
        public void onResponse(ResponseEntity response) {
            wrap.setValue(response.getBody());
            countDownLatch.countDown();
        }

        @Override
        public void onException(Exception e) {
        }
    });
    countDownLatch.await();
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, wrap.getValue());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 2 with RequestCallback

use of com.tvd12.ezyhttp.client.callback.RequestCallback in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method fireJsonButExceptionInCallbackTest.

@Test
public void fireJsonButExceptionInCallbackTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<TestResponse> wrap = new EzyWrap<>();
    sut.fire(request, new RequestCallback<TestResponse>() {

        @Override
        public void onResponse(TestResponse response) {
            wrap.setValue(response);
            countDownLatch.countDown();
            throw new RuntimeException("just test");
        }

        @Override
        public void onException(Exception e) {
        }
    });
    countDownLatch.await();
    Thread.sleep(100);
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, wrap.getValue());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 3 with RequestCallback

use of com.tvd12.ezyhttp.client.callback.RequestCallback in project ezyhttp by youngmonkeys.

the class HttpClientProxy method execute.

public void execute(Request request, RequestCallback<ResponseEntity> callback) {
    EzyFuture future = new RequestFutureTask(callback);
    futures.addFuture(request, future);
    try {
        addRequest(request);
    } catch (Exception e) {
        futures.removeFuture(request);
        throw e;
    }
}
Also used : EzyFuture(com.tvd12.ezyfox.concurrent.EzyFuture) RequestFutureTask(com.tvd12.ezyhttp.client.concurrent.RequestFutureTask) EzyProcessor.processWithException(com.tvd12.ezyfox.util.EzyProcessor.processWithException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) IOException(java.io.IOException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException)

Example 4 with RequestCallback

use of com.tvd12.ezyhttp.client.callback.RequestCallback in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method fireExceptionTest.

@Test
public void fireExceptionTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(String.class).setResponseType(StatusCodes.OK, String.class).setURL("http://unknow-host:18081/greet");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<Exception> wrap = new EzyWrap<>();
    sut.fire(request, new RequestCallback<String>() {

        @Override
        public void onResponse(String response) {
        }

        @Override
        public void onException(Exception e) {
            wrap.setValue(e);
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
    // then
    Asserts.assertEquals(BadRequestException.class, wrap.getValue().getClass());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 5 with RequestCallback

use of com.tvd12.ezyhttp.client.callback.RequestCallback in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method clientWasNotActiveAtExecute.

@Test
public void clientWasNotActiveAtExecute() {
    // given
    HttpClientProxy sut = HttpClientProxy.builder().build();
    PostRequest request = new PostRequest().setConnectTimeout(15000).setEntity(String.class).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet");
    // when
    Throwable e = Asserts.assertThrows(() -> sut.execute(request, new RequestCallback<ResponseEntity>() {

        public void onException(Exception e) {
        }

        public void onResponse(ResponseEntity response) {
        }
    }));
    // then
    Asserts.assertThat(e).isEqualsType(ClientNotActiveException.class);
    sut.close();
    sut.stop();
}
Also used : ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) RequestCallback(com.tvd12.ezyhttp.client.callback.RequestCallback) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)7 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)7 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)6 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)6 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)6 BaseTest (com.tvd12.test.base.BaseTest)6 UnknownHostException (java.net.UnknownHostException)6 BeforeTest (org.testng.annotations.BeforeTest)6 Test (org.testng.annotations.Test)6 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)3 EzyFuture (com.tvd12.ezyfox.concurrent.EzyFuture)1 EzyProcessor.processWithException (com.tvd12.ezyfox.util.EzyProcessor.processWithException)1 RequestCallback (com.tvd12.ezyhttp.client.callback.RequestCallback)1 RequestFutureTask (com.tvd12.ezyhttp.client.concurrent.RequestFutureTask)1 IOException (java.io.IOException)1