Browse Source

Mutate RSocketStrategies in RSocketMessageHandler

Use rsocketStrategies field with mutate() to ensure consistency
with internal state.

Remove transparent initialization of decoders in MetadataExtractor
and expect them to be set to avoid unintended side effects.
pull/23382/head
Rossen Stoyanchev 5 years ago
parent
commit
8574f977a2
  1. 8
      spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java
  2. 4
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultMetadataExtractor.java
  3. 29
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketStrategies.java
  4. 6
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/RSocketStrategies.java
  5. 171
      spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandler.java
  6. 37
      spring-messaging/src/test/java/org/springframework/messaging/rsocket/DefaultRSocketStrategiesTests.java
  7. 8
      spring-messaging/src/test/java/org/springframework/messaging/rsocket/RSocketServerToClientIntegrationTests.java
  8. 85
      spring-messaging/src/test/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandlerTests.java

8
spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java

@ -36,11 +36,7 @@ import org.springframework.context.ConfigurableApplicationContext; @@ -36,11 +36,7 @@ import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.core.KotlinDetector;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.codec.ByteArrayDecoder;
import org.springframework.core.codec.ByteBufferDecoder;
import org.springframework.core.codec.DataBufferDecoder;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.lang.Nullable;
@ -103,10 +99,6 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C @@ -103,10 +99,6 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
public MessageMappingMessageHandler() {
setHandlerPredicate(type -> AnnotatedElementUtils.hasAnnotation(type, Controller.class));
this.decoders.add(StringDecoder.allMimeTypes());
this.decoders.add(new ByteBufferDecoder());
this.decoders.add(new ByteArrayDecoder());
this.decoders.add(new DataBufferDecoder());
}

4
spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultMetadataExtractor.java

@ -59,10 +59,6 @@ public class DefaultMetadataExtractor implements MetadataExtractor { @@ -59,10 +59,6 @@ public class DefaultMetadataExtractor implements MetadataExtractor {
/**
* Configure the decoders to use for de-serializing metadata entries.
* <p>By default this is not set.
* <p>When this extractor is passed into {@link RSocketStrategies.Builder} or
* {@link org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler
* RSocketMessageHandler}, the decoders may be left not set, and they will
* be initialized from the decoders already configured there.
*/
public void setDecoders(List<? extends Decoder<?>> decoders) {
this.decoders.clear();

29
spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketStrategies.java

@ -39,7 +39,6 @@ import org.springframework.core.io.buffer.DataBufferFactory; @@ -39,7 +39,6 @@ import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.NettyDataBufferFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.RouteMatcher;
import org.springframework.util.SimpleRouteMatcher;
@ -209,9 +208,9 @@ final class DefaultRSocketStrategies implements RSocketStrategies { @@ -209,9 +208,9 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
return new DefaultRSocketStrategies(
this.encoders, this.decoders,
this.routeMatcher != null ? this.routeMatcher : initRouteMatcher(),
getOrInitMetadataExtractor(),
this.metadataExtractor != null ? this.metadataExtractor : initMetadataExtractor(),
this.bufferFactory != null ? this.bufferFactory : initBufferFactory(),
this.adapterRegistry != null ? this.adapterRegistry : initReactiveAdapterRegistry());
this.adapterRegistry != null ? this.adapterRegistry : ReactiveAdapterRegistry.getSharedInstance());
}
private RouteMatcher initRouteMatcher() {
@ -220,31 +219,15 @@ final class DefaultRSocketStrategies implements RSocketStrategies { @@ -220,31 +219,15 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
return new SimpleRouteMatcher(pathMatcher);
}
private MetadataExtractor getOrInitMetadataExtractor() {
if (this.metadataExtractor != null) {
if (this.metadataExtractor instanceof DefaultMetadataExtractor) {
DefaultMetadataExtractor extractor = (DefaultMetadataExtractor) this.metadataExtractor;
if (extractor.getDecoders().isEmpty()) {
extractor.setDecoders(this.decoders);
}
}
return this.metadataExtractor;
}
else {
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor();
extractor.setDecoders(this.decoders);
extractor.metadataToExtract(MimeTypeUtils.TEXT_PLAIN, String.class, MetadataExtractor.ROUTE_KEY);
return extractor;
}
private MetadataExtractor initMetadataExtractor() {
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor();
extractor.setDecoders(this.decoders);
return extractor;
}
private DataBufferFactory initBufferFactory() {
return new NettyDataBufferFactory(PooledByteBufAllocator.DEFAULT);
}
private ReactiveAdapterRegistry initReactiveAdapterRegistry() {
return ReactiveAdapterRegistry.getSharedInstance();
}
}
}

6
spring-messaging/src/main/java/org/springframework/messaging/rsocket/RSocketStrategies.java

@ -125,7 +125,8 @@ public interface RSocketStrategies { @@ -125,7 +125,8 @@ public interface RSocketStrategies {
/**
* Create an {@code RSocketStrategies} instance with default settings.
* Equivalent to {@code RSocketStrategies.builder().build()}.
* Equivalent to {@code RSocketStrategies.builder().build()}. See individual
* builder methods for details on default settings.
*/
static RSocketStrategies create() {
return new DefaultRSocketStrategies.DefaultRSocketStrategiesBuilder().build();
@ -190,9 +191,6 @@ public interface RSocketStrategies { @@ -190,9 +191,6 @@ public interface RSocketStrategies {
* <p>By default this is {@link DefaultMetadataExtractor} extracting a
* route from {@code "message/x.rsocket.routing.v0"} or
* {@code "text/plain"} metadata entries.
* <p>If the extractor is a {@code DefaultMetadataExtractor}, its
* {@code decoders} property will be set, if not already set, to the
* {@link #decoder(Decoder[]) decoders} configured here.
*/
Builder metadataExtractor(@Nullable MetadataExtractor metadataExtractor);

171
spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandler.java

@ -32,10 +32,6 @@ import reactor.core.publisher.Mono; @@ -32,10 +32,6 @@ import reactor.core.publisher.Mono;
import org.springframework.beans.BeanUtils;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.codec.ByteArrayEncoder;
import org.springframework.core.codec.ByteBufferEncoder;
import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.codec.DataBufferEncoder;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.Encoder;
import org.springframework.lang.Nullable;
@ -47,7 +43,6 @@ import org.springframework.messaging.handler.annotation.MessageMapping; @@ -47,7 +43,6 @@ import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.reactive.MessageMappingMessageHandler;
import org.springframework.messaging.handler.invocation.reactive.HandlerMethodReturnValueHandler;
import org.springframework.messaging.rsocket.ClientRSocketFactoryConfigurer;
import org.springframework.messaging.rsocket.DefaultMetadataExtractor;
import org.springframework.messaging.rsocket.MetadataExtractor;
import org.springframework.messaging.rsocket.RSocketRequester;
import org.springframework.messaging.rsocket.RSocketStrategies;
@ -74,12 +69,10 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { @@ -74,12 +69,10 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
private final List<Encoder<?>> encoders = new ArrayList<>();
@Nullable
private RSocketStrategies rsocketStrategies;
@Nullable
private MetadataExtractor metadataExtractor;
private RSocketStrategies strategies;
@Nullable
private MimeType defaultDataMimeType;
@ -87,37 +80,49 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { @@ -87,37 +80,49 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
public RSocketMessageHandler() {
this.encoders.add(CharSequenceEncoder.allMimeTypes());
this.encoders.add(new ByteBufferEncoder());
this.encoders.add(new ByteArrayEncoder());
this.encoders.add(new DataBufferEncoder());
setRSocketStrategies(RSocketStrategies.create());
}
/**
* {@inheritDoc}
* <p>If {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is also set, this property is re-initialized with the decoders in it.
* Or vice versa, if {@link #setRSocketStrategies(RSocketStrategies)
* rsocketStrategies} is not set, it will be initialized from this and
* other properties.
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is set, this property is re-initialized with the decoders in it, and
* vice versa, setting this property mutates the {@code RSocketStrategies}
* to change its decoders.
* <p>By default this is set to the
* {@link RSocketStrategies.Builder#decoder(Decoder[]) defaults} from
* {@code RSocketStrategies}.
*/
@Override
public void setDecoders(List<? extends Decoder<?>> decoders) {
super.setDecoders(decoders);
this.strategies = this.strategies.mutate()
.decoders(list -> {
list.clear();
list.addAll(decoders);
})
.build();
}
/**
* Configure the encoders to use for encoding handler method return values.
* <p>If {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is also set, this property is re-initialized with the encoders in it.
* Or vice versa, if {@link #setRSocketStrategies(RSocketStrategies)
* rsocketStrategies} is not set, it will be initialized from this and
* other properties.
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is set, this property is re-initialized with the encoders in it, and
* vice versa, setting this property mutates the {@code RSocketStrategies}
* to change its encoders.
* <p>By default this is set to the
* {@link RSocketStrategies.Builder#encoder(Encoder[]) defaults} from
* {@code RSocketStrategies}.
*/
public void setEncoders(List<? extends Encoder<?>> encoders) {
this.encoders.clear();
this.encoders.addAll(encoders);
this.strategies = this.strategies.mutate()
.encoders(list -> {
list.clear();
list.addAll(encoders);
})
.build();
}
/**
@ -128,59 +133,53 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { @@ -128,59 +133,53 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
}
/**
* Provide configuration in the form of {@link RSocketStrategies} instance
* which can also be re-used to initialize a client-side
* {@link RSocketRequester}.
* <p>When this is set, in turn it sets the following:
* <ul>
* <li>{@link #setDecoders(List)}
* <li>{@link #setEncoders(List)}
* <li>{@link #setRouteMatcher(RouteMatcher)}
* <li>{@link #setMetadataExtractor(MetadataExtractor)}
* <li>{@link #setReactiveAdapterRegistry(ReactiveAdapterRegistry)}
* </ul>
* <p>By default if this is not set, it is initialized from the above.
* {@inheritDoc}
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is set, this property is re-initialized with the RouteMatcher in it, and
* vice versa, setting this property mutates the {@code RSocketStrategies}
* to change its route matcher.
* <p>By default this is set to the
* {@link RSocketStrategies.Builder#routeMatcher(RouteMatcher) defaults}
* from {@code RSocketStrategies}.
*/
public void setRSocketStrategies(RSocketStrategies rsocketStrategies) {
setDecoders(rsocketStrategies.decoders());
setEncoders(rsocketStrategies.encoders());
setRouteMatcher(rsocketStrategies.routeMatcher());
setMetadataExtractor(rsocketStrategies.metadataExtractor());
setReactiveAdapterRegistry(rsocketStrategies.reactiveAdapterRegistry());
@Override
public void setRouteMatcher(RouteMatcher routeMatcher) {
super.setRouteMatcher(routeMatcher);
this.strategies = this.strategies.mutate().routeMatcher(routeMatcher).build();
}
/**
* Return an {@link RSocketStrategies} instance initialized from the
* corresponding properties listed under {@link #setRSocketStrategies}.
* Configure the registry for adapting various reactive types.
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is set, this property is re-initialized with the
* {@code ReactiveAdapterRegistry} in it, and vice versa, setting this
* property mutates the {@code RSocketStrategies} to change its adapter
* registry.
* <p>By default this is set to the
* {@link RSocketStrategies.Builder#reactiveAdapterStrategy(ReactiveAdapterRegistry) defaults}
* from {@code RSocketStrategies}.
*/
public RSocketStrategies getRSocketStrategies() {
return this.rsocketStrategies != null ? this.rsocketStrategies : initRSocketStrategies();
}
private RSocketStrategies initRSocketStrategies() {
return RSocketStrategies.builder()
.decoders(List::clear)
.encoders(List::clear)
.decoders(decoders -> decoders.addAll(getDecoders()))
.encoders(encoders -> encoders.addAll(getEncoders()))
.routeMatcher(getRouteMatcher())
.metadataExtractor(getMetadataExtractor())
.reactiveAdapterStrategy(getReactiveAdapterRegistry())
.build();
@Override
public void setReactiveAdapterRegistry(ReactiveAdapterRegistry registry) {
super.setReactiveAdapterRegistry(registry);
this.strategies = this.strategies.mutate().reactiveAdapterStrategy(registry).build();
}
/**
* Configure a {@link MetadataExtractor} to extract the route along with
* other metadata.
* <p>By default this is {@link DefaultMetadataExtractor} extracting a
* route from {@code "message/x.rsocket.routing.v0"} or {@code "text/plain"}.
* <p>If the extractor is a {@code DefaultMetadataExtractor}, its
* {@code decoders} property will be set, if not already set, to the
* {@link #setDecoders(List)} configured here.
* <p>When {@link #setRSocketStrategies(RSocketStrategies) rsocketStrategies}
* is set, this property is re-initialized with the {@code MetadataExtractor}
* in it, and vice versa, setting this property mutates the
* {@code RSocketStrategies} to change its {@code MetadataExtractor}.
* <p>By default this is set to the
* {@link RSocketStrategies.Builder#metadataExtractor(MetadataExtractor)} defaults}
* from {@code RSocketStrategies}.
* @param extractor the extractor to use
*/
public void setMetadataExtractor(MetadataExtractor extractor) {
this.metadataExtractor = extractor;
this.strategies = this.strategies.mutate().metadataExtractor(this.metadataExtractor).build();
}
/**
@ -192,6 +191,40 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { @@ -192,6 +191,40 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
return this.metadataExtractor;
}
/**
* Configure this handler through an {@link RSocketStrategies} instance which
* can be re-used to initialize a client-side {@link RSocketRequester}.
* <p>When this property is set, in turn it sets the following:
* <ul>
* <li>{@link #setDecoders(List)}
* <li>{@link #setEncoders(List)}
* <li>{@link #setRouteMatcher(RouteMatcher)}
* <li>{@link #setMetadataExtractor(MetadataExtractor)}
* <li>{@link #setReactiveAdapterRegistry(ReactiveAdapterRegistry)}
* </ul>
* <p>By default this is set to {@link RSocketStrategies#create()} which in
* turn sets default settings for all related properties.
*/
public void setRSocketStrategies(RSocketStrategies rsocketStrategies) {
this.strategies = rsocketStrategies;
updateStateFromRSocketStrategies();
}
private void updateStateFromRSocketStrategies() {
setDecoders(this.strategies.decoders());
setEncoders(this.strategies.encoders());
setRouteMatcher(this.strategies.routeMatcher());
setMetadataExtractor(this.strategies.metadataExtractor());
setReactiveAdapterRegistry(this.strategies.reactiveAdapterRegistry());
}
/**
* Return the {@link #setRSocketStrategies configured} {@code RSocketStrategies}.
*/
public RSocketStrategies getRSocketStrategies() {
return this.strategies;
}
/**
* Configure the default content type to use for data payloads if the
* {@code SETUP} frame did not specify one.
@ -238,15 +271,6 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { @@ -238,15 +271,6 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
getArgumentResolverConfigurer().addCustomResolver(new RSocketRequesterMethodArgumentResolver());
super.afterPropertiesSet();
if (getMetadataExtractor() == null) {
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor();
extractor.setDecoders(getDecoders());
extractor.metadataToExtract(MimeTypeUtils.TEXT_PLAIN, String.class, MetadataExtractor.ROUTE_KEY);
setMetadataExtractor(extractor);
}
this.rsocketStrategies = initRSocketStrategies();
}
@Override
@ -351,8 +375,7 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler { @@ -351,8 +375,7 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
MimeType metaMimeType = StringUtils.hasText(s) ? MimeTypeUtils.parseMimeType(s) : this.defaultMetadataMimeType;
Assert.notNull(metaMimeType, "No `metadataMimeType` in ConnectionSetupPayload and no default value");
RSocketStrategies strategies = this.rsocketStrategies;
Assert.notNull(strategies, "No RSocketStrategies. Was afterPropertiesSet not called?");
RSocketStrategies strategies = getRSocketStrategies();
RSocketRequester requester = RSocketRequester.wrap(rsocket, dataMimeType, metaMimeType, strategies);
Assert.state(this.metadataExtractor != null,

37
spring-messaging/src/test/java/org/springframework/messaging/rsocket/DefaultRSocketStrategiesTests.java

@ -15,8 +15,6 @@ @@ -15,8 +15,6 @@
*/
package org.springframework.messaging.rsocket;
import java.util.Collections;
import org.junit.Test;
import org.springframework.core.ReactiveAdapterRegistry;
@ -59,6 +57,8 @@ public class DefaultRSocketStrategiesTests { @@ -59,6 +57,8 @@ public class DefaultRSocketStrategiesTests {
assertThat(strategies.routeMatcher()).isNotNull();
assertThat(strategies.metadataExtractor()).isNotNull();
assertThat(strategies.reactiveAdapterRegistry()).isNotNull();
assertThat(((DefaultMetadataExtractor) strategies.metadataExtractor()).getDecoders()).hasSize(4);
}
@Test
@ -89,39 +89,6 @@ public class DefaultRSocketStrategiesTests { @@ -89,39 +89,6 @@ public class DefaultRSocketStrategiesTests {
assertThat(strategies.reactiveAdapterRegistry()).isSameAs(registry);
}
@Test
public void metadataExtractorInitializedWithDecoders() {
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor();
RSocketStrategies strategies = RSocketStrategies.builder()
.decoders(decoders -> {
decoders.clear();
decoders.add(new ByteArrayDecoder());
decoders.add(new ByteBufferDecoder());
})
.metadataExtractor(extractor)
.build();
assertThat(((DefaultMetadataExtractor) strategies.metadataExtractor()).getDecoders()).hasSize(2);
}
@Test
public void metadataExtractorWithExplicitlySetDecoders() {
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor();
extractor.setDecoders(Collections.singletonList(StringDecoder.allMimeTypes()));
RSocketStrategies strategies = RSocketStrategies.builder()
.decoders(decoders -> {
decoders.clear();
decoders.add(new ByteArrayDecoder());
decoders.add(new ByteBufferDecoder());
})
.metadataExtractor(extractor)
.build();
assertThat(((DefaultMetadataExtractor) strategies.metadataExtractor()).getDecoders()).hasSize(1);
}
@Test
public void copyConstructor() {
RSocketStrategies strategies1 = RSocketStrategies.create();

8
spring-messaging/src/test/java/org/springframework/messaging/rsocket/RSocketServerToClientIntegrationTests.java

@ -17,6 +17,7 @@ @@ -17,6 +17,7 @@
package org.springframework.messaging.rsocket;
import java.time.Duration;
import java.util.Collections;
import io.rsocket.RSocketFactory;
import io.rsocket.SocketAcceptor;
@ -37,10 +38,12 @@ import reactor.test.StepVerifier; @@ -37,10 +38,12 @@ import reactor.test.StepVerifier;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.codec.StringDecoder;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.rsocket.annotation.ConnectMapping;
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
import org.springframework.stereotype.Controller;
import org.springframework.util.MimeTypeUtils;
/**
* Client-side handling of requests initiated from the server side.
@ -262,7 +265,10 @@ public class RSocketServerToClientIntegrationTests { @@ -262,7 +265,10 @@ public class RSocketServerToClientIntegrationTests {
@Bean
public RSocketStrategies rsocketStrategies() {
return RSocketStrategies.create();
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor();
extractor.setDecoders(Collections.singletonList(StringDecoder.allMimeTypes()));
extractor.metadataToExtract(MimeTypeUtils.TEXT_PLAIN, String.class, MetadataExtractor.ROUTE_KEY);
return RSocketStrategies.builder().metadataExtractor(extractor).build();
}
}

85
spring-messaging/src/test/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandlerTests.java

@ -17,7 +17,6 @@ package org.springframework.messaging.rsocket.annotation.support; @@ -17,7 +17,6 @@ package org.springframework.messaging.rsocket.annotation.support;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import io.rsocket.frame.FrameType;
@ -29,8 +28,6 @@ import org.springframework.core.codec.ByteArrayEncoder; @@ -29,8 +28,6 @@ import org.springframework.core.codec.ByteArrayEncoder;
import org.springframework.core.codec.ByteBufferDecoder;
import org.springframework.core.codec.ByteBufferEncoder;
import org.springframework.core.codec.CharSequenceEncoder;
import org.springframework.core.codec.DataBufferDecoder;
import org.springframework.core.codec.DataBufferEncoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.CompositeMessageCondition;
@ -58,14 +55,13 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -58,14 +55,13 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class RSocketMessageHandlerTests {
@Test
public void rsocketStrategiesInitializedFromOtherProperties() {
public void getRSocketStrategies() {
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.setDecoders(Collections.singletonList(new ByteArrayDecoder()));
handler.setEncoders(Collections.singletonList(new ByteArrayEncoder()));
handler.setRouteMatcher(new SimpleRouteMatcher(new AntPathMatcher()));
handler.setMetadataExtractor(new DefaultMetadataExtractor());
handler.setReactiveAdapterRegistry(new ReactiveAdapterRegistry());
handler.afterPropertiesSet();
RSocketStrategies strategies = handler.getRSocketStrategies();
assertThat(strategies).isNotNull();
@ -77,39 +73,10 @@ public class RSocketMessageHandlerTests { @@ -77,39 +73,10 @@ public class RSocketMessageHandlerTests {
}
@Test
public void rsocketStrategiesInitializedFromDefaults() {
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.afterPropertiesSet();
RSocketStrategies strategies = handler.getRSocketStrategies();
assertThat(strategies).isNotNull();
assertThat(strategies.encoders()).hasSize(4).hasOnlyElementsOfTypes(
CharSequenceEncoder.class,
ByteArrayEncoder.class,
ByteBufferEncoder.class,
DataBufferEncoder.class);
assertThat(strategies.decoders()).hasSize(4).hasOnlyElementsOfTypes(
StringDecoder.class,
ByteArrayDecoder.class,
ByteBufferDecoder.class,
DataBufferDecoder.class);
assertThat(strategies.routeMatcher()).isSameAs(handler.getRouteMatcher()).isNotNull();
assertThat(strategies.metadataExtractor()).isSameAs(handler.getMetadataExtractor()).isNotNull();
assertThat(strategies.reactiveAdapterRegistry()).isSameAs(handler.getReactiveAdapterRegistry()).isNotNull();
}
@Test
public void rsocketStrategiesSetsOtherProperties() {
public void setRSocketStrategies() {
RSocketStrategies strategies = RSocketStrategies.builder()
.encoders(List::clear)
.decoders(List::clear)
.encoders(encoders -> encoders.add(new ByteArrayEncoder()))
.decoders(decoders -> decoders.add(new ByteArrayDecoder()))
.encoder(new ByteArrayEncoder())
.decoder(new ByteArrayDecoder())
.routeMatcher(new SimpleRouteMatcher(new AntPathMatcher()))
.metadataExtractor(new DefaultMetadataExtractor())
.reactiveAdapterStrategy(new ReactiveAdapterRegistry())
@ -117,7 +84,6 @@ public class RSocketMessageHandlerTests { @@ -117,7 +84,6 @@ public class RSocketMessageHandlerTests {
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.setRSocketStrategies(strategies);
handler.afterPropertiesSet();
assertThat(handler.getEncoders()).isEqualTo(strategies.encoders());
assertThat(handler.getDecoders()).isEqualTo(strategies.decoders());
@ -127,48 +93,38 @@ public class RSocketMessageHandlerTests { @@ -127,48 +93,38 @@ public class RSocketMessageHandlerTests {
}
@Test
public void rsocketStrategiesReflectsFurtherChangesToOtherProperties() {
public void getRSocketStrategiesReflectsCurrentState() {
RSocketMessageHandler handler = new RSocketMessageHandler();
// RSocketStrategies sets other properties first
handler.setRSocketStrategies(RSocketStrategies.builder()
.encoders(List::clear)
.decoders(List::clear)
.encoders(encoders -> encoders.add(new ByteArrayEncoder()))
.decoders(decoders -> decoders.add(new ByteArrayDecoder()))
.routeMatcher(new SimpleRouteMatcher(new AntPathMatcher()))
.metadataExtractor(new DefaultMetadataExtractor())
.reactiveAdapterStrategy(new ReactiveAdapterRegistry())
.build());
// Followed by further changes to other properties
handler.setDecoders(Collections.singletonList(StringDecoder.allMimeTypes()));
handler.setEncoders(Collections.singletonList(CharSequenceEncoder.allMimeTypes()));
// 1. Set properties
handler.setDecoders(Collections.singletonList(new ByteArrayDecoder()));
handler.setEncoders(Collections.singletonList(new ByteArrayEncoder()));
handler.setRouteMatcher(new SimpleRouteMatcher(new AntPathMatcher()));
handler.setMetadataExtractor(new DefaultMetadataExtractor());
handler.setReactiveAdapterRegistry(new ReactiveAdapterRegistry());
handler.afterPropertiesSet();
// RSocketStrategies should reflect current state
RSocketStrategies strategies = handler.getRSocketStrategies();
assertThat(strategies.encoders()).isEqualTo(handler.getEncoders());
assertThat(strategies.decoders()).isEqualTo(handler.getDecoders());
assertThat(strategies.routeMatcher()).isSameAs(handler.getRouteMatcher());
assertThat(strategies.metadataExtractor()).isSameAs(handler.getMetadataExtractor());
assertThat(strategies.reactiveAdapterRegistry()).isSameAs(handler.getReactiveAdapterRegistry());
}
@Test
public void metadataExtractorInitializedWithDecoders() {
DefaultMetadataExtractor extractor = new DefaultMetadataExtractor();
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.setDecoders(Arrays.asList(new ByteArrayDecoder(), new ByteBufferDecoder()));
handler.setMetadataExtractor(extractor);
// 2. Set properties again
handler.setDecoders(Collections.singletonList(StringDecoder.allMimeTypes()));
handler.setEncoders(Collections.singletonList(CharSequenceEncoder.allMimeTypes()));
handler.setRouteMatcher(new SimpleRouteMatcher(new AntPathMatcher()));
handler.setMetadataExtractor(new DefaultMetadataExtractor());
handler.setReactiveAdapterRegistry(new ReactiveAdapterRegistry());
handler.afterPropertiesSet();
assertThat(((DefaultMetadataExtractor) handler.getMetadataExtractor()).getDecoders()).hasSize(2);
strategies = handler.getRSocketStrategies();
assertThat(strategies.encoders()).isEqualTo(handler.getEncoders());
assertThat(strategies.decoders()).isEqualTo(handler.getDecoders());
assertThat(strategies.routeMatcher()).isSameAs(handler.getRouteMatcher());
assertThat(strategies.metadataExtractor()).isSameAs(handler.getMetadataExtractor());
assertThat(strategies.reactiveAdapterRegistry()).isSameAs(handler.getReactiveAdapterRegistry());
}
@Test
@ -178,6 +134,7 @@ public class RSocketMessageHandlerTests { @@ -178,6 +134,7 @@ public class RSocketMessageHandlerTests {
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.setDecoders(Arrays.asList(new ByteArrayDecoder(), new ByteBufferDecoder()));
handler.setEncoders(Collections.singletonList(new ByteBufferEncoder()));
handler.setMetadataExtractor(extractor);
handler.afterPropertiesSet();

Loading…
Cancel
Save