@ -29,19 +29,18 @@ import java.util.LinkedHashMap;
@@ -29,19 +29,18 @@ import java.util.LinkedHashMap;
import java.util.LinkedList ;
import java.util.List ;
import java.util.Map ;
import java.util.Set ;
import com.mongodb.BasicDBObject ;
import org.bson.BsonDocument ;
import org.bson.Document ;
import org.bson.conversions.Bson ;
import org.bson.types.ObjectId ;
import org.junit.Before ;
import org.junit.Test ;
import org.junit.runner.RunWith ;
import org.mockito.Matchers ;
import org.mockito.Mock ;
import org.mockito.Mockito ;
import org.mockito.junit.MockitoJUnitRunner ;
import org.springframework.data.annotation.AccessType ;
import org.springframework.data.annotation.AccessType.Type ;
import org.springframework.data.annotation.Id ;
@ -65,7 +64,7 @@ import com.mongodb.client.MongoDatabase;
@@ -65,7 +64,7 @@ import com.mongodb.client.MongoDatabase;
/ * *
* Unit tests for { @link DbRefMappingMongoConverter } .
*
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
@ -485,7 +484,8 @@ public class DbRefMappingMongoConverterUnitTests {
@@ -485,7 +484,8 @@ public class DbRefMappingMongoConverterUnitTests {
ClassWithLazyDbRefs result = converter . read ( ClassWithLazyDbRefs . class , object ) ;
PersistentPropertyAccessor accessor = propertyEntity . getPropertyAccessor ( result . dbRefToConcreteType ) ;
MongoPersistentProperty idProperty = mappingContext . getRequiredPersistentEntity ( LazyDbRefTarget . class ) . getIdProperty ( ) . get ( ) ;
MongoPersistentProperty idProperty = mappingContext . getRequiredPersistentEntity ( LazyDbRefTarget . class )
. getIdProperty ( ) . get ( ) ;
assertThat ( accessor . getProperty ( idProperty ) , is ( notNullValue ( ) ) ) ;
assertProxyIsResolved ( result . dbRefToConcreteType , false ) ;
@ -512,7 +512,8 @@ public class DbRefMappingMongoConverterUnitTests {
@@ -512,7 +512,8 @@ public class DbRefMappingMongoConverterUnitTests {
@Test // DATAMONGO-1076
public void shouldNotTriggerResolvingOfLazyLoadedProxyWhenFinalizeMethodIsInvoked ( ) throws Exception {
MongoPersistentEntity < ? > entity = mappingContext . getRequiredPersistentEntity ( WithObjectMethodOverrideLazyDbRefs . class ) ;
MongoPersistentEntity < ? > entity = mappingContext
. getRequiredPersistentEntity ( WithObjectMethodOverrideLazyDbRefs . class ) ;
MongoPersistentProperty property = entity . getRequiredPersistentProperty ( "dbRefToPlainObject" ) ;
String idValue = new ObjectId ( ) . toString ( ) ;
@ -534,8 +535,9 @@ public class DbRefMappingMongoConverterUnitTests {
@@ -534,8 +535,9 @@ public class DbRefMappingMongoConverterUnitTests {
String value = "val" ;
MappingMongoConverter converterSpy = spy ( converter ) ;
doReturn ( Arrays . asList ( new Document ( "_id" , id1 ) . append ( "value" , value ) ,
new Document ( "_id" , id2 ) . append ( "value" , value ) ) ) . when ( converterSpy ) . bulkReadRefs ( anyListOf ( DBRef . class ) ) ;
doReturn (
Arrays . asList ( new Document ( "_id" , id1 ) . append ( "value" , value ) , new Document ( "_id" , id2 ) . append ( "value" , value ) ) )
. when ( converterSpy ) . bulkReadRefs ( anyListOf ( DBRef . class ) ) ;
Document document = new Document ( ) ;
ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs ( ) ;
@ -553,6 +555,28 @@ public class DbRefMappingMongoConverterUnitTests {
@@ -553,6 +555,28 @@ public class DbRefMappingMongoConverterUnitTests {
verify ( converterSpy , never ( ) ) . readRef ( Mockito . any ( DBRef . class ) ) ;
}
@Test // DATAMONGO-1666
public void shouldBulkFetchSetOfReferencesForConstructorCreation ( ) {
String id1 = "1" ;
String id2 = "2" ;
String value = "val" ;
MappingMongoConverter converterSpy = spy ( converter ) ;
doReturn (
Arrays . asList ( new Document ( "_id" , id1 ) . append ( "value" , value ) , new Document ( "_id" , id2 ) . append ( "value" , value ) ) )
. when ( converterSpy ) . bulkReadRefs ( any ( ) ) ;
Document document = new Document ( "dbRefToInterface" ,
Arrays . asList ( new DBRef ( "lazyDbRefTarget" , "1" ) , new DBRef ( "lazyDbRefTarget" , "2" ) ) ) ;
ClassWithDbRefSetConstructor result = converterSpy . read ( ClassWithDbRefSetConstructor . class , document ) ;
assertThat ( result . dbRefToInterface , is ( instanceOf ( Set . class ) ) ) ;
verify ( converterSpy , never ( ) ) . readRef ( Mockito . any ( DBRef . class ) ) ;
}
@Test // DATAMONGO-1194
public void shouldFallbackToOneByOneFetchingWhenElementsInListOfReferencesPointToDifferentCollections ( ) {
@ -561,9 +585,8 @@ public class DbRefMappingMongoConverterUnitTests {
@@ -561,9 +585,8 @@ public class DbRefMappingMongoConverterUnitTests {
String value = "val" ;
MappingMongoConverter converterSpy = spy ( converter ) ;
doReturn ( new Document ( "_id" , id1 ) . append ( "value" , value ) )
. doReturn ( new Document ( "_id" , id2 ) . append ( "value" , value ) ) . when ( converterSpy )
. readRef ( Mockito . any ( DBRef . class ) ) ;
doReturn ( new Document ( "_id" , id1 ) . append ( "value" , value ) ) . doReturn ( new Document ( "_id" , id2 ) . append ( "value" , value ) )
. when ( converterSpy ) . readRef ( Mockito . any ( DBRef . class ) ) ;
Document document = new Document ( ) ;
ClassWithLazyDbRefs lazyDbRefs = new ClassWithLazyDbRefs ( ) ;
@ -678,6 +701,15 @@ public class DbRefMappingMongoConverterUnitTests {
@@ -678,6 +701,15 @@ public class DbRefMappingMongoConverterUnitTests {
lazy = true ) LazyDbRefTargetWithPeristenceConstructorWithoutDefaultConstructor dbRefToConcreteTypeWithPersistenceConstructorWithoutDefaultConstructor ;
}
static class ClassWithDbRefSetConstructor {
final @org.springframework.data.mongodb.core.mapping.DBRef Set < LazyDbRefTarget > dbRefToInterface ;
public ClassWithDbRefSetConstructor ( Set < LazyDbRefTarget > dbRefToInterface ) {
this . dbRefToInterface = dbRefToInterface ;
}
}
static class SerializableClassWithLazyDbRefs implements Serializable {
private static final long serialVersionUID = 1L ;
@ -785,7 +817,7 @@ public class DbRefMappingMongoConverterUnitTests {
@@ -785,7 +817,7 @@ public class DbRefMappingMongoConverterUnitTests {
super ( id , value ) ;
}
/ *
/ *
* ( non - Javadoc )
* @see java . lang . Object # toString ( )
* /