@ -19,16 +19,23 @@ import static org.assertj.core.api.Assertions.*;
@@ -19,16 +19,23 @@ import static org.assertj.core.api.Assertions.*;
import java.util.Date ;
import org.bson.types.ObjectId ;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.extension.ExtendWith ;
import org.springframework.context.support.AbstractApplicationContext ;
import org.springframework.context.support.ClassPathXml ApplicationContext ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.context.ApplicationContext ;
import org.springframework.data.annotation.CreatedDate ;
import org.springframework.data.annotation.Id ;
import org.springframework.data.annotation.LastModifiedDate ;
import org.springframework.data.mapping.callback.EntityCallbacks ;
import org.springframework.data.mongodb.core.MongoTemplate ;
import org.springframework.data.mongodb.core.mapping.DBRef ;
import org.springframework.data.mongodb.core.mapping.DocumentReference ;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext ;
import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback ;
import org.springframework.test.context.ContextConfiguration ;
import org.springframework.test.context.junit.jupiter.SpringExtension ;
/ * *
* Integration test for the auditing support .
@ -36,14 +43,17 @@ import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback
@@ -36,14 +43,17 @@ import org.springframework.data.mongodb.core.mapping.event.BeforeConvertCallback
* @author Oliver Gierke
* @author Mark Paluch
* /
public class AuditingIntegrationTests {
@ContextConfiguration ( locations = "auditing.xml" )
@ExtendWith ( SpringExtension . class )
class AuditingIntegrationTests {
@Test // DATAMONGO-577, DATAMONGO-800, DATAMONGO-883, DATAMONGO-2261
public void enablesAuditingAndSetsPropertiesAccordingly ( ) throws Exception {
@Autowired MongoMappingContext mappingContext ;
@Autowired ApplicationContext context ;
@Autowired MongoTemplate template ;
AbstractApplicationContext context = new ClassPathXmlApplicationContext ( "auditing.xml" , getClass ( ) ) ;
@Test // DATAMONGO-577, DATAMONGO-800, DATAMONGO-883, DATAMONGO-2261
void enablesAuditingAndSetsPropertiesAccordingly ( ) throws Exception {
MongoMappingContext mappingContext = context . getBean ( MongoMappingContext . class ) ;
mappingContext . getPersistentEntity ( Entity . class ) ;
EntityCallbacks callbacks = EntityCallbacks . create ( context ) ;
@ -55,18 +65,67 @@ public class AuditingIntegrationTests {
@@ -55,18 +65,67 @@ public class AuditingIntegrationTests {
assertThat ( entity . modified ) . isEqualTo ( entity . created ) ;
Thread . sleep ( 10 ) ;
entity . id = 1L ;
entity . id = "foo" ;
entity = callbacks . callback ( BeforeConvertCallback . class , entity , "collection-1" ) ;
assertThat ( entity . created ) . isNotNull ( ) ;
assertThat ( entity . modified ) . isNotEqualTo ( entity . created ) ;
context . close ( ) ;
}
class Entity {
@Test // GH-5031
void handlesDocumentReferenceAuditingCorrectly ( ) throws InterruptedException {
template . remove ( TopDocumentReferenceLevelEntity . class ) . all ( ) ;
Entity entity = new Entity ( ) ;
template . insert ( entity ) ;
TopDocumentReferenceLevelEntity tle = new TopDocumentReferenceLevelEntity ( ) ;
tle . entity = entity ;
template . insert ( tle ) ;
Thread . sleep ( 200 ) ;
TopDocumentReferenceLevelEntity loadAndModify = template . findById ( tle . id , TopDocumentReferenceLevelEntity . class ) ;
Date created = loadAndModify . entity . getCreated ( ) ;
Date modified = loadAndModify . entity . getModified ( ) ;
template . save ( loadAndModify . entity ) ;
TopDocumentReferenceLevelEntity loaded = template . findById ( tle . id , TopDocumentReferenceLevelEntity . class ) ;
assertThat ( loaded . entity . getCreated ( ) ) . isEqualTo ( created ) ;
assertThat ( loaded . entity . getModified ( ) ) . isNotEqualTo ( modified ) ;
}
@Test // GH-5031
void handlesDbRefAuditingCorrectly ( ) throws InterruptedException {
template . remove ( TopDbRefLevelEntity . class ) . all ( ) ;
Entity entity = new Entity ( ) ;
template . insert ( entity ) ;
TopDbRefLevelEntity tle = new TopDbRefLevelEntity ( ) ;
tle . entity = entity ;
template . insert ( tle ) ;
Thread . sleep ( 200 ) ;
TopDbRefLevelEntity loadAndModify = template . findById ( tle . id , TopDbRefLevelEntity . class ) ;
Date created = loadAndModify . entity . getCreated ( ) ;
Date modified = loadAndModify . entity . getModified ( ) ;
template . save ( loadAndModify . entity ) ;
TopDbRefLevelEntity loaded = template . findById ( tle . id , TopDbRefLevelEntity . class ) ;
@Id Long id ;
assertThat ( loaded . entity . getCreated ( ) ) . isEqualTo ( created ) ;
assertThat ( loaded . entity . getModified ( ) ) . isNotEqualTo ( modified ) ;
}
static class Entity {
@Id String id ;
@CreatedDate Date created ;
Date modified ;
@ -74,5 +133,32 @@ public class AuditingIntegrationTests {
@@ -74,5 +133,32 @@ public class AuditingIntegrationTests {
public Date getModified ( ) {
return modified ;
}
public Date getCreated ( ) {
return created ;
}
public void setCreated ( Date created ) {
this . created = created ;
}
public void setModified ( Date modified ) {
this . modified = modified ;
}
}
static class TopDocumentReferenceLevelEntity {
@Id ObjectId id ;
@DocumentReference ( lazy = true ) Entity entity ;
}
static class TopDbRefLevelEntity {
@Id ObjectId id ;
@DBRef ( lazy = true ) Entity entity ;
}
}