|
| 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.gh2500; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import reactor.test.StepVerifier; |
| 21 | + |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.neo4j.driver.Driver; |
| 27 | +import org.neo4j.driver.Session; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.data.neo4j.config.AbstractReactiveNeo4jConfig; |
| 32 | +import org.springframework.data.neo4j.core.ReactiveDatabaseSelectionProvider; |
| 33 | +import org.springframework.data.neo4j.core.ReactiveNeo4jTemplate; |
| 34 | +import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager; |
| 35 | +import org.springframework.data.neo4j.core.transaction.ReactiveNeo4jTransactionManager; |
| 36 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 37 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 38 | +import org.springframework.transaction.ReactiveTransactionManager; |
| 39 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Michael J. Simons |
| 43 | + */ |
| 44 | +@Neo4jIntegrationTest |
| 45 | +public class ReactiveGH2500IT extends TestBase { |
| 46 | + |
| 47 | + @Test // GH-2498 |
| 48 | + void shouldNotDeleteFreshlyCreatedRelationships(@Autowired Driver driver, @Autowired |
| 49 | + ReactiveNeo4jTemplate template) { |
| 50 | + |
| 51 | + Group group = new Group(); |
| 52 | + group.setName("test"); |
| 53 | + template.findById(1L, Device.class) |
| 54 | + .flatMap(d -> { |
| 55 | + group.getDevices().add(d); |
| 56 | + return template.save(group); |
| 57 | + }) |
| 58 | + .as(StepVerifier::create) |
| 59 | + .expectNextCount(1L) |
| 60 | + .verifyComplete(); |
| 61 | + |
| 62 | + |
| 63 | + try (Session session = driver.session()) { |
| 64 | + Map<String, Object> parameters = new HashMap<>(); |
| 65 | + parameters.put("name", group.getName()); |
| 66 | + parameters.put("deviceId", 1L); |
| 67 | + long cnt = session.run( |
| 68 | + "MATCH (g:Group {name: $name}) <-[:BELONGS_TO]- (d:Device {id: $deviceId}) RETURN count(*)", |
| 69 | + parameters) |
| 70 | + .single().get(0).asLong(); |
| 71 | + assertThat(cnt).isOne(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Configuration |
| 76 | + @EnableTransactionManagement |
| 77 | + static class Config extends AbstractReactiveNeo4jConfig { |
| 78 | + |
| 79 | + @Bean |
| 80 | + public BookmarkCapture bookmarkCapture() { |
| 81 | + return new BookmarkCapture(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public ReactiveTransactionManager reactiveTransactionManager(Driver driver, |
| 86 | + ReactiveDatabaseSelectionProvider databaseSelectionProvider) { |
| 87 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 88 | + return new ReactiveNeo4jTransactionManager(driver, databaseSelectionProvider, |
| 89 | + Neo4jBookmarkManager.create(bookmarkCapture)); |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + public Driver driver() { |
| 94 | + |
| 95 | + return neo4jConnectionSupport.getDriver(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments