Search in sources :

Example 11 with IndexInfo

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

the class DefaultIndexOperationsIntegrationTests method shouldCreateIndexWithCollationCorrectly.

// DATAMONGO-1518
@Test
public void shouldCreateIndexWithCollationCorrectly() {
    assumeThat(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR), is(true));
    IndexDefinition id = new Index().named("with-collation").on("xyz", Direction.ASC).collation(Collation.of("de_AT").caseFirst(CaseFirst.off()));
    new DefaultIndexOperations(template.getMongoDbFactory(), this.template.getCollectionName(DefaultIndexOperationsIntegrationTestsSample.class), new QueryMapper(template.getConverter()), MappingToSameCollection.class);
    indexOps.ensureIndex(id);
    Document expected = // 
    new Document("locale", "de_AT").append("caseLevel", // 
    false).append("caseFirst", // 
    "off").append("strength", // 
    3).append("numericOrdering", // 
    false).append("alternate", // 
    "non-ignorable").append("maxVariable", // 
    "punct").append("normalization", // 
    false).append("backwards", false);
    IndexInfo info = findAndReturnIndexInfo(indexOps.getIndexInfo(), "with-collation");
    assertThat(info.getCollation()).isPresent();
    // version is set by MongoDB server - we remove it to avoid errors when upgrading server version.
    Document result = info.getCollation().get();
    result.remove("version");
    assertThat(result).isEqualTo(expected);
}
Also used : IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Document(org.bson.Document) Test(org.junit.Test)

Example 12 with IndexInfo

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

the class DefaultReactiveIndexOperationsTests method shouldApplyPartialDBOFilterCorrectly.

// DATAMONGO-1682
@Test
public void shouldApplyPartialDBOFilterCorrectly() {
    assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
    IndexDefinition id = new Index().named("partial-with-dbo").on("k3y", Direction.ASC).partial(of(new org.bson.Document("qty", new org.bson.Document("$gte", 10))));
    StepVerifier.create(indexOps.ensureIndex(id)).expectNextCount(1).verifyComplete();
    // 
    StepVerifier.create(indexOps.getIndexInfo().filter(this.indexByName("partial-with-dbo"))).consumeNextWith(indexInfo -> {
        assertThat(indexInfo.getPartialFilterExpression()).isEqualTo("{ \"qty\" : { \"$gte\" : 10 } }");
    }).verifyComplete();
}
Also used : Document(org.bson.Document) Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) AbstractReactiveMongoConfiguration(org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClients(com.mongodb.reactivestreams.client.MongoClients) Collation(org.springframework.data.mongodb.core.query.Collation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) MongoClient(com.mongodb.reactivestreams.client.MongoClient) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Assertions(org.assertj.core.api.Assertions) Version(org.springframework.data.util.Version) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) Before(org.junit.Before) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Configuration(org.springframework.context.annotation.Configuration) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) Document(org.bson.Document) Test(org.junit.Test)

Example 13 with IndexInfo

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

the class DefaultReactiveIndexOperationsTests method shouldCreateIndexWithCollationCorrectly.

// DATAMONGO-1518
@Test
public void shouldCreateIndexWithCollationCorrectly() {
    assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_FOUR));
    IndexDefinition id = new Index().named("with-collation").on("xyz", Direction.ASC).collation(Collation.of("de_AT").caseFirst(CaseFirst.off()));
    StepVerifier.create(indexOps.ensureIndex(id)).expectNextCount(1).verifyComplete();
    Document expected = // 
    new Document("locale", "de_AT").append("caseLevel", // 
    false).append("caseFirst", // 
    "off").append("strength", // 
    3).append("numericOrdering", // 
    false).append("alternate", // 
    "non-ignorable").append("maxVariable", // 
    "punct").append("normalization", // 
    false).append("backwards", false);
    // 
    StepVerifier.create(indexOps.getIndexInfo().filter(this.indexByName("with-collation"))).consumeNextWith(indexInfo -> {
        assertThat(indexInfo.getCollation()).isPresent();
        // version is set by MongoDB server - we remove it to avoid errors when upgrading server version.
        Document result = indexInfo.getCollation().get();
        result.remove("version");
        assertThat(result).isEqualTo(expected);
    }).verifyComplete();
}
Also used : Document(org.bson.Document) StepVerifier(reactor.test.StepVerifier) AbstractReactiveMongoConfiguration(org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) MongoClients(com.mongodb.reactivestreams.client.MongoClients) Collation(org.springframework.data.mongodb.core.query.Collation) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Index(org.springframework.data.mongodb.core.index.Index) MongoClient(com.mongodb.reactivestreams.client.MongoClient) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Assertions(org.assertj.core.api.Assertions) Version(org.springframework.data.util.Version) Assume(org.junit.Assume) Direction(org.springframework.data.domain.Sort.Direction) CaseFirst(org.springframework.data.mongodb.core.query.Collation.CaseFirst) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) Before(org.junit.Before) QueryMapper(org.springframework.data.mongodb.core.convert.QueryMapper) Predicate(java.util.function.Predicate) Test(org.junit.Test) Criteria(org.springframework.data.mongodb.core.query.Criteria) Field(org.springframework.data.mongodb.core.mapping.Field) Configuration(org.springframework.context.annotation.Configuration) PartialIndexFilter(org.springframework.data.mongodb.core.index.PartialIndexFilter) ContextConfiguration(org.springframework.test.context.ContextConfiguration) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) IndexDefinition(org.springframework.data.mongodb.core.index.IndexDefinition) Index(org.springframework.data.mongodb.core.index.Index) Document(org.bson.Document) Test(org.junit.Test)

Example 14 with IndexInfo

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

the class MongoTemplateTests method testEnsureIndex.

@Test
@SuppressWarnings("deprecation")
public void testEnsureIndex() throws Exception {
    Person p1 = new Person("Oliver");
    p1.setAge(25);
    template.insert(p1);
    Person p2 = new Person("Sven");
    p2.setAge(40);
    template.insert(p2);
    template.indexOps(Person.class).ensureIndex(new Index().on("age", Direction.DESC).unique());
    MongoCollection<org.bson.Document> coll = template.getCollection(template.getCollectionName(Person.class));
    List<org.bson.Document> indexInfo = new ArrayList<org.bson.Document>();
    coll.listIndexes().into(indexInfo);
    assertThat(indexInfo.size(), is(2));
    Object indexKey = null;
    boolean unique = false;
    for (org.bson.Document ix : indexInfo) {
        if ("age_-1".equals(ix.get("name"))) {
            indexKey = ix.get("key");
            unique = (Boolean) ix.get("unique");
            assertThat(ix.get("dropDups"), is(nullValue()));
        }
    }
    assertThat(((org.bson.Document) indexKey), IsMapContaining.<String, Object>hasEntry("age", -1));
    assertThat(unique, is(true));
    List<IndexInfo> indexInfoList = template.indexOps(Person.class).getIndexInfo();
    assertThat(indexInfoList.size(), is(2));
    IndexInfo ii = indexInfoList.get(1);
    assertThat(ii.isUnique(), is(true));
    assertThat(ii.isSparse(), is(false));
    List<IndexField> indexFields = ii.getIndexFields();
    IndexField field = indexFields.get(0);
    assertThat(field, is(IndexField.create("age", Direction.DESC)));
}
Also used : Index(org.springframework.data.mongodb.core.index.Index) IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexField(org.springframework.data.mongodb.core.index.IndexField) Test(org.junit.Test)

Example 15 with IndexInfo

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

the class MongoTemplateTests method testReadIndexInfoForIndicesCreatedViaMongoShellCommands.

// DATAMONGO-746
@Test
public void testReadIndexInfoForIndicesCreatedViaMongoShellCommands() throws Exception {
    String command = "db." + template.getCollectionName(Person.class) + ".createIndex({'age':-1}, {'unique':true, 'sparse':true}), 1";
    template.indexOps(Person.class).dropAllIndexes();
    assertThat(template.indexOps(Person.class).getIndexInfo().isEmpty(), is(true));
    factory.getDb().runCommand(new org.bson.Document("eval", command));
    ListIndexesIterable<org.bson.Document> indexInfo = template.getCollection(template.getCollectionName(Person.class)).listIndexes();
    org.bson.Document indexKey = null;
    boolean unique = false;
    MongoCursor<org.bson.Document> cursor = indexInfo.iterator();
    while (cursor.hasNext()) {
        org.bson.Document ix = cursor.next();
        if ("age_-1".equals(ix.get("name"))) {
            indexKey = (org.bson.Document) ix.get("key");
            unique = (Boolean) ix.get("unique");
        }
    }
    assertThat(indexKey, IsMapContaining.<String, Object>hasEntry("age", -1D));
    assertThat(unique, is(true));
    IndexInfo info = template.indexOps(Person.class).getIndexInfo().get(1);
    assertThat(info.isUnique(), is(true));
    assertThat(info.isSparse(), is(true));
    List<IndexField> indexFields = info.getIndexFields();
    IndexField field = indexFields.get(0);
    assertThat(field, is(IndexField.create("age", Direction.DESC)));
}
Also used : IndexInfo(org.springframework.data.mongodb.core.index.IndexInfo) IndexField(org.springframework.data.mongodb.core.index.IndexField) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 IndexInfo (org.springframework.data.mongodb.core.index.IndexInfo)16 Index (org.springframework.data.mongodb.core.index.Index)11 IndexDefinition (org.springframework.data.mongodb.core.index.IndexDefinition)10 Document (org.bson.Document)7 QueryMapper (org.springframework.data.mongodb.core.convert.QueryMapper)7 MongoClient (com.mongodb.reactivestreams.client.MongoClient)5 MongoClients (com.mongodb.reactivestreams.client.MongoClients)5 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)5 Predicate (java.util.function.Predicate)5 Assertions (org.assertj.core.api.Assertions)5 Assume (org.junit.Assume)5 Before (org.junit.Before)5 RunWith (org.junit.runner.RunWith)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 Configuration (org.springframework.context.annotation.Configuration)5 Direction (org.springframework.data.domain.Sort.Direction)5 AbstractReactiveMongoConfiguration (org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration)5 PartialIndexFilter (org.springframework.data.mongodb.core.index.PartialIndexFilter)5 Field (org.springframework.data.mongodb.core.mapping.Field)5