DATACMNS-836 - Polishing.
RepositoryMetadata now exposes an ….isReactiveRepository() that's implemented by inspecting the backing repository interface for any reactive wrapper type appearing in method signatures. That allows us to move down the use of a ConversionService down to ReactiveRepositoryInformation.WrapperConversionMatch. Made a couple of methods static that could be. Simplified some logic using streams. A bit better wording in assertions. Some formatting when using streams.
This commit is contained in:
@@ -93,4 +93,9 @@ public final class DummyRepositoryInformation implements RepositoryInformation {
|
||||
public Set<Class<?>> getAlternativeDomainTypes() {
|
||||
return metadata.getAlternativeDomainTypes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReactiveRepository() {
|
||||
return metadata.isReactiveRepository();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2015 the original author or authors.
|
||||
* Copyright 2011-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.
|
||||
@@ -37,6 +37,7 @@ import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
* Unit test for {@link QueryExecuterMethodInterceptor}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class QueryExecutorMethodInterceptorUnitTests {
|
||||
@@ -61,6 +62,7 @@ public class QueryExecutorMethodInterceptorUnitTests {
|
||||
when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(strategy);
|
||||
|
||||
factory.new QueryExecutorMethodInterceptor(information);
|
||||
|
||||
verify(strategy, times(0)).resolveQuery(any(Method.class), any(RepositoryMetadata.class),
|
||||
any(ProjectionFactory.class), any(NamedQueries.class));
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.data.repository.core.support;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@@ -32,18 +34,20 @@ import org.springframework.data.repository.reactive.ReactiveSortingRepository;
|
||||
import org.springframework.data.repository.reactive.RxJava1CrudRepository;
|
||||
import org.springframework.data.repository.util.ReactiveWrapperConverters;
|
||||
|
||||
import rx.Observable;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ReactiveRepositoryInformation}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ReactiveRepositoryInformationUnitTests {
|
||||
|
||||
static final Class<ReactiveJavaInterfaceWithGenerics> REPOSITORY = ReactiveJavaInterfaceWithGenerics.class;
|
||||
|
||||
/**
|
||||
* @see DATACMNS-836
|
||||
*/
|
||||
@Test
|
||||
public void discoversMethodWithoutComparingReturnType() throws Exception {
|
||||
|
||||
@@ -56,6 +60,9 @@ public class ReactiveRepositoryInformationUnitTests {
|
||||
assertThat(reference.getName(), is("deleteAll"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-836
|
||||
*/
|
||||
@Test
|
||||
public void discoversMethodWithConvertibleArguments() throws Exception {
|
||||
|
||||
@@ -64,8 +71,7 @@ public class ReactiveRepositoryInformationUnitTests {
|
||||
|
||||
Method method = RxJava1InterfaceWithGenerics.class.getMethod("save", Observable.class);
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(RxJava1InterfaceWithGenerics.class);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null,
|
||||
conversionService);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null);
|
||||
|
||||
Method reference = information.getTargetClassMethod(method);
|
||||
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
|
||||
@@ -73,16 +79,15 @@ public class ReactiveRepositoryInformationUnitTests {
|
||||
assertThat(reference.getParameterTypes()[0], is(equalTo(Publisher.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-836
|
||||
*/
|
||||
@Test
|
||||
public void discoversMethodAssignableArguments() throws Exception {
|
||||
|
||||
DefaultConversionService conversionService = new DefaultConversionService();
|
||||
ReactiveWrapperConverters.registerConvertersIn(conversionService);
|
||||
|
||||
Method method = ReactiveSortingRepository.class.getMethod("save", Publisher.class);
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(ReactiveJavaInterfaceWithGenerics.class);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null,
|
||||
conversionService);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null);
|
||||
|
||||
Method reference = information.getTargetClassMethod(method);
|
||||
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
|
||||
@@ -90,16 +95,15 @@ public class ReactiveRepositoryInformationUnitTests {
|
||||
assertThat(reference.getParameterTypes()[0], is(equalTo(Publisher.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-836
|
||||
*/
|
||||
@Test
|
||||
public void discoversMethodExactIterableArguments() throws Exception {
|
||||
|
||||
DefaultConversionService conversionService = new DefaultConversionService();
|
||||
ReactiveWrapperConverters.registerConvertersIn(conversionService);
|
||||
|
||||
Method method = ReactiveJavaInterfaceWithGenerics.class.getMethod("save", Iterable.class);
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(ReactiveJavaInterfaceWithGenerics.class);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null,
|
||||
conversionService);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null);
|
||||
|
||||
Method reference = information.getTargetClassMethod(method);
|
||||
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
|
||||
@@ -107,6 +111,9 @@ public class ReactiveRepositoryInformationUnitTests {
|
||||
assertThat(reference.getParameterTypes()[0], is(equalTo(Iterable.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-836
|
||||
*/
|
||||
@Test
|
||||
public void discoversMethodExactObjectArguments() throws Exception {
|
||||
|
||||
@@ -115,8 +122,7 @@ public class ReactiveRepositoryInformationUnitTests {
|
||||
|
||||
Method method = ReactiveJavaInterfaceWithGenerics.class.getMethod("save", Object.class);
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(ReactiveJavaInterfaceWithGenerics.class);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null,
|
||||
conversionService);
|
||||
DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null);
|
||||
|
||||
Method reference = information.getTargetClassMethod(method);
|
||||
assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass());
|
||||
|
||||
@@ -15,8 +15,12 @@
|
||||
*/
|
||||
package org.springframework.data.repository.core.support;
|
||||
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import rx.Single;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -31,9 +35,6 @@ import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
|
||||
import org.springframework.data.repository.util.ReactiveWrapperConverters;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import rx.Single;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RepositoryFactorySupport} using reactive wrapper types.
|
||||
*
|
||||
@@ -56,7 +57,6 @@ public class ReactiveWrapperRepositoryFactorySupportUnitTests {
|
||||
ReactiveWrapperConverters.registerConvertersIn(defaultConversionService);
|
||||
|
||||
factory = new DummyRepositoryFactory(backingRepo);
|
||||
factory.setConversionService(defaultConversionService);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,6 +102,7 @@ public class ReactiveWrapperRepositoryFactorySupportUnitTests {
|
||||
* @see DATACMNS-836
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void callsMethodOnBaseImplementationWithTypeConversion() {
|
||||
|
||||
Single<Long> ids = Single.just(1L);
|
||||
|
||||
@@ -19,6 +19,11 @@ import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import rx.Observable;
|
||||
import rx.Single;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -40,11 +45,6 @@ import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import rx.Observable;
|
||||
import rx.Single;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ResultProcessor}.
|
||||
*
|
||||
@@ -109,7 +109,7 @@ public class ResultProcessorUnitTests {
|
||||
ResultProcessor information = getProcessor("findAllProjection");
|
||||
|
||||
List<Map<String, Object>> source = new ArrayList<Map<String, Object>>(
|
||||
Arrays.asList(Collections.<String, Object> singletonMap("lastname", "Matthews")));
|
||||
Arrays.asList(Collections.<String, Object>singletonMap("lastname", "Matthews")));
|
||||
|
||||
List<SampleProjection> result = information.processResult(source);
|
||||
|
||||
@@ -278,6 +278,7 @@ public class ResultProcessorUnitTests {
|
||||
Object result = getProcessor("findMonoSample").processResult(samples);
|
||||
|
||||
assertThat(result, is(instanceOf(Mono.class)));
|
||||
|
||||
Object content = ((Mono<Object>) result).block();
|
||||
|
||||
assertThat(content, is(instanceOf(Sample.class)));
|
||||
@@ -295,6 +296,7 @@ public class ResultProcessorUnitTests {
|
||||
Object result = getProcessor("findSingleSample").processResult(samples);
|
||||
|
||||
assertThat(result, is(instanceOf(Single.class)));
|
||||
|
||||
Object content = ((Single<Object>) result).toBlocking().value();
|
||||
|
||||
assertThat(content, is(instanceOf(Sample.class)));
|
||||
@@ -304,20 +306,16 @@ public class ResultProcessorUnitTests {
|
||||
* @see DATACMNS-836
|
||||
*/
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void refrainsFromProjectingUsingReactiveWrappersIfThePreparingConverterReturnsACompatibleInstance()
|
||||
throws Exception {
|
||||
|
||||
ResultProcessor processor = getProcessor("findMonoSampleDto");
|
||||
|
||||
Object result = processor.processResult(Mono.just(new Sample("Dave", "Matthews")), new Converter<Object, Object>() {
|
||||
|
||||
@Override
|
||||
public Object convert(Object source) {
|
||||
return new SampleDto();
|
||||
}
|
||||
});
|
||||
Object result = processor.processResult(Mono.just(new Sample("Dave", "Matthews")), source -> new SampleDto());
|
||||
|
||||
assertThat(result, is(instanceOf(Mono.class)));
|
||||
|
||||
Object content = ((Mono<Object>) result).block();
|
||||
|
||||
assertThat(content, is(instanceOf(SampleDto.class)));
|
||||
@@ -335,6 +333,7 @@ public class ResultProcessorUnitTests {
|
||||
Object result = getProcessor("findFluxProjection").processResult(samples);
|
||||
|
||||
assertThat(result, is(instanceOf(Flux.class)));
|
||||
|
||||
List<Object> content = ((Flux<Object>) result).collectList().block();
|
||||
|
||||
assertThat(content, is(not(empty())));
|
||||
@@ -353,6 +352,7 @@ public class ResultProcessorUnitTests {
|
||||
Object result = getProcessor("findObservableProjection").processResult(samples);
|
||||
|
||||
assertThat(result, is(instanceOf(Observable.class)));
|
||||
|
||||
List<Object> content = ((Observable<Object>) result).toList().toBlocking().single();
|
||||
|
||||
assertThat(content, is(not(empty())));
|
||||
|
||||
Reference in New Issue
Block a user