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.
This commit is contained in:
Michael Simons
2020-08-28 15:43:15 +02:00
parent a6c92d6abc
commit e32fe4513c
6 changed files with 162 additions and 9 deletions

View File

@@ -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<Role> 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 {

View File

@@ -31,6 +31,8 @@ public class Actor {
@Relationship(type = "ACTS_IN")
private List<Role> 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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}

View File

@@ -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<Role, Long> {
List<Role> findAllByMovieGenreName(String genre);
List<Role> findAllByActorCountryName(String country);
}