diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/queries/QueryResultIntegrationTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/queries/QueryResultIntegrationTests.java index dec1670bb..c4156c464 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/queries/QueryResultIntegrationTests.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/queries/QueryResultIntegrationTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.neo4j.queries; -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; import java.util.List; @@ -26,13 +26,20 @@ import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.Transaction; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.data.neo4j.queries.gh1849.Cat; +import org.springframework.data.neo4j.queries.gh1849.Kennel; +import org.springframework.data.neo4j.queries.gh1849.KennelRepository; +import org.springframework.data.neo4j.queries.gh1849.Person; +import org.springframework.data.neo4j.queries.gh1849.Pet; import org.springframework.data.neo4j.queries.ogmgh551.AnotherThing; import org.springframework.data.neo4j.queries.ogmgh551.ThingRepository; import org.springframework.data.neo4j.queries.ogmgh551.ThingResult; import org.springframework.data.neo4j.test.Neo4jIntegrationTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; +import org.springframework.transaction.support.TransactionTemplate; /** * Those tests might have also fitted in {@link QueryReturnTypesTests} and {@link QueryIntegrationTests}, but those use @@ -42,7 +49,7 @@ import org.springframework.transaction.annotation.Transactional; */ @ContextConfiguration(classes = { QueryResultIntegrationTests.ContextConfig.class }) @RunWith(SpringRunner.class) -@Transactional + public class QueryResultIntegrationTests { @Autowired @@ -51,17 +58,26 @@ public class QueryResultIntegrationTests { @Autowired private ThingRepository thingRepository; + @Autowired + private KennelRepository kennelRepository; + + @Autowired + private TransactionTemplate transactionTemplate; + @Before public void prepareTestDa() { try (Transaction tx = graphDatabaseService.beginTx()) { - graphDatabaseService - .execute("unwind range(1,10) as x with x create (n:ThingEntity {name: 'Thing ' + x}) return n"); + graphDatabaseService.execute("MATCH (n) DETACH DELETE n"); + graphDatabaseService.execute("unwind range(1,10) as x with x create (n:ThingEntity {name: 'Thing ' + x}) return n"); + graphDatabaseService.execute("MERGE (p:Person {name:'Billy'})<-[:OWNED_BY]-(d:Dog {name: 'Ralph', breed: 'Muppet'})<-[:HOUSES]-(k:Kennel)"); + graphDatabaseService.execute("MERGE (p:Person {name:'Sally'})<-[:LIVES_WITH]-(c:Cat {name: 'Mittens', food: 'Birds'})<-[:HOUSES]-(k:Kennel)"); tx.success(); } } @Test + @Transactional public void queryResultsShouldWorkWithNestedObjectsFromMap() { List thingResults = this.thingRepository.findResults(); @@ -75,8 +91,31 @@ public class QueryResultIntegrationTests { .allSatisfy(s -> s.startsWith("Thing")); } + @Test // GH-1849 + public void polymorphicQueryShouldIncludeAllRelTypes() { + + + for (int depth : new int[] { -1, 10 }) { + Iterable kennels = kennelRepository.findAll(depth); + assertThat(kennels).hasSize(2); + assertThat(kennels).allSatisfy(kennel -> { + Pet pet = kennel.getPet(); + String expectedName = pet instanceof Cat ? "Mittens" : "Ralph"; + String expectedPerson = pet instanceof Cat ? "Sally" : "Billy"; + assertThat(pet).extracting(Pet::getName).isEqualTo(expectedName); + assertThat(pet.getPerson()).isNotNull().extracting(Person::getName).isEqualTo(expectedPerson); + System.out.println("what?"); + }); + } + } + @Configuration - @Neo4jIntegrationTest(repositoryPackages = "org.springframework.data.neo4j.queries.ogmgh551", domainPackages = "org.springframework.data.neo4j.queries.ogmgh551") + @Neo4jIntegrationTest( + repositoryPackages = { "org.springframework.data.neo4j.queries.ogmgh551", + "org.springframework.data.neo4j.queries.gh1849" }, + domainPackages = { "org.springframework.data.neo4j.queries.ogmgh551", + "org.springframework.data.neo4j.queries.gh1849" } + ) static class ContextConfig { } } diff --git a/spring-data-neo4j/src/test/kotlin/org/springframework/data/neo4j/queries/gh1849/GH1849.kt b/spring-data-neo4j/src/test/kotlin/org/springframework/data/neo4j/queries/gh1849/GH1849.kt new file mode 100644 index 000000000..125bc5a6c --- /dev/null +++ b/spring-data-neo4j/src/test/kotlin/org/springframework/data/neo4j/queries/gh1849/GH1849.kt @@ -0,0 +1,66 @@ +/* + * Copyright 2011-2021 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.queries.gh1849 + +import org.neo4j.ogm.annotation.GeneratedValue +import org.neo4j.ogm.annotation.Id +import org.neo4j.ogm.annotation.Relationship +import org.springframework.data.neo4j.repository.Neo4jRepository +import org.springframework.transaction.annotation.Transactional + +class Kennel { + @Id + @GeneratedValue + var id: Long? = null + + @Relationship(type = "HOUSES", direction = Relationship.OUTGOING) + lateinit var pet: Pet +} + +class Person(var name: String) { + @Id + @GeneratedValue + var id: Long? = null + + override fun toString() = "Person(${name})" +} + +abstract class Pet(var name: String) { + @Id + @GeneratedValue + var id: Long? = null + + abstract var person: Person? + + override fun toString() = "${this::class.simpleName}(${name})" +} + +class Dog(name: String, val breed: String) : Pet(name) { + @Relationship(type = "OWNED_BY", direction = Relationship.OUTGOING) + override var person: Person? = null +} + +class Cat(name: String, val food: String) : Pet(name) { + @Relationship(type = "LIVES_WITH", direction = Relationship.OUTGOING) + override var person: Person? = null +} + +@Transactional +interface KennelRepository : Neo4jRepository { + + @Transactional + override fun findAll(depth: Int): MutableIterable; +} \ No newline at end of file