Search in sources :

Example 1 with VersionedEntity

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());
}
Also used : Serializable(java.io.Serializable) VersionedEntity(io.crnk.jpa.model.VersionedEntity) OptimisticLockException(javax.persistence.OptimisticLockException) QueryParams(io.crnk.legacy.queryParams.QueryParams) Test(org.junit.Test) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest)

Example 2 with VersionedEntity

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());
}
Also used : Serializable(java.io.Serializable) VersionedEntity(io.crnk.jpa.model.VersionedEntity) OptimisticLockException(javax.persistence.OptimisticLockException) QuerySpec(io.crnk.core.queryspec.QuerySpec) Test(org.junit.Test) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest)

Aggregations

AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)2 VersionedEntity (io.crnk.jpa.model.VersionedEntity)2 Serializable (java.io.Serializable)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 Test (org.junit.Test)2 QuerySpec (io.crnk.core.queryspec.QuerySpec)1 QueryParams (io.crnk.legacy.queryParams.QueryParams)1