|
| 1 | +/* |
| 2 | + * Copyright 2011-2021 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.gh2323; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.BeforeAll; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.neo4j.driver.Driver; |
| 27 | +import org.neo4j.driver.Session; |
| 28 | +import org.neo4j.driver.Transaction; |
| 29 | +import org.springframework.beans.factory.annotation.Autowired; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.ComponentScan; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | +import org.springframework.data.neo4j.config.AbstractNeo4jConfig; |
| 34 | +import org.springframework.data.neo4j.repository.Neo4jRepository; |
| 35 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 36 | +import org.springframework.data.neo4j.repository.query.Query; |
| 37 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 38 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 39 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 40 | +import org.springframework.data.repository.query.Param; |
| 41 | +import org.springframework.stereotype.Repository; |
| 42 | +import org.springframework.stereotype.Service; |
| 43 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 44 | + |
| 45 | +/** |
| 46 | + * @author Michael J. Simons |
| 47 | + */ |
| 48 | +@Neo4jIntegrationTest |
| 49 | +class GH2323IT { |
| 50 | + |
| 51 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 52 | + |
| 53 | + protected static String personId; |
| 54 | + |
| 55 | + @BeforeAll |
| 56 | + protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) { |
| 57 | + try (Session session = neo4jConnectionSupport.getDriver().session(bookmarkCapture.createSessionConfig()); |
| 58 | + Transaction transaction = session.beginTransaction(); |
| 59 | + ) { |
| 60 | + transaction.run("MATCH (n) detach delete n"); |
| 61 | + personId = transaction.run("CREATE (n:Person {id: randomUUID(), name: 'Helge'}) return n.id").single() |
| 62 | + .get(0) |
| 63 | + .asString(); |
| 64 | + transaction.run("unwind ['German', 'English'] as name create (n:Language {name: name}) return name") |
| 65 | + .consume(); |
| 66 | + transaction.commit(); |
| 67 | + bookmarkCapture.seedWith(session.lastBookmark()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test // GH-2323 |
| 72 | + void listOfRelationshipPropertiesShouldBeUnwindable(@Autowired PersonService personService) { |
| 73 | + Person person = personService.updateRel(personId, Arrays.asList("German")); |
| 74 | + assertThat(person).isNotNull(); |
| 75 | + assertThat(person.getKnownLanguages()).hasSize(1); |
| 76 | + assertThat(person.getKnownLanguages()).first().satisfies(knows -> { |
| 77 | + assertThat(knows.getDescription()).isEqualTo("Some description"); |
| 78 | + assertThat(knows.getLanguage()).extracting(Language::getName).isEqualTo("German"); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + @Repository |
| 83 | + public interface PersonRepository extends Neo4jRepository<Person, String> { |
| 84 | + |
| 85 | + @Query("UNWIND $relations As rel WITH rel " + |
| 86 | + "CREATE (f:Person {id: $from}) - [r:KNOWS {description: rel.__properties__.description}] -> (t:Language {name: rel.__properties__.__target__.__id__}) " |
| 87 | + + |
| 88 | + "RETURN f, collect(r), collect(t)") |
| 89 | + Person updateRel(@Param("from") String from, @Param("relations") List<Knows> relations); |
| 90 | + } |
| 91 | + |
| 92 | + @Service |
| 93 | + static class PersonService { |
| 94 | + |
| 95 | + private final PersonRepository personRepository; |
| 96 | + |
| 97 | + PersonService(PersonRepository personRepository) { |
| 98 | + this.personRepository = personRepository; |
| 99 | + } |
| 100 | + |
| 101 | + public Person updateRel(String from, List<String> languageNames) { |
| 102 | + |
| 103 | + List<Knows> knownLanguages = languageNames.stream().map(Language::new) |
| 104 | + .map(language -> new Knows("Some description", language)) |
| 105 | + .collect(Collectors.toList()); |
| 106 | + return personRepository.updateRel(from, knownLanguages); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + @Configuration |
| 111 | + @EnableTransactionManagement |
| 112 | + @EnableNeo4jRepositories(considerNestedRepositories = true) |
| 113 | + @ComponentScan |
| 114 | + static class Config extends AbstractNeo4jConfig { |
| 115 | + |
| 116 | + @Bean |
| 117 | + public BookmarkCapture bookmarkCapture() { |
| 118 | + return new BookmarkCapture(); |
| 119 | + } |
| 120 | + |
| 121 | + @Bean |
| 122 | + public Driver driver() { |
| 123 | + |
| 124 | + return neo4jConnectionSupport.getDriver(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments