|
| 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.lite; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.BeforeAll; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.neo4j.driver.Driver; |
| 26 | +import org.neo4j.driver.Session; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.data.neo4j.config.AbstractNeo4jConfig; |
| 31 | +import org.springframework.data.neo4j.core.DatabaseSelectionProvider; |
| 32 | +import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager; |
| 33 | +import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager; |
| 34 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 35 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 36 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 37 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 38 | +import org.springframework.transaction.PlatformTransactionManager; |
| 39 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 40 | + |
| 41 | +@Neo4jIntegrationTest |
| 42 | +class LightweightMappingIT { |
| 43 | + |
| 44 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + static void setupData(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture) { |
| 48 | + |
| 49 | + try (Session session = driver.session(bookmarkCapture.createSessionConfig())) { |
| 50 | + session.run("MATCH (n) DETACH DELETE n").consume(); |
| 51 | + // language=cypher |
| 52 | + session.run( |
| 53 | + "CREATE (u1:User {login: 'michael', id: randomUUID()})\n" + |
| 54 | + "CREATE (u2:User {login: 'gerrit', id: randomUUID()})\n" + |
| 55 | + "CREATE (so1:SomeDomainObject {name: 'name1', id: randomUUID()})\n" + |
| 56 | + "CREATE (so2:SomeDomainObject {name: 'name2', id: randomUUID()})\n" + |
| 57 | + "CREATE (so1)<-[:OWNS]-(u1)-[:OWNS]->(so2)\n" |
| 58 | + ); |
| 59 | + bookmarkCapture.seedWith(session.lastBookmark()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void getAllFlatShouldWork(@Autowired SomeDomainRepository repository) { |
| 65 | + |
| 66 | + Collection<MyDTO> dtos = repository.getAllFlat(); |
| 67 | + assertThat(dtos).hasSize(10) |
| 68 | + .allSatisfy(dto -> { |
| 69 | + assertThat(dto.counter).isGreaterThan(0); |
| 70 | + assertThat(dto.resyncId).isNotNull(); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void getOneFlatShouldWork(@Autowired SomeDomainRepository repository) { |
| 76 | + |
| 77 | + Optional<MyDTO> dtos = repository.getOneFlat(); |
| 78 | + assertThat(dtos).hasValueSatisfying(dto -> { |
| 79 | + assertThat(dto.counter).isEqualTo(4711L); |
| 80 | + assertThat(dto.resyncId).isNotNull(); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void getAllNestedShouldWork(@Autowired SomeDomainRepository repository) { |
| 86 | + |
| 87 | + Collection<MyDTO> dtos = repository.getNestedStuff(); |
| 88 | + assertThat(dtos).hasSize(1) |
| 89 | + .first() |
| 90 | + .satisfies(dto -> { |
| 91 | + assertThat(dto.counter).isEqualTo(4711L); |
| 92 | + assertThat(dto.resyncId).isNotNull(); |
| 93 | + assertThat(dto.user) |
| 94 | + .isNotNull() |
| 95 | + .extracting(User::getLogin) |
| 96 | + .isEqualTo("michael"); |
| 97 | + assertThat(dto.user.getOwnedObjects()) |
| 98 | + .hasSize(2); |
| 99 | + |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + @Test |
| 105 | + void getTestedDTOsShouldWork(@Autowired SomeDomainRepository repository) { |
| 106 | + |
| 107 | + Optional<A> dto = repository.getOneNestedDTO(); |
| 108 | + assertThat(dto).hasValueSatisfying(v -> { |
| 109 | + assertThat(v.getOuter()).isEqualTo("av"); |
| 110 | + assertThat(v.getNested()).isNotNull() |
| 111 | + .extracting(B::getInner).isEqualTo("bv"); |
| 112 | + }); |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + @Configuration |
| 117 | + @EnableTransactionManagement |
| 118 | + @EnableNeo4jRepositories(considerNestedRepositories = true) |
| 119 | + static class Config extends AbstractNeo4jConfig { |
| 120 | + |
| 121 | + @Bean |
| 122 | + public Driver driver() { |
| 123 | + return neo4jConnectionSupport.getDriver(); |
| 124 | + } |
| 125 | + |
| 126 | + @Bean |
| 127 | + public BookmarkCapture bookmarkCapture() { |
| 128 | + return new BookmarkCapture(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public PlatformTransactionManager transactionManager(Driver driver, DatabaseSelectionProvider databaseNameProvider) { |
| 133 | + |
| 134 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 135 | + return new Neo4jTransactionManager(driver, databaseNameProvider, Neo4jBookmarkManager.create(bookmarkCapture)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | +} |
0 commit comments