Search in sources :

Example 1 with PersonValue

use of org.qi4j.library.conversion.values.TestModel.PersonValue in project qi4j-sdk by Qi4j.

the class ValueToEntityTest method givenQualifiedValueWhenCreatingEntityExpectCorrectEntity.

@Test
public void givenQualifiedValueWhenCreatingEntityExpectCorrectEntity() throws UnitOfWorkCompletionException {
    ValueBuilder<PersonValue> builder = module.newValueBuilder(PersonValue.class);
    builder.prototype().firstName().set("Ed");
    builder.prototype().lastName().set("Flintstone");
    builder.prototype().dateOfBirth().set(someBirthDate);
    builder.prototype().spouse().set(ednaIdentity);
    builder.prototype().children().set(Arrays.asList(zekeIdentity, fredIdentity));
    PersonValue edValue = builder.newInstance();
    try (UnitOfWork uow = module.newUnitOfWork(newUsecase("CreatingEntityFromQualifiedValue"))) {
        // START SNIPPET: creation
        ValueToEntity conversion = module.findService(ValueToEntity.class).get();
        PersonEntity edEntity = conversion.create(PersonEntity.class, edValue);
        // END SNIPPET: creation
        assertThat(edEntity.firstName(), equalTo("Ed"));
        assertThat(edEntity.lastName(), equalTo("Flintstone"));
        assertThat(edEntity.spouse().get().firstName(), equalTo("Edna"));
        assertThat(Iterables.count(Iterables.filter(new Specification<PersonEntity>() {

            @Override
            public boolean satisfiedBy(PersonEntity child) {
                return "Zeke".equals(child.firstName()) || "Fred".equals(child.firstName());
            }
        }, edEntity.children())), is(2L));
        uow.complete();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PersonValue(org.qi4j.library.conversion.values.TestModel.PersonValue) PersonEntity(org.qi4j.library.conversion.values.TestModel.PersonEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 2 with PersonValue

use of org.qi4j.library.conversion.values.TestModel.PersonValue 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();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PersonValue(org.qi4j.library.conversion.values.TestModel.PersonValue) PersonEntity(org.qi4j.library.conversion.values.TestModel.PersonEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 3 with PersonValue

use of org.qi4j.library.conversion.values.TestModel.PersonValue 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();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PersonValue(org.qi4j.library.conversion.values.TestModel.PersonValue) PersonEntity(org.qi4j.library.conversion.values.TestModel.PersonEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Example 4 with PersonValue

use of org.qi4j.library.conversion.values.TestModel.PersonValue in project qi4j-sdk by Qi4j.

the class EntityToValueTest method whenConvertingEntityToValueExpectCorrectValues.

@Test
public void whenConvertingEntityToValueExpectCorrectValues() throws UnitOfWorkCompletionException {
    UnitOfWork uow = module.newUnitOfWork();
    try {
        PersonEntity entity = setupPersonEntities(uow);
        // START SNIPPET: conversion
        EntityToValueService conversion = module.findService(EntityToValueService.class).get();
        PersonValue value = conversion.convert(PersonValue.class, entity);
        // END SNIPPET: conversion
        assertEquals("Niclas", 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();
    }
}
Also used : UnitOfWork(org.qi4j.api.unitofwork.UnitOfWork) PersonValue(org.qi4j.library.conversion.values.TestModel.PersonValue) PersonEntity(org.qi4j.library.conversion.values.TestModel.PersonEntity) AbstractQi4jTest(org.qi4j.test.AbstractQi4jTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)4 PersonEntity (org.qi4j.library.conversion.values.TestModel.PersonEntity)4 PersonValue (org.qi4j.library.conversion.values.TestModel.PersonValue)4 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)4