|
| 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.gh2493; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 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.Configuration; |
| 32 | +import org.springframework.data.neo4j.config.AbstractNeo4jConfig; |
| 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.Neo4jIntegrationTest; |
| 40 | +import org.springframework.transaction.PlatformTransactionManager; |
| 41 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author Michael J. Simons |
| 45 | + */ |
| 46 | +@Neo4jIntegrationTest |
| 47 | +public class Gh2493IT { |
| 48 | + |
| 49 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 50 | + |
| 51 | + @BeforeAll |
| 52 | + protected static void setupData(@Autowired BookmarkCapture bookmarkCapture) { |
| 53 | + |
| 54 | + try (Session session = neo4jConnectionSupport.getDriver().session(bookmarkCapture.createSessionConfig()); |
| 55 | + Transaction transaction = session.beginTransaction(); |
| 56 | + ) { |
| 57 | + transaction.run("MATCH (n) DETACH DELETE n").consume(); |
| 58 | + transaction.commit(); |
| 59 | + bookmarkCapture.seedWith(session.lastBookmark()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void saveOneShouldWork(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture, |
| 65 | + @Autowired TestObjectRepository repository) { |
| 66 | + |
| 67 | + TestObject testObject = new TestObject(new TestData(4711, "Foobar")); |
| 68 | + testObject = repository.save(testObject); |
| 69 | + |
| 70 | + assertThat(testObject.getId()).isNotNull(); |
| 71 | + assertThatTestObjectHasBeenCreated(driver, bookmarkCapture, testObject); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void saveAllShouldWork(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture, |
| 76 | + @Autowired TestObjectRepository repository) { |
| 77 | + |
| 78 | + TestObject testObject = new TestObject(new TestData(4711, "Foobar")); |
| 79 | + testObject = repository.saveAll(Collections.singletonList(testObject)).get(0); |
| 80 | + |
| 81 | + assertThat(testObject.getId()).isNotNull(); |
| 82 | + assertThatTestObjectHasBeenCreated(driver, bookmarkCapture, testObject); |
| 83 | + } |
| 84 | + |
| 85 | + private static void assertThatTestObjectHasBeenCreated(Driver driver, BookmarkCapture bookmarkCapture, |
| 86 | + TestObject testObject) { |
| 87 | + try (Session session = driver.session(bookmarkCapture.createSessionConfig())) { |
| 88 | + Map<String, Object> arguments = new HashMap<>(); |
| 89 | + arguments.put("id", testObject.getId()); |
| 90 | + arguments.put("num", testObject.getData().getNum()); |
| 91 | + arguments.put("string", testObject.getData().getString()); |
| 92 | + long cnt = session.run( |
| 93 | + "MATCH (n:TestObject) WHERE n.id = $id AND n.dataNum = $num AND n.dataString = $string RETURN count(n)", |
| 94 | + arguments) |
| 95 | + .single().get(0).asLong(); |
| 96 | + assertThat(cnt).isOne(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Configuration |
| 101 | + @EnableTransactionManagement |
| 102 | + @EnableNeo4jRepositories |
| 103 | + static class Config extends AbstractNeo4jConfig { |
| 104 | + |
| 105 | + @Bean |
| 106 | + public Driver driver() { |
| 107 | + |
| 108 | + return neo4jConnectionSupport.getDriver(); |
| 109 | + } |
| 110 | + |
| 111 | + @Bean |
| 112 | + public BookmarkCapture bookmarkCapture() { |
| 113 | + return new BookmarkCapture(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public PlatformTransactionManager transactionManager(Driver driver, |
| 118 | + DatabaseSelectionProvider databaseNameProvider) { |
| 119 | + |
| 120 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 121 | + return new Neo4jTransactionManager(driver, databaseNameProvider, |
| 122 | + Neo4jBookmarkManager.create(bookmarkCapture)); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments