Search in sources :

Example 16 with TypeReference

use of cn.taketoday.core.TypeReference in project today-framework 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);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) MediaType(cn.taketoday.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(cn.taketoday.core.TypeReference) Test(org.junit.jupiter.api.Test)

Example 17 with TypeReference

use of cn.taketoday.core.TypeReference in project today-framework by TAKETODAY.

the class MappingJackson2HttpMessageConverterTests method writeSubTypeList.

// SPR-13318
@Test
public void writeSubTypeList() throws Exception {
    MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
    List<MyBean> beans = new ArrayList<>();
    MyBean foo = new MyBean();
    foo.setString("Foo");
    foo.setNumber(42);
    beans.add(foo);
    MyBean bar = new MyBean();
    bar.setString("Bar");
    bar.setNumber(123);
    beans.add(bar);
    TypeReference<List<MyInterface>> typeReference = new TypeReference<List<MyInterface>>() {
    };
    this.converter.writeInternal(beans, typeReference.getType(), outputMessage);
    String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
    assertThat(result.contains("\"string\":\"Foo\"")).isTrue();
    assertThat(result.contains("\"number\":42")).isTrue();
    assertThat(result.contains("\"string\":\"Bar\"")).isTrue();
    assertThat(result.contains("\"number\":123")).isTrue();
}
Also used : MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(cn.taketoday.core.TypeReference) Test(org.junit.jupiter.api.Test)

Example 18 with TypeReference

use of cn.taketoday.core.TypeReference in project today-framework by TAKETODAY.

the class MultipartBodyBuilderTests method builder.

@Test
public void builder() {
    MultipartBodyBuilder builder = new MultipartBodyBuilder();
    MultiValueMap<String, String> multipartData = new DefaultMultiValueMap<>();
    multipartData.add("form field", "form value");
    builder.part("key", multipartData).header("foo", "bar");
    Resource logo = new ClassPathResource("/cn/taketoday/http/converter/logo.jpg");
    builder.part("logo", logo).header("baz", "qux");
    HttpHeaders entityHeaders = HttpHeaders.create();
    entityHeaders.add("foo", "bar");
    HttpEntity<String> entity = new HttpEntity<>("body", entityHeaders);
    builder.part("entity", entity).header("baz", "qux");
    Publisher<String> publisher = Flux.just("foo", "bar", "baz");
    builder.asyncPart("publisherClass", publisher, String.class).header("baz", "qux");
    builder.asyncPart("publisherPtr", publisher, new TypeReference<String>() {
    }).header("baz", "qux");
    MultiValueMap<String, HttpEntity<?>> result = builder.build();
    assertThat(result.size()).isEqualTo(5);
    HttpEntity<?> resultEntity = result.getFirst("key");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(multipartData);
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    resultEntity = result.getFirst("logo");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(logo);
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("entity");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo("body");
    assertThat(resultEntity.getHeaders().getFirst("foo")).isEqualTo("bar");
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherClass");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.fromClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
    resultEntity = result.getFirst("publisherPtr");
    assertThat(resultEntity).isNotNull();
    assertThat(resultEntity.getBody()).isEqualTo(publisher);
    assertThat(((PublisherEntity<?, ?>) resultEntity).getResolvableType()).isEqualTo(ResolvableType.fromClass(String.class));
    assertThat(resultEntity.getHeaders().getFirst("baz")).isEqualTo("qux");
}
Also used : HttpHeaders(cn.taketoday.http.HttpHeaders) DefaultMultiValueMap(cn.taketoday.core.DefaultMultiValueMap) HttpEntity(cn.taketoday.http.HttpEntity) Resource(cn.taketoday.core.io.Resource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) ClassPathResource(cn.taketoday.core.io.ClassPathResource) TypeReference(cn.taketoday.core.TypeReference) PublisherEntity(cn.taketoday.http.client.MultipartBodyBuilder.PublisherEntity) Test(org.junit.jupiter.api.Test)

Example 19 with TypeReference

use of cn.taketoday.core.TypeReference in project today-framework by TAKETODAY.

the class GsonHttpMessageConverterTests 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);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) MediaType(cn.taketoday.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(cn.taketoday.core.TypeReference) Test(org.junit.jupiter.api.Test)

Example 20 with TypeReference

use of cn.taketoday.core.TypeReference in project today-framework by TAKETODAY.

the class GsonHttpMessageConverterTests 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\":[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, baseList.getType(), new MediaType("application", "json"), outputMessage);
    JSONAssert.assertEquals(body, outputMessage.getBodyAsString(StandardCharsets.UTF_8), true);
}
Also used : MockHttpInputMessage(cn.taketoday.http.MockHttpInputMessage) MockHttpOutputMessage(cn.taketoday.http.MockHttpOutputMessage) MediaType(cn.taketoday.http.MediaType) ArrayList(java.util.ArrayList) List(java.util.List) TypeReference(cn.taketoday.core.TypeReference) Test(org.junit.jupiter.api.Test)

Aggregations

TypeReference (cn.taketoday.core.TypeReference)30 Test (org.junit.jupiter.api.Test)28 List (java.util.List)22 MediaType (cn.taketoday.http.MediaType)16 ArrayList (java.util.ArrayList)16 MockHttpOutputMessage (cn.taketoday.http.MockHttpOutputMessage)14 MockHttpInputMessage (cn.taketoday.http.MockHttpInputMessage)12 DataBuffer (cn.taketoday.core.io.buffer.DataBuffer)6 HttpEntity (cn.taketoday.http.HttpEntity)6 Resource (cn.taketoday.core.io.Resource)4 DefaultDataBuffer (cn.taketoday.core.io.buffer.DefaultDataBuffer)4 HttpHeaders (cn.taketoday.http.HttpHeaders)4 HttpInputMessage (cn.taketoday.http.HttpInputMessage)4 DecoderHttpMessageReader (cn.taketoday.http.codec.DecoderHttpMessageReader)4 HttpMessageReader (cn.taketoday.http.codec.HttpMessageReader)4 HttpMessageWriter (cn.taketoday.http.codec.HttpMessageWriter)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 DefaultMultiValueMap (cn.taketoday.core.DefaultMultiValueMap)2 LinkedMultiValueMap (cn.taketoday.core.LinkedMultiValueMap)2 MultiValueMap (cn.taketoday.core.MultiValueMap)2