Search in sources :

Example 36 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class ReactivePerformanceTests method setUp.

@Before
public void setUp() throws Exception {
    mongo = MongoClients.create();
    SimpleReactiveMongoDatabaseFactory mongoDbFactory = new SimpleReactiveMongoDatabaseFactory(this.mongo, DATABASE_NAME);
    MongoMappingContext context = new MongoMappingContext();
    context.setInitialEntitySet(Collections.singleton(Person.class));
    context.afterPropertiesSet();
    converter = new MappingMongoConverter(new DbRefResolver() {

        @Override
        public Object resolveDbRef(MongoPersistentProperty property, DBRef dbref, DbRefResolverCallback callback, DbRefProxyHandler proxyHandler) {
            return null;
        }

        @Override
        public DBRef createDbRef(org.springframework.data.mongodb.core.mapping.DBRef annotation, MongoPersistentEntity<?> entity, Object id) {
            return null;
        }

        @Override
        public Document fetch(DBRef dbRef) {
            return null;
        }

        @Override
        public List<Document> bulkFetch(List<DBRef> dbRefs) {
            return null;
        }
    }, context);
    operations = new ReactiveMongoTemplate(mongoDbFactory, converter);
    ReactiveMongoRepositoryFactory factory = new ReactiveMongoRepositoryFactory(operations);
    repository = factory.getRepository(ReactivePersonRepository.class);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DbRefResolverCallback(org.springframework.data.mongodb.core.convert.DbRefResolverCallback) DBRef(com.mongodb.DBRef) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) Document(org.bson.Document) SimpleReactiveMongoDatabaseFactory(org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory) ReactiveMongoTemplate(org.springframework.data.mongodb.core.ReactiveMongoTemplate) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) ReactiveMongoRepositoryFactory(org.springframework.data.mongodb.repository.support.ReactiveMongoRepositoryFactory) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DbRefProxyHandler(org.springframework.data.mongodb.core.convert.DbRefProxyHandler) Before(org.junit.Before)

Example 37 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class PathUnitTests method isCycleShouldReturnFalseWhenNoCyclePresent.

// DATAMONGO-1782
@Test
public void isCycleShouldReturnFalseWhenNoCyclePresent() {
    MongoPersistentProperty foo = createPersistentPropertyMock(entityMock, "foo");
    MongoPersistentProperty bar = createPersistentPropertyMock(entityMock, "bar");
    Path path = Path.of(foo).append(bar);
    assertThat(path.isCycle(), is(false));
    assertThat(path.toCyclePath(), is(equalTo("")));
    assertThat(path.toString(), is(equalTo("foo -> bar")));
}
Also used : Path(org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver.CycleGuard.Path) MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) Test(org.junit.Test)

Example 38 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class PathUnitTests method createPersistentPropertyMock.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static MongoPersistentProperty createPersistentPropertyMock(MongoPersistentEntity owner, String fieldname) {
    MongoPersistentProperty property = Mockito.mock(MongoPersistentProperty.class);
    when(property.getOwner()).thenReturn(owner);
    when(property.getName()).thenReturn(fieldname);
    return property;
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)

Example 39 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method shouldNotTriggerResolvingOfLazyLoadedProxyWhenFinalizeMethodIsInvoked.

// DATAMONGO-1076
@Test
public void shouldNotTriggerResolvingOfLazyLoadedProxyWhenFinalizeMethodIsInvoked() throws Exception {
    MongoPersistentEntity<?> entity = mappingContext.getRequiredPersistentEntity(WithObjectMethodOverrideLazyDbRefs.class);
    MongoPersistentProperty property = entity.getRequiredPersistentProperty("dbRefToPlainObject");
    String idValue = new ObjectId().toString();
    DBRef dbRef = converter.toDBRef(new LazyDbRefTargetPropertyAccess(idValue), property);
    WithObjectMethodOverrideLazyDbRefs result = converter.read(WithObjectMethodOverrideLazyDbRefs.class, new Document("dbRefToPlainObject", dbRef));
    ReflectionTestUtils.invokeMethod(result.dbRefToPlainObject, "finalize");
    assertProxyIsResolved(result.dbRefToPlainObject, false);
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) ObjectId(org.bson.types.ObjectId) DBRef(com.mongodb.DBRef) Document(org.bson.Document) Test(org.junit.Test)

Example 40 with MongoPersistentProperty

use of org.springframework.data.mongodb.core.mapping.MongoPersistentProperty in project spring-data-mongodb by spring-projects.

the class MappingMongoConverterUnitTests method eagerlyReturnsDBRefObjectIfTargetAlreadyIsOne.

// DATAMGONGO-508
@Test
public void eagerlyReturnsDBRefObjectIfTargetAlreadyIsOne() {
    DBRef dbRef = new DBRef("collection", "id");
    MongoPersistentProperty property = mock(MongoPersistentProperty.class);
    assertThat(converter.createDBRef(dbRef, property), is(dbRef));
}
Also used : MongoPersistentProperty(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty) DBRef(com.mongodb.DBRef) Test(org.junit.Test)

Aggregations

MongoPersistentProperty (org.springframework.data.mongodb.core.mapping.MongoPersistentProperty)40 Document (org.bson.Document)13 DBRef (com.mongodb.DBRef)10 Test (org.junit.Test)9 MappingException (org.springframework.data.mapping.MappingException)6 PersistentPropertyAccessor (org.springframework.data.mapping.PersistentPropertyAccessor)6 ConvertingPropertyAccessor (org.springframework.data.mapping.model.ConvertingPropertyAccessor)6 MongoPersistentEntity (org.springframework.data.mongodb.core.mapping.MongoPersistentEntity)6 BasicDBObject (com.mongodb.BasicDBObject)5 DBObject (com.mongodb.DBObject)4 ReturnDocument (com.mongodb.client.model.ReturnDocument)4 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 Arrays (java.util.Arrays)3 Iterator (java.util.Iterator)3 ObjectId (org.bson.types.ObjectId)3 InvalidDataAccessApiUsageException (org.springframework.dao.InvalidDataAccessApiUsageException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 List (java.util.List)2