|
| 1 | +/* |
| 2 | + * Copyright 2011-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.neo4j.integration.issues.gh2533; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.neo4j.driver.Driver; |
| 21 | +import org.neo4j.driver.Session; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | +import org.springframework.data.neo4j.config.AbstractNeo4jConfig; |
| 26 | +import org.springframework.data.neo4j.core.DatabaseSelectionProvider; |
| 27 | +import org.springframework.data.neo4j.core.Neo4jTemplate; |
| 28 | +import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager; |
| 29 | +import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager; |
| 30 | +import org.springframework.data.neo4j.repository.Neo4jRepository; |
| 31 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 32 | +import org.springframework.data.neo4j.repository.query.Query; |
| 33 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 34 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 35 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 36 | +import org.springframework.data.repository.query.Param; |
| 37 | +import org.springframework.transaction.PlatformTransactionManager; |
| 38 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 39 | + |
| 40 | +import java.util.Arrays; |
| 41 | +import java.util.Collections; |
| 42 | +import java.util.Optional; |
| 43 | + |
| 44 | +import static org.assertj.core.api.Assertions.assertThat; |
| 45 | + |
| 46 | +/** |
| 47 | + * Test for projection with dynamic relationships. |
| 48 | + */ |
| 49 | +@Neo4jIntegrationTest |
| 50 | +public class GH2533IT { |
| 51 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + private GH2533Repository repository; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private Neo4jTemplate neo4jTemplate; |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setupData(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture) { |
| 61 | + |
| 62 | + try (Session session = driver.session()) { |
| 63 | + session.run("MATCH (n) DETACH DELETE n").consume(); |
| 64 | + bookmarkCapture.seedWith(session.lastBookmark()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test // GH-2533 |
| 69 | + void projectionWorksForDynamicRelationshipsOnSave() { |
| 70 | + EntitiesAndProjections.GH2533Entity rootEntity = createData(); |
| 71 | + |
| 72 | + rootEntity = repository.findByIdWithLevelOneLinks(rootEntity.id).get(); |
| 73 | + |
| 74 | + // this would cause the rootEntity -> child -X-> child relationship to get removed (X). |
| 75 | + neo4jTemplate.saveAs(rootEntity, EntitiesAndProjections.GH2533EntityNodeWithOneLevelLinks.class); |
| 76 | + |
| 77 | + EntitiesAndProjections.GH2533Entity savedEntity = neo4jTemplate.findById(rootEntity.id, EntitiesAndProjections.GH2533Entity.class).get(); |
| 78 | + |
| 79 | + assertThat(savedEntity.relationships).isNotEmpty(); |
| 80 | + assertThat(savedEntity.relationships.get("has_relationship_with")).isNotEmpty(); |
| 81 | + assertThat(savedEntity.relationships.get("has_relationship_with").get(0).target).isNotNull(); |
| 82 | + assertThat(savedEntity.relationships.get("has_relationship_with").get(0).target.relationships).isNotEmpty(); |
| 83 | + } |
| 84 | + |
| 85 | + private EntitiesAndProjections.GH2533Entity createData() { |
| 86 | + EntitiesAndProjections.GH2533Entity n1 = new EntitiesAndProjections.GH2533Entity(); |
| 87 | + EntitiesAndProjections.GH2533Entity n2 = new EntitiesAndProjections.GH2533Entity(); |
| 88 | + EntitiesAndProjections.GH2533Entity n3 = new EntitiesAndProjections.GH2533Entity(); |
| 89 | + |
| 90 | + EntitiesAndProjections.GH2533Relationship r1 = new EntitiesAndProjections.GH2533Relationship(); |
| 91 | + EntitiesAndProjections.GH2533Relationship r2 = new EntitiesAndProjections.GH2533Relationship(); |
| 92 | + |
| 93 | + r1.target = n2; |
| 94 | + r2.target = n3; |
| 95 | + |
| 96 | + // Add relationships |
| 97 | + n1.relationships = Collections.singletonMap("has_relationship_with", Arrays.asList(r1)); |
| 98 | + n2.relationships = Collections.singletonMap("has_relationship_with", Arrays.asList(r2)); |
| 99 | + |
| 100 | + return repository.save(n1); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + interface GH2533Repository extends Neo4jRepository<EntitiesAndProjections.GH2533Entity, Long> { |
| 105 | + @Query("MATCH p=(n)-[*0..1]->(m) WHERE id(n)=$id RETURN n, collect(relationships(p)), collect(m);") |
| 106 | + Optional<EntitiesAndProjections.GH2533Entity> findByIdWithLevelOneLinks(@Param("id") Long id); |
| 107 | + } |
| 108 | + |
| 109 | + @Configuration |
| 110 | + @EnableTransactionManagement |
| 111 | + @EnableNeo4jRepositories(considerNestedRepositories = true) |
| 112 | + static class Config extends AbstractNeo4jConfig { |
| 113 | + |
| 114 | + @Bean |
| 115 | + public BookmarkCapture bookmarkCapture() { |
| 116 | + return new BookmarkCapture(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public PlatformTransactionManager transactionManager( |
| 121 | + Driver driver, DatabaseSelectionProvider databaseNameProvider) { |
| 122 | + |
| 123 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 124 | + return new Neo4jTransactionManager(driver, databaseNameProvider, |
| 125 | + Neo4jBookmarkManager.create(bookmarkCapture)); |
| 126 | + } |
| 127 | + |
| 128 | + @Bean |
| 129 | + public Driver driver() { |
| 130 | + return neo4jConnectionSupport.getDriver(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments