|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | + * http://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.mongodb.config; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import reactor.core.publisher.Mono; |
| 22 | +import reactor.test.StepVerifier; |
| 23 | + |
| 24 | +import java.util.concurrent.atomic.AtomicReference; |
| 25 | +import java.util.function.Function; |
| 26 | + |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 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.annotation.Version; |
| 33 | +import org.springframework.data.domain.AuditorAware; |
| 34 | +import org.springframework.data.mongodb.core.AuditablePerson; |
| 35 | +import org.springframework.data.mongodb.core.ReactiveMongoOperations; |
| 36 | +import org.springframework.data.mongodb.core.mapping.MongoMappingContext; |
| 37 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
| 38 | +import org.springframework.data.mongodb.repository.ReactiveMongoRepository; |
| 39 | +import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; |
| 40 | +import org.springframework.test.context.ContextConfiguration; |
| 41 | +import org.springframework.test.context.junit4.SpringRunner; |
| 42 | + |
| 43 | +import com.mongodb.reactivestreams.client.MongoClient; |
| 44 | +import com.mongodb.reactivestreams.client.MongoClients; |
| 45 | + |
| 46 | +/** |
| 47 | + * Integration test for the auditing support via {@link org.springframework.data.mongodb.core.ReactiveMongoTemplate}. |
| 48 | + * |
| 49 | + * @author Mark Paluch |
| 50 | + */ |
| 51 | +@RunWith(SpringRunner.class) |
| 52 | +@ContextConfiguration |
| 53 | +public class ReactiveAuditingTests { |
| 54 | + |
| 55 | + @Autowired ReactiveAuditablePersonRepository auditablePersonRepository; |
| 56 | + @Autowired AuditorAware<AuditablePerson> auditorAware; |
| 57 | + @Autowired MongoMappingContext context; |
| 58 | + @Autowired ReactiveMongoOperations operations; |
| 59 | + |
| 60 | + @Configuration |
| 61 | + @EnableMongoAuditing(auditorAwareRef = "auditorProvider") |
| 62 | + @EnableReactiveMongoRepositories(basePackageClasses = ReactiveAuditingTests.class, considerNestedRepositories = true) |
| 63 | + static class Config extends AbstractReactiveMongoConfiguration { |
| 64 | + |
| 65 | + @Override |
| 66 | + protected String getDatabaseName() { |
| 67 | + return "database"; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public MongoClient reactiveMongoClient() { |
| 72 | + return MongoClients.create(); |
| 73 | + } |
| 74 | + |
| 75 | + @Bean |
| 76 | + @SuppressWarnings("unchecked") |
| 77 | + public AuditorAware<AuditablePerson> auditorProvider() { |
| 78 | + return mock(AuditorAware.class); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test // DATAMONGO-2139, DATAMONGO-2150 |
| 83 | + public void auditingWorksForVersionedEntityWithWrapperVersion() { |
| 84 | + |
| 85 | + verifyAuditingViaVersionProperty(new VersionedAuditablePerson(), // |
| 86 | + it -> it.version, // |
| 87 | + auditablePersonRepository::save, // |
| 88 | + null, 0L, 1L); |
| 89 | + } |
| 90 | + |
| 91 | + @Test // DATAMONGO-2139, DATAMONGO-2150 |
| 92 | + public void auditingWorksForVersionedEntityWithSimpleVersion() { |
| 93 | + |
| 94 | + verifyAuditingViaVersionProperty(new SimpleVersionedAuditablePerson(), // |
| 95 | + it -> it.version, // |
| 96 | + auditablePersonRepository::save, // |
| 97 | + 0L, 1L, 2L); |
| 98 | + } |
| 99 | + |
| 100 | + @Test // DATAMONGO-2139, DATAMONGO-2150 |
| 101 | + public void auditingWorksForVersionedEntityWithWrapperVersionOnTemplate() { |
| 102 | + |
| 103 | + verifyAuditingViaVersionProperty(new VersionedAuditablePerson(), // |
| 104 | + it -> it.version, // |
| 105 | + operations::save, // |
| 106 | + null, 0L, 1L); |
| 107 | + } |
| 108 | + |
| 109 | + @Test // DATAMONGO-2139, DATAMONGO-2150 |
| 110 | + public void auditingWorksForVersionedEntityWithSimpleVersionOnTemplate() { |
| 111 | + verifyAuditingViaVersionProperty(new SimpleVersionedAuditablePerson(), // |
| 112 | + it -> it.version, // |
| 113 | + operations::save, // |
| 114 | + 0L, 1L, 2L); |
| 115 | + } |
| 116 | + |
| 117 | + private <T extends AuditablePerson> void verifyAuditingViaVersionProperty(T instance, |
| 118 | + Function<T, Object> versionExtractor, Function<T, Mono<T>> persister, Object... expectedValues) { |
| 119 | + |
| 120 | + AtomicReference<T> instanceHolder = new AtomicReference<>(instance); |
| 121 | + MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(instance.getClass()); |
| 122 | + |
| 123 | + assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[0]); |
| 124 | + assertThat(entity.isNew(instance)).isTrue(); |
| 125 | + |
| 126 | + persister.apply(instanceHolder.get()) // |
| 127 | + .as(StepVerifier::create).consumeNextWith(actual -> { |
| 128 | + |
| 129 | + instanceHolder.set(actual); |
| 130 | + |
| 131 | + assertThat(versionExtractor.apply(actual)).isEqualTo(expectedValues[1]); |
| 132 | + assertThat(entity.isNew(actual)).isFalse(); |
| 133 | + }).verifyComplete(); |
| 134 | + |
| 135 | + persister.apply(instanceHolder.get()) // |
| 136 | + .as(StepVerifier::create).consumeNextWith(actual -> { |
| 137 | + |
| 138 | + instanceHolder.set(actual); |
| 139 | + |
| 140 | + assertThat(versionExtractor.apply(actual)).isEqualTo(expectedValues[2]); |
| 141 | + assertThat(entity.isNew(actual)).isFalse(); |
| 142 | + }).verifyComplete(); |
| 143 | + } |
| 144 | + |
| 145 | + interface ReactiveAuditablePersonRepository extends ReactiveMongoRepository<AuditablePerson, String> {} |
| 146 | + |
| 147 | + static class VersionedAuditablePerson extends AuditablePerson { |
| 148 | + @Version Long version; |
| 149 | + } |
| 150 | + |
| 151 | + static class SimpleVersionedAuditablePerson extends AuditablePerson { |
| 152 | + @Version long version; |
| 153 | + } |
| 154 | +} |
0 commit comments