|
| 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.imperative; |
| 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.Optional; |
| 23 | +import java.util.UUID; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import org.junit.jupiter.api.BeforeEach; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | +import org.neo4j.driver.Driver; |
| 30 | +import org.neo4j.driver.Session; |
| 31 | +import org.neo4j.driver.Transaction; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.context.annotation.Bean; |
| 34 | +import org.springframework.context.annotation.Configuration; |
| 35 | +import org.springframework.core.Ordered; |
| 36 | +import org.springframework.data.domain.AuditorAware; |
| 37 | +import org.springframework.data.neo4j.config.EnableNeo4jAuditing; |
| 38 | +import org.springframework.data.neo4j.core.DatabaseSelectionProvider; |
| 39 | +import org.springframework.data.neo4j.core.mapping.callback.AuditingBeforeBindCallback; |
| 40 | +import org.springframework.data.neo4j.core.mapping.callback.BeforeBindCallback; |
| 41 | +import org.springframework.data.neo4j.core.transaction.Neo4jBookmarkManager; |
| 42 | +import org.springframework.data.neo4j.core.transaction.Neo4jTransactionManager; |
| 43 | +import org.springframework.data.neo4j.integration.shared.common.Book; |
| 44 | +import org.springframework.data.neo4j.integration.shared.common.Editor; |
| 45 | +import org.springframework.data.neo4j.integration.shared.common.ImmutableAuditableThing; |
| 46 | +import org.springframework.data.neo4j.repository.Neo4jRepository; |
| 47 | +import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; |
| 48 | +import org.springframework.data.neo4j.test.BookmarkCapture; |
| 49 | +import org.springframework.data.neo4j.test.Neo4jExtension; |
| 50 | +import org.springframework.data.neo4j.test.Neo4jImperativeTestConfiguration; |
| 51 | +import org.springframework.data.neo4j.test.Neo4jIntegrationTest; |
| 52 | +import org.springframework.transaction.PlatformTransactionManager; |
| 53 | +import org.springframework.transaction.annotation.EnableTransactionManagement; |
| 54 | + |
| 55 | +/** |
| 56 | + * @author Michael J. Simons |
| 57 | + */ |
| 58 | +@Neo4jIntegrationTest |
| 59 | +public class ChainedAuditingIT { |
| 60 | + |
| 61 | + protected static Neo4jExtension.Neo4jConnectionSupport neo4jConnectionSupport; |
| 62 | + |
| 63 | + @BeforeEach |
| 64 | + protected void setupData(@Autowired Driver driver, @Autowired BookmarkCapture bookmarkCapture) { |
| 65 | + try (Session session = driver.session(bookmarkCapture.createSessionConfig()); |
| 66 | + Transaction transaction = session.beginTransaction()) { |
| 67 | + transaction.run("MATCH (n) detach delete n"); |
| 68 | + transaction.commit(); |
| 69 | + bookmarkCapture.seedWith(session.lastBookmark()); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void auditingCallbacksShouldBeCombinableWithOtherCallbacks(@Autowired BookRepository bookRepository) { |
| 75 | + |
| 76 | + Book book = new Book("Dune"); |
| 77 | + book = bookRepository.save(book); |
| 78 | + |
| 79 | + assertThat(book.getCreatedAt()).isNotNull(); |
| 80 | + assertThat(book.getModifiedAt()).isNotNull(); |
| 81 | + assertThat(book.getCreatedBy()).isNotNull(); |
| 82 | + assertThat(book.getModifiedBy()).isNotNull(); |
| 83 | + |
| 84 | + for (int i = 1; i <= 5; ++i) { |
| 85 | + book.setContent(String.format("Content was edited %d times", i)); |
| 86 | + book = bookRepository.save(book); |
| 87 | + } |
| 88 | + |
| 89 | + assertThat(book.getModifiedBy()).isEqualTo("User 6"); |
| 90 | + |
| 91 | + String[] names = Stream.of(5, 3, 2, 1).map(i -> "User " + i).toArray(String[]::new); |
| 92 | + Editor editor = book.getPreviousEditor(); |
| 93 | + for (int i = 0; i < names.length; i++) { |
| 94 | + assertThat(editor).isNotNull(); |
| 95 | + assertThat(editor.getName()).isEqualTo(names[i]); |
| 96 | + editor = editor.getPredecessor(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + interface BookRepository extends Neo4jRepository<Book, UUID> { |
| 101 | + } |
| 102 | + |
| 103 | + static class BookEditorHistorian implements BeforeBindCallback<Book>, Ordered { |
| 104 | + |
| 105 | + @Override |
| 106 | + public Book onBeforeBind(Book entity) { |
| 107 | + if (entity.getModifiedBy() != null) { |
| 108 | + Editor previousEditor = entity.getPreviousEditor(); |
| 109 | + if (previousEditor == null || !previousEditor.getName().equals(entity.getModifiedBy())) { |
| 110 | + previousEditor = new Editor(entity.getModifiedBy(), previousEditor); |
| 111 | + } |
| 112 | + entity.setPreviousEditor(previousEditor); |
| 113 | + } |
| 114 | + return entity; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public int getOrder() { |
| 119 | + return AuditingBeforeBindCallback.NEO4J_AUDITING_ORDER - 50; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Configuration |
| 124 | + @EnableTransactionManagement |
| 125 | + @EnableNeo4jRepositories(considerNestedRepositories = true) |
| 126 | + @EnableNeo4jAuditing(auditorAwareRef = "auditorProvider") |
| 127 | + static class Config extends Neo4jImperativeTestConfiguration { |
| 128 | + |
| 129 | + @Bean |
| 130 | + public Driver driver() { |
| 131 | + return neo4jConnectionSupport.getDriver(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + protected Collection<String> getMappingBasePackages() { |
| 136 | + return Collections.singleton(ImmutableAuditableThing.class.getPackage().getName()); |
| 137 | + } |
| 138 | + |
| 139 | + @Bean |
| 140 | + public BookmarkCapture bookmarkCapture() { |
| 141 | + return new BookmarkCapture(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public PlatformTransactionManager transactionManager(Driver driver, |
| 146 | + DatabaseSelectionProvider databaseNameProvider) { |
| 147 | + |
| 148 | + BookmarkCapture bookmarkCapture = bookmarkCapture(); |
| 149 | + return new Neo4jTransactionManager(driver, databaseNameProvider, |
| 150 | + Neo4jBookmarkManager.create(bookmarkCapture)); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean isCypher5Compatible() { |
| 155 | + return neo4jConnectionSupport.isCypher5SyntaxCompatible(); |
| 156 | + } |
| 157 | + |
| 158 | + @Bean |
| 159 | + public AuditorAware<String> auditorProvider() { |
| 160 | + AtomicInteger state = new AtomicInteger(0); |
| 161 | + return () -> { |
| 162 | + int i = state.compareAndSet(3, 4) ? 3 : state.incrementAndGet(); |
| 163 | + return Optional.of("User " + i); |
| 164 | + }; |
| 165 | + } |
| 166 | + |
| 167 | + @Bean |
| 168 | + public BeforeBindCallback<Book> bookEditorHistorian() { |
| 169 | + return new BookEditorHistorian(); |
| 170 | + } |
| 171 | + |
| 172 | + } |
| 173 | +} |
0 commit comments