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);
}
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);
}
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();
}
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);
}
}
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());
}
Aggregations