Minor revision of reactive support layout (ahead of 5.0 M1)
DataSourceUtils moved to main core.io.buffer package. Consistently named Jackson2JsonDecoder/Encoder and Jaxb2XmlDecoder/Encoder. Plenty of related polishing.
This commit is contained in:
@@ -28,10 +28,7 @@ import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
import org.springframework.http.codec.SseEvent;
|
||||
import org.springframework.http.codec.SseEventHttpMessageWriter;
|
||||
import org.springframework.http.codec.json.JacksonJsonEncoder;
|
||||
import org.springframework.http.codec.json.Jackson2JsonEncoder;
|
||||
import org.springframework.http.server.reactive.MockServerHttpResponse;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
@@ -39,11 +36,11 @@ import static org.junit.Assert.*;
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class SseEventHttpMessageWriterTests
|
||||
extends AbstractDataBufferAllocatingTestCase {
|
||||
public class SseEventHttpMessageWriterTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private SseEventHttpMessageWriter converter = new SseEventHttpMessageWriter(
|
||||
Collections.singletonList(new JacksonJsonEncoder()));
|
||||
Collections.singletonList(new Jackson2JsonEncoder()));
|
||||
|
||||
|
||||
@Test
|
||||
public void nullMimeType() {
|
||||
|
||||
@@ -21,7 +21,6 @@ import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -33,19 +32,19 @@ import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link JacksonJsonDecoder}.
|
||||
* Unit tests for {@link Jackson2JsonDecoder}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class Jackson2JsonDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
@Test
|
||||
public void canDecode() {
|
||||
JacksonJsonDecoder decoder = new JacksonJsonDecoder();
|
||||
Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
|
||||
|
||||
assertTrue(decoder.canDecode(null, MediaType.APPLICATION_JSON));
|
||||
assertFalse(decoder.canDecode(null, MediaType.APPLICATION_XML));
|
||||
@@ -55,7 +54,7 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
public void decodePojo() {
|
||||
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"));
|
||||
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
|
||||
Flux<Object> flux = new JacksonJsonDecoder().decode(source, elementType, null);
|
||||
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null);
|
||||
|
||||
TestSubscriber.subscribe(flux).assertNoError().assertComplete().
|
||||
assertValues(new Pojo("foofoo", "barbar"));
|
||||
@@ -68,7 +67,7 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
|
||||
Method method = getClass().getDeclaredMethod("handle", List.class);
|
||||
ResolvableType elementType = ResolvableType.forMethodParameter(method, 0);
|
||||
Mono<Object> mono = new JacksonJsonDecoder().decodeToMono(source, elementType, null);
|
||||
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType, null);
|
||||
|
||||
TestSubscriber.subscribe(mono).assertNoError().assertComplete().
|
||||
assertValues(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2")));
|
||||
@@ -80,7 +79,7 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
"[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
|
||||
|
||||
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
|
||||
Flux<Object> flux = new JacksonJsonDecoder().decode(source, elementType, null);
|
||||
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null);
|
||||
|
||||
TestSubscriber.subscribe(flux).assertNoError().assertComplete().
|
||||
assertValues(new Pojo("f1", "b1"), new Pojo("f2", "b2"));
|
||||
@@ -92,7 +91,7 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
stringBuffer("{\"withView1\" : \"with\", \"withView2\" : \"with\", \"withoutView\" : \"without\"}"));
|
||||
ResolvableType elementType = ResolvableType
|
||||
.forMethodParameter(JacksonController.class.getMethod("foo", JacksonViewBean.class), 0);
|
||||
Flux<JacksonViewBean> flux = new JacksonJsonDecoder()
|
||||
Flux<JacksonViewBean> flux = new Jackson2JsonDecoder()
|
||||
.decode(source, elementType, null).cast(JacksonViewBean.class);
|
||||
|
||||
TestSubscriber
|
||||
@@ -106,13 +105,16 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void handle(List<Pojo> list) {
|
||||
}
|
||||
|
||||
|
||||
private interface MyJacksonView1 {}
|
||||
|
||||
private interface MyJacksonView2 {}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class JacksonViewBean {
|
||||
|
||||
@@ -149,6 +151,7 @@ public class JacksonJsonDecoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class JacksonController {
|
||||
|
||||
public JacksonViewBean foo(@JsonView(MyJacksonView1.class) JacksonViewBean bean) {
|
||||
@@ -19,7 +19,6 @@ package org.springframework.http.codec.json;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
import com.fasterxml.jackson.annotation.JsonView;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
@@ -31,21 +30,14 @@ import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class JacksonJsonEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class Jackson2JsonEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private JacksonJsonEncoder encoder;
|
||||
|
||||
|
||||
@Before
|
||||
public void createEncoder() {
|
||||
this.encoder = new JacksonJsonEncoder();
|
||||
}
|
||||
private final Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
|
||||
|
||||
|
||||
@Test
|
||||
@@ -123,10 +115,12 @@ public class JacksonJsonEncoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
private static class Bar extends ParentClass {
|
||||
}
|
||||
|
||||
|
||||
private interface MyJacksonView1 {}
|
||||
|
||||
private interface MyJacksonView2 {}
|
||||
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private static class JacksonViewBean {
|
||||
|
||||
@@ -163,6 +157,7 @@ public class JacksonJsonEncoderTests extends AbstractDataBufferAllocatingTestCas
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class JacksonController {
|
||||
|
||||
@JsonView(MyJacksonView1.class)
|
||||
@@ -30,7 +30,6 @@ import org.springframework.core.io.buffer.DataBuffer;
|
||||
*/
|
||||
public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
|
||||
@Test
|
||||
public void decodeSingleChunkToJsonObject() {
|
||||
JsonObjectDecoder decoder = new JsonObjectDecoder();
|
||||
@@ -82,6 +81,7 @@ public class JsonObjectDecoderTests extends AbstractDataBufferAllocatingTestCase
|
||||
"{\"foo\": \"foofoofoo\", \"bar\": \"barbarbar\"}");
|
||||
}
|
||||
|
||||
|
||||
private static String toString(DataBuffer buffer) {
|
||||
byte[] b = new byte[buffer.readableByteCount()];
|
||||
buffer.read(b);
|
||||
|
||||
@@ -36,14 +36,12 @@ import org.springframework.http.codec.xml.jaxb.XmlType;
|
||||
import org.springframework.http.codec.xml.jaxb.XmlTypeWithName;
|
||||
import org.springframework.http.codec.xml.jaxb.XmlTypeWithNameAndNamespace;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
public class Jaxb2DecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class Jaxb2XmlDecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private static final String POJO_ROOT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
|
||||
"<pojo>" +
|
||||
@@ -64,7 +62,8 @@ public class Jaxb2DecoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
"</pojo>" +
|
||||
"<root/>";
|
||||
|
||||
private final Jaxb2Decoder decoder = new Jaxb2Decoder();
|
||||
|
||||
private final Jaxb2XmlDecoder decoder = new Jaxb2XmlDecoder();
|
||||
|
||||
private final XmlEventDecoder xmlEventDecoder = new XmlEventDecoder();
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.http.codec.xml;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.test.TestSubscriber;
|
||||
@@ -26,28 +25,22 @@ import reactor.test.TestSubscriber;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.io.buffer.AbstractDataBufferAllocatingTestCase;
|
||||
import org.springframework.core.io.buffer.DataBuffer;
|
||||
import org.springframework.core.io.buffer.DataBufferUtils;
|
||||
import org.springframework.core.io.buffer.support.DataBufferTestUtils;
|
||||
import org.springframework.core.io.buffer.support.DataBufferUtils;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.codec.Pojo;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.xmlunit.matchers.CompareMatcher.*;
|
||||
|
||||
/**
|
||||
* @author Sebastien Deleuze
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class Jaxb2EncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
public class Jaxb2XmlEncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
|
||||
private Jaxb2Encoder encoder;
|
||||
private final Jaxb2XmlEncoder encoder = new Jaxb2XmlEncoder();
|
||||
|
||||
@Before
|
||||
public void createEncoder() {
|
||||
this.encoder = new Jaxb2Encoder();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void canEncode() {
|
||||
@@ -59,7 +52,7 @@ public class Jaxb2EncoderTests extends AbstractDataBufferAllocatingTestCase {
|
||||
MediaType.APPLICATION_JSON));
|
||||
|
||||
assertTrue(this.encoder.canEncode(
|
||||
ResolvableType.forClass(Jaxb2DecoderTests.TypePojo.class),
|
||||
ResolvableType.forClass(Jaxb2XmlDecoderTests.TypePojo.class),
|
||||
MediaType.APPLICATION_XML));
|
||||
|
||||
assertFalse(this.encoder.canEncode(ResolvableType.forClass(getClass()),
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.client.reactive;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
@@ -19,18 +35,14 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.client.reactive.ClientHttpResponse;
|
||||
import org.springframework.http.codec.json.JacksonJsonDecoder;
|
||||
import org.springframework.http.codec.DecoderHttpMessageReader;
|
||||
import org.springframework.http.codec.HttpMessageReader;
|
||||
import org.springframework.http.codec.json.Jackson2JsonDecoder;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.containsString;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
@@ -40,7 +52,7 @@ import static org.mockito.Mockito.mock;
|
||||
*/
|
||||
public class ResponseExtractorsTests {
|
||||
|
||||
private HttpHeaders headers = new HttpHeaders();
|
||||
private HttpHeaders headers;
|
||||
|
||||
private ClientHttpResponse response;
|
||||
|
||||
@@ -50,6 +62,7 @@ public class ResponseExtractorsTests {
|
||||
|
||||
private ResponseErrorHandler errorHandler;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
this.headers = new HttpHeaders();
|
||||
@@ -57,13 +70,14 @@ public class ResponseExtractorsTests {
|
||||
given(this.response.getHeaders()).willReturn(headers);
|
||||
this.messageReaders = Arrays.asList(
|
||||
new DecoderHttpMessageReader<>(new StringDecoder()),
|
||||
new DecoderHttpMessageReader<>(new JacksonJsonDecoder()));
|
||||
new DecoderHttpMessageReader<>(new Jackson2JsonDecoder()));
|
||||
this.webClientConfig = mock(WebClientConfig.class);
|
||||
this.errorHandler = mock(ResponseErrorHandler.class);
|
||||
given(this.webClientConfig.getMessageReaders()).willReturn(this.messageReaders);
|
||||
given(this.webClientConfig.getResponseErrorHandler()).willReturn(this.errorHandler);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldExtractResponseEntityMono() throws Exception {
|
||||
this.headers.setContentType(MediaType.TEXT_PLAIN);
|
||||
@@ -189,7 +203,6 @@ public class ResponseExtractorsTests {
|
||||
|
||||
|
||||
private Flux<DataBuffer> createFluxBody(String... items) throws Exception {
|
||||
|
||||
DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
|
||||
return Flux.just(items)
|
||||
.map(item -> {
|
||||
@@ -204,7 +217,9 @@ public class ResponseExtractorsTests {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
protected class SomePojo {
|
||||
|
||||
public String foo;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user