Search in sources :

Example 1 with DateTimeRfc1123

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

the class HttpResponseMapperTests method dateTimeRfc1123EncodedContentMap.

@Test
public void dateTimeRfc1123EncodedContentMap() throws Throwable {
    Class<DateTimeRfc1123EncodedContentMethods> clazz = DateTimeRfc1123EncodedContentMethods.class;
    Method dateTimeRfc1123MapMethod = clazz.getDeclaredMethod("dateTimeRfc1123Map", Callback.class);
    HttpResponseMapper mapperDateTimeRfc1123 = new HttpResponseMapper(dateTimeRfc1123MapMethod, extractCallbackType(dateTimeRfc1123MapMethod), 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, String> wireDateTimeRfc1123Map = new HashMap<>();
    wireDateTimeRfc1123Map.put("v0", new DateTimeRfc1123(offsetDateTime0).toString());
    wireDateTimeRfc1123Map.put("v1", new DateTimeRfc1123(offsetDateTime1).toString());
    String wireDateTimeRfc1123JsonMap = jacksonSerder.serialize(wireDateTimeRfc1123Map, SerdeEncoding.JSON);
    MockHttpResponse httpResponseOffsetDateTimeMap = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireDateTimeRfc1123JsonMap.getBytes());
    Response<Map<String, OffsetDateTime>> restResponseBase64OffsetDataTimeMap = (Response<Map<String, OffsetDateTime>>) mapperDateTimeRfc1123.map(httpResponseOffsetDateTimeMap, jacksonSerder);
    Map<String, OffsetDateTime> dateTimeMapReceived = restResponseBase64OffsetDataTimeMap.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) OffsetDateTime(org.threeten.bp.OffsetDateTime) DateTimeRfc1123(com.azure.android.core.util.DateTimeRfc1123) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with DateTimeRfc1123

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

the class HttpResponseMapperTests method dateTimeRfc1123EncodedContent.

@Test
public void dateTimeRfc1123EncodedContent() throws Throwable {
    Class<DateTimeRfc1123EncodedContentMethods> clazz = DateTimeRfc1123EncodedContentMethods.class;
    Method dateTimeRfc1123Method = clazz.getDeclaredMethod("dateTimeRfc1123", Callback.class);
    HttpResponseMapper mapperDateTimeRfc1123 = new HttpResponseMapper(dateTimeRfc1123Method, extractCallbackType(dateTimeRfc1123Method), logger);
    JacksonSerder jacksonSerder = new JacksonSerder();
    OffsetDateTime offsetDateTime = OffsetDateTime.parse("1980-01-01T10:00:00Z");
    DateTimeRfc1123 dateTimeRfc1123 = new DateTimeRfc1123(offsetDateTime);
    String wireDateTimeRfc1123JsonString = '"' + dateTimeRfc1123.toString() + '"';
    MockHttpResponse httpResponseDateTimeRfc1123 = new MockHttpResponse(HttpMethod.GET, "https://raw.host.com", 200, new HttpHeaders(), wireDateTimeRfc1123JsonString.getBytes());
    Response<OffsetDateTime> restResponseBase64OffsetDataTime = (Response<OffsetDateTime>) mapperDateTimeRfc1123.map(httpResponseDateTimeRfc1123, jacksonSerder);
    OffsetDateTime dateTimeReceived = restResponseBase64OffsetDataTime.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) OffsetDateTime(org.threeten.bp.OffsetDateTime) DateTimeRfc1123(com.azure.android.core.util.DateTimeRfc1123) 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 3 with DateTimeRfc1123

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

the class MockHttpClient method send.

@Override
public void send(HttpRequest request, CancellationToken cancellationToken, HttpCallback httpCallback) {
    HttpResponse response = null;
    try {
        final URL requestUrl = request.getUrl();
        final String requestHost = requestUrl.getHost();
        final String contentType = request.getHeaders().getValue("Content-Type");
        if ("localhost".equalsIgnoreCase(requestHost)) {
            final String requestPath = requestUrl.getPath();
            final String requestPathLower = requestPath.toLowerCase();
            if (requestPathLower.startsWith("/anything")) {
                if ("HEAD".equals(request.getHttpMethod().name())) {
                    response = new MockHttpResponse(request, 200, new byte[0]);
                } else {
                    final HttpBinJSON json = new HttpBinJSON();
                    json.url(cleanseUrl(requestUrl));
                    json.headers(toMap(request.getHeaders()));
                    response = new MockHttpResponse(request, 200, json);
                }
            } else if (requestPathLower.startsWith("/bytes/")) {
                final String byteCountString = requestPath.substring("/bytes/".length());
                final int byteCount = Integer.parseInt(byteCountString);
                HttpHeaders newHeaders = new HttpHeaders(RESPONSE_HEADERS).put("Content-Type", "application/octet-stream").put("Content-Length", Integer.toString(byteCount));
                response = new MockHttpResponse(request, 200, newHeaders, byteCount == 0 ? null : new byte[byteCount]);
            } else if (requestPathLower.startsWith("/base64urlbytes/")) {
                final String byteCountString = requestPath.substring("/base64urlbytes/".length());
                final int byteCount = Integer.parseInt(byteCountString);
                final byte[] bytes = new byte[byteCount];
                for (int i = 0; i < byteCount; ++i) {
                    bytes[i] = (byte) i;
                }
                final Base64Url base64EncodedBytes = bytes.length == 0 ? null : Base64Url.encode(bytes);
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, base64EncodedBytes);
            } else if (requestPathLower.equals("/base64urllistofbytes")) {
                final List<String> base64EncodedBytesList = new ArrayList<>();
                for (int i = 0; i < 3; ++i) {
                    final int byteCount = (i + 1) * 10;
                    final byte[] bytes = new byte[byteCount];
                    for (int j = 0; j < byteCount; ++j) {
                        bytes[j] = (byte) j;
                    }
                    base64EncodedBytesList.add(Base64Url.encode(bytes).toString());
                }
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, base64EncodedBytesList);
            } else if (requestPathLower.equals("/base64urllistoflistofbytes")) {
                final List<List<String>> result = new ArrayList<>();
                for (int i = 0; i < 2; ++i) {
                    final List<String> innerList = new ArrayList<>();
                    for (int j = 0; j < (i + 1) * 2; ++j) {
                        final int byteCount = (j + 1) * 5;
                        final byte[] bytes = new byte[byteCount];
                        for (int k = 0; k < byteCount; ++k) {
                            bytes[k] = (byte) k;
                        }
                        innerList.add(Base64Url.encode(bytes).toString());
                    }
                    result.add(innerList);
                }
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, result);
            } else if (requestPathLower.equals("/base64urlmapofbytes")) {
                final Map<String, String> result = new HashMap<>();
                for (int i = 0; i < 2; ++i) {
                    final String key = Integer.toString(i);
                    final int byteCount = (i + 1) * 10;
                    final byte[] bytes = new byte[byteCount];
                    for (int j = 0; j < byteCount; ++j) {
                        bytes[j] = (byte) j;
                    }
                    result.put(key, Base64Url.encode(bytes).toString());
                }
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, result);
            } else if (requestPathLower.equals("/datetimerfc1123")) {
                final DateTimeRfc1123 now = new DateTimeRfc1123(OffsetDateTime.ofInstant(Instant.ofEpochSecond(0), ZoneOffset.UTC));
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, now.toString());
            } else if (requestPathLower.equals("/unixtime")) {
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, 0);
            } else if (requestPathLower.equals("/delete")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.data(createHttpBinResponseDataForRequest(request));
                response = new MockHttpResponse(request, 200, json);
            } else if (requestPathLower.equals("/get")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.headers(toMap(request.getHeaders()));
                response = new MockHttpResponse(request, 200, json);
            } else if (requestPathLower.equals("/patch")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.data(createHttpBinResponseDataForRequest(request));
                response = new MockHttpResponse(request, 200, json);
            } else if (requestPathLower.equals("/post")) {
                if (contentType != null && contentType.contains("x-www-form-urlencoded")) {
                    Map<String, String> parsed = bodyToMap(request);
                    final HttpBinFormDataJSON json = new HttpBinFormDataJSON();
                    HttpBinFormDataJSON.Form form = new HttpBinFormDataJSON.Form();
                    form.customerName(parsed.get("custname"));
                    form.customerEmail(parsed.get("custemail"));
                    form.customerTelephone(parsed.get("custtel"));
                    form.pizzaSize(HttpBinFormDataJSON.PizzaSize.valueOf(parsed.get("size")));
                    form.toppings(Arrays.asList(parsed.get("toppings").split(",")));
                    json.form(form);
                    response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
                } else {
                    final HttpBinJSON json = new HttpBinJSON();
                    json.url(cleanseUrl(requestUrl));
                    json.data(createHttpBinResponseDataForRequest(request));
                    json.headers(toMap(request.getHeaders()));
                    response = new MockHttpResponse(request, 200, json);
                }
            } else if (requestPathLower.equals("/put")) {
                final HttpBinJSON json = new HttpBinJSON();
                json.url(cleanseUrl(requestUrl));
                json.data(createHttpBinResponseDataForRequest(request));
                json.headers(toMap(request.getHeaders()));
                response = new MockHttpResponse(request, 200, RESPONSE_HEADERS, json);
            } else if (requestPathLower.startsWith("/status/")) {
                final String statusCodeString = requestPathLower.substring("/status/".length());
                final int statusCode = Integer.parseInt(statusCodeString);
                response = new MockHttpResponse(request, statusCode);
            }
        } else if ("echo.org".equalsIgnoreCase(requestHost)) {
            response = new MockHttpResponse(request, 200, new HttpHeaders(request.getHeaders()), request.getBody());
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    if (response == null) {
        response = new MockHttpResponse(request, 500);
    }
    httpCallback.onSuccess(response);
}
Also used : HttpHeaders(com.azure.android.core.http.HttpHeaders) HttpBinFormDataJSON(com.azure.android.core.test.implementation.entities.HttpBinFormDataJSON) ArrayList(java.util.ArrayList) HttpResponse(com.azure.android.core.http.HttpResponse) URL(java.net.URL) Base64Url(com.azure.android.core.util.Base64Url) DateTimeRfc1123(com.azure.android.core.util.DateTimeRfc1123) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) HttpBinJSON(com.azure.android.core.test.implementation.entities.HttpBinJSON)

Example 4 with DateTimeRfc1123

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

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

the class HttpResponseMapper method expandContentEncodedType.

private Type expandContentEncodedType(Type contentEncodedType, Type contentDecodeType) {
    Objects.requireNonNull(contentEncodedType);
    if (contentDecodeType == byte[].class) {
        if (contentEncodedType == Base64Url.class) {
            return Base64Url.class;
        }
    } else if (contentDecodeType == OffsetDateTime.class) {
        if (contentEncodedType == DateTimeRfc1123.class) {
            return DateTimeRfc1123.class;
        } else if (contentEncodedType == UnixTime.class) {
            return UnixTime.class;
        }
    } else if (TypeUtil.isTypeOrSubTypeOf(contentDecodeType, List.class)) {
        final Type resultElementType = TypeUtil.getTypeArgument(contentDecodeType);
        final Type wireResponseElementType = expandContentEncodedType(contentEncodedType, resultElementType);
        return TypeUtil.createParameterizedType(((ParameterizedType) contentDecodeType).getRawType(), wireResponseElementType);
    } else if (TypeUtil.isTypeOrSubTypeOf(contentDecodeType, Map.class)) {
        final Type[] typeArguments = TypeUtil.getTypeArguments(contentDecodeType);
        final Type resultValueType = typeArguments[1];
        final Type wireResponseValueType = expandContentEncodedType(contentEncodedType, resultValueType);
        return TypeUtil.createParameterizedType(((ParameterizedType) contentDecodeType).getRawType(), typeArguments[0], wireResponseValueType);
    }
    return contentDecodeType;
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) 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) HashMap(java.util.HashMap) Map(java.util.Map) Base64Url(com.azure.android.core.util.Base64Url)

Aggregations

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