|
| 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.gh2289; |
| 17 | + |
| 18 | +import reactor.test.StepVerifier; |
| 19 | + |
| 20 | +import java.util.concurrent.atomic.AtomicLong; |
| 21 | +import java.util.concurrent.atomic.AtomicReference; |
| 22 | + |
| 23 | +import org.assertj.core.api.Assertions; |
| 24 | +import org.junit.jupiter.api.BeforeAll; |
| 25 | +import org.junit.jupiter.api.RepeatedTest; |
| 26 | +import org.junit.jupiter.api.Tag; |
| 27 | +import org.neo4j.driver.Driver; |
| 28 | +import org.neo4j.driver.Session; |
| 29 | +import org.neo4j.driver.Transaction; |
| 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.config.AbstractReactiveNeo4jConfig; |
| 34 | +import org.springframework.data.neo4j.repository.ReactiveNeo4jRepository; |
| 35 | +import org.springframework.data.neo4j.repository.config.EnableReactiveNeo4jRepositories; |
| 36 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 37 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 38 | +import org.springframework.stereotype.Repository; |
| 39 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Michael J. Simons |
| 43 | + */ |
| 44 | +@Neo4jIntegrationTest |
| 45 | +@Tag(Neo4jExtension.NEEDS_REACTIVE_SUPPORT) |
| 46 | +class ReactiveGH2289IT { |
| 47 | + |
| 48 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 49 | + |
| 50 | + @BeforeAll |
| 51 | + protected static void setupData() { |
| 52 | + try (Session session = neo4jConnectionSupport.getDriver().session(); |
| 53 | + Transaction transaction = session.beginTransaction(); |
| 54 | + ) { |
| 55 | + transaction.run("MATCH (n) detach delete n"); |
| 56 | + transaction.commit(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @RepeatedTest(23) |
| 61 | + void testNewRelation(@Autowired SkuRepository skuRepo) { |
| 62 | + |
| 63 | + AtomicLong bId = new AtomicLong(); |
| 64 | + AtomicReference<Sku> cRef = new AtomicReference<>(); |
| 65 | + skuRepo.save(new Sku(0L, "A")) |
| 66 | + .zipWith(skuRepo.save(new Sku(1L, "B"))) |
| 67 | + .zipWith(skuRepo.save(new Sku(2L, "C"))) |
| 68 | + .zipWith(skuRepo.save(new Sku(3L, "D"))).flatMap(t -> { |
| 69 | + Sku a = t.getT1().getT1().getT1(); |
| 70 | + Sku b = t.getT1().getT1().getT2(); |
| 71 | + Sku c = t.getT1().getT2(); |
| 72 | + Sku d = t.getT2(); |
| 73 | + |
| 74 | + bId.set(b.getId()); |
| 75 | + cRef.set(c); |
| 76 | + a.rangeRelationTo(b, 1, 1, RelationType.MULTIPLICATIVE); |
| 77 | + a.rangeRelationTo(c, 1, 1, RelationType.MULTIPLICATIVE); |
| 78 | + a.rangeRelationTo(d, 1, 1, RelationType.MULTIPLICATIVE); |
| 79 | + return skuRepo.save(a); |
| 80 | + }).as(StepVerifier::create) |
| 81 | + .expectNextMatches(a -> a.getRangeRelationsOut().size() == 3) |
| 82 | + .verifyComplete(); |
| 83 | + |
| 84 | + skuRepo.findById(bId.get()) |
| 85 | + .doOnNext(b -> Assertions.assertThat(b.getRangeRelationsIn()).hasSize(1)) |
| 86 | + .flatMap(b -> { |
| 87 | + b.rangeRelationTo(cRef.get(), 1, 1, RelationType.MULTIPLICATIVE); |
| 88 | + return skuRepo.save(b); |
| 89 | + }) |
| 90 | + .as(StepVerifier::create) |
| 91 | + .expectNextMatches(a -> a.getRangeRelationsIn().size() == 1 && a.getRangeRelationsOut().size() == 1) |
| 92 | + .verifyComplete(); |
| 93 | + } |
| 94 | + |
| 95 | + @Repository |
| 96 | + public interface SkuRepository extends ReactiveNeo4jRepository<Sku, Long> { |
| 97 | + } |
| 98 | + |
| 99 | + @Configuration |
| 100 | + @EnableTransactionManagement |
| 101 | + @EnableReactiveNeo4jRepositories(considerNestedRepositories = true) |
| 102 | + static class Config extends AbstractReactiveNeo4jConfig { |
| 103 | + |
| 104 | + @Bean |
| 105 | + public Driver driver() { |
| 106 | + |
| 107 | + return neo4jConnectionSupport.getDriver(); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments