Search in sources :

Example 21 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class DefaultClientResponseTests method toEntityListTypeReference.

@Test
public void toEntityListTypeReference() {
    byte[] bytes = "foo".getBytes(StandardCharsets.UTF_8);
    DefaultDataBuffer dataBuffer = DefaultDataBufferFactory.sharedInstance.wrap(ByteBuffer.wrap(bytes));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    mockTextPlainResponse(body);
    List<HttpMessageReader<?>> messageReaders = Collections.singletonList(new DecoderHttpMessageReader<>(StringDecoder.allMimeTypes()));
    given(mockExchangeStrategies.messageReaders()).willReturn(messageReaders);
    ResponseEntity<List<String>> result = defaultClientResponse.toEntityList(new ParameterizedTypeReference<String>() {
    }).block();
    assertThat(result.getBody()).isEqualTo(Collections.singletonList("foo"));
    assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
    assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) List(java.util.List) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 22 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class HttpMessageConverterExtractorTests method generics.

@Test
@SuppressWarnings("unchecked")
void generics() throws IOException {
    responseHeaders.setContentType(contentType);
    String expected = "Foo";
    ParameterizedTypeReference<List<String>> reference = new ParameterizedTypeReference<>() {
    };
    Type type = reference.getType();
    GenericHttpMessageConverter<String> converter = mock(GenericHttpMessageConverter.class);
    HttpMessageConverterExtractor<?> extractor = new HttpMessageConverterExtractor<List<String>>(type, asList(converter));
    given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value());
    given(response.getHeaders()).willReturn(responseHeaders);
    given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes()));
    given(converter.canRead(type, null, contentType)).willReturn(true);
    given(converter.read(eq(type), eq(null), any(HttpInputMessage.class))).willReturn(expected);
    Object result = extractor.extractData(response);
    assertThat(result).isEqualTo(expected);
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) MediaType(org.springframework.http.MediaType) Type(java.lang.reflect.Type) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) ByteArrayInputStream(java.io.ByteArrayInputStream) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.jupiter.api.Test)

Example 23 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class RestTemplateTests method exchangeParameterizedType.

@Test
@SuppressWarnings("rawtypes")
void exchangeParameterizedType() throws Exception {
    GenericHttpMessageConverter converter = mock(GenericHttpMessageConverter.class);
    template.setMessageConverters(Collections.<HttpMessageConverter<?>>singletonList(converter));
    ParameterizedTypeReference<List<Integer>> intList = new ParameterizedTypeReference<>() {
    };
    given(converter.canRead(intList.getType(), null, null)).willReturn(true);
    given(converter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN));
    given(converter.canWrite(String.class, String.class, null)).willReturn(true);
    HttpHeaders requestHeaders = new HttpHeaders();
    mockSentRequest(POST, "https://example.com", requestHeaders);
    List<Integer> expected = Collections.singletonList(42);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.TEXT_PLAIN);
    responseHeaders.setContentLength(10);
    mockResponseStatus(HttpStatus.OK);
    given(response.getHeaders()).willReturn(responseHeaders);
    given(response.getBody()).willReturn(new ByteArrayInputStream(Integer.toString(42).getBytes()));
    given(converter.canRead(intList.getType(), null, MediaType.TEXT_PLAIN)).willReturn(true);
    given(converter.read(eq(intList.getType()), eq(null), any(HttpInputMessage.class))).willReturn(expected);
    HttpHeaders entityHeaders = new HttpHeaders();
    entityHeaders.set("MyHeader", "MyValue");
    HttpEntity<String> requestEntity = new HttpEntity<>("Hello World", entityHeaders);
    ResponseEntity<List<Integer>> result = template.exchange("https://example.com", POST, requestEntity, intList);
    assertThat(result.getBody()).as("Invalid POST result").isEqualTo(expected);
    assertThat(result.getHeaders().getContentType()).as("Invalid Content-Type").isEqualTo(MediaType.TEXT_PLAIN);
    assertThat(requestHeaders.getFirst("Accept")).as("Invalid Accept header").isEqualTo(MediaType.TEXT_PLAIN_VALUE);
    assertThat(requestHeaders.getFirst("MyHeader")).as("Invalid custom header").isEqualTo("MyValue");
    assertThat(result.getStatusCode()).as("Invalid status code").isEqualTo(HttpStatus.OK);
    verify(response).close();
}
Also used : HttpInputMessage(org.springframework.http.HttpInputMessage) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) List(java.util.List) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) Test(org.junit.jupiter.api.Test)

Example 24 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project java-chassis by ServiceComb.

the class TestRestTemplateWrapper method exchangeUsingParameterizedTypeWithUnderlyingRestTemplate.

@Test
public void exchangeUsingParameterizedTypeWithUnderlyingRestTemplate() {
    ParameterizedTypeReference<List<String>> typeReference = new ParameterizedTypeReference<List<String>>() {
    };
    ResponseEntity<List<String>> actual;
    for (HttpMethod method : httpMethods) {
        when(underlying.exchange(url, method, requestEntity, typeReference, param1, param2)).thenReturn(typedResponse);
        actual = wrapper.exchange(url, method, requestEntity, typeReference, param1, param2);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(url, method, requestEntity, typeReference, param1, param2);
        when(underlying.exchange(url, method, requestEntity, typeReference, paramsMap)).thenReturn(typedResponse);
        actual = wrapper.exchange(url, method, requestEntity, typeReference, paramsMap);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(url, method, requestEntity, typeReference, paramsMap);
        when(underlying.exchange(uri, method, requestEntity, typeReference)).thenReturn(typedResponse);
        actual = wrapper.exchange(uri, method, requestEntity, typeReference);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(uri, method, requestEntity, typeReference);
        RequestEntity<String> request = new RequestEntity<>(method, uri);
        when(underlying.exchange(request, typeReference)).thenReturn(typedResponse);
        actual = wrapper.exchange(request, typeReference);
        assertThat(actual, is(typedResponse));
        verify(underlying).exchange(request, typeReference);
    }
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) RequestEntity(org.springframework.http.RequestEntity) HttpMethod(org.springframework.http.HttpMethod) Test(org.junit.Test)

Example 25 with ParameterizedTypeReference

use of org.springframework.core.ParameterizedTypeReference in project spring-framework by spring-projects.

the class MappingJackson2HttpMessageConverterTests method readParameterizedType.

@Test
@SuppressWarnings("unchecked")
public void readParameterizedType() throws IOException {
    ParameterizedTypeReference<List<MyBean>> beansList = new ParameterizedTypeReference<List<MyBean>>() {
    };
    String body = "[{\"bytes\":\"AQI=\",\"array\":[\"Foo\",\"Bar\"],\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes("UTF-8"));
    inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
    assertEquals(1, results.size());
    MyBean result = results.get(0);
    assertEquals("Foo", result.getString());
    assertEquals(42, result.getNumber());
    assertEquals(42F, result.getFraction(), 0F);
    assertArrayEquals(new String[] { "Foo", "Bar" }, result.getArray());
    assertTrue(result.isBool());
    assertArrayEquals(new byte[] { 0x1, 0x2 }, result.getBytes());
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) MediaType(org.springframework.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)57 MediaType (org.springframework.http.MediaType)29 HttpHeaders (org.springframework.http.HttpHeaders)25 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)22 List (java.util.List)20 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)18 Test (org.junit.jupiter.api.Test)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)12 URI (java.net.URI)11 Test (org.junit.Test)10 MockHttpInputMessage (org.springframework.http.MockHttpInputMessage)8 URISyntaxException (java.net.URISyntaxException)7 MockHttpOutputMessage (org.springframework.http.MockHttpOutputMessage)7 DataBuffer (org.springframework.core.io.buffer.DataBuffer)6 HttpEntity (org.springframework.http.HttpEntity)6 Resources (org.springframework.hateoas.Resources)5 Traverson (org.springframework.hateoas.client.Traverson)5 IOException (java.io.IOException)4 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)4