From e32fe4513c552a53a48c80f7bddfaf8d1a3fb4e5 Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Fri, 28 Aug 2020 15:43:15 +0200 Subject: [PATCH] DATAGRAPH-1331 - Ensure that properties of relationship entities can be traversed. This adds additional tests to ensure the proper traversal of multiple hops from relationship entities to related nodes after the upgrade to Neo4j-OGM 3.2.15 and 3.1.21. --- ...inimalRelationshipEntityMappingTests.java} | 45 +++++++++++++++++-- .../repository/relcentric/app/Actor.java | 10 +++++ .../repository/relcentric/app/Country.java | 37 +++++++++++++++ .../repository/relcentric/app/Genre.java | 45 +++++++++++++++++++ .../repository/relcentric/app/Movie.java | 28 +++++++++--- .../relcentric/app/RoleRepository.java | 6 +++ 6 files changed, 162 insertions(+), 9 deletions(-) rename spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/{MinimalRelationshipEntityMappingTest.java => MinimalRelationshipEntityMappingTests.java} (68%) create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Country.java create mode 100644 spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Genre.java diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/MinimalRelationshipEntityMappingTest.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/MinimalRelationshipEntityMappingTests.java similarity index 68% rename from spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/MinimalRelationshipEntityMappingTest.java rename to spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/MinimalRelationshipEntityMappingTests.java index 33344fbe0..7f932f776 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/MinimalRelationshipEntityMappingTest.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/MinimalRelationshipEntityMappingTests.java @@ -15,9 +15,11 @@ */ package org.springframework.data.neo4j.repository.relcentric; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Map; import org.junit.Test; @@ -26,6 +28,8 @@ import org.neo4j.ogm.session.SessionFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.data.neo4j.repository.relcentric.app.Actor; +import org.springframework.data.neo4j.repository.relcentric.app.Country; +import org.springframework.data.neo4j.repository.relcentric.app.Genre; import org.springframework.data.neo4j.repository.relcentric.app.Movie; import org.springframework.data.neo4j.repository.relcentric.app.Role; import org.springframework.data.neo4j.repository.relcentric.app.RoleRepository; @@ -38,8 +42,8 @@ import org.springframework.transaction.support.TransactionTemplate; * @author Michael J. Simons */ @RunWith(SpringRunner.class) -@ContextConfiguration(classes = MinimalRelationshipEntityMappingTest.Config.class) -public class MinimalRelationshipEntityMappingTest { +@ContextConfiguration(classes = MinimalRelationshipEntityMappingTests.Config.class) +public class MinimalRelationshipEntityMappingTests { @Autowired RoleRepository repository; @@ -78,6 +82,41 @@ public class MinimalRelationshipEntityMappingTest { .satisfies(m -> assertThat((String[]) m.get("titles")).containsOnly("M2")); } + @Test // DATAGRAPH-1331 + public void shouldTraverseDeepNestedPropertiesOnRelationshipEntities() { + transactionTemplate.executeWithoutResult(status -> { + Country country1 = new Country(); + country1.setName("C1"); + + Country country2 = new Country(); + country2.setName("C2"); + + Actor actor = new Actor("A2"); + actor.setCountry(country1); + + Actor actor2 = new Actor("A3"); + actor2.setCountry(country2); + + Movie m2 = new Movie("M2"); + m2.setGenre(new Genre("G2")); + + Movie m3 = new Movie("M3"); + m3.setGenre(new Genre("G3")); + + Movie m4 = new Movie("M4"); + m4.setGenre(new Genre("G2")); + + repository.saveAll( + Arrays.asList(new Role("R2", actor, m2), new Role("R3", actor, m3), new Role("R4", actor, m4), + new Role("R5", actor2, m3))); + }); + + List roles = repository.findAllByMovieGenreName("G2"); + assertThat(roles).hasSize(2).extracting(Role::getMovie).extracting(Movie::getName).containsOnly("M2", "M4"); + roles = repository.findAllByActorCountryName("C2"); + assertThat(roles).hasSize(1).extracting(Role::getMovie).extracting(Movie::getName).containsOnly("M3"); + } + @Configuration @Neo4jIntegrationTest(domainPackages = "org.springframework.data.neo4j.repository.relcentric.app") static class Config { diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Actor.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Actor.java index 6baa5cbf7..52f545987 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Actor.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Actor.java @@ -31,6 +31,8 @@ public class Actor { @Relationship(type = "ACTS_IN") private List roles = new ArrayList<>(); + private Country country; + public Actor() { } @@ -45,6 +47,14 @@ public class Actor { public String getName() { return name; } + + public Country getCountry() { + return country; + } + + public void setCountry(Country country) { + this.country = country; + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Country.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Country.java new file mode 100644 index 000000000..cf34fce00 --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Country.java @@ -0,0 +1,37 @@ +/* + * Copyright 2011-2020 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 + * + * https://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 org.springframework.data.neo4j.repository.relcentric.app; + +/** + * @author Michael J. Simons + */ +public class Country { + + private Long id; + private String name; + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Genre.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Genre.java new file mode 100644 index 000000000..e19a2973f --- /dev/null +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Genre.java @@ -0,0 +1,45 @@ +/* + * Copyright 2011-2020 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 + * + * https://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 org.springframework.data.neo4j.repository.relcentric.app; + +/** + * @author Michael J. Simons + */ +public class Genre { + + private Long id; + + private String name; + + public Genre() { + } + + public Genre(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Movie.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Movie.java index e71fb1d4d..388bcb919 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Movie.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/Movie.java @@ -15,19 +15,35 @@ */ package org.springframework.data.neo4j.repository.relcentric.app; +import org.neo4j.ogm.annotation.Relationship; /** * @author Michael J. Simons */ public class Movie { - private Long id; + private Long id; private String name; - public Movie() { - } + @Relationship("HAS") + private Genre genre; - public Movie(String name) { - this.name = name; - } + public Movie() { + } + + public Movie(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public Genre getGenre() { + return genre; + } + + public void setGenre(Genre genre) { + this.genre = genre; + } } diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/RoleRepository.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/RoleRepository.java index 2208b2fc2..3ea112348 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/RoleRepository.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/relcentric/app/RoleRepository.java @@ -15,10 +15,16 @@ */ package org.springframework.data.neo4j.repository.relcentric.app; +import java.util.List; + import org.springframework.data.neo4j.repository.Neo4jRepository; /** * @author Michael J. Simons */ public interface RoleRepository extends Neo4jRepository { + + List findAllByMovieGenreName(String genre); + + List findAllByActorCountryName(String country); }