Search in sources :

Example 1 with CodecConfigurer

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

the class CodecConfigurerTests method cloneEmptyCustomCodecs.

@Test
void cloneEmptyCustomCodecs() {
    this.configurer.registerDefaults(false);
    assertThat(this.configurer.getReaders()).isEmpty();
    assertThat(this.configurer.getWriters()).isEmpty();
    CodecConfigurer clone = this.configurer.clone();
    clone.customCodecs().register(new Jackson2JsonEncoder());
    clone.customCodecs().register(new Jackson2JsonDecoder());
    clone.customCodecs().register(new ServerSentEventHttpMessageReader());
    clone.customCodecs().register(new ServerSentEventHttpMessageWriter());
    assertThat(this.configurer.getReaders()).isEmpty();
    assertThat(this.configurer.getWriters()).isEmpty();
    assertThat(clone.getReaders()).hasSize(2);
    assertThat(clone.getWriters()).hasSize(2);
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) Test(org.junit.jupiter.api.Test)

Example 2 with CodecConfigurer

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

the class CodecConfigurerTests method cloneCustomCodecs.

@Test
void cloneCustomCodecs() {
    this.configurer.registerDefaults(false);
    assertThat(this.configurer.getReaders()).isEmpty();
    assertThat(this.configurer.getWriters()).isEmpty();
    this.configurer.customCodecs().register(new Jackson2JsonEncoder());
    this.configurer.customCodecs().register(new Jackson2JsonDecoder());
    this.configurer.customCodecs().register(new ServerSentEventHttpMessageReader());
    this.configurer.customCodecs().register(new ServerSentEventHttpMessageWriter());
    assertThat(this.configurer.getReaders()).hasSize(2);
    assertThat(this.configurer.getWriters()).hasSize(2);
    CodecConfigurer clone = this.configurer.clone();
    assertThat(this.configurer.getReaders()).hasSize(2);
    assertThat(this.configurer.getWriters()).hasSize(2);
    assertThat(clone.getReaders()).hasSize(2);
    assertThat(clone.getWriters()).hasSize(2);
}
Also used : ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) Test(org.junit.jupiter.api.Test)

Example 3 with CodecConfigurer

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

the class CodecConfigurerTests method cloneDefaultCodecs.

@Test
void cloneDefaultCodecs() {
    CodecConfigurer clone = this.configurer.clone();
    Jackson2JsonDecoder jacksonDecoder = new Jackson2JsonDecoder();
    Jackson2JsonEncoder jacksonEncoder = new Jackson2JsonEncoder();
    Jaxb2XmlDecoder jaxb2Decoder = new Jaxb2XmlDecoder();
    Jaxb2XmlEncoder jaxb2Encoder = new Jaxb2XmlEncoder();
    ProtobufDecoder protoDecoder = new ProtobufDecoder();
    ProtobufEncoder protoEncoder = new ProtobufEncoder();
    clone.defaultCodecs().jackson2JsonDecoder(jacksonDecoder);
    clone.defaultCodecs().jackson2JsonEncoder(jacksonEncoder);
    clone.defaultCodecs().jaxb2Decoder(jaxb2Decoder);
    clone.defaultCodecs().jaxb2Encoder(jaxb2Encoder);
    clone.defaultCodecs().protobufDecoder(protoDecoder);
    clone.defaultCodecs().protobufEncoder(protoEncoder);
    // Clone has the customized the customizations
    List<Decoder<?>> decoders = clone.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    List<Encoder<?>> encoders = clone.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
    assertThat(decoders).contains(jacksonDecoder, jaxb2Decoder, protoDecoder);
    assertThat(encoders).contains(jacksonEncoder, jaxb2Encoder, protoEncoder);
    // Original does not have the customizations
    decoders = this.configurer.getReaders().stream().filter(reader -> reader instanceof DecoderHttpMessageReader).map(reader -> ((DecoderHttpMessageReader<?>) reader).getDecoder()).collect(Collectors.toList());
    encoders = this.configurer.getWriters().stream().filter(writer -> writer instanceof EncoderHttpMessageWriter).map(reader -> ((EncoderHttpMessageWriter<?>) reader).getEncoder()).collect(Collectors.toList());
    assertThat(decoders).doesNotContain(jacksonDecoder, jaxb2Decoder, protoDecoder);
    assertThat(encoders).doesNotContain(jacksonEncoder, jaxb2Encoder, protoEncoder);
}
Also used : CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) Decoder(org.springframework.core.codec.Decoder) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ResourceHttpMessageWriter(org.springframework.http.codec.ResourceHttpMessageWriter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FormHttpMessageReader(org.springframework.http.codec.FormHttpMessageReader) HttpMessageReader(org.springframework.http.codec.HttpMessageReader) ServerSentEventHttpMessageReader(org.springframework.http.codec.ServerSentEventHttpMessageReader) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) NettyByteBufDecoder(org.springframework.core.codec.NettyByteBufDecoder) KotlinSerializationJsonEncoder(org.springframework.http.codec.json.KotlinSerializationJsonEncoder) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BDDMockito.given(org.mockito.BDDMockito.given) ServerSentEventHttpMessageWriter(org.springframework.http.codec.ServerSentEventHttpMessageWriter) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) ResolvableType(org.springframework.core.ResolvableType) ResourceHttpMessageReader(org.springframework.http.codec.ResourceHttpMessageReader) Encoder(org.springframework.core.codec.Encoder) Jackson2SmileDecoder(org.springframework.http.codec.json.Jackson2SmileDecoder) ProtobufDecoder(org.springframework.http.codec.protobuf.ProtobufDecoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) KotlinSerializationJsonDecoder(org.springframework.http.codec.json.KotlinSerializationJsonDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) MediaType(org.springframework.http.MediaType) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) NettyByteBufEncoder(org.springframework.core.codec.NettyByteBufEncoder) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) MimeTypeUtils(org.springframework.util.MimeTypeUtils) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) Collectors(java.util.stream.Collectors) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) Test(org.junit.jupiter.api.Test) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) List(java.util.List) Jackson2SmileEncoder(org.springframework.http.codec.json.Jackson2SmileEncoder) ProtobufHttpMessageWriter(org.springframework.http.codec.protobuf.ProtobufHttpMessageWriter) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) HttpMessageWriter(org.springframework.http.codec.HttpMessageWriter) Mockito.mock(org.mockito.Mockito.mock) DecoderHttpMessageReader(org.springframework.http.codec.DecoderHttpMessageReader) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) ProtobufDecoder(org.springframework.http.codec.protobuf.ProtobufDecoder) Decoder(org.springframework.core.codec.Decoder) NettyByteBufDecoder(org.springframework.core.codec.NettyByteBufDecoder) DataBufferDecoder(org.springframework.core.codec.DataBufferDecoder) Jaxb2XmlDecoder(org.springframework.http.codec.xml.Jaxb2XmlDecoder) Jackson2SmileDecoder(org.springframework.http.codec.json.Jackson2SmileDecoder) ProtobufDecoder(org.springframework.http.codec.protobuf.ProtobufDecoder) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) KotlinSerializationJsonDecoder(org.springframework.http.codec.json.KotlinSerializationJsonDecoder) StringDecoder(org.springframework.core.codec.StringDecoder) ByteBufferDecoder(org.springframework.core.codec.ByteBufferDecoder) ByteArrayDecoder(org.springframework.core.codec.ByteArrayDecoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) CharSequenceEncoder(org.springframework.core.codec.CharSequenceEncoder) ByteBufferEncoder(org.springframework.core.codec.ByteBufferEncoder) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) KotlinSerializationJsonEncoder(org.springframework.http.codec.json.KotlinSerializationJsonEncoder) Encoder(org.springframework.core.codec.Encoder) Jaxb2XmlEncoder(org.springframework.http.codec.xml.Jaxb2XmlEncoder) ByteArrayEncoder(org.springframework.core.codec.ByteArrayEncoder) NettyByteBufEncoder(org.springframework.core.codec.NettyByteBufEncoder) DataBufferEncoder(org.springframework.core.codec.DataBufferEncoder) ProtobufEncoder(org.springframework.http.codec.protobuf.ProtobufEncoder) Jackson2SmileEncoder(org.springframework.http.codec.json.Jackson2SmileEncoder) CodecConfigurer(org.springframework.http.codec.CodecConfigurer) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)3 CodecConfigurer (org.springframework.http.codec.CodecConfigurer)3 ServerSentEventHttpMessageReader (org.springframework.http.codec.ServerSentEventHttpMessageReader)3 ServerSentEventHttpMessageWriter (org.springframework.http.codec.ServerSentEventHttpMessageWriter)3 Jackson2JsonDecoder (org.springframework.http.codec.json.Jackson2JsonDecoder)3 Jackson2JsonEncoder (org.springframework.http.codec.json.Jackson2JsonEncoder)3 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)1 List (java.util.List)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 BDDMockito.given (org.mockito.BDDMockito.given)1 Mockito.mock (org.mockito.Mockito.mock)1 ResolvableType (org.springframework.core.ResolvableType)1 ByteArrayDecoder (org.springframework.core.codec.ByteArrayDecoder)1 ByteArrayEncoder (org.springframework.core.codec.ByteArrayEncoder)1 ByteBufferDecoder (org.springframework.core.codec.ByteBufferDecoder)1 ByteBufferEncoder (org.springframework.core.codec.ByteBufferEncoder)1 CharSequenceEncoder (org.springframework.core.codec.CharSequenceEncoder)1