Search in sources :

Example 16 with JacksonSerder

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

the class RestProxyXMLTests method canWriteXMLRequest.

@Test
public void canWriteXMLRequest() throws Exception {
    URL url = getClass().getClassLoader().getResource("GetContainerACLs.xml");
    byte[] bytes = Files.readAllBytes(Paths.get(url.toURI()));
    HttpRequest request = new HttpRequest(HttpMethod.PUT, "http://unused/SetContainerACLs");
    request.setBody(bytes);
    SignedIdentifierInner si = new SignedIdentifierInner();
    si.withId("MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=");
    AccessPolicy ap = new AccessPolicy();
    ap.withStart(OffsetDateTime.parse("2009-09-28T08:49:37.0000000Z"));
    ap.withExpiry(OffsetDateTime.parse("2009-09-29T08:49:37.0000000Z"));
    ap.withPermission("rwd");
    si.withAccessPolicy(ap);
    List<SignedIdentifierInner> expectedAcls = Collections.singletonList(si);
    JacksonSerder serderAdapter = new JacksonSerder();
    MockXMLReceiverClient httpClient = new MockXMLReceiverClient();
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(httpClient).build();
    MyXMLService myXMLService = RestProxy.create(MyXMLService.class, pipeline, serderAdapter);
    SignedIdentifiersWrapper wrapper = new SignedIdentifiersWrapper(expectedAcls);
    CountDownLatch latch = new CountDownLatch(1);
    myXMLService.setContainerACLs(wrapper, new Callback<Response<Void>>() {

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

        @Override
        public void onFailure(Throwable error) {
            latch.countDown();
        }
    });
    awaitOnLatch(latch, "canWriteXMLRequest");
    SignedIdentifiersWrapper actualAclsWrapped = serderAdapter.deserialize(new String(httpClient.receivedBytes, StandardCharsets.UTF_8), SignedIdentifiersWrapper.class, SerdeEncoding.XML);
    List<SignedIdentifierInner> actualAcls = actualAclsWrapped.signedIdentifiers();
    // Ideally we'd just check for "things that matter" about the XML-- e.g. the tag names, structure, and attributes needs to be the same,
    // but it doesn't matter if one document has a trailing newline or has UTF-8 in the header instead of utf-8, or if comments are missing.
    assertEquals(expectedAcls.size(), actualAcls.size());
    assertEquals(expectedAcls.get(0).id(), actualAcls.get(0).id());
    assertEquals(expectedAcls.get(0).accessPolicy().expiry(), actualAcls.get(0).accessPolicy().expiry());
    assertEquals(expectedAcls.get(0).accessPolicy().start(), actualAcls.get(0).accessPolicy().start());
    assertEquals(expectedAcls.get(0).accessPolicy().permission(), actualAcls.get(0).accessPolicy().permission());
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) HttpResponse(com.azure.android.core.http.HttpResponse) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 17 with JacksonSerder

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

the class RestProxyXMLTests method canReadXMLResponse.

@Test
public void canReadXMLResponse() {
    final HttpPipeline pipeline = new HttpPipelineBuilder().httpClient(new MockXMLHTTPClient()).build();
    MyXMLService myXMLService = RestProxy.create(MyXMLService.class, pipeline, new JacksonSerder());
    final SignedIdentifiersWrapperOrError wrapperOrError = new SignedIdentifiersWrapperOrError();
    CountDownLatch latch = new CountDownLatch(1);
    myXMLService.getContainerACLs(new Callback<Response<SignedIdentifiersWrapper>>() {

        @Override
        public void onSuccess(Response<SignedIdentifiersWrapper> response) {
            try {
                wrapperOrError.wrapper = response.getValue();
            } finally {
                latch.countDown();
            }
        }

        @Override
        public void onFailure(Throwable error) {
            try {
                wrapperOrError.error = error;
            } finally {
                latch.countDown();
            }
        }
    });
    awaitOnLatch(latch, "canReadXMLResponse");
    if (wrapperOrError.error != null) {
        fail(wrapperOrError.error);
    } else {
        assertNotNull(wrapperOrError.wrapper);
        assertNotNull(wrapperOrError.wrapper.signedIdentifiers());
        assertNotEquals(0, wrapperOrError.wrapper.signedIdentifiers().size());
    }
}
Also used : JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) HttpPipelineBuilder(com.azure.android.core.http.HttpPipelineBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) HttpResponse(com.azure.android.core.http.HttpResponse) HttpPipeline(com.azure.android.core.http.HttpPipeline) Test(org.junit.jupiter.api.Test)

Example 18 with JacksonSerder

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

the class SwaggerMethodParserTests method cancellationTokenIndex.

@Test
public void cancellationTokenIndex() throws NoSuchMethodException {
    Class<CancellationTokenParamMethods> clazz = CancellationTokenParamMethods.class;
    Method cancellationTokenCorrectIndexMethod = clazz.getDeclaredMethod("cancellationTokenCorrectIndex", Integer.class, CancellationToken.class, Callback.class);
    SwaggerMethodParser methodParser0 = new SwaggerMethodParser("https://raw.host.com", cancellationTokenCorrectIndexMethod, new JacksonSerder(), this.logger);
    Assertions.assertEquals(1, methodParser0.cancellationTokenArgIndex);
    Method cancellationTokenIncorrectIndexMethod = clazz.getDeclaredMethod("cancellationTokenIncorrectIndex", CancellationToken.class, Integer.class, Callback.class);
    SwaggerMethodParser methodParser1 = new SwaggerMethodParser("https://raw.host.com", cancellationTokenIncorrectIndexMethod, new JacksonSerder(), this.logger);
    // CancellationToken when present must be second last arg, its the contract, presence of
    // it in any other index (like in this case) is ignored.
    Assertions.assertEquals(-1, methodParser1.cancellationTokenArgIndex);
}
Also used : JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) Method(java.lang.reflect.Method) Test(org.junit.jupiter.api.Test)

Example 19 with JacksonSerder

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

the class HttpRequestMapperTests method formSubstitution.

@ParameterizedTest
@MethodSource("formSubstitutionSupplier")
public void formSubstitution(Method method, Object[] arguments, String expectedBodyContentType, Object expectedBody) {
    HttpRequestMapper mapper = new HttpRequestMapper("https://raw.host.com", method, new JacksonSerder());
    assertEquals(expectedBody, mapper.applyFormDataMapping(arguments));
}
Also used : JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 20 with JacksonSerder

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

the class HttpRequestMapperTests method jsonBodySubstitution.

@ParameterizedTest
@MethodSource("jsonBodySubstitutionSupplier")
public void jsonBodySubstitution(Method method, Object[] arguments, String expectedBodyContentType, Object expectedBody) throws IOException {
    HttpRequestMapper mapper = new HttpRequestMapper("https://raw.host.com", method, new JacksonSerder());
    HttpRequest httpRequest = mapper.map(arguments);
    byte[] content = httpRequest.getBody();
    if (expectedBody == null) {
        Assertions.assertNull(content);
    } else {
        assertEquals(expectedBodyContentType, httpRequest.getHeaders().getValue("Content-Type"));
        assertEquals(expectedBody, new String(content, StandardCharsets.UTF_8));
    }
}
Also used : HttpRequest(com.azure.android.core.http.HttpRequest) JacksonSerder(com.azure.android.core.serde.jackson.JacksonSerder) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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