use of dev.morphia.test.models.versioned.Versioned in project morphia by mongodb.
the class TestVersioning method testThrowsExceptionWhenTryingToSaveAnOldVersion.
@Test
public void testThrowsExceptionWhenTryingToSaveAnOldVersion() {
assertThrows(VersionMismatchException.class, () -> {
getDs().find(Versioned.class).delete(new DeleteOptions().multi(true));
// given
final Versioned version1 = new Versioned();
getDs().save(version1);
final Versioned version2 = getDs().find(Versioned.class).filter(eq("_id", version1.getId())).first();
getDs().save(version2);
// when
getDs().save(version1);
});
}
use of dev.morphia.test.models.versioned.Versioned 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.test.models.versioned.Versioned in project morphia by mongodb.
the class TestVersioning method testIncVersionNotOverridingOtherInc.
@Test
public void testIncVersionNotOverridingOtherInc() {
final Versioned version1 = new Versioned();
version1.setCount(0);
getDs().save(version1);
assertEquals(version1.getVersion(), Long.valueOf(1));
assertEquals(version1.getCount(), 0);
Query<Versioned> query = getDs().find(Versioned.class);
query.filter(eq("_id", version1.getId()));
query.update(inc("count")).execute(new UpdateOptions().upsert(true));
final Versioned version2 = getDs().find(Versioned.class).filter(eq("_id", version1.getId())).first();
assertEquals(version2.getVersion(), Long.valueOf(2));
assertEquals(version2.getCount(), 1);
}
use of dev.morphia.test.models.versioned.Versioned 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.test.models.versioned.Versioned 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