use of org.qi4j.library.conversion.values.TestModel.PersonEntity in project qi4j-sdk by Qi4j.
the class ValueToEntityTest method givenQualifiedValueWhenUpdatingEntityExpectCorrectEntity.
@Test
public void givenQualifiedValueWhenUpdatingEntityExpectCorrectEntity() throws UnitOfWorkCompletionException {
String rickyIdentity;
try (UnitOfWork uow = module.newUnitOfWork(newUsecase("CreateRickySlaghoopleWithTypo"))) {
PersonEntity ricky = createPerson(uow, "Ricky", "Slaghople", someBirthDate);
ricky.spouse().set(uow.get(PersonEntity.class, ednaIdentity));
ricky.children().add(uow.get(PersonEntity.class, zekeIdentity));
rickyIdentity = ricky.identity().get();
assertThat(ricky.spouse().get(), notNullValue());
assertThat(ricky.children().count(), is(1));
uow.complete();
}
ValueBuilder<PersonValue> builder = module.newValueBuilder(PersonValue.class);
builder.prototype().firstName().set("Ricky");
builder.prototype().lastName().set("Slaghoople");
builder.prototype().dateOfBirth().set(someBirthDate);
PersonValue rickyNewStateValue = builder.newInstance();
try (UnitOfWork uow = module.newUnitOfWork(newUsecase("UpdateRickySlaghoople"))) {
PersonEntity rickyEntity = uow.get(PersonEntity.class, rickyIdentity);
// START SNIPPET: update
ValueToEntity conversion = module.findService(ValueToEntity.class).get();
conversion.update(rickyEntity, rickyNewStateValue);
// END SNIPPET: update
assertThat(rickyEntity.lastName(), equalTo("Slaghoople"));
assertThat(rickyEntity.spouse().get(), nullValue());
assertThat(rickyEntity.children().count(), is(0));
uow.complete();
}
}
use of org.qi4j.library.conversion.values.TestModel.PersonEntity in project qi4j-sdk by Qi4j.
the class EntityToValueTest method givenUnqualifiedValueWhenConvertingEntityExpectCorrectMapping.
@Test
public void givenUnqualifiedValueWhenConvertingEntityExpectCorrectMapping() throws UnitOfWorkCompletionException {
UnitOfWork uow = module.newUnitOfWork();
try {
PersonEntity niclas = setupPersonEntities(uow);
ServiceReference<EntityToValueService> reference = module.findService(EntityToValueService.class);
EntityToValueService service = reference.get();
PersonValue2 niclasValue = service.convert(PersonValue2.class, niclas);
assertEquals("Niclas", niclasValue.firstName().get());
assertEquals("Hedhman", niclasValue.lastName().get());
assertEquals("id:Lis", niclasValue.spouse().get());
assertEquals("id:Eric", niclasValue.children().get().get(0));
uow.complete();
} finally {
uow.discard();
}
}
use of org.qi4j.library.conversion.values.TestModel.PersonEntity in project qi4j-sdk by Qi4j.
the class EntityToValueTest method setupPersonEntities.
private static PersonEntity setupPersonEntities(UnitOfWork uow) {
PersonEntity niclas = createNiclas(uow);
PersonEntity lis = createLis(uow);
PersonEntity eric = createEric(uow);
niclas.spouse().set(lis);
niclas.children().add(eric);
lis.spouse().set(niclas);
lis.children().add(eric);
assertEquals("Niclas", niclas.firstName());
assertEquals("Hedhman", niclas.lastName());
assertEquals("Lis", lis.firstName());
assertEquals("Gazi", lis.lastName());
assertEquals("Eric", eric.firstName());
assertEquals("Hedman", eric.lastName());
return niclas;
}
use of org.qi4j.library.conversion.values.TestModel.PersonEntity in project qi4j-sdk by Qi4j.
the class EntityToValueTest method givenQualifiedValueNotFromSameInterfaceWhenConvertingEntityExpectNonOptionalException.
@Test(expected = ConstraintViolationException.class)
public void givenQualifiedValueNotFromSameInterfaceWhenConvertingEntityExpectNonOptionalException() throws UnitOfWorkCompletionException {
UnitOfWork uow = module.newUnitOfWork();
try {
PersonEntity niclas = setupPersonEntities(uow);
ServiceReference<EntityToValueService> reference = module.findService(EntityToValueService.class);
EntityToValueService service = reference.get();
PersonValue4 niclasValue = service.convert(PersonValue4.class, niclas);
uow.complete();
} finally {
uow.discard();
}
}
use of org.qi4j.library.conversion.values.TestModel.PersonEntity in project qi4j-sdk by Qi4j.
the class EntityToValueTest method whenConvertingEntityToValueUsingPrototypeOpportunityExpectCorrectValues.
@Test
public void whenConvertingEntityToValueUsingPrototypeOpportunityExpectCorrectValues() throws UnitOfWorkCompletionException {
UnitOfWork uow = module.newUnitOfWork();
try {
PersonEntity entity = setupPersonEntities(uow);
// START SNIPPET: prototypeOpportunity
EntityToValueService conversion = module.findService(EntityToValueService.class).get();
PersonValue value = conversion.convert(PersonValue.class, entity, new Function<PersonValue, PersonValue>() {
@Override
public PersonValue map(PersonValue prototype) {
prototype.firstName().set("Prototype Opportunity");
return prototype;
}
});
// END SNIPPET: prototypeOpportunity
assertEquals("Prototype Opportunity", value.firstName().get());
assertEquals("Hedhman", value.lastName().get());
assertEquals("id:Lis", value.spouse().get());
assertEquals("id:Eric", value.children().get().get(0));
uow.complete();
} finally {
uow.discard();
}
}
Aggregations