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