Search in sources :

Example 1 with UnixTime

use of com.azure.android.core.util.UnixTime 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 2 with UnixTime

use of com.azure.android.core.util.UnixTime 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)

Example 3 with UnixTime

use of com.azure.android.core.util.UnixTime in project azure-sdk-for-android by Azure.

the class HttpResponseMapper method decodeContent.

private Object decodeContent(final Object encodedContent, final Type encodedType, final Type decodeType) {
    if (decodeType == byte[].class) {
        if (encodedType == Base64Url.class) {
            return ((Base64Url) encodedContent).decodedBytes();
        }
    } else if (decodeType == OffsetDateTime.class) {
        if (encodedType == DateTimeRfc1123.class) {
            return ((DateTimeRfc1123) encodedContent).getDateTime();
        } else if (encodedType == UnixTime.class) {
            return ((UnixTime) encodedContent).getDateTime();
        }
    } else if (TypeUtil.isTypeOrSubTypeOf(decodeType, List.class)) {
        final Type decodeElementType = TypeUtil.getTypeArgument(decodeType);
        @SuppressWarnings("unchecked") final List<Object> list = (List<Object>) encodedContent;
        final int size = list.size();
        for (int i = 0; i < size; ++i) {
            final Object encodedElement = list.get(i);
            final Object decodedElement = decodeContent(encodedElement, encodedType, decodeElementType);
            if (encodedElement != decodedElement) {
                list.set(i, decodedElement);
            }
        }
        return list;
    } else if (TypeUtil.isTypeOrSubTypeOf(decodeType, Map.class)) {
        final Type decodeValueType = TypeUtil.getTypeArguments(decodeType)[1];
        @SuppressWarnings("unchecked") final Map<String, Object> map = (Map<String, Object>) encodedContent;
        final Set<Map.Entry<String, Object>> encodedEntries = map.entrySet();
        for (Map.Entry<String, Object> encodedEntry : encodedEntries) {
            final Object encodedValue = encodedEntry.getValue();
            final Object decodedValue = decodeContent(encodedValue, encodedType, decodeValueType);
            if (encodedValue != decodedValue) {
                map.put(encodedEntry.getKey(), decodedValue);
            }
        }
        return map;
    }
    return encodedContent;
}
Also used : Set(java.util.Set) BitSet(java.util.BitSet) Base64Url(com.azure.android.core.util.Base64Url) UnexpectedResponseExceptionType(com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType) ReturnValueWireType(com.azure.android.core.rest.annotation.ReturnValueWireType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) UnixTime(com.azure.android.core.util.UnixTime) OffsetDateTime(org.threeten.bp.OffsetDateTime) DateTimeRfc1123(com.azure.android.core.util.DateTimeRfc1123) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with UnixTime

use of com.azure.android.core.util.UnixTime in project azure-sdk-for-android by Azure.

the class HttpResponseMapperTests method unixTimeEncodedContent.

@Test
public void unixTimeEncodedContent() throws Throwable {
    Class<UnixTimeEncodedContentMethods> clazz = UnixTimeEncodedContentMethods.class;
    Method unixTimeMethod = clazz.getDeclaredMethod("unixTime", Callback.class);
    HttpResponseMapper mapperUnixTime = new HttpResponseMapper(unixTimeMethod, extractCallbackType(unixTimeMethod), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    OffsetDateTime offsetDateTime = OffsetDateTime.parse("1980-01-01T10:00:00Z");
    UnixTime unixTime = new UnixTime(offsetDateTime);
    String wireUnixTimeJsonNumber = unixTime.toString();
    MockHttpResponse httpResponseUnixTime = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireUnixTimeJsonNumber.getBytes());
    Response<OffsetDateTime> restResponseUnixTime = (Response<OffsetDateTime>) mapperUnixTime.map(httpResponseUnixTime, jacksonSerder);
    OffsetDateTime dateTimeReceived = restResponseUnixTime.getValue();
    assertNotNull(dateTimeReceived);
    assertEquals(0, dateTimeReceived.compareTo(offsetDateTime));
}
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) UnixTime(com.azure.android.core.util.UnixTime) OffsetDateTime(org.threeten.bp.OffsetDateTime) Method(java.lang.reflect.Method) HttpMethod(com.azure.android.core.http.HttpMethod) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

UnixTime (com.azure.android.core.util.UnixTime)4 OffsetDateTime (org.threeten.bp.OffsetDateTime)4 HttpHeaders (com.azure.android.core.http.HttpHeaders)3 HttpMethod (com.azure.android.core.http.HttpMethod)3 HttpResponse (com.azure.android.core.http.HttpResponse)3 PagedResponse (com.azure.android.core.rest.util.paging.PagedResponse)3 JacksonSerder (com.azure.android.core.serde.jackson.JacksonSerder)3 Method (java.lang.reflect.Method)3 Test (org.junit.jupiter.api.Test)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ReturnValueWireType (com.azure.android.core.rest.annotation.ReturnValueWireType)1 UnexpectedResponseExceptionType (com.azure.android.core.rest.annotation.UnexpectedResponseExceptionType)1 Base64Url (com.azure.android.core.util.Base64Url)1 DateTimeRfc1123 (com.azure.android.core.util.DateTimeRfc1123)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1