Search in sources :

Example 11 with MongoDatabaseFactory

use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.

the class MongoDbFactoryParserIntegrationTests method readsReplicasWriteConcernCorrectly.

// DATAMONGO-331
@Test
public void readsReplicasWriteConcernCorrectly() {
    AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("namespace/db-factory-bean-custom-write-concern.xml");
    MongoDatabaseFactory factory = ctx.getBean("second", MongoDatabaseFactory.class);
    MongoDatabase db = factory.getMongoDatabase();
    assertThat(db.getWriteConcern()).isEqualTo(WriteConcern.W2);
    ctx.close();
}
Also used : AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 12 with MongoDatabaseFactory

use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-boot by spring-projects.

the class MongoDataAutoConfigurationTests method createsMongoDatabaseFactoryForPreferredMongoClient.

@Test
void createsMongoDatabaseFactoryForPreferredMongoClient() {
    this.contextRunner.run((context) -> {
        MongoDatabaseFactory dbFactory = context.getBean(MongoDatabaseFactory.class);
        assertThat(dbFactory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
    });
}
Also used : MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) Test(org.junit.jupiter.api.Test)

Example 13 with MongoDatabaseFactory

use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateTests method setUp.

@BeforeEach
public void setUp() {
    MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(client, "session-bound-mongo-template-tests") {

        @Override
        public MongoDatabase getMongoDatabase() throws DataAccessException {
            MongoDatabase spiedDatabse = Mockito.spy(super.getMongoDatabase());
            spiedDatabases.add(spiedDatabse);
            return spiedDatabse;
        }
    };
    session = client.startSession(ClientSessionOptions.builder().build());
    this.template = new MongoTemplate(factory);
    this.sessionBoundTemplate = new SessionBoundMongoTemplate(session, new MongoTemplate(factory, getDefaultMongoConverter(factory))) {

        @Override
        protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
            injectCollectionSpy(collection);
            return super.prepareCollection(collection);
        }

        @SuppressWarnings({ "ConstantConditions", "unchecked" })
        private void injectCollectionSpy(MongoCollection<Document> collection) {
            InvocationHandler handler = Proxy.getInvocationHandler(collection);
            Advised advised = (Advised) ReflectionTestUtils.getField(handler, "advised");
            for (Advisor advisor : advised.getAdvisors()) {
                Advice advice = advisor.getAdvice();
                if (advice instanceof SessionAwareMethodInterceptor) {
                    MongoCollection<Document> spiedCollection = Mockito.spy((MongoCollection<Document>) ReflectionTestUtils.getField(advice, "target"));
                    spiedCollections.add(spiedCollection);
                    ReflectionTestUtils.setField(advice, "target", spiedCollection);
                }
            }
        }
    };
}
Also used : SessionAwareMethodInterceptor(org.springframework.data.mongodb.SessionAwareMethodInterceptor) Advisor(org.springframework.aop.Advisor) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Document(org.bson.Document) InvocationHandler(java.lang.reflect.InvocationHandler) MongoCollection(com.mongodb.client.MongoCollection) Advised(org.springframework.aop.framework.Advised) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) Advice(org.aopalliance.aop.Advice) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) MongoDatabase(com.mongodb.client.MongoDatabase) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 14 with MongoDatabaseFactory

use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.

the class SimpleMongoClientDatabaseFactoryUnitTests method cascadedWithSessionUsesRootFactory.

// DATAMONGO-1880
@Test
void cascadedWithSessionUsesRootFactory() {
    when(mongo.getDatabase("foo")).thenReturn(database);
    MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(mongo, "foo");
    MongoDatabaseFactory wrapped = factory.withSession(clientSession).withSession(clientSession);
    InvocationHandler invocationHandler = Proxy.getInvocationHandler(wrapped.getMongoDatabase());
    Object singletonTarget = AopProxyUtils.getSingletonTarget(ReflectionTestUtils.getField(invocationHandler, "advised"));
    assertThat(singletonTarget).isSameAs(database);
}
Also used : MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.jupiter.api.Test)

Example 15 with MongoDatabaseFactory

use of org.springframework.data.mongodb.MongoDatabaseFactory in project spring-data-mongodb by spring-projects.

the class SimpleMongoClientDatabaseFactoryUnitTests method mongoUriConstructor.

// DATADOC-295
@Test
void mongoUriConstructor() {
    ConnectionString mongoURI = new ConnectionString("mongodb://myUsername:myPassword@localhost/myDatabase.myCollection");
    MongoDatabaseFactory mongoDbFactory = new SimpleMongoClientDatabaseFactory(mongoURI);
    assertThat(mongoDbFactory).hasFieldOrPropertyWithValue("databaseName", "myDatabase");
}
Also used : MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) ConnectionString(com.mongodb.ConnectionString) Test(org.junit.jupiter.api.Test)

Aggregations

MongoDatabaseFactory (org.springframework.data.mongodb.MongoDatabaseFactory)19 Test (org.junit.jupiter.api.Test)9 Test (org.junit.Test)7 MongoConverter (org.springframework.data.mongodb.core.convert.MongoConverter)5 MongoDatabase (com.mongodb.client.MongoDatabase)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 GridFsOperations (org.springframework.data.mongodb.gridfs.GridFsOperations)3 ServerAddress (com.mongodb.ServerAddress)2 WriteConcern (com.mongodb.WriteConcern)2 MongoClient (com.mongodb.client.MongoClient)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MongoOperations (org.springframework.data.mongodb.core.MongoOperations)2 ClientSessionOptions (com.mongodb.ClientSessionOptions)1 ConnectionString (com.mongodb.ConnectionString)1 CursorType (com.mongodb.CursorType)1 MongoException (com.mongodb.MongoException)1 ReadPreference (com.mongodb.ReadPreference)1 MongoCollection (com.mongodb.client.MongoCollection)1 com.mongodb.client.model (com.mongodb.client.model)1