Skip to content

DATAMONGO-2150 - Fixed broken auditing for entities using optimistic locking. #627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2150-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2150-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-mongodb-cross-store/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2150-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2150-SNAPSHOT</version>
</dependency>

<!-- reactive -->
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2150-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<version>2.2.0.DATAMONGO-2150-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
*
* @author Thomas Darimont
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
Expand Down Expand Up @@ -159,19 +160,19 @@ private <T extends AuditablePerson> void verifyAuditingViaVersionProperty(T inst
assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[0]);
assertThat(entity.isNew(instance)).isTrue();

instance = auditablePersonRepository.save(instance);
instance = persister.apply(instance);

assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[1]);
assertThat(entity.isNew(instance)).isFalse();

instance = auditablePersonRepository.save(instance);
instance = persister.apply(instance);

assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[2]);
assertThat(entity.isNew(instance)).isFalse();
}

@Repository
static interface AuditablePersonRepository extends MongoRepository<AuditablePerson, String> {}
interface AuditablePersonRepository extends MongoRepository<AuditablePerson, String> {}

@Configuration
@EnableMongoRepositories
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.config;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.annotation.Version;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.mongodb.core.AuditablePerson;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;

/**
* Integration test for the auditing support via {@link org.springframework.data.mongodb.core.ReactiveMongoTemplate}.
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@ContextConfiguration
public class ReactiveAuditingTests {

@Autowired ReactiveAuditablePersonRepository auditablePersonRepository;
@Autowired AuditorAware<AuditablePerson> auditorAware;
@Autowired MongoMappingContext context;
@Autowired ReactiveMongoOperations operations;

@Configuration
@EnableMongoAuditing(auditorAwareRef = "auditorProvider")
@EnableReactiveMongoRepositories(basePackageClasses = ReactiveAuditingTests.class, considerNestedRepositories = true)
static class Config extends AbstractReactiveMongoConfiguration {

@Override
protected String getDatabaseName() {
return "database";
}

@Override
public MongoClient reactiveMongoClient() {
return MongoClients.create();
}

@Bean
@SuppressWarnings("unchecked")
public AuditorAware<AuditablePerson> auditorProvider() {
return mock(AuditorAware.class);
}
}

@Test // DATAMONGO-2139, DATAMONGO-2150
public void auditingWorksForVersionedEntityWithWrapperVersion() {

verifyAuditingViaVersionProperty(new VersionedAuditablePerson(), //
it -> it.version, //
auditablePersonRepository::save, //
null, 0L, 1L);
}

@Test // DATAMONGO-2139, DATAMONGO-2150
public void auditingWorksForVersionedEntityWithSimpleVersion() {

verifyAuditingViaVersionProperty(new SimpleVersionedAuditablePerson(), //
it -> it.version, //
auditablePersonRepository::save, //
0L, 1L, 2L);
}

@Test // DATAMONGO-2139, DATAMONGO-2150
public void auditingWorksForVersionedEntityWithWrapperVersionOnTemplate() {

verifyAuditingViaVersionProperty(new VersionedAuditablePerson(), //
it -> it.version, //
operations::save, //
null, 0L, 1L);
}

@Test // DATAMONGO-2139, DATAMONGO-2150
public void auditingWorksForVersionedEntityWithSimpleVersionOnTemplate() {
verifyAuditingViaVersionProperty(new SimpleVersionedAuditablePerson(), //
it -> it.version, //
operations::save, //
0L, 1L, 2L);
}

private <T extends AuditablePerson> void verifyAuditingViaVersionProperty(T instance,
Function<T, Object> versionExtractor, Function<T, Mono<T>> persister, Object... expectedValues) {

AtomicReference<T> instanceHolder = new AtomicReference<>(instance);
MongoPersistentEntity<?> entity = context.getRequiredPersistentEntity(instance.getClass());

assertThat(versionExtractor.apply(instance)).isEqualTo(expectedValues[0]);
assertThat(entity.isNew(instance)).isTrue();

persister.apply(instanceHolder.get()) //
.as(StepVerifier::create).consumeNextWith(actual -> {

instanceHolder.set(actual);

assertThat(versionExtractor.apply(actual)).isEqualTo(expectedValues[1]);
assertThat(entity.isNew(actual)).isFalse();
}).verifyComplete();

persister.apply(instanceHolder.get()) //
.as(StepVerifier::create).consumeNextWith(actual -> {

instanceHolder.set(actual);

assertThat(versionExtractor.apply(actual)).isEqualTo(expectedValues[2]);
assertThat(entity.isNew(actual)).isFalse();
}).verifyComplete();
}

interface ReactiveAuditablePersonRepository extends ReactiveMongoRepository<AuditablePerson, String> {}

static class VersionedAuditablePerson extends AuditablePerson {
@Version Long version;
}

static class SimpleVersionedAuditablePerson extends AuditablePerson {
@Version long version;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -774,12 +774,9 @@ public void savesMapCorrectly() {
StepVerifier.create(template.save(map, "maps")).expectNextCount(1).verifyComplete();
}

@Test // DATAMONGO-1444, DATAMONGO-1730
@Test(expected = MappingException.class) // DATAMONGO-1444, DATAMONGO-1730, DATAMONGO-2150
public void savesMongoPrimitiveObjectCorrectly() {

StepVerifier.create(template.save(new Object(), "collection")) //
.expectError(MappingException.class) //
.verify();
template.save(new Object(), "collection");
}

@Test // DATAMONGO-1444
Expand Down Expand Up @@ -852,12 +849,9 @@ public void writesPlainString() {
.verifyComplete();
}

@Test // DATAMONGO-1444
@Test(expected = MappingException.class) // DATAMONGO-1444, DATAMONGO-2150
public void rejectsNonJsonStringForSave() {

StepVerifier.create(template.save("Foobar!", "collection")) //
.expectError(MappingException.class) //
.verify();
template.save("Foobar!", "collection");
}

@Test // DATAMONGO-1444
Expand Down