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");
}
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);
}
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());
}
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);
}
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);
}
Aggregations