Search in sources :

Example 1 with AndroidServiceClient

use of net.servicestack.android.AndroidServiceClient in project ServiceStack.Java by ServiceStack.

the class TestServiceTestsAsync method test_Does_handle_404_Error_Async.

public void test_Does_handle_404_Error_Async() throws InterruptedException {
    AndroidServiceClient testClient = new AndroidServiceClient("http://test.servicestack.net");
    // Wow Java, you suck.
    final Exception[] globalError = new Exception[1];
    final Exception[] localError = new Exception[1];
    final WebServiceException[] thrownError = { null };
    AndroidServiceClient.GlobalExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            globalError[0] = ex;
        }
    };
    testClient.ExceptionFilter = new ExceptionFilter() {

        @Override
        public void exec(HttpURLConnection res, Exception ex) {
            localError[0] = ex;
        }
    };
    ThrowType request = new ThrowType().setType("NotFound").setMessage("not here");
    final CountDownLatch signal = new CountDownLatch(1);
    testClient.putAsync(request, new AsyncResult<ThrowTypeResponse>() {

        @Override
        public void success(ThrowTypeResponse response) {
            fail("should not succeed");
        }

        @Override
        public void error(Exception ex) {
            thrownError[0] = (WebServiceException) ex;
        }

        @Override
        public void complete() {
            signal.countDown();
        }
    });
    assertTrue(signal.await(5, TimeUnit.SECONDS));
    assertNotNull(globalError[0]);
    assertNotNull(localError[0]);
    assertNotNull(thrownError[0]);
    ResponseStatus status = thrownError[0].getResponseStatus();
    assertEquals("NotFound", status.getErrorCode());
    assertEquals("not here", status.getMessage());
    assertNotNull(status.getStackTrace());
}
Also used : WebServiceException(net.servicestack.client.WebServiceException) ExceptionFilter(net.servicestack.client.ExceptionFilter) CountDownLatch(java.util.concurrent.CountDownLatch) WebServiceException(net.servicestack.client.WebServiceException) HttpURLConnection(java.net.HttpURLConnection) AndroidServiceClient(net.servicestack.android.AndroidServiceClient) ResponseStatus(net.servicestack.client.ResponseStatus)

Example 2 with AndroidServiceClient

use of net.servicestack.android.AndroidServiceClient in project ServiceStack.Java by ServiceStack.

the class TestServiceTestsAsync method test_does_fire_Request_and_Response_Filters_Async.

public void test_does_fire_Request_and_Response_Filters_Async() throws InterruptedException {
    AndroidServiceClient client = new AndroidServiceClient("http://test.servicestack.net");
    final ArrayList<String> events = new ArrayList<>();
    AndroidServiceClient.GlobalRequestFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("GlobalRequestFilter");
        }
    };
    AndroidServiceClient.GlobalResponseFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("GlobalResponseFilter");
        }
    };
    client.RequestFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("RequestFilter");
        }
    };
    client.ResponseFilter = new ConnectionFilter() {

        @Override
        public void exec(HttpURLConnection conn) {
            events.add("ResponseFilter");
        }
    };
    Hello request = new Hello().setName("World");
    final CountDownLatch signal = new CountDownLatch(1);
    client.getAsync(request, new AsyncResult<HelloResponse>() {

        @Override
        public void success(HelloResponse response) {
            assertEquals("Hello, World!", response.getResult());
            String results = TextUtils.join(", ", events);
            assertEquals("RequestFilter, GlobalRequestFilter, ResponseFilter, GlobalResponseFilter", results);
            AndroidServiceClient.GlobalRequestFilter = null;
            AndroidServiceClient.GlobalResponseFilter = null;
        }

        @Override
        public void complete() {
            signal.countDown();
        }
    });
    assertTrue(signal.await(5, TimeUnit.SECONDS));
}
Also used : ConnectionFilter(net.servicestack.client.ConnectionFilter) HttpURLConnection(java.net.HttpURLConnection) AndroidServiceClient(net.servicestack.android.AndroidServiceClient) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AndroidServiceClient (net.servicestack.android.AndroidServiceClient)2 ArrayList (java.util.ArrayList)1 ConnectionFilter (net.servicestack.client.ConnectionFilter)1 ExceptionFilter (net.servicestack.client.ExceptionFilter)1 ResponseStatus (net.servicestack.client.ResponseStatus)1 WebServiceException (net.servicestack.client.WebServiceException)1