From 5f617e40ac2960c89a229d64f30b4bddb9fc191a Mon Sep 17 00:00:00 2001 From: Michael Simons Date: Thu, 14 Mar 2024 12:02:35 +0100 Subject: [PATCH] refactor: Add a test for GH-2872. Closes #2872. --- .../neo4j/integration/issues/IssuesIT.java | 40 +++++++++++++ .../issues/gh2872/Gh2872Entity.java | 39 ++++++++++++ .../gh2872/Gh2872UserChangesEntity.java | 60 +++++++++++++++++++ .../issues/gh2872/UserChangesRepository.java | 24 ++++++++ src/test/resources/logback-test.xml | 2 +- 5 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872Entity.java create mode 100644 src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872UserChangesEntity.java create mode 100644 src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/UserChangesRepository.java diff --git a/src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java b/src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java index 38af06af2e..9f02fa73a6 100644 --- a/src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java +++ b/src/test/java/org/springframework/data/neo4j/integration/issues/IssuesIT.java @@ -147,6 +147,7 @@ import org.springframework.data.neo4j.integration.issues.gh2819.GH2819Repository; import org.springframework.data.neo4j.integration.issues.gh2858.GH2858; import org.springframework.data.neo4j.integration.issues.gh2858.GH2858Repository; +import org.springframework.data.neo4j.integration.issues.gh2872.UserChangesRepository; import org.springframework.data.neo4j.integration.issues.qbe.A; import org.springframework.data.neo4j.integration.issues.qbe.ARepository; import org.springframework.data.neo4j.integration.issues.qbe.B; @@ -205,6 +206,7 @@ protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) { setupGH2459(transaction); setupGH2572(transaction); setupGH2583(transaction); + setupGH2872(transaction); transaction.run("CREATE (:A {name: 'A name', id: randomUUID()}) -[:HAS] ->(:B {anotherName: 'Whatever', id: randomUUID()})"); @@ -214,6 +216,21 @@ protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) { } } + private static void setupGH2872(QueryRunner queryRunner) { + queryRunner.run(""" + CREATE (p:`UserChanges`:`Gh2872Entity` {nodeId: randomUUID(), someField: -10}), + (c:`UserChanges`:`Gh2872Entity` {nodeId: randomUUID(), someField: 0}), + (n10:`UserChanges`:`Gh2872Entity` {nodeId: randomUUID(), someField: 10}), + (n20:`UserChanges`:`Gh2872Entity` {nodeId: randomUUID(), someField: 20}), + (p)-[:HAS_USER_CHANGES]->(c), + (c)-[:PREVIOUS_USER_CHANGE]->(p), + (c)-[:HAS_USER_CHANGES]->(n10), + (c)-[:HAS_USER_CHANGES]->(n20), + (n10)-[:PREVIOUS_USER_CHANGE]->(c), + (n20)-[:PREVIOUS_USER_CHANGE]->(c) + """).consume(); + } + private static void setupGH2168(QueryRunner queryRunner) { queryRunner.run("CREATE (:DomainObject{id: 'A'})").consume(); } @@ -300,6 +317,29 @@ void qbeWithRelationship(@Autowired ARepository repository) { assertThat(allResults).hasSize(1); } + + @Test + @Tag("GH-2872") + void findAllWithPossibleCircles(@Autowired UserChangesRepository userChangesRepository) { + var results = userChangesRepository.findAll(); + assertThat(results).hasSize(4); + for (var result : results) { + var someField = result.getSomeField(); + assertThat(someField).isNotNull(); + if (someField.equals(0)) { + assertThat(result.getPrevious()).isNotNull(); + assertThat(result.getPrevious().getSomeField()).isEqualTo(-10); + } else if (someField.equals(-10)) { + assertThat(result.getPrevious()).isNull(); + assertThat(result.getUsers()).hasSize(1).first().matches(e -> e.getSomeField().equals(0)); + } else { + assertThat(result.getPrevious()).isNotNull(); + assertThat(result.getPrevious().getSomeField()).isEqualTo(0); + assertThat(result.getUsers()).isEmpty(); + } + } + } + @Test @Tag("GH-2168") void findByIdShouldWork(@Autowired DomainObjectRepository domainObjectRepository) { diff --git a/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872Entity.java b/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872Entity.java new file mode 100644 index 0000000000..30572c6293 --- /dev/null +++ b/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872Entity.java @@ -0,0 +1,39 @@ +/* + * Copyright 2011-2024 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.integration.issues.gh2872; + +import org.springframework.data.neo4j.core.schema.GeneratedValue; +import org.springframework.data.neo4j.core.schema.Id; +import org.springframework.data.neo4j.core.schema.Node; + +/** + * Test-reproducer. + */ +@Node("Gh2872Entity") +public class Gh2872Entity { + + @Id + @GeneratedValue(GeneratedValue.UUIDGenerator.class) + protected String nodeId; + + public String getNodeId() { + return nodeId; + } + + public void setNodeId(String nodeId) { + this.nodeId = nodeId; + } +} diff --git a/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872UserChangesEntity.java b/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872UserChangesEntity.java new file mode 100644 index 0000000000..32455a37fc --- /dev/null +++ b/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/Gh2872UserChangesEntity.java @@ -0,0 +1,60 @@ +/* + * Copyright 2011-2024 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.integration.issues.gh2872; + +import java.util.List; + +import org.springframework.data.neo4j.core.schema.Node; +import org.springframework.data.neo4j.core.schema.Relationship; + +/** + * Test-reproducer. + */ +@Node("UserChanges") +public class Gh2872UserChangesEntity extends Gh2872Entity { + + private Integer someField; + + @Relationship(type = "HAS_USER_CHANGES") + private List users; + + @Relationship(type = "PREVIOUS_USER_CHANGE") + Gh2872UserChangesEntity previous; + + public Integer getSomeField() { + return someField; + } + + public void setSomeField(Integer someField) { + this.someField = someField; + } + + public List getUsers() { + return users; + } + + public void setUsers(List users) { + this.users = users; + } + + public Gh2872UserChangesEntity getPrevious() { + return previous; + } + + public void setPrevious(Gh2872UserChangesEntity previous) { + this.previous = previous; + } +} diff --git a/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/UserChangesRepository.java b/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/UserChangesRepository.java new file mode 100644 index 0000000000..b18b38c7c0 --- /dev/null +++ b/src/test/java/org/springframework/data/neo4j/integration/issues/gh2872/UserChangesRepository.java @@ -0,0 +1,24 @@ +/* + * Copyright 2011-2024 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.integration.issues.gh2872; + +import org.springframework.data.neo4j.repository.Neo4jRepository; + +/** + * Test-reproducer. + */ +public interface UserChangesRepository extends Neo4jRepository { +} diff --git a/src/test/resources/logback-test.xml b/src/test/resources/logback-test.xml index 5646e1426a..fb7e85408d 100644 --- a/src/test/resources/logback-test.xml +++ b/src/test/resources/logback-test.xml @@ -29,7 +29,7 @@ - +