Search in sources :

Example 6 with SimpleMongoClientDatabaseFactory

use of org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory in project cas by apereo.

the class MongoDbPropertySourceLocatorTests method verifyOperation.

@Test
public void verifyOperation() {
    val factory = new SimpleMongoClientDatabaseFactory(MongoDbCloudConfigBootstrapConfigurationTests.MONGODB_URI);
    val template = new MongoTemplate(factory);
    val loc = new MongoDbPropertySourceLocator(template);
    assertNull(loc.locate(mock(Environment.class)));
}
Also used : lombok.val(lombok.val) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Test(org.junit.jupiter.api.Test)

Example 7 with SimpleMongoClientDatabaseFactory

use of org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory in project spring-integration-samples by spring-projects.

the class DemoUtils method prepareMongoFactory.

public static MongoDatabaseFactory prepareMongoFactory(String... additionalCollectionToDrop) throws Exception {
    MongoDatabaseFactory mongoDbFactory = new SimpleMongoClientDatabaseFactory(MongoClients.create(), "test");
    MongoTemplate template = new MongoTemplate(mongoDbFactory);
    template.dropCollection("messages");
    template.dropCollection("data");
    for (String additionalCollection : additionalCollectionToDrop) {
        template.dropCollection(additionalCollection);
    }
    return mongoDbFactory;
}
Also used : MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate)

Example 8 with SimpleMongoClientDatabaseFactory

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

the class MongoDbFactoryParserIntegrationTests method testWriteConcern.

// DATAMONGO-2199
@Test
public void testWriteConcern() throws Exception {
    try (MongoClient client = MongoTestUtils.client()) {
        SimpleMongoClientDatabaseFactory dbFactory = new SimpleMongoClientDatabaseFactory(client, "database");
        dbFactory.setWriteConcern(WriteConcern.ACKNOWLEDGED);
        dbFactory.getMongoDatabase();
        assertThat(ReflectionTestUtils.getField(dbFactory, "writeConcern")).isEqualTo(WriteConcern.ACKNOWLEDGED);
    }
}
Also used : MongoClient(com.mongodb.client.MongoClient) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) Test(org.junit.jupiter.api.Test)

Example 9 with SimpleMongoClientDatabaseFactory

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

the class MongoDbFactoryParserIntegrationTests method assertWriteConcern.

private static void assertWriteConcern(ClassPathXmlApplicationContext ctx, WriteConcern expectedWriteConcern) {
    SimpleMongoClientDatabaseFactory dbFactory = ctx.getBean("first", SimpleMongoClientDatabaseFactory.class);
    MongoDatabase db = dbFactory.getMongoDatabase();
    assertThat(db.getName()).isEqualTo("db");
    WriteConcern configuredConcern = (WriteConcern) ReflectionTestUtils.getField(dbFactory, "writeConcern");
    assertThat(configuredConcern).isEqualTo(expectedWriteConcern);
    assertThat(db.getWriteConcern()).isEqualTo(expectedWriteConcern);
    assertThat(db.getWriteConcern()).isEqualTo(expectedWriteConcern);
}
Also used : WriteConcern(com.mongodb.WriteConcern) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 10 with SimpleMongoClientDatabaseFactory

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

the class MappingTests method testIndexesCreatedInRightCollection.

@Test
public void testIndexesCreatedInRightCollection() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    mappingContext.setAutoIndexCreation(true);
    MongoTemplate template = new MongoTemplate(new SimpleMongoClientDatabaseFactory(client, DB_NAME), new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext));
    CustomCollectionWithIndex ccwi = new CustomCollectionWithIndex("test");
    template.insert(ccwi);
    assertThat(template.execute("foobar", new CollectionCallback<Boolean>() {

        public Boolean doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
            List<Document> indexes = new ArrayList<Document>();
            collection.listIndexes(Document.class).into(indexes);
            for (Document document : indexes) {
                if (document.get("name") != null && document.get("name") instanceof String && ((String) document.get("name")).startsWith("name")) {
                    return true;
                }
            }
            return false;
        }
    })).isTrue();
    DetectedCollectionWithIndex dcwi = new DetectedCollectionWithIndex("test");
    template.insert(dcwi);
    assertThat(template.execute(MongoCollectionUtils.getPreferredCollectionName(DetectedCollectionWithIndex.class), new CollectionCallback<Boolean>() {

        public Boolean doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
            List<Document> indexes = new ArrayList<Document>();
            collection.listIndexes(Document.class).into(indexes);
            for (Document document : indexes) {
                if (document.get("name") != null && document.get("name") instanceof String && ((String) document.get("name")).startsWith("name")) {
                    return true;
                }
            }
            return false;
        }
    })).isTrue();
}
Also used : MongoException(com.mongodb.MongoException) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) Document(org.bson.Document) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) ArrayList(java.util.ArrayList) List(java.util.List) DataAccessException(org.springframework.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleMongoClientDatabaseFactory (org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory)16 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)13 Test (org.junit.jupiter.api.Test)4 BeforeAll (org.junit.jupiter.api.BeforeAll)3 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)3 Date (java.util.Date)2 lombok.val (lombok.val)2 MongoProperties (org.springframework.boot.autoconfigure.mongo.MongoProperties)2 MongoDatabaseFactory (org.springframework.data.mongodb.MongoDatabaseFactory)2 Flysql (com.isxcode.oxygen.flysql.core.Flysql)1 MongoException (com.mongodb.MongoException)1 WriteConcern (com.mongodb.WriteConcern)1 MongoClient (com.mongodb.client.MongoClient)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1