Search in sources :

Example 6 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class Jaxb2XmlDecoderTests method decodeSingleXmlRootElement.

@Test
public void decodeSingleXmlRootElement() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_ROOT));
    Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap());
    StepVerifier.create(output).expectNext(new Pojo("foofoo", "barbar")).expectComplete().verify();
}
Also used : Pojo(org.springframework.http.codec.Pojo) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 7 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class Jaxb2XmlDecoderTests method splitMultipleBranches.

@Test
public void splitMultipleBranches() throws Exception {
    Flux<XMLEvent> xmlEvents = this.xmlEventDecoder.decode(Flux.just(stringBuffer(POJO_CHILD)), null, null, Collections.emptyMap());
    Flux<List<XMLEvent>> result = this.decoder.split(xmlEvents, new QName("pojo"));
    StepVerifier.create(result).consumeNextWith(events -> {
        assertEquals(8, events.size());
        assertStartElement(events.get(0), "pojo");
        assertStartElement(events.get(1), "foo");
        assertCharacters(events.get(2), "foo");
        assertEndElement(events.get(3), "foo");
        assertStartElement(events.get(4), "bar");
        assertCharacters(events.get(5), "bar");
        assertEndElement(events.get(6), "bar");
        assertEndElement(events.get(7), "pojo");
    }).consumeNextWith(events -> {
        assertEquals(8, events.size());
        assertStartElement(events.get(0), "pojo");
        assertStartElement(events.get(1), "foo");
        assertCharacters(events.get(2), "foofoo");
        assertEndElement(events.get(3), "foo");
        assertStartElement(events.get(4), "bar");
        assertCharacters(events.get(5), "barbar");
        assertEndElement(events.get(6), "bar");
        assertEndElement(events.get(7), "pojo");
    }).expectComplete().verify();
}
Also used : XmlRootElement(org.springframework.http.codec.xml.jaxb.XmlRootElement) StepVerifier(reactor.test.StepVerifier) AbstractDataBufferAllocatingTestCase(org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase) XmlRootElementWithNameAndNamespace(org.springframework.http.codec.xml.jaxb.XmlRootElementWithNameAndNamespace) MediaType(org.springframework.http.MediaType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DataBuffer(org.springframework.core.io.buffer.DataBuffer) XmlRootElementWithName(org.springframework.http.codec.xml.jaxb.XmlRootElementWithName) Flux(reactor.core.publisher.Flux) List(java.util.List) XMLEvent(javax.xml.stream.events.XMLEvent) Assert.assertFalse(org.junit.Assert.assertFalse) QName(javax.xml.namespace.QName) ResolvableType(org.springframework.core.ResolvableType) XmlType(org.springframework.http.codec.xml.jaxb.XmlType) XmlTypeWithName(org.springframework.http.codec.xml.jaxb.XmlTypeWithName) XmlTypeWithNameAndNamespace(org.springframework.http.codec.xml.jaxb.XmlTypeWithNameAndNamespace) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Pojo(org.springframework.http.codec.Pojo) QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) List(java.util.List) Test(org.junit.Test)

Example 8 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class Jaxb2XmlDecoderTests method toExpectedQName.

@Test
public void toExpectedQName() {
    assertEquals(new QName("pojo"), this.decoder.toQName(Pojo.class));
    assertEquals(new QName("pojo"), this.decoder.toQName(TypePojo.class));
    assertEquals(new QName("namespace", "name"), this.decoder.toQName(XmlRootElementWithNameAndNamespace.class));
    assertEquals(new QName("namespace", "name"), this.decoder.toQName(XmlRootElementWithName.class));
    assertEquals(new QName("namespace", "xmlRootElement"), this.decoder.toQName(XmlRootElement.class));
    assertEquals(new QName("namespace", "name"), this.decoder.toQName(XmlTypeWithNameAndNamespace.class));
    assertEquals(new QName("namespace", "name"), this.decoder.toQName(XmlTypeWithName.class));
    assertEquals(new QName("namespace", "xmlType"), this.decoder.toQName(XmlType.class));
}
Also used : XmlRootElement(org.springframework.http.codec.xml.jaxb.XmlRootElement) Pojo(org.springframework.http.codec.Pojo) XmlRootElementWithNameAndNamespace(org.springframework.http.codec.xml.jaxb.XmlRootElementWithNameAndNamespace) QName(javax.xml.namespace.QName) XmlTypeWithNameAndNamespace(org.springframework.http.codec.xml.jaxb.XmlTypeWithNameAndNamespace) XmlRootElementWithName(org.springframework.http.codec.xml.jaxb.XmlRootElementWithName) XmlTypeWithName(org.springframework.http.codec.xml.jaxb.XmlTypeWithName) XmlType(org.springframework.http.codec.xml.jaxb.XmlType) Test(org.junit.Test)

Example 9 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class Jaxb2XmlDecoderTests method decodeMultipleXmlRootElement.

@Test
public void decodeMultipleXmlRootElement() throws Exception {
    Flux<DataBuffer> source = Flux.just(stringBuffer(POJO_CHILD));
    Flux<Object> output = this.decoder.decode(source, ResolvableType.forClass(Pojo.class), null, Collections.emptyMap());
    StepVerifier.create(output).expectNext(new Pojo("foo", "bar")).expectNext(new Pojo("foofoo", "barbar")).expectComplete().verify();
}
Also used : Pojo(org.springframework.http.codec.Pojo) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 10 with Pojo

use of org.springframework.http.codec.Pojo in project spring-framework by spring-projects.

the class WebClientIntegrationTests method jsonPojoFlux.

@Test
public void jsonPojoFlux() throws Exception {
    this.server.enqueue(new MockResponse().setHeader("Content-Type", "application/json").setBody("[{\"bar\":\"bar1\",\"foo\":\"foo1\"},{\"bar\":\"bar2\",\"foo\":\"foo2\"}]"));
    Flux<Pojo> result = this.webClient.get().uri("/pojos").accept(MediaType.APPLICATION_JSON).exchange().flatMap(response -> response.bodyToFlux(Pojo.class));
    StepVerifier.create(result).consumeNextWith(p -> assertThat(p.getBar(), Matchers.is("bar1"))).consumeNextWith(p -> assertThat(p.getBar(), Matchers.is("bar2"))).expectComplete().verify(Duration.ofSeconds(3));
    RecordedRequest recordedRequest = server.takeRequest();
    Assert.assertEquals(1, server.getRequestCount());
    Assert.assertEquals("/pojos", recordedRequest.getPath());
    Assert.assertEquals("application/json", recordedRequest.getHeader(HttpHeaders.ACCEPT));
}
Also used : StepVerifier(reactor.test.StepVerifier) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Matchers(org.hamcrest.Matchers) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Flux(reactor.core.publisher.Flux) HttpStatus(org.springframework.http.HttpStatus) Duration(java.time.Duration) After(org.junit.After) MockWebServer(okhttp3.mockwebserver.MockWebServer) BodyInserters(org.springframework.web.reactive.function.BodyInserters) MockResponse(okhttp3.mockwebserver.MockResponse) Assert(org.junit.Assert) Before(org.junit.Before) Pojo(org.springframework.http.codec.Pojo) RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) Pojo(org.springframework.http.codec.Pojo) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 Pojo (org.springframework.http.codec.Pojo)11 DataBuffer (org.springframework.core.io.buffer.DataBuffer)7 ResolvableType (org.springframework.core.ResolvableType)5 MockResponse (okhttp3.mockwebserver.MockResponse)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 QName (javax.xml.namespace.QName)2 MediaType (org.springframework.http.MediaType)2 XmlRootElement (org.springframework.http.codec.xml.jaxb.XmlRootElement)2 XmlRootElementWithName (org.springframework.http.codec.xml.jaxb.XmlRootElementWithName)2 XmlRootElementWithNameAndNamespace (org.springframework.http.codec.xml.jaxb.XmlRootElementWithNameAndNamespace)2 XmlType (org.springframework.http.codec.xml.jaxb.XmlType)2 XmlTypeWithName (org.springframework.http.codec.xml.jaxb.XmlTypeWithName)2 XmlTypeWithNameAndNamespace (org.springframework.http.codec.xml.jaxb.XmlTypeWithNameAndNamespace)2 Flux (reactor.core.publisher.Flux)2 StepVerifier (reactor.test.StepVerifier)2 Duration (java.time.Duration)1 Collections (java.util.Collections)1 List (java.util.List)1 XMLEvent (javax.xml.stream.events.XMLEvent)1