Search in sources :

Example 1 with Versioned

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);
    });
}
Also used : DeleteOptions(dev.morphia.DeleteOptions) Versioned(dev.morphia.test.models.versioned.Versioned) Test(org.testng.annotations.Test)

Example 2 with Versioned

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);
}
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 3 with Versioned

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);
}
Also used : Versioned(dev.morphia.test.models.versioned.Versioned) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 4 with Versioned

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());
}
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 5 with Versioned

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);
}
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

Versioned (dev.morphia.test.models.versioned.Versioned)8 Test (org.testng.annotations.Test)8 Datastore (dev.morphia.Datastore)3 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)3 DeleteOptions (dev.morphia.DeleteOptions)2 UpdateOptions (dev.morphia.UpdateOptions)2 ModifyOptions (dev.morphia.ModifyOptions)1 FindOptions (dev.morphia.query.FindOptions)1