Search in sources :

Example 6 with Pojo

use of org.springframework.web.testfixture.xml.Pojo in project spring-framework by spring-projects.

the class Jackson2JsonEncoderTests method encodeNonStream.

@Test
public void encodeNonStream() {
    Flux<Pojo> input = Flux.just(new Pojo("foo", "bar"), new Pojo("foofoo", "barbar"), new Pojo("foofoofoo", "barbarbar"));
    testEncode(input, Pojo.class, step -> step.consumeNextWith(expectString("[" + "{\"foo\":\"foo\",\"bar\":\"bar\"}," + "{\"foo\":\"foofoo\",\"bar\":\"barbar\"}," + "{\"foo\":\"foofoofoo\",\"bar\":\"barbarbar\"}]").andThen(DataBufferUtils::release)).verifyComplete());
}
Also used : Pojo(org.springframework.web.testfixture.xml.Pojo) Test(org.junit.jupiter.api.Test)

Example 7 with Pojo

use of org.springframework.web.testfixture.xml.Pojo in project spring-framework by spring-projects.

the class Jackson2JsonDecoderTests method decodeToMono.

@Override
@Test
public void decodeToMono() {
    Flux<DataBuffer> input = Flux.concat(stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},"), stringBuffer("{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
    ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
    testDecodeToMonoAll(input, elementType, step -> step.expectNext(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2"))).expectComplete().verify(), null, null);
}
Also used : Pojo(org.springframework.web.testfixture.xml.Pojo) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 8 with Pojo

use of org.springframework.web.testfixture.xml.Pojo in project spring-framework by spring-projects.

the class CancelWithoutDemandCodecTests method cancelWithJackson.

// gh-22107
@Test
public void cancelWithJackson() {
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    Flux<DataBuffer> flux = encoder.encode(Flux.just(new Pojo("foofoo", "barbar"), new Pojo("bar", "baz")), this.bufferFactory, ResolvableType.forClass(Pojo.class), MediaType.APPLICATION_JSON, Collections.emptyMap());
    BaseSubscriber<DataBuffer> subscriber = new ZeroDemandSubscriber();
    // Assume sync execution (e.g. encoding with Flux.just)..
    flux.subscribe(subscriber);
    subscriber.cancel();
}
Also used : Pojo(org.springframework.web.testfixture.xml.Pojo) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 9 with Pojo

use of org.springframework.web.testfixture.xml.Pojo in project spring-framework by spring-projects.

the class Jackson2SmileEncoderTests method encodeAsStream.

@Test
public void encodeAsStream() throws Exception {
    Pojo pojo1 = new Pojo("foo", "bar");
    Pojo pojo2 = new Pojo("foofoo", "barbar");
    Pojo pojo3 = new Pojo("foofoofoo", "barbarbar");
    Flux<Pojo> input = Flux.just(pojo1, pojo2, pojo3);
    ResolvableType type = ResolvableType.forClass(Pojo.class);
    Flux<DataBuffer> result = this.encoder.encode(input, bufferFactory, type, STREAM_SMILE_MIME_TYPE, null);
    Mono<MappingIterator<Pojo>> joined = DataBufferUtils.join(result).map(buffer -> {
        try {
            return this.mapper.reader().forType(Pojo.class).readValues(buffer.asInputStream(true));
        } catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
    });
    StepVerifier.create(joined).assertNext(iter -> assertThat(iter).toIterable().contains(pojo1, pojo2, pojo3)).verifyComplete();
}
Also used : Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Pojo(org.springframework.web.testfixture.xml.Pojo) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) APPLICATION_XML(org.springframework.http.MediaType.APPLICATION_XML) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test) MimeType(org.springframework.util.MimeType) Flux(reactor.core.publisher.Flux) List(java.util.List) ServerSentEvent(org.springframework.http.codec.ServerSentEvent) DataBufferUtils.release(org.springframework.core.io.buffer.DataBufferUtils.release) Jackson2ObjectMapperBuilder(org.springframework.http.converter.json.Jackson2ObjectMapperBuilder) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) ResolvableType(org.springframework.core.ResolvableType) AbstractEncoderTests(org.springframework.core.testfixture.codec.AbstractEncoderTests) Pojo(org.springframework.web.testfixture.xml.Pojo) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ResolvableType(org.springframework.core.ResolvableType) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 10 with Pojo

use of org.springframework.web.testfixture.xml.Pojo in project spring-framework by spring-projects.

the class ServerSentEventHttpMessageReaderTests method readPojo.

@Test
public void readPojo() {
    MockServerHttpRequest request = MockServerHttpRequest.post("/").body(Mono.just(stringBuffer("data:{\"foo\": \"foofoo\", \"bar\": \"barbar\"}\n\n" + "data:{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}\n\n")));
    Flux<Pojo> data = reader.read(ResolvableType.forClass(Pojo.class), request, Collections.emptyMap()).cast(Pojo.class);
    StepVerifier.create(data).consumeNextWith(pojo -> {
        assertThat(pojo.getFoo()).isEqualTo("foofoo");
        assertThat(pojo.getBar()).isEqualTo("barbar");
    }).consumeNextWith(pojo -> {
        assertThat(pojo.getFoo()).isEqualTo("foofoofoo");
        assertThat(pojo.getBar()).isEqualTo("barbarbar");
    }).expectComplete().verify();
}
Also used : Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) StepVerifier(reactor.test.StepVerifier) AbstractLeakCheckingTests(org.springframework.core.testfixture.io.buffer.AbstractLeakCheckingTests) MediaType(org.springframework.http.MediaType) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Pojo(org.springframework.web.testfixture.xml.Pojo) Mono(reactor.core.publisher.Mono) DataBuffer(org.springframework.core.io.buffer.DataBuffer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) DataBufferLimitException(org.springframework.core.io.buffer.DataBufferLimitException) Duration(java.time.Duration) ResolvableType(org.springframework.core.ResolvableType) Collections(java.util.Collections) Pojo(org.springframework.web.testfixture.xml.Pojo) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Aggregations

Pojo (org.springframework.web.testfixture.xml.Pojo)20 Test (org.junit.jupiter.api.Test)17 DataBuffer (org.springframework.core.io.buffer.DataBuffer)10 ResolvableType (org.springframework.core.ResolvableType)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)3 MediaType (org.springframework.http.MediaType)3 MimeType (org.springframework.util.MimeType)3 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 StepVerifier (reactor.test.StepVerifier)3 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 Arrays (java.util.Arrays)2 List (java.util.List)2 DataBufferUtils.release (org.springframework.core.io.buffer.DataBufferUtils.release)2 AbstractEncoderTests (org.springframework.core.testfixture.codec.AbstractEncoderTests)2