Search in sources :

Example 26 with UnitOfWork

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

the class EntityBuilderWithStateTest method test.

@Test
public void test() throws UnitOfWorkCompletionException {
    final String associatedIdentity;
    try (UnitOfWork uow = module.newUnitOfWork()) {
        EntityBuilder<SomeEntity> builder = uow.newEntityBuilder(SomeEntity.class);
        builder.instance().prop().set("Associated");
        SomeEntity entity = builder.newInstance();
        associatedIdentity = entity.identity().get();
        uow.complete();
    }
    try (UnitOfWork uow = module.newUnitOfWork()) {
        SomeEntity entity = uow.newEntityBuilderWithState(SomeEntity.class, new Function<PropertyDescriptor, Object>() {

            @Override
            public Object map(PropertyDescriptor descriptor) {
                if ("prop".equals(descriptor.qualifiedName().name())) {
                    return "Foo";
                }
                return null;
            }
        }, new Function<AssociationDescriptor, EntityReference>() {

            @Override
            public EntityReference map(AssociationDescriptor descriptor) {
                if ("ass".equals(descriptor.qualifiedName().name())) {
                    return EntityReference.parseEntityReference(associatedIdentity);
                }
                return null;
            }
        }, new Function<AssociationDescriptor, Iterable<EntityReference>>() {

            @Override
            public Iterable<EntityReference> map(AssociationDescriptor descriptor) {
                if ("manyAss".equals(descriptor.qualifiedName().name())) {
                    return Arrays.asList(EntityReference.parseEntityReference(associatedIdentity));
                }
                return null;
            }
        }, new Function<AssociationDescriptor, Map<String, EntityReference>>() {

            @Override
            public Map<String, EntityReference> map(AssociationDescriptor descriptor) {
                if ("namedAss".equals(descriptor.qualifiedName().name())) {
                    return Collections.singletonMap("foo", EntityReference.parseEntityReference(associatedIdentity));
                }
                return null;
            }
        }).newInstance();
        assertThat(entity.prop().get(), equalTo("Foo"));
        assertThat(entity.ass().get().identity().get(), equalTo(associatedIdentity));
        assertThat(entity.manyAss().get(0).identity().get(), equalTo(associatedIdentity));
        assertThat(entity.namedAss().get("foo").identity().get(), equalTo(associatedIdentity));
        uow.complete();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) Function(org.qi4j.functional.Function) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 27 with UnitOfWork

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

the class EntityCreationTest method doTestUseUowNewEntity.

@Test
public void doTestUseUowNewEntity() {
    UnitOfWork uow = module.newUnitOfWork();
    SomeEntity entity = uow.newEntity(SomeEntity.class);
    uow.discard();
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 28 with UnitOfWork

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

the class EntityVisibilityTest method givenFromEntityWhenAccessingModuleApplicationVisibleExpectSuccess.

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

Example 29 with UnitOfWork

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

the class EntityVisibilityTest method givenFromEntityWhenAccessingModuleModuleVisibleExpectSuccess.

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

Example 30 with UnitOfWork

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

the class EntityVisibilityTest method givenFromEntityWhenAccessingBelowModuleVisibleExpectException.

@Test(expected = EntityTypeNotFoundException.class)
public void givenFromEntityWhenAccessingBelowModuleVisibleExpectException() {
    UnitOfWork unitOfWork = module.newUnitOfWork();
    try {
        FromEntity entity = unitOfWork.newEntity(FromEntity.class, "123");
        entity.belowModuleVisible();
    } finally {
        if (unitOfWork.isOpen()) {
            unitOfWork.discard();
        }
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) 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