Search in sources :

Example 51 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class ObjectVisibilityTest method givenFromEntityWhenAccessingModuleLayerVisibleExpectSuccess.

@Test
public void givenFromEntityWhenAccessingModuleLayerVisibleExpectSuccess() {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    try {
        FromEntity entity = unitOfWork.newEntity(FromEntity.class, "123");
        entity.moduleLayerVisible();
    } finally {
        if (unitOfWork.isOpen()) {
            unitOfWork.discard();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Test(org.junit.Test)

Example 52 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class AbstractEntityStoreTest method whenNewEntityThenCanFindEntityAndCorrectValues.

@Test
public void whenNewEntityThenCanFindEntityAndCorrectValues() throws Exception {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    try {
        TestEntity instance = createEntity(unitOfWork);
        unitOfWork.complete();
        // Find entity
        unitOfWork = module.newUnitOfWork();
        instance = unitOfWork.get(instance);
        // Check state
        assertThat("property 'intValue' has correct value", instance.intValue().get(), equalTo(42));
        assertThat("property 'longValue' has correct value", instance.longValue().get(), equalTo(42L));
        assertThat("property 'doubleValue' has correct value", instance.doubleValue().get(), equalTo(42D));
        assertThat("property 'floatValue' has correct value", instance.floatValue().get(), equalTo(42F));
        assertThat("property 'booleanValue' has correct value", instance.booleanValue().get(), equalTo(Boolean.TRUE));
        assertThat("property 'bigInteger' has correct value", instance.bigIntegerValue().get(), equalTo(new BigInteger("42")));
        assertThat("property 'bigDecimal' has correct value", instance.bigDecimalValue().get(), equalTo(new BigDecimal("42")));
        assertThat("property 'dateValue' has correct value", instance.dateValue().get(), equalTo(new DateTime("2020-03-04T13:24:35", UTC).toDate()));
        assertThat("property 'dateTimeValue' has correct value", instance.dateTimeValue().get(), equalTo(new DateTime("2020-03-04T13:24:35", UTC)));
        assertThat("property 'localDateTimeValue' has correct value", instance.localDateTimeValue().get(), equalTo(new LocalDateTime("2020-03-04T13:23:00", UTC)));
        assertThat("property 'localDateValue' has correct value", instance.localDateValue().get(), equalTo(new LocalDate("2020-03-04")));
        assertThat("property 'name' has correct value", instance.name().get(), equalTo("Test"));
        assertThat("property 'unsetName' has correct value", instance.unsetName().get(), equalTo(null));
        assertThat("property 'emptyName' has correct value", instance.emptyName().get(), equalTo(""));
        assertThat("property 'valueProperty.stringValue' has correct value", instance.valueProperty().get().valueProperty().get().stringValue().get(), equalTo("Bar"));
        assertThat("property 'valueProperty.listProperty' has correct value", instance.valueProperty().get().listProperty().get().get(0), equalTo("Foo"));
        assertThat("property 'valueProperty.enumProperty' has correct value", instance.valueProperty().get().enumProperty().get(), equalTo(TestEnum.VALUE3));
        assertThat("property 'valueProperty.anotherValue.bling' has correct value", instance.valueProperty().get().valueProperty().get().anotherValue().get().bling().get(), equalTo("BlinkLjus"));
        assertThat("property 'valueProperty.tjabbaProperty.bling' has correct value", instance.valueProperty().get().tjabbaProperty().get().bling().get(), equalTo("Brakfis"));
        Map<String, String> mapValue = new HashMap<>();
        mapValue.put("foo", "bar");
        assertThat("property 'valueProperty.mapStringStringProperty' has correct value", instance.valueProperty().get().mapStringStringProperty().get(), equalTo(mapValue));
        assertThat("association has correct value", instance.association().get(), equalTo(instance));
        assertThat("manyAssociation has correct value", instance.manyAssociation().iterator().next(), equalTo(instance));
        assertThat("namedAssociation has correct 'foo' value", instance.namedAssociation().get("foo"), equalTo(instance));
        assertThat("namedAssociation has correct 'bar' value", instance.namedAssociation().get("bar"), equalTo(instance));
        unitOfWork.discard();
    } finally {
        unitOfWork.discard();
    }
}
Also used : LocalDateTime(org.joda.time.LocalDateTime) UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) HashMap(java.util.HashMap) BigInteger(java.math.BigInteger) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) DateTime(org.joda.time.DateTime) LocalDateTime(org.joda.time.LocalDateTime) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 53 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class AbstractEntityStoreTest method givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification.

@Test
public void givenConcurrentUnitOfWorksWhenUoWCompletesThenCheckConcurrentModification() throws UnitOfWorkCompletionException {
    TestEntity testEntity;
    {
        UnitOfWork unitOfWork = module.newUnitOfWork();
        EntityBuilder<TestEntity> builder = unitOfWork.newEntityBuilder(TestEntity.class);
        testEntity = builder.newInstance();
        unitOfWork.complete();
    }
    UnitOfWork unitOfWork1;
    TestEntity testEntity1;
    String version;
    {
        // Start working with Entity in one UoW
        unitOfWork1 = module.newUnitOfWork();
        testEntity1 = unitOfWork1.get(testEntity);
        version = spi.entityStateOf(testEntity1).version();
        if (version.isEmpty()) {
            unitOfWork1.discard();
            // Store doesn't track versions - no point in testing it
            return;
        }
        testEntity1.name().set("A");
        testEntity1.unsetName().set("A");
    }
    {
        // Start working with same Entity in another UoW, and complete it
        UnitOfWork unitOfWork = module.newUnitOfWork();
        TestEntity testEntity2 = unitOfWork.get(testEntity);
        assertThat("version is correct", spi.entityStateOf(testEntity1).version(), equalTo(version));
        testEntity2.name().set("B");
        unitOfWork.complete();
    }
    {
        // Try to complete first UnitOfWork
        try {
            unitOfWork1.complete();
            fail("Should have thrown concurrent modification exception");
        } catch (ConcurrentEntityModificationException e) {
            unitOfWork1.discard();
        }
    }
    {
        // Check values
        unitOfWork1 = module.newUnitOfWork();
        testEntity1 = unitOfWork1.get(testEntity);
        assertThat("property name has not been set", testEntity1.name().get(), equalTo("B"));
        assertThat("version is incorrect", spi.entityStateOf(testEntity1).version(), not(equalTo(version)));
        unitOfWork1.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) ConcurrentEntityModificationException(org.qi4j.api.unitofwork.ConcurrentEntityModificationException) EntityBuilder(org.qi4j.api.entity.EntityBuilder) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 54 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class AbstractEntityStoreTest method whenRemovedEntityThenCannotFindEntity.

@Test
public void whenRemovedEntityThenCannotFindEntity() throws Exception {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    TestEntity newInstance = createEntity(unitOfWork);
    String identity = newInstance.identity().get();
    unitOfWork.complete();
    // Remove entity
    unitOfWork = module.newUnitOfWork();
    TestEntity instance = unitOfWork.get(newInstance);
    unitOfWork.remove(instance);
    unitOfWork.complete();
    // Find entity
    unitOfWork = module.newUnitOfWork();
    try {
        unitOfWork.get(TestEntity.class, identity);
        fail("Should not be able to find entity");
    } catch (NoSuchEntityException e) {
    // Ok!
    } finally {
        unitOfWork.discard();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) NoSuchEntityException(org.qi4j.api.unitofwork.NoSuchEntityException) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 55 with UnitOfWork

use of org.qi4j.api.unitofwork.UnitOfWork in project qi4j-sdk by Qi4j.

the class UnitOfWorkFactoryTest method testUnitOfWork.

@Test
public void testUnitOfWork() throws Exception {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    // Create product
    EntityBuilder<ProductEntity> cb = unitOfWork.newEntityBuilder(ProductEntity.class);
    cb.instance().name().set("Chair");
    cb.instance().price().set(57);
    Product chair = cb.newInstance();
    String actual = chair.name().get();
    org.junit.Assert.assertThat("Chair.name()", actual, org.hamcrest.CoreMatchers.equalTo("Chair"));
    org.junit.Assert.assertThat("Chair.price()", chair.price().get(), org.hamcrest.CoreMatchers.equalTo(57));
    unitOfWork.complete();
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)332 Test (org.junit.Test)232 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)108 Before (org.junit.Before)21 AssemblyException (org.qi4j.bootstrap.AssemblyException)21 UnitOfWorkCompletionException (org.qi4j.api.unitofwork.UnitOfWorkCompletionException)18 Delivery (org.qi4j.sample.dcicargo.sample_a.data.shipping.delivery.Delivery)17 HandlingEventsEntity (org.qi4j.sample.dcicargo.sample_a.data.entity.HandlingEventsEntity)15 PersonEntity (org.qi4j.library.conversion.values.TestModel.PersonEntity)13 CargoAggregateRoot (org.qi4j.sample.dcicargo.sample_b.data.aggregateroot.CargoAggregateRoot)13 IOException (java.io.IOException)12 ConcurrentEntityModificationException (org.qi4j.api.unitofwork.ConcurrentEntityModificationException)12 ModuleAssembly (org.qi4j.bootstrap.ModuleAssembly)12 Cargos (org.qi4j.sample.dcicargo.sample_a.data.shipping.cargo.Cargos)12 HandlingEvent (org.qi4j.sample.dcicargo.sample_a.data.shipping.handling.HandlingEvent)12 Location (org.qi4j.sample.dcicargo.sample_a.data.shipping.location.Location)11 HandlingEventAggregateRoot (org.qi4j.sample.dcicargo.sample_b.data.aggregateroot.HandlingEventAggregateRoot)11 Date (java.util.Date)10 BalanceData (org.qi4j.dci.moneytransfer.domain.data.BalanceData)10 File (java.io.File)8