|
| 1 | +/* |
| 2 | + * Copyright 2011-2024 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.gh2886; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import java.util.stream.Stream; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.neo4j.driver.Driver; |
| 29 | +import org.neo4j.driver.Session; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.context.annotation.Bean; |
| 32 | +import org.springframework.context.annotation.Configuration; |
| 33 | +import org.springframework.data.neo4j.core.DatabaseSelectionProvider; |
| 34 | +import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager; |
| 35 | +import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager; |
| 36 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 37 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 38 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 39 | +import org.springframework.data.neo4j.test.Neo4jImperativeTestConfiguration; |
| 40 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 41 | +import org.springframework.transaction.PlatformTransactionManager; |
| 42 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 43 | + |
| 44 | +/** |
| 45 | + * GH-2886 |
| 46 | + */ |
| 47 | +@Neo4jIntegrationTest |
| 48 | +public class Gh2886IT { |
| 49 | + |
| 50 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 51 | + |
| 52 | + @BeforeAll |
| 53 | + static void setup(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture) { |
| 54 | + try (Session session = driver.session(bookmarkCapture.createSessionConfig())) { |
| 55 | + session.run("MATCH (n) detach delete n").consume(); |
| 56 | + bookmarkCapture.seedWith(session.lastBookmark()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void dynamicLabels(@Autowired FruitRepository repository) { |
| 62 | + |
| 63 | + Apple f1 = new Apple(); |
| 64 | + f1.setVolume(1.0); |
| 65 | + f1.setColor("Red"); |
| 66 | + f1.setLabels(Collections.singleton("X")); |
| 67 | + |
| 68 | + Apple f2 = new Apple(); |
| 69 | + f2.setColor("Blue"); |
| 70 | + |
| 71 | + Orange f3 = new Orange(); |
| 72 | + f2.setVolume(3.0); |
| 73 | + f3.setColor("Red"); |
| 74 | + f3.setLabels(Collections.singleton("Y")); |
| 75 | + |
| 76 | + Orange f4 = new Orange(); |
| 77 | + f4.setColor("Yellow"); |
| 78 | + |
| 79 | + repository.saveAll(Stream.of(f1, f2, f3, f4).collect(Collectors.toList())); |
| 80 | + |
| 81 | + List<Fruit> fruits = repository.findAllFruits(); |
| 82 | + assertThat(fruits).allMatch(f -> f instanceof Apple || f instanceof Orange); |
| 83 | + } |
| 84 | + |
| 85 | + @Configuration |
| 86 | + @EnableTransactionManagement |
| 87 | + @EnableNeo4jRepositories(considerNestedRepositories = true) |
| 88 | + static class Config extends Neo4jImperativeTestConfiguration { |
| 89 | + |
| 90 | + @Bean |
| 91 | + public BookmarkCapture bookmarkCapture() { |
| 92 | + return new BookmarkCapture(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public PlatformTransactionManager transactionManager( |
| 97 | + Driver driver, DatabaseSelectionProvider databaseNameProvider) { |
| 98 | + |
| 99 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 100 | + return new Neo4jTransactionManager(driver, databaseNameProvider, |
| 101 | + Neo4jBookmarkManager.create(bookmarkCapture)); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected Collection<String> getMappingBasePackages() { |
| 106 | + return Collections.singleton(Apple.class.getPackage().getName()); |
| 107 | + } |
| 108 | + |
| 109 | + @Bean |
| 110 | + public Driver driver() { |
| 111 | + return neo4jConnectionSupport.getDriver(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean isCypher5Compatible() { |
| 116 | + return neo4jConnectionSupport.isCypher5SyntaxCompatible(); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments