Search in sources :

Example 6 with PersonEntity

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

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

Example 8 with PersonEntity

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;
}
Also used : PersonEntity(org.qi4j.library.conversion.values.TestModel.PersonEntity)

Example 9 with PersonEntity

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

Example 10 with PersonEntity

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

PersonEntity (org.qi4j.library.conversion.values.TestModel.PersonEntity)14 Test (org.junit.Test)13 UnitOfWork (org.qi4j.api.unitofwork.UnitOfWork)13 AbstractQi4jTest (org.qi4j.test.AbstractQi4jTest)13 PersonValue (org.qi4j.library.conversion.values.TestModel.PersonValue)4 PersonValue2 (org.qi4j.library.conversion.values.TestModel.PersonValue2)3 PersonValue3 (org.qi4j.library.conversion.values.TestModel.PersonValue3)3 PersonValue4 (org.qi4j.library.conversion.values.TestModel.PersonValue4)3