Search in sources :

Example 1 with HttpResponseException

use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method anyError.

@Test
public void anyError() throws Throwable {
    Class<AnyErrorMethods> clazz = AnyErrorMethods.class;
    Method getAnyErrorMethod = clazz.getDeclaredMethod("getAnyError", Callback.class);
    HttpResponseMapper mapper = new HttpResponseMapper(getAnyErrorMethod, extractCallbackType(getAnyErrorMethod), logger);
    MockHttpResponse httpResponse = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 409, new HttpHeaders(), "Unknown error".getBytes());
    HttpResponseException ex = null;
    try {
        mapper.map(httpResponse, new JacksonSerder());
    } catch (HttpResponseException e) {
        ex = e;
    }
    assertNotNull(ex);
    assertNotNull(ex.getMessage());
    assertTrue(ex.getMessage().contains("Unknown error"));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with HttpResponseException

use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.

the class RestProxyTests method putRequestWithUnexpectedResponseAndNoFallthroughExceptionType.

@Test
public void putRequestWithUnexpectedResponseAndNoFallthroughExceptionType() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<HttpBinJSON> cbResult = new CallbackResult<>();
    createService(Service9.class).putWithUnexpectedResponseAndNoFallthroughExceptionType("I'm the body!", new Callback<Response<HttpBinJSON>>() {

        @Override
        public void onSuccess(Response<HttpBinJSON> response) {
            cbResult.response = response;
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable error) {
            cbResult.error = error;
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "putRequestWithUnexpectedResponseAndNoFallthroughExceptionType");
    if (cbResult.response != null) {
        fail("Expected HttpResponseException would be thrown.");
    } else {
        Assertions.assertNotNull(cbResult.error);
        Assertions.assertTrue(cbResult.error instanceof HttpResponseException, "Expected HttpResponseException would be thrown. Instead got " + cbResult.error.getClass().getSimpleName());
        HttpResponseException e = (HttpResponseException) cbResult.error;
        assertNotNull(e.getValue());
        assertTrue(e.getValue() instanceof LinkedHashMap);
        @SuppressWarnings("unchecked") final LinkedHashMap<String, String> expectedBody = (LinkedHashMap<String, String>) e.getValue();
        assertEquals("I'm the body!", expectedBody.get("data"));
    }
}
Also used : HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedHashMap(java.util.LinkedHashMap) Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON) Test(org.junit.jupiter.api.Test)

Example 3 with HttpResponseException

use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.

the class RestProxyTests method service18GetStatus500.

@Test
public void service18GetStatus500() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<Void> cbResult = new CallbackResult<>();
    createService(Service18.class).getStatus500(new Callback<Response<Void>>() {

        @Override
        public void onSuccess(Response<Void> response) {
            cbResult.response = response;
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable error) {
            cbResult.error = error;
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "service18GetStatus400");
    if (cbResult.response != null) {
        fail("Expected HttpResponseException would be thrown.");
    } else {
        Assertions.assertNotNull(cbResult.error);
        Assertions.assertTrue(cbResult.error instanceof HttpResponseException, "Expected HttpResponseException would be thrown. Instead got " + cbResult.error.getClass().getSimpleName());
        HttpResponseException e = (HttpResponseException) cbResult.error;
        assertNull(e.getValue());
        Assertions.assertEquals(500, e.getResponse().getStatusCode());
    }
}
Also used : Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 4 with HttpResponseException

use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.

the class RestProxyTests method service18GetStatus400.

@Test
public void service18GetStatus400() {
    CountDownLatch latch = new CountDownLatch(1);
    CallbackResult<Void> cbResult = new CallbackResult<>();
    createService(Service18.class).getStatus400(new Callback<Response<Void>>() {

        @Override
        public void onSuccess(Response<Void> response) {
            cbResult.response = response;
            latch.countDown();
        }

        @Override
        public void onFailure(Throwable error) {
            cbResult.error = error;
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "service18GetStatus400");
    if (cbResult.response != null) {
        fail("Expected HttpResponseException would be thrown.");
    } else {
        Assertions.assertNotNull(cbResult.error);
        Assertions.assertTrue(cbResult.error instanceof HttpResponseException, "Expected HttpResponseException would be thrown. Instead got " + cbResult.error.getClass().getSimpleName());
        HttpResponseException e = (HttpResponseException) cbResult.error;
        assertNull(e.getValue());
        Assertions.assertEquals(400, e.getResponse().getStatusCode());
    }
}
Also used : Response(com.azure.android.core.rest.Response) StreamResponse(com.azure.android.core.rest.StreamResponse) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Example 5 with HttpResponseException

use of com.azure.android.core.http.exception.HttpResponseException in project azure-sdk-for-android by Azure.

the class RestProxy method invoke.

@Override
public Object invoke(final Object restProxy, final Method swaggerMethod, final Object[] swaggerMethodArgs) {
    final SwaggerMethodParser methodParser = this.interfaceParser.getMethodParser(swaggerMethod, this.logger);
    final Callback<Response<?>> restCallback;
    restCallback = (Callback<Response<?>>) swaggerMethodArgs[methodParser.callbackArgIndex];
    Objects.requireNonNull(restCallback);
    final CancellationToken cancellationToken;
    if (methodParser.cancellationTokenArgIndex == -1) {
        cancellationToken = CancellationToken.NONE;
    } else {
        cancellationToken = (CancellationToken) swaggerMethodArgs[methodParser.cancellationTokenArgIndex];
    }
    final HttpRequest httpRequest;
    try {
        httpRequest = methodParser.mapToHttpRequest(swaggerMethodArgs);
    } catch (IOException e) {
        restCallback.onFailure(e);
        return null;
    } catch (HttpResponseException e) {
        restCallback.onFailure(e);
        return null;
    }
    this.httpPipeline.send(httpRequest, RequestContext.NONE, cancellationToken, new HttpPipelineCallback(methodParser, restCallback));
    return null;
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse) HttpRequest(com.azure.android.core.http.HttpRequest) CancellationToken(com.azure.android.core.util.CancellationToken) HttpResponseException(com.azure.android.core.http.exception.HttpResponseException) IOException(java.io.IOException)

Aggregations

HttpResponseException (com.azure.android.core.http.exception.HttpResponseException)10 Test (org.junit.jupiter.api.Test)6 Response (com.azure.android.core.rest.Response)5 StreamResponse (com.azure.android.core.rest.StreamResponse)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 CommunicationErrorResponseException (com.azure.android.communication.chat.implementation.models.CommunicationErrorResponseException)2 HttpBinJSON (com.azure.android.core.test.implementation.entities.HttpBinJSON)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 ExecutionException (java.util.concurrent.ExecutionException)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 ChatThreadProperties (com.azure.android.communication.chat.implementation.models.ChatThreadProperties)1 HttpHeaders (com.azure.android.core.http.HttpHeaders)1 HttpMethod (com.azure.android.core.http.HttpMethod)1 HttpRequest (com.azure.android.core.http.HttpRequest)1 HttpResponse (com.azure.android.core.http.HttpResponse)1 ReturnValueWireType (com.azure.android.core.rest.annotation.ReturnValueWireType)1 UnexpectedResponseExceptionType (com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType)1 HttpResponseExceptionInfo (com.azure.android.core.rest.implementation.HttpResponseExceptionInfo)1