From cd36f6f0da0bad8385993c1b7587ec7985543699 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 14 Dec 2016 14:05:38 +0100 Subject: [PATCH] #231 - Added example for Javaslang support on repositories. Added an example that shows how Javaslang's collection types and Option can be used as repository query methods starting with Spring Data Ingalls RC1. --- jpa/javaslang/pom.xml | 26 +++++++ .../src/main/java/example/Person.java | 38 ++++++++++ .../main/java/example/PersonRepository.java | 54 ++++++++++++++ .../PersonRepositoryIntegrationTests.java | 74 +++++++++++++++++++ jpa/pom.xml | 1 + 5 files changed, 193 insertions(+) create mode 100644 jpa/javaslang/pom.xml create mode 100644 jpa/javaslang/src/main/java/example/Person.java create mode 100644 jpa/javaslang/src/main/java/example/PersonRepository.java create mode 100644 jpa/javaslang/src/test/java/example/PersonRepositoryIntegrationTests.java diff --git a/jpa/javaslang/pom.xml b/jpa/javaslang/pom.xml new file mode 100644 index 00000000..d7a5438e --- /dev/null +++ b/jpa/javaslang/pom.xml @@ -0,0 +1,26 @@ + + 4.0.0 + + + org.springframework.data.examples + spring-data-jpa-examples + 1.0.0.BUILD-SNAPSHOT + + + spring-data-jpa-javaslang + Spring Data JPA - Javaslang integration + + + Ingalls-BUILD-SNAPSHOT + + + + + io.javaslang + javaslang + 2.0.5 + + + + diff --git a/jpa/javaslang/src/main/java/example/Person.java b/jpa/javaslang/src/main/java/example/Person.java new file mode 100644 index 00000000..63f989f5 --- /dev/null +++ b/jpa/javaslang/src/main/java/example/Person.java @@ -0,0 +1,38 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example; + +import lombok.AccessLevel; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Oliver Gierke + */ +@Data +@Entity +@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE) +@RequiredArgsConstructor +public class Person { + + private @GeneratedValue @Id Long id; + private final String firstname, lastname; +} diff --git a/jpa/javaslang/src/main/java/example/PersonRepository.java b/jpa/javaslang/src/main/java/example/PersonRepository.java new file mode 100644 index 00000000..d96cb68e --- /dev/null +++ b/jpa/javaslang/src/main/java/example/PersonRepository.java @@ -0,0 +1,54 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example; + +import javaslang.collection.Map; +import javaslang.collection.Seq; +import javaslang.collection.Set; +import javaslang.control.Option; + +import java.util.List; +import java.util.Optional; + +import org.springframework.data.repository.Repository; + +/** + * Repository interface showing the usage of Javaslang collections and its {@link Option} type as repository query + * method return types. + * + * @author Oliver Gierke + */ +public interface PersonRepository extends Repository { + + Person save(Person person); + + /** + * {@link Option} can be used as alternative to JDK 8's {@link Optional}. + * + * @param id + * @return + */ + Option findById(Long id); + + /** + * {@link Seq} can be used as alternative to JDK's {@link List}. Javaslang's {@link Set} and {@link Map} are + * supported, too, and transparently mapped from their JDK counterparts. + * + * @param firstname + * @return + */ + Seq findByFirstnameContaining(String firstname); +} diff --git a/jpa/javaslang/src/test/java/example/PersonRepositoryIntegrationTests.java b/jpa/javaslang/src/test/java/example/PersonRepositoryIntegrationTests.java new file mode 100644 index 00000000..d1630f7a --- /dev/null +++ b/jpa/javaslang/src/test/java/example/PersonRepositoryIntegrationTests.java @@ -0,0 +1,74 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example; + +import static org.assertj.core.api.Assertions.*; + +import javaslang.collection.Seq; +import javaslang.control.Option; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +/** + * Integration tests for {@link PersonRepository} showing Javaslang support at repsoitory query methods. + * + * @author Oliver Gierke + */ +@RunWith(SpringRunner.class) +@Transactional +@SpringBootTest +public class PersonRepositoryIntegrationTests { + + @Autowired PersonRepository people; + + @SpringBootApplication + static class Configuration {} + + /** + * @see #231 + */ + @Test + public void readsPersonIntoJavaslangOptionById() { + + Person dave = people.save(new Person("Dave", "Matthews")); + + Option result = people.findById(dave.getId()); + + assertThat(result.isEmpty()).isFalse(); + assertThat(result.get()).isEqualTo(dave); + } + + /** + * @see #231 + */ + @Test + public void readsPeopleIntoJavaslangSeq() { + + Person dave = people.save(new Person("Dave", "Matthews")); + Person carter = people.save(new Person("Carter", "Beauford")); + + Seq result = people.findByFirstnameContaining("art"); + + assertThat(result.contains(carter)).isTrue(); + assertThat(result.contains(dave)).isFalse(); + } +} diff --git a/jpa/pom.xml b/jpa/pom.xml index 93b4dd28..6dab4a82 100644 --- a/jpa/pom.xml +++ b/jpa/pom.xml @@ -21,6 +21,7 @@ showcase interceptors java8 + javaslang jpa21 security multiple-datasources