Search in sources :

Example 36 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

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)

Example 37 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestVersioning method testBulkUpdate.

@Test
public void testBulkUpdate() {
    final Datastore datastore = getDs();
    Versioned entity = new Versioned();
    entity.setName("Value 1");
    datastore.save(entity);
    entity = datastore.find(Versioned.class).filter(eq("_id", entity.getId())).first();
    assertEquals(entity.getName(), "Value 1");
    assertEquals(entity.getVersion().longValue(), 1);
    entity.setName("Value 2");
    datastore.save(entity);
    entity = datastore.find(Versioned.class).filter(eq("_id", entity.getId())).first();
    assertEquals(entity.getName(), "Value 2");
    assertEquals(entity.getVersion().longValue(), 2);
    Query<Versioned> query = datastore.find(Versioned.class);
    query.filter(eq("id", entity.getId()));
    query.update(set("name", "Value 3")).execute();
    entity = datastore.find(Versioned.class).filter(eq("_id", entity.getId())).first();
    assertEquals(entity.getName(), "Value 3");
    assertEquals(entity.getVersion().longValue(), 3);
}
Also used : Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Versioned(dev.morphia.test.models.versioned.Versioned) Test(org.testng.annotations.Test)

Example 38 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestVersioning method testFindAndModify.

@Test
public void testFindAndModify() {
    final Datastore datastore = getDs();
    Versioned entity = new Versioned();
    entity.setName("Value 1");
    Query<Versioned> query = datastore.find(Versioned.class);
    query.filter(eq("name", "Value 1"));
    entity = query.modify(set("name", "Value 3")).execute(new ModifyOptions().returnDocument(ReturnDocument.AFTER).upsert(true));
    assertEquals(entity.getName(), "Value 3");
    assertEquals(entity.getVersion().longValue(), 1);
    assertNotNull(entity.getId());
}
Also used : ModifyOptions(dev.morphia.ModifyOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Versioned(dev.morphia.test.models.versioned.Versioned) Test(org.testng.annotations.Test)

Example 39 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestDatastore method testLifecycle.

@Test
public void testLifecycle() {
    final LifecycleTestObj life1 = new LifecycleTestObj();
    getMapper().map(List.of(LifecycleTestObj.class));
    getDs().save(life1);
    assertTrue(LifecycleListener.foundDatastore);
    assertTrue(life1.prePersist);
    assertTrue(life1.prePersistWithParam);
    assertTrue(life1.prePersistWithParamAndReturn);
    assertTrue(life1.postPersist);
    assertTrue(life1.postPersistWithParam);
    final Datastore datastore = getDs();
    final LifecycleTestObj loaded = datastore.find(LifecycleTestObj.class).filter(eq("_id", life1.id)).first();
    assertTrue(loaded.preLoad);
    assertTrue(loaded.preLoadWithParam);
    assertTrue(loaded.postLoad);
    assertTrue(loaded.postLoadWithParam);
}
Also used : Datastore(dev.morphia.Datastore) Test(org.testng.annotations.Test)

Example 40 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestVersioning method testVersionedUpsert.

@Test
public void testVersionedUpsert() {
    final Datastore datastore = getDs();
    datastore.find(Versioned.class).delete(new DeleteOptions().multi(true));
    Versioned entity = new Versioned();
    entity.setName("Value 1");
    Query<Versioned> query = datastore.find(Versioned.class);
    query.filter(eq("name", "Value 1"));
    query.update(set("name", "Value 3")).execute(new UpdateOptions().upsert(true));
    entity = datastore.find(Versioned.class).iterator(new FindOptions().limit(1)).tryNext();
    assertEquals(entity.getName(), "Value 3");
    assertEquals(entity.getVersion().longValue(), 1);
}
Also used : FindOptions(dev.morphia.query.FindOptions) DeleteOptions(dev.morphia.DeleteOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) Versioned(dev.morphia.test.models.versioned.Versioned) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Aggregations

Datastore (dev.morphia.Datastore)46 Test (org.testng.annotations.Test)38 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)12 FindOptions (dev.morphia.query.FindOptions)12 ArrayList (java.util.ArrayList)9 Document (org.bson.Document)9 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)6 UpdateOptions (dev.morphia.UpdateOptions)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 MapperOptions (dev.morphia.mapping.MapperOptions)4 ObjectId (org.bson.types.ObjectId)4 Mapper (dev.morphia.mapping.Mapper)3 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)3 Circle (dev.morphia.test.models.Circle)3 Rectangle (dev.morphia.test.models.Rectangle)3 Versioned (dev.morphia.test.models.versioned.Versioned)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 DeleteOptions (dev.morphia.DeleteOptions)2 Key (dev.morphia.Key)2