Search in sources :

Example 6 with JacksonSerder

use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method base64EncodedContent.

@Test
public void base64EncodedContent() throws Throwable {
    Class<Base64EncodedContentMethods> clazz = Base64EncodedContentMethods.class;
    Method base64UrlMethod = clazz.getDeclaredMethod("base64Url", Callback.class);
    HttpResponseMapper mapperBase64 = new HttpResponseMapper(base64UrlMethod, extractCallbackType(base64UrlMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    final String valueToEncode = "hello azure android";
    // aGVsbG8gYXp1cmUgYW5kcm9pZA==  (plain bytes).
    final byte[] base64EncodedBytes = Base64Url.encode(valueToEncode.getBytes()).encodedBytes();
    MockHttpResponse httpResponseBase64EncodedBytes0 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), base64EncodedBytes);
    Response<byte[]> restResponseBase64DecodedBytes0 = (Response<byte[]>) mapperBase64.map(httpResponseBase64EncodedBytes0, jacksonSerder);
    byte[] decodedBytes0 = restResponseBase64DecodedBytes0.getValue();
    assertNotNull(decodedBytes0);
    assertArrayEquals(valueToEncode.getBytes(), decodedBytes0);
    // "aGVsbG8gYXp1cmUgYW5kcm9pZA==" (bytes wrapped in a string, so a valid json string).
    byte[] base64EncodedBytesAsJsonString = new byte[base64EncodedBytes.length + 2];
    System.arraycopy(base64EncodedBytes, 0, base64EncodedBytesAsJsonString, 1, base64EncodedBytes.length);
    base64EncodedBytesAsJsonString[0] = '"';
    base64EncodedBytesAsJsonString[base64EncodedBytesAsJsonString.length - 1] = '"';
    MockHttpResponse httpResponseBase64EncodedBytes1 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), base64EncodedBytesAsJsonString);
    Response<byte[]> restResponseBase64DecodedBytes1 = (Response<byte[]>) mapperBase64.map(httpResponseBase64EncodedBytes1, jacksonSerder);
    byte[] decodedBytes1 = restResponseBase64DecodedBytes1.getValue();
    assertNotNull(decodedBytes1);
    assertArrayEquals(valueToEncode.getBytes(), decodedBytes1);
}
Also used : HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) 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 7 with JacksonSerder

use of com.azure.android.core.serde.jackson.JacksonSerder 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 8 with JacksonSerder

use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method unixTimeEncodedContentList.

@Test
public void unixTimeEncodedContentList() throws Throwable {
    Class<UnixTimeEncodedContentMethods> clazz = UnixTimeEncodedContentMethods.class;
    Method unixTimeListMethod = clazz.getDeclaredMethod("unixTimeList", Callback.class);
    HttpResponseMapper mapperUnixTime = new HttpResponseMapper(unixTimeListMethod, extractCallbackType(unixTimeListMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    OffsetDateTime offsetDateTime0 = OffsetDateTime.parse("1980-01-01T10:00:00Z");
    OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("1981-01-01T10:00:00Z");
    List<Integer> wireUnixTimeList = new ArrayList<>();
    wireUnixTimeList.add(Integer.parseInt(new UnixTime(offsetDateTime0).toString()));
    wireUnixTimeList.add(Integer.parseInt(new UnixTime(offsetDateTime1).toString()));
    String wireUnixTimeJsonList = jacksonSerder.serialize(wireUnixTimeList, SerdeEncoding.JSON);
    MockHttpResponse httpResponseOffsetDateTimeList = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireUnixTimeJsonList.getBytes());
    Response<List<OffsetDateTime>> restResponseBase64OffsetDataTimeList = (Response<List<OffsetDateTime>>) mapperUnixTime.map(httpResponseOffsetDateTimeList, jacksonSerder);
    List<OffsetDateTime> dateTimeListReceived = restResponseBase64OffsetDataTimeList.getValue();
    assertNotNull(dateTimeListReceived);
    assertEquals(2, dateTimeListReceived.size());
    assertEquals(0, dateTimeListReceived.get(0).compareTo(offsetDateTime0));
    assertEquals(0, dateTimeListReceived.get(1).compareTo(offsetDateTime1));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) UnixTime(com.azure.android.core.util.UnixTime) OffsetDateTime(org.threeten.bp.OffsetDateTime) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with JacksonSerder

use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method streamAndBytes.

@Test
public void streamAndBytes() throws Throwable {
    byte[] wireBytes = new byte[20];
    new Random().nextBytes(wireBytes);
    Class<StreamAndBytesMethods> clazz = StreamAndBytesMethods.class;
    final Method photoStreamMethod = clazz.getDeclaredMethod("photoStream", Callback.class);
    HttpResponseMapper mapper1 = new HttpResponseMapper(photoStreamMethod, extractCallbackType(photoStreamMethod), logger);
    MockHttpResponse httpResponse1 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireBytes);
    Response<InputStream> restStreamResponse = (Response<InputStream>) mapper1.map(httpResponse1, new JacksonSerder());
    InputStream photoStream = restStreamResponse.getValue();
    assertNotNull(photoStream);
    assertFalse(httpResponse1.isClosed());
    assertArrayEquals(wireBytes, streamToBytes(photoStream));
    final Method photoBytesMethod = clazz.getDeclaredMethod("photoBytes", Callback.class);
    HttpResponseMapper mapper2 = new HttpResponseMapper(photoBytesMethod, extractCallbackType(photoBytesMethod), logger);
    MockHttpResponse httpResponse2 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireBytes);
    Response<byte[]> restBytesResponse = (Response<byte[]>) mapper2.map(httpResponse2, new JacksonSerder());
    byte[] photoBytes = restBytesResponse.getValue();
    assertNotNull(photoBytes);
    assertFalse(httpResponse2.isClosed());
    assertArrayEquals(wireBytes, photoBytes);
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) Random(java.util.Random) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with JacksonSerder

use of com.azure.android.core.serde.jackson.JacksonSerder in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method unixTimeEncodedContentMap.

@Test
public void unixTimeEncodedContentMap() throws Throwable {
    Class<UnixTimeEncodedContentMethods> clazz = UnixTimeEncodedContentMethods.class;
    Method unixTimeMapMethod = clazz.getDeclaredMethod("unixTimeMap", Callback.class);
    HttpResponseMapper mapperDateTimeRfc1123 = new HttpResponseMapper(unixTimeMapMethod, extractCallbackType(unixTimeMapMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    OffsetDateTime offsetDateTime0 = OffsetDateTime.parse("1980-01-01T10:00:00Z");
    OffsetDateTime offsetDateTime1 = OffsetDateTime.parse("1981-01-01T10:00:00Z");
    Map<String, Integer> wireUnixTimeMap = new HashMap<>();
    wireUnixTimeMap.put("v0", Integer.parseInt(new UnixTime(offsetDateTime0).toString()));
    wireUnixTimeMap.put("v1", Integer.parseInt(new UnixTime(offsetDateTime1).toString()));
    String wireDateTimeRfc1123JsonMap = jacksonSerder.serialize(wireUnixTimeMap, SerdeEncoding.JSON);
    MockHttpResponse httpResponseDateTimeOffsetMap = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireDateTimeRfc1123JsonMap.getBytes());
    Response<Map<String, OffsetDateTime>> restResponseOffsetDataTimeMap = (Response<Map<String, OffsetDateTime>>) mapperDateTimeRfc1123.map(httpResponseDateTimeOffsetMap, jacksonSerder);
    Map<String, OffsetDateTime> dateTimeMapReceived = restResponseOffsetDataTimeMap.getValue();
    assertNotNull(dateTimeMapReceived);
    assertEquals(2, dateTimeMapReceived.size());
    assertEquals(0, dateTimeMapReceived.get("v0").compareTo(offsetDateTime0));
    assertEquals(0, dateTimeMapReceived.get("v1").compareTo(offsetDateTime1));
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HashMap(java.util.HashMap) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) HttpResponse(com.azure.android.core.http.HttpResponse) PagedResponse(com.azure.android.core.rest.util.paging.PagedResponse) UnixTime(com.azure.android.core.util.UnixTime) OffsetDateTime(org.threeten.bp.OffsetDateTime) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)32 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)27 Test (org.junit.jupiter.api.Test)23 Method (java.lang.reflect.Method)20 HttpMethod (com.azure.android.core.http.HttpMethod)18 HttpHeaders (com.azure.android.core.http.HttpHeaders)17 HttpResponse (com.azure.android.core.http.HttpResponse)16 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)13 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 OffsetDateTime (org.threeten.bp.OffsetDateTime)6 HttpPipeline (com.azure.android.core.http.HttpPipeline)3 HttpPipelineBuilder (com.azure.android.core.http.HttpPipelineBuilder)3 HttpRequest (com.azure.android.core.http.HttpRequest)3 UrlBuilder (com.azure.android.core.http.util.UrlBuilder)3 DateTimeRfc1123 (com.azure.android.core.util.DateTimeRfc1123)3 UnixTime (com.azure.android.core.util.UnixTime)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3