Search in sources :

Example 1 with IndexOptions

use of dev.morphia.annotations.IndexOptions in project morphia by mongodb.

the class IndexHelperTest method indexOptionsConversion.

@Test
public void indexOptionsConversion() {
    IndexOptions indexOptions = buildOptions(true);
    com.mongodb.client.model.IndexOptions options = getIndexHelper().convert(indexOptions);
    assertEquals(options.getName(), "index_name");
    assertTrue(options.isBackground());
    assertTrue(options.isUnique());
    assertTrue(options.isSparse());
    assertEquals(options.getExpireAfter(TimeUnit.SECONDS), Long.valueOf(42));
    assertEquals(options.getDefaultLanguage(), "en");
    assertEquals(options.getLanguageOverride(), "de");
    assertEquals(getIndexHelper().convert(indexOptions.collation()), options.getCollation());
    assertTrue(getIndexHelper().convert(indexOptions).isBackground());
    assertTrue(getIndexHelper().convert(indexOptions).isBackground());
    assertFalse(getIndexHelper().convert(buildOptions(false)).isBackground());
}
Also used : IndexOptions(dev.morphia.annotations.IndexOptions) Test(org.testng.annotations.Test)

Example 2 with IndexOptions

use of dev.morphia.annotations.IndexOptions in project morphia by mongodb.

the class IndexHelperTest method index.

@Test
public void index() {
    MongoCollection<Document> collection = getDatabase().getCollection("indexes");
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    IndexOptions options = indexOptionsBuilder().name("index_name").background(true).collation(collation().build()).disableValidation(true).language("en").languageOverride("de").sparse(true).unique(true).build();
    Index index = indexBuilder().fields(fieldBuilder().value("indexName").build(), fieldBuilder().value("text").type(IndexType.DESC).build()).options(options).build();
    getIndexHelper().createIndex(collection, model, index);
    List<Document> indexInfo = getIndexInfo(IndexedClass.class);
    for (Document document : indexInfo) {
        if (document.get("name").equals("indexName")) {
            checkIndex(document);
            assertEquals(document.get("default_language"), "en");
            assertEquals(document.get("language_override"), "de");
            assertEquals(document.get("collation"), new Document().append("locale", "en").append("caseLevel", true).append("caseFirst", "upper").append("strength", 5).append("numericOrdering", true).append("alternate", "shifted").append("maxVariable", "space").append("backwards", true).append("normalization", true).append("version", "57.1"));
        }
    }
}
Also used : IndexOptions(dev.morphia.annotations.IndexOptions) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Index(dev.morphia.annotations.Index) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 3 with IndexOptions

use of dev.morphia.annotations.IndexOptions in project morphia by mongodb.

the class IndexHelperTest method findField.

@Test
public void findField() {
    getMapper().map(MappedInterface.class, MappedInterfaceImpl.class, AbstractParent.class, IndexedClass.class);
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    IndexOptions options = indexOptionsBuilder().build();
    assertEquals(getIndexHelper().findField(model, options, "indexName"), "indexName");
    assertEquals(getIndexHelper().findField(model, options, "nested.name"), "nest.name");
    assertEquals(getIndexHelper().findField(model, options, "nest.name"), "nest.name");
    try {
        assertEquals(getIndexHelper().findField(model, options, "nest.whatsit"), "nest.whatsit");
        fail("Should have failed on the bad index path");
    } catch (ValidationException e) {
    // alles ist gut
    }
    assertEquals(getIndexHelper().findField(model, indexOptionsBuilder().disableValidation(true).build(), "nest.whatsit.nested.more.deeply.than.the.object.model"), "nest.whatsit.nested.more.deeply.than.the.object.model");
}
Also used : ValidationException(dev.morphia.query.ValidationException) IndexOptions(dev.morphia.annotations.IndexOptions) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Aggregations

IndexOptions (dev.morphia.annotations.IndexOptions)3 Test (org.testng.annotations.Test)3 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)2 Index (dev.morphia.annotations.Index)1 ValidationException (dev.morphia.query.ValidationException)1 Document (org.bson.Document)1