Search in sources :

Example 1 with JacksonAdapter

use of com.microsoft.rest.serializer.JacksonAdapter in project autorest-clientruntime-for-java by Azure.

the class FlatteningSerializerTests method canFlatten.

@Test
public void canFlatten() throws Exception {
    Foo foo = new Foo();
    foo.bar = "hello.world";
    foo.baz = new ArrayList<>();
    foo.baz.add("hello");
    foo.baz.add("hello.world");
    foo.qux = new HashMap<>();
    foo.qux.put("hello", "world");
    foo.qux.put("a.b", "c.d");
    String serialized = new JacksonAdapter().serialize(foo);
    Assert.assertEquals("{\"properties\":{\"bar\":\"hello.world\",\"props\":{\"baz\":[\"hello\",\"hello.world\"],\"q\":{\"qux\":{\"a.b\":\"c.d\",\"hello\":\"world\"}}}}}", serialized);
}
Also used : JacksonAdapter(com.microsoft.rest.serializer.JacksonAdapter) Test(org.junit.Test)

Example 2 with JacksonAdapter

use of com.microsoft.rest.serializer.JacksonAdapter in project autorest-clientruntime-for-java by Azure.

the class RestClientTests method newBuilderClonesProperties.

@Test
public void newBuilderClonesProperties() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new JacksonAdapter()).withResponseBuilderFactory(new ServiceResponseBuilder.Factory()).withCredentials(new TokenCredentials("Bearer", "token")).withLogLevel(LogLevel.BASIC.withPrettyJson(true)).withInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withUserAgent("user").withNetworkInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withConnectionTimeout(100, TimeUnit.MINUTES).build();
    RestClient newClient = restClient.newBuilder().withBaseUrl("https://contoso.com").withCredentials(new BasicAuthenticationCredentials("user", "pass")).withLogLevel(LogLevel.BODY_AND_HEADERS).withUserAgent("anotheruser").withConnectionTimeout(200, TimeUnit.SECONDS).withSerializerAdapter(new SerializerAdapter<Object>() {

        @Override
        public Object serializer() {
            return null;
        }

        @Override
        public Converter.Factory converterFactory() {
            return retrofit2.converter.jackson.JacksonConverterFactory.create();
        }

        @Override
        public String serialize(Object object) throws IOException {
            return null;
        }

        @Override
        public String serializeRaw(Object object) {
            return null;
        }

        @Override
        public String serializeList(List<?> list, CollectionFormat format) {
            return null;
        }

        @Override
        public <U> U deserialize(String value, Type type) throws IOException {
            return null;
        }
    }).withResponseBuilderFactory(new ResponseBuilder.Factory() {

        @Override
        public <T, E extends RestException> ResponseBuilder<T, E> newInstance(SerializerAdapter<?> serializerAdapter) {
            return null;
        }
    }).build();
    Assert.assertNotEquals(restClient.retrofit().baseUrl().toString(), newClient.retrofit().baseUrl().toString());
    Assert.assertNotEquals(restClient.logLevel(), newClient.logLevel());
    Assert.assertNotEquals(restClient.logLevel().isPrettyJson(), newClient.logLevel().isPrettyJson());
    Assert.assertNotEquals(restClient.serializerAdapter(), newClient.serializerAdapter());
    Assert.assertNotEquals(restClient.responseBuilderFactory(), newClient.responseBuilderFactory());
    Assert.assertNotEquals(restClient.credentials(), newClient.credentials());
    for (Interceptor interceptor : restClient.httpClient().interceptors()) {
        if (interceptor instanceof UserAgentInterceptor) {
            Assert.assertEquals("user", ((UserAgentInterceptor) interceptor).userAgent());
        }
    }
    for (Interceptor interceptor : newClient.httpClient().interceptors()) {
        if (interceptor instanceof UserAgentInterceptor) {
            Assert.assertEquals("anotheruser", ((UserAgentInterceptor) interceptor).userAgent());
        }
    }
    Assert.assertNotEquals(restClient.httpClient().connectTimeoutMillis(), newClient.httpClient().connectTimeoutMillis());
}
Also used : UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) BasicAuthenticationCredentials(com.microsoft.rest.credentials.BasicAuthenticationCredentials) SerializerAdapter(com.microsoft.rest.protocol.SerializerAdapter) ResponseBuilder(com.microsoft.rest.protocol.ResponseBuilder) JacksonAdapter(com.microsoft.rest.serializer.JacksonAdapter) IOException(java.io.IOException) Response(okhttp3.Response) Type(java.lang.reflect.Type) Interceptor(okhttp3.Interceptor) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) TokenCredentials(com.microsoft.rest.credentials.TokenCredentials) Test(org.junit.Test)

Example 3 with JacksonAdapter

use of com.microsoft.rest.serializer.JacksonAdapter in project autorest-clientruntime-for-java by Azure.

the class RestClientTests method newBuilderKeepsConfigs.

@Test
public void newBuilderKeepsConfigs() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("http://localhost").withSerializerAdapter(new JacksonAdapter()).withResponseBuilderFactory(new ServiceResponseBuilder.Factory()).withCredentials(new TokenCredentials("Bearer", "token")).withLogLevel(LogLevel.BASIC).withInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withUserAgent("user").withNetworkInterceptor(new Interceptor() {

        @Override
        public Response intercept(Chain chain) throws IOException {
            return chain.proceed(chain.request());
        }
    }).withConnectionTimeout(100, TimeUnit.MINUTES).build();
    RestClient newClient = restClient.newBuilder().build();
    Assert.assertEquals(restClient.retrofit().baseUrl().toString(), newClient.retrofit().baseUrl().toString());
    Assert.assertEquals(restClient.logLevel(), newClient.logLevel());
    Assert.assertEquals(restClient.logLevel().isPrettyJson(), newClient.logLevel().isPrettyJson());
    Assert.assertEquals(restClient.serializerAdapter(), newClient.serializerAdapter());
    Assert.assertEquals(restClient.responseBuilderFactory(), newClient.responseBuilderFactory());
    Assert.assertEquals(restClient.credentials(), newClient.credentials());
    for (Interceptor interceptor : newClient.httpClient().interceptors()) {
        if (interceptor instanceof UserAgentInterceptor) {
            Assert.assertEquals("user", ((UserAgentInterceptor) interceptor).userAgent());
        }
    }
    Assert.assertEquals(restClient.httpClient().interceptors().size(), newClient.httpClient().interceptors().size());
    Assert.assertEquals(restClient.httpClient().networkInterceptors().size(), newClient.httpClient().networkInterceptors().size());
    Assert.assertEquals(TimeUnit.MINUTES.toMillis(100), newClient.httpClient().connectTimeoutMillis());
}
Also used : Response(okhttp3.Response) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) ResponseBuilder(com.microsoft.rest.protocol.ResponseBuilder) JacksonAdapter(com.microsoft.rest.serializer.JacksonAdapter) IOException(java.io.IOException) Interceptor(okhttp3.Interceptor) UserAgentInterceptor(com.microsoft.rest.interceptors.UserAgentInterceptor) TokenCredentials(com.microsoft.rest.credentials.TokenCredentials) Test(org.junit.Test)

Example 4 with JacksonAdapter

use of com.microsoft.rest.serializer.JacksonAdapter in project autorest-clientruntime-for-java by Azure.

the class RestClientTests method defaultConfigs.

@Test
public void defaultConfigs() {
    RestClient restClient = new RestClient.Builder().withBaseUrl("https://management.azure.com/").withSerializerAdapter(new JacksonAdapter()).withResponseBuilderFactory(new ServiceResponseBuilder.Factory()).build();
    Assert.assertEquals("https://management.azure.com/", restClient.retrofit().baseUrl().toString());
    Assert.assertEquals(LogLevel.NONE, restClient.logLevel());
    Assert.assertTrue(restClient.responseBuilderFactory() instanceof ServiceResponseBuilder.Factory);
    Assert.assertTrue(restClient.serializerAdapter() instanceof JacksonAdapter);
    Assert.assertNull(restClient.credentials());
}
Also used : ResponseBuilder(com.microsoft.rest.protocol.ResponseBuilder) JacksonAdapter(com.microsoft.rest.serializer.JacksonAdapter) Test(org.junit.Test)

Aggregations

JacksonAdapter (com.microsoft.rest.serializer.JacksonAdapter)4 Test (org.junit.Test)4 ResponseBuilder (com.microsoft.rest.protocol.ResponseBuilder)3 TokenCredentials (com.microsoft.rest.credentials.TokenCredentials)2 UserAgentInterceptor (com.microsoft.rest.interceptors.UserAgentInterceptor)2 IOException (java.io.IOException)2 Interceptor (okhttp3.Interceptor)2 Response (okhttp3.Response)2 BasicAuthenticationCredentials (com.microsoft.rest.credentials.BasicAuthenticationCredentials)1 SerializerAdapter (com.microsoft.rest.protocol.SerializerAdapter)1 Type (java.lang.reflect.Type)1