Search in sources :

Example 41 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class IndexHelperTest method wildcardTextIndex.

@Test
public void wildcardTextIndex() {
    MongoCollection<Document> indexes = getDatabase().getCollection("indexes");
    EntityModel model = getMapper().getEntityModel(IndexedClass.class);
    Index index = indexBuilder().fields(fieldBuilder().value("$**").type(IndexType.TEXT).build()).build();
    getIndexHelper().createIndex(indexes, model, index);
    List<Document> wildcard = getIndexInfo(IndexedClass.class);
    boolean found = false;
    for (Document document : wildcard) {
        found |= document.get("name").equals("$**_text");
    }
    assertTrue(found, "Should have found the wildcard index");
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Index(dev.morphia.annotations.Index) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 42 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class TestDocumentValidation method validationDocuments.

@Test
public void validationDocuments() {
    Document validator = parse("{ \"jelly\" : { \"$ne\" : \"rhubarb\" } }");
    getMapper().map(DocumentValidation.class);
    EntityModel model = getMapper().getEntityModel(DocumentValidation.class);
    for (ValidationLevel level : EnumSet.allOf(ValidationLevel.class)) {
        for (ValidationAction action : EnumSet.allOf(ValidationAction.class)) {
            checkValidation(validator, model, level, action);
        }
    }
}
Also used : ValidationAction(com.mongodb.client.model.ValidationAction) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) ValidationLevel(com.mongodb.client.model.ValidationLevel) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 43 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class TestGenerics method testMethodMappedGenericEntities.

@Test
public void testMethodMappedGenericEntities() {
    Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
    EntityModel entityModel = datastore.getMapper().map(MethodMappedSpecializedEntity.class).get(0);
    PropertyModel test = entityModel.getProperty("test");
    assertEquals(test.getType(), UUID.class);
    MethodMappedSpecializedEntity beforeDB = new MethodMappedSpecializedEntity();
    beforeDB.setId(UUID.randomUUID());
    beforeDB.setTest(UUID.randomUUID());
    datastore.save(beforeDB);
    MethodMappedSpecializedEntity loaded = datastore.find(MethodMappedSpecializedEntity.class).filter(eq("_id", beforeDB.getId())).first();
    assertEquals(loaded.getId(), beforeDB.getId());
    assertEquals(loaded.getTest(), beforeDB.getTest());
}
Also used : Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) MethodMappedSpecializedEntity(dev.morphia.test.models.methods.MethodMappedSpecializedEntity) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Test(org.testng.annotations.Test)

Example 44 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class TestGenerics method testGenericEntities.

@Test
public void testGenericEntities() {
    EntityModel entityModel = getMapper().map(SpecializedEntity.class).get(0);
    PropertyModel test = entityModel.getProperty("test");
    assertEquals(test.getType(), UUID.class);
    SpecializedEntity beforeDB = new SpecializedEntity();
    beforeDB.setId(UUID.randomUUID());
    beforeDB.setTest(UUID.randomUUID());
    getDs().save(beforeDB);
    SpecializedEntity loaded = getDs().find(SpecializedEntity.class).filter(eq("_id", beforeDB.getId())).first();
    assertEquals(loaded.getId(), beforeDB.getId());
    assertEquals(loaded.getTest(), beforeDB.getTest());
}
Also used : EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MethodMappedSpecializedEntity(dev.morphia.test.models.methods.MethodMappedSpecializedEntity) SpecializedEntity(dev.morphia.test.models.SpecializedEntity) Test(org.testng.annotations.Test)

Example 45 with EntityModel

use of dev.morphia.mapping.codec.pojo.EntityModel in project morphia by MorphiaOrg.

the class TestMapping method collectionNaming.

@Test
public void collectionNaming() {
    MapperOptions options = MapperOptions.builder().collectionNaming(NamingStrategy.lowerCase()).build();
    Datastore datastore = createDatastore(TestBase.TEST_DB_NAME, options);
    List<EntityModel> map = datastore.getMapper().map(ContainsMapWithEmbeddedInterface.class, ContainsIntegerList.class);
    assertEquals(map.get(0).getCollectionName(), "containsmapwithembeddedinterface");
    assertEquals(map.get(1).getCollectionName(), "cil");
    options = MapperOptions.builder().collectionNaming(NamingStrategy.kebabCase()).build();
    datastore = createDatastore(TestBase.TEST_DB_NAME, options);
    map = datastore.getMapper().map(ContainsMapWithEmbeddedInterface.class, ContainsIntegerList.class);
    assertEquals(map.get(0).getCollectionName(), "contains-map-with-embedded-interface");
    assertEquals(map.get(1).getCollectionName(), "cil");
}
Also used : MapperOptions(dev.morphia.mapping.MapperOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) Test(org.testng.annotations.Test)

Aggregations

EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)94 Test (org.testng.annotations.Test)56 Document (org.bson.Document)36 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)22 ArrayList (java.util.ArrayList)16 Datastore (dev.morphia.Datastore)14 Entity (dev.morphia.annotations.Entity)14 PathTarget (dev.morphia.internal.PathTarget)12 Mapper (dev.morphia.mapping.Mapper)12 Nullable (com.mongodb.lang.Nullable)10 Index (dev.morphia.annotations.Index)10 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)8 VersionedChildEntity (dev.morphia.test.models.versioned.VersionedChildEntity)8 List (java.util.List)8 ExternalEntity (dev.morphia.annotations.experimental.ExternalEntity)6 WriteConcern (com.mongodb.WriteConcern)4 Key (dev.morphia.Key)4 IndexOptions (dev.morphia.annotations.IndexOptions)4 MappingException (dev.morphia.mapping.MappingException)4 EntityModelBuilder (dev.morphia.mapping.codec.pojo.EntityModelBuilder)4