Example usage for org.springframework.web.client RestTemplate getUriTemplateHandler

List of usage examples for org.springframework.web.client RestTemplate getUriTemplateHandler

Introduction

In this page you can find the example usage for org.springframework.web.client RestTemplate getUriTemplateHandler.

Prototype

public UriTemplateHandler getUriTemplateHandler() 

Source Link

Document

Return the configured URI template handler.

Usage

From source file:org.springframework.boot.test.web.client.TestRestTemplateTests.java

@Test
public void restOperationsAreAvailable() throws Exception {
    RestTemplate delegate = mock(RestTemplate.class);
    given(delegate.getUriTemplateHandler()).willReturn(new DefaultUriBuilderFactory());
    final TestRestTemplate restTemplate = new TestRestTemplate(delegate);
    ReflectionUtils.doWithMethods(RestOperations.class, new MethodCallback() {

        @Override/*from   w  ww. j  a  va  2s  .co m*/
        public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
            Method equivalent = ReflectionUtils.findMethod(TestRestTemplate.class, method.getName(),
                    method.getParameterTypes());
            assertThat(equivalent).as("Method %s not found", method).isNotNull();
            assertThat(Modifier.isPublic(equivalent.getModifiers()))
                    .as("Method %s should have been public", equivalent).isTrue();
            try {
                equivalent.invoke(restTemplate, mockArguments(method.getParameterTypes()));
            } catch (Exception ex) {
                throw new IllegalStateException(ex);
            }
        }

        private Object[] mockArguments(Class<?>[] parameterTypes) throws Exception {
            Object[] arguments = new Object[parameterTypes.length];
            for (int i = 0; i < parameterTypes.length; i++) {
                arguments[i] = mockArgument(parameterTypes[i]);
            }
            return arguments;
        }

        @SuppressWarnings("rawtypes")
        private Object mockArgument(Class<?> type) throws Exception {
            if (String.class.equals(type)) {
                return "String";
            }
            if (Object[].class.equals(type)) {
                return new Object[0];
            }
            if (URI.class.equals(type)) {
                return new URI("http://localhost");
            }
            if (HttpMethod.class.equals(type)) {
                return HttpMethod.GET;
            }
            if (Class.class.equals(type)) {
                return Object.class;
            }
            if (RequestEntity.class.equals(type)) {
                return new RequestEntity(HttpMethod.GET, new URI("http://localhost"));
            }
            return mock(type);
        }

    }, (method) -> Modifier.isPublic(method.getModifiers()));

}

From source file:org.springframework.boot.web.client.RestTemplateBuilderTests.java

@Test
public void uriTemplateHandlerShouldApply() throws Exception {
    UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class);
    RestTemplate template = this.builder.uriTemplateHandler(uriTemplateHandler).build();
    assertThat(template.getUriTemplateHandler()).isSameAs(uriTemplateHandler);
}

From source file:org.springframework.boot.web.client.RestTemplateBuilderTests.java

@Test
public void rootUriShouldApplyAfterUriTemplateHandler() throws Exception {
    UriTemplateHandler uriTemplateHandler = mock(UriTemplateHandler.class);
    RestTemplate template = this.builder.uriTemplateHandler(uriTemplateHandler).rootUri("http://example.com")
            .build();//from w  w  w  . ja  v  a2 s .c om
    UriTemplateHandler handler = template.getUriTemplateHandler();
    handler.expand("/hello");
    assertThat(handler).isInstanceOf(RootUriTemplateHandler.class);
    verify(uriTemplateHandler).expand("http://example.com/hello");
}