diff --git a/jpa/vavr/pom.xml b/jpa/vavr/pom.xml index aa73fe84..6f1aa235 100644 --- a/jpa/vavr/pom.xml +++ b/jpa/vavr/pom.xml @@ -10,6 +10,10 @@ spring-data-jpa-vavr Spring Data JPA - Vavr integration + + + Lovelace-BUILD-SNAPSHOT + diff --git a/jpa/vavr/src/main/java/example/PersonRepository.java b/jpa/vavr/src/main/java/example/PersonRepository.java index 8735286e..36588337 100644 --- a/jpa/vavr/src/main/java/example/PersonRepository.java +++ b/jpa/vavr/src/main/java/example/PersonRepository.java @@ -19,6 +19,7 @@ import io.vavr.collection.Map; import io.vavr.collection.Seq; import io.vavr.collection.Set; import io.vavr.control.Option; +import io.vavr.control.Try; import java.util.List; import java.util.Optional; @@ -51,4 +52,12 @@ public interface PersonRepository extends Repository { * @return */ Seq findByFirstnameContaining(String firstname); + + /** + * Returning a {@link Try} is supported out of the box with all exceptions being handled by {@link Try} immediately. + * + * @param lastname + * @return + */ + Try> findByLastnameContaining(String lastname); } diff --git a/jpa/vavr/src/test/java/example/PersonRepositoryIntegrationTests.java b/jpa/vavr/src/test/java/example/PersonRepositoryIntegrationTests.java index 1277de2a..56fdecb6 100644 --- a/jpa/vavr/src/test/java/example/PersonRepositoryIntegrationTests.java +++ b/jpa/vavr/src/test/java/example/PersonRepositoryIntegrationTests.java @@ -19,6 +19,9 @@ import static org.assertj.core.api.Assertions.*; import io.vavr.collection.Seq; import io.vavr.control.Option; +import io.vavr.control.Try; + +import javax.persistence.NonUniqueResultException; import org.junit.Test; import org.junit.runner.RunWith; @@ -71,4 +74,34 @@ public class PersonRepositoryIntegrationTests { assertThat(result.contains(carter)).isTrue(); assertThat(result.contains(dave)).isFalse(); } + + /** + * @see #370 + */ + @Test + public void returnsSuccessOfOption() { + + Person dave = people.save(new Person("Dave", "Matthews")); + people.save(new Person("Carter", "Beauford")); + + Try> result = people.findByLastnameContaining("w"); + + assertThat(result.isSuccess()).isTrue(); + assertThat(result.get()).contains(dave); + } + + /** + * @see #370 + */ + @Test + public void returnsFailureOfOption() { + + people.save(new Person("Dave", "Matthews")); + people.save(new Person("Carter", "Beauford")); + + Try> result = people.findByLastnameContaining("e"); + + assertThat(result.isFailure()).isTrue(); + assertThat(result.getCause()).isInstanceOf(NonUniqueResultException.class); + } }