Search in sources :

Example 16 with MongoDatabaseFactory

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

the class MongoDbFactoryParserIntegrationTests method setsUpMongoDbFactoryUsingAMongoUriWithoutCredentials.

// DATAMONGO-306
@Test
public void setsUpMongoDbFactoryUsingAMongoUriWithoutCredentials() {
    reader.loadBeanDefinitions(new ClassPathResource("namespace/mongo-uri-no-credentials.xml"));
    BeanDefinition definition = factory.getBeanDefinition("mongoDbFactory");
    ConstructorArgumentValues constructorArguments = definition.getConstructorArgumentValues();
    assertThat(constructorArguments.getArgumentCount()).isOne();
    ValueHolder argument = constructorArguments.getArgumentValue(0, ConnectionString.class);
    assertThat(argument).isNotNull();
    MongoDatabaseFactory dbFactory = factory.getBean("mongoDbFactory", MongoDatabaseFactory.class);
    MongoDatabase db = dbFactory.getMongoDatabase();
    assertThat(db.getName()).isEqualTo("database");
}
Also used : MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ValueHolder(org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder) ClassPathResource(org.springframework.core.io.ClassPathResource) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) MongoDatabase(com.mongodb.client.MongoDatabase) Test(org.junit.jupiter.api.Test)

Example 17 with MongoDatabaseFactory

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

the class MongoNamespaceTests method testThirdMongoDbFactory.

// DATAMONGO-789
@Test
public void testThirdMongoDbFactory() {
    assertThat(ctx.containsBean("thirdMongoDbFactory")).isTrue();
    MongoDatabaseFactory dbf = (MongoDatabaseFactory) ctx.getBean("thirdMongoDbFactory");
    MongoClient mongo = (MongoClient) getField(dbf, "mongoClient");
    assertThat(mongo.getClusterDescription().getClusterSettings().getHosts()).containsExactly(new ServerAddress());
    assertThat(getField(dbf, "databaseName")).isEqualTo("database");
}
Also used : MongoClient(com.mongodb.client.MongoClient) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) ServerAddress(com.mongodb.ServerAddress) Test(org.junit.Test)

Example 18 with MongoDatabaseFactory

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

the class MongoNamespaceTests method testGridFsTemplateFactory.

// DATAMONGO-628
@Test
public void testGridFsTemplateFactory() {
    assertThat(ctx.containsBean("gridFsTemplate")).isTrue();
    GridFsOperations operations = (GridFsOperations) ctx.getBean("gridFsTemplate");
    MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
    assertThat(getField(dbf, "databaseName")).isEqualTo("database");
    MongoConverter converter = (MongoConverter) getField(operations, "converter");
    assertThat(converter).isNotNull();
}
Also used : MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) GridFsOperations(org.springframework.data.mongodb.gridfs.GridFsOperations) Test(org.junit.Test)

Example 19 with MongoDatabaseFactory

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

the class AbstractMongoQueryUnitTests method setUp.

@BeforeEach
void setUp() {
    doReturn("persons").when(persitentEntityMock).getCollection();
    doReturn(persitentEntityMock).when(mappingContextMock).getPersistentEntity(Mockito.any(Class.class));
    doReturn(persitentEntityMock).when(mappingContextMock).getRequiredPersistentEntity(Mockito.any(Class.class));
    doReturn(Person.class).when(persitentEntityMock).getType();
    MongoDatabaseFactory mongoDbFactory = mock(MongoDatabaseFactory.class);
    when(mongoDbFactory.getExceptionTranslator()).thenReturn(new MongoExceptionTranslator());
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
    MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mappingContextMock);
    converter.afterPropertiesSet();
    doReturn(converter).when(mongoOperationsMock).getConverter();
    doReturn(executableFind).when(mongoOperationsMock).query(any());
    doReturn(withQueryMock).when(executableFind).as(any());
    doReturn(withQueryMock).when(withQueryMock).matching(any(Query.class));
    doReturn(executableUpdate).when(mongoOperationsMock).update(any());
    doReturn(updateWithQuery).when(executableUpdate).matching(any(Query.class));
    doReturn(terminatingUpdate).when(updateWithQuery).apply(any(UpdateDefinition.class));
    when(mongoOperationsMock.remove(any(), any(), anyString())).thenReturn(deleteResultMock);
    when(mongoOperationsMock.updateMulti(any(), any(), any(), anyString())).thenReturn(updateResultMock);
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) FindWithQuery(org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery) UpdateWithQuery(org.springframework.data.mongodb.core.ExecutableUpdateOperation.UpdateWithQuery) Query(org.springframework.data.mongodb.core.query.Query) UpdateDefinition(org.springframework.data.mongodb.core.query.UpdateDefinition) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) DbRefResolver(org.springframework.data.mongodb.core.convert.DbRefResolver) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) MongoExceptionTranslator(org.springframework.data.mongodb.core.MongoExceptionTranslator) BeforeEach(org.junit.jupiter.api.BeforeEach)

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