use of io.crnk.jpa.model.VersionedEntity in project crnk-framework by crnk-project.
the class JpaQueryParamsEndToEndTest method testOptimisticLocking.
@Test
public void testOptimisticLocking() {
ResourceRepositoryStub<VersionedEntity, Serializable> repo = client.getQueryParamsRepository(VersionedEntity.class);
VersionedEntity entity = new VersionedEntity();
entity.setId(1L);
entity.setLongValue(13L);
VersionedEntity saved = repo.create(entity);
Assert.assertEquals(0, saved.getVersion());
saved.setLongValue(14L);
saved = repo.save(saved);
Assert.assertEquals(1, saved.getVersion());
saved.setLongValue(15L);
saved = repo.save(saved);
Assert.assertEquals(2, saved.getVersion());
saved.setLongValue(16L);
saved.setVersion(saved.getVersion() - 1);
try {
saved = repo.save(saved);
Assert.fail();
} catch (OptimisticLockException e) {
// ok
}
VersionedEntity persisted = repo.findOne(1L, new QueryParams());
Assert.assertEquals(2, persisted.getVersion());
Assert.assertEquals(15L, persisted.getLongValue());
}
use of io.crnk.jpa.model.VersionedEntity in project crnk-framework by crnk-project.
the class OptimisticLockingEndToEndTest method testOptimisticLocking.
@Test
public void testOptimisticLocking() {
ResourceRepositoryV2<VersionedEntity, Serializable> repo = client.getRepositoryForType(VersionedEntity.class);
VersionedEntity entity = new VersionedEntity();
entity.setId(1L);
entity.setLongValue(13L);
VersionedEntity saved = repo.create(entity);
Assert.assertEquals(0, saved.getVersion());
saved.setLongValue(14L);
saved = repo.save(saved);
Assert.assertEquals(1, saved.getVersion());
saved.setLongValue(15L);
saved = repo.save(saved);
Assert.assertEquals(2, saved.getVersion());
saved.setLongValue(16L);
saved.setVersion(saved.getVersion() - 1);
try {
saved = repo.save(saved);
Assert.fail();
} catch (OptimisticLockException e) {
// ok
}
VersionedEntity persisted = repo.findOne(1L, new QuerySpec(VersionedEntity.class));
Assert.assertEquals(2, persisted.getVersion());
Assert.assertEquals(15L, persisted.getLongValue());
}
Aggregations