use of cn.taketoday.core.TypeReference in project today-framework by TAKETODAY.
the class DefaultClientResponseTests method toEntityTypeReference.
@Test
public void toEntityTypeReference() {
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<String> result = defaultClientResponse.toEntity(new TypeReference<String>() {
}).block();
assertThat(result.getBody()).isEqualTo("foo");
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getStatusCodeValue()).isEqualTo(HttpStatus.OK.value());
assertThat(result.getHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
}
use of cn.taketoday.core.TypeReference in project today-framework by TAKETODAY.
the class DefaultClientRequestBuilderTests method bodyParameterizedTypeReference.
@Test
public void bodyParameterizedTypeReference() {
String body = "foo";
Publisher<String> publisher = Mono.just(body);
TypeReference<String> typeReference = new TypeReference<>() {
};
ClientRequest result = ClientRequest.create(POST, DEFAULT_URL).body(publisher, typeReference).build();
List<HttpMessageWriter<?>> messageWriters = new ArrayList<>();
messageWriters.add(new EncoderHttpMessageWriter<>(CharSequenceEncoder.allMimeTypes()));
ExchangeStrategies strategies = mock(ExchangeStrategies.class);
given(strategies.messageWriters()).willReturn(messageWriters);
MockClientHttpRequest request = new MockClientHttpRequest(GET, "/");
result.writeTo(request, strategies).block();
assertThat(request.getBody()).isNotNull();
StepVerifier.create(request.getBody()).expectNextCount(1).verifyComplete();
}
use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class BodyInserters method fromProducer.
/**
* Inserter to write the given producer of value(s) which must be a {@link Publisher}
* or another producer adaptable to a {@code Publisher} via
* {@link ReactiveAdapterRegistry}.
* <p>Alternatively, consider using the {@code body} shortcuts on
* {@link WebClient WebClient} and
* {@link cn.taketoday.web.reactive.function.server.ServerResponse ServerResponse}.
*
* @param <T> the type of the body
* @param producer the source of body value(s).
* @param elementTypeRef the type of values to be produced
* @return the inserter to write a producer
* @since 4.0
*/
public static <T> BodyInserter<T, ReactiveHttpOutputMessage> fromProducer(T producer, TypeReference<?> elementTypeRef) {
Assert.notNull(producer, "'producer' must not be null");
Assert.notNull(elementTypeRef, "'elementTypeRef' must not be null");
ReactiveAdapter adapter = ReactiveAdapterRegistry.getSharedInstance().getAdapter(producer.getClass());
Assert.notNull(adapter, "'producer' type is unknown to ReactiveAdapterRegistry");
return (message, context) -> writeWithMessageWriters(message, context, producer, ResolvableType.fromType(elementTypeRef), adapter);
}
use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class MappingJackson2HttpMessageConverterTests method writeParameterizedBaseType.
@Test
@SuppressWarnings("unchecked")
public void writeParameterizedBaseType() throws Exception {
TypeReference<List<MyBean>> beansList = new TypeReference<List<MyBean>>() {
};
TypeReference<List<MyBase>> baseList = new TypeReference<List<MyBase>>() {
};
String body = "[{" + "\"bytes\":\"AQI=\"," + "\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42," + "\"string\":\"Foo\"," + "\"bool\":true," + "\"fraction\":42.0}]";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
assertThat(results.size()).isEqualTo(1);
MyBean result = results.get(0);
assertThat(result.getString()).isEqualTo("Foo");
assertThat(result.getNumber()).isEqualTo(42);
assertThat(result.getFraction()).isCloseTo(42F, within(0F));
assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
assertThat(result.isBool()).isTrue();
assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(results, baseList.getType(), new MediaType("application", "json"), outputMessage);
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
use of cn.taketoday.core.TypeReference in project today-infrastructure by TAKETODAY.
the class JsonbHttpMessageConverterTests method readAndWriteParameterizedType.
@Test
@SuppressWarnings("unchecked")
public void readAndWriteParameterizedType() throws Exception {
TypeReference<List<MyBean>> beansList = new TypeReference<List<MyBean>>() {
};
String body = "[{\"bytes\":[1,2],\"array\":[\"Foo\",\"Bar\"]," + "\"number\":42,\"string\":\"Foo\",\"bool\":true,\"fraction\":42.0}]";
MockHttpInputMessage inputMessage = new MockHttpInputMessage(body.getBytes(StandardCharsets.UTF_8));
inputMessage.getHeaders().setContentType(new MediaType("application", "json"));
List<MyBean> results = (List<MyBean>) converter.read(beansList.getType(), null, inputMessage);
assertThat(results.size()).isEqualTo(1);
MyBean result = results.get(0);
assertThat(result.getString()).isEqualTo("Foo");
assertThat(result.getNumber()).isEqualTo(42);
assertThat(result.getFraction()).isCloseTo(42F, within(0F));
assertThat(result.getArray()).isEqualTo(new String[] { "Foo", "Bar" });
assertThat(result.isBool()).isTrue();
assertThat(result.getBytes()).isEqualTo(new byte[] { 0x1, 0x2 });
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(results, beansList.getType(), new MediaType("application", "json"), outputMessage);
JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Aggregations