DATACMNS-867 - RepositoryInvoker now returns an Optional for invokeFindOne(…).
Adapted ReflectionRepositoryInvoker to transparently unwrap other potentially used wrapper types to then convert the result into an Optional.
This commit is contained in:
@@ -18,6 +18,8 @@ package org.springframework.data.repository.support;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -59,9 +61,9 @@ public class DefaultRepositoryInvokerFactoryIntegrationTests {
|
||||
Product product = new Product();
|
||||
when(productRepository.findOne(4711L)).thenReturn(product);
|
||||
|
||||
Object invokeFindOne = factory.getInvokerFor(Product.class).invokeFindOne(4711L);
|
||||
Optional<Object> invokeFindOne = factory.getInvokerFor(Product.class).invokeFindOne(4711L);
|
||||
|
||||
assertThat(invokeFindOne).isEqualTo(product);
|
||||
assertThat(invokeFindOne).isEqualTo(Optional.of(product));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-374, DATACMNS-589
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.repository.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.repository.support.RepositoryInvocationTestUtils.*;
|
||||
|
||||
@@ -25,6 +26,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -239,6 +241,19 @@ public class ReflectionRepositoryInvokerUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATACMNS-867
|
||||
public void convertsWrapperTypeToJdkOptional() {
|
||||
|
||||
GuavaRepository mock = mock(GuavaRepository.class);
|
||||
when(mock.findOne(any())).thenReturn(com.google.common.base.Optional.of(new Domain()));
|
||||
|
||||
RepositoryInvoker invoker = getInvokerFor(mock);
|
||||
|
||||
Optional<Object> invokeFindOne = invoker.invokeFindOne(1L);
|
||||
|
||||
assertThat(invokeFindOne).isPresent();
|
||||
}
|
||||
|
||||
private static RepositoryInvoker getInvokerFor(Object repository) {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repository.getClass().getInterfaces()[0]);
|
||||
@@ -298,4 +313,9 @@ public class ReflectionRepositoryInvokerUnitTests {
|
||||
|
||||
Domain findByClass(@Param("value") int value);
|
||||
}
|
||||
|
||||
interface GuavaRepository extends Repository<Domain, Long> {
|
||||
|
||||
com.google.common.base.Optional<Domain> findOne(Long id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user