Search in sources :

Example 1 with UpdatablePersonView

use of com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView in project blaze-persistence by Blazebit.

the class EntityViewUpdateListenerCollectionsTest method testUpdateAddToCollectionCreate.

@Test
public void testUpdateAddToCollectionCreate() {
    // Given
    final UpdatableDocumentWithCollectionsView docView = getDoc1View();
    UpdatablePersonView newPerson = evm.create(UpdatablePersonView.class);
    // When
    newPerson.setName("newPerson");
    docView.setPeople(new ArrayList<>(docView.getPeople()));
    docView.getPeople().add(newPerson);
    saveWith(docView, flusherBuilder -> {
        flusherBuilder.onPrePersist(UpdatablePersonView.class, Person.class, (view, entity) -> {
            entity.setAge(10L);
        });
    });
    // Then
    clearPersistenceContextAndReload();
    assertSubviewEquals(doc1.getPeople(), docView.getPeople());
    assertEquals("newPerson", doc1.getPeople().get(1).getName());
    assertEquals(10L, doc1.getPeople().get(1).getAge());
}
Also used : UpdatableDocumentWithCollectionsView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentWithCollectionsView) UpdatablePersonView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView) Test(org.junit.Test) AbstractEntityViewUpdateDocumentTest(com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateDocumentTest)

Example 2 with UpdatablePersonView

use of com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView in project blaze-persistence by Blazebit.

the class EntityViewUpdateListenerCollectionsTest method assertSubviewEquals.

public void assertSubviewEquals(Collection<Person> persons, Collection<UpdatablePersonView> personSubviews) {
    if (persons == null) {
        assertNull(personSubviews);
        return;
    }
    assertNotNull(personSubviews);
    assertEquals(persons.size(), personSubviews.size());
    for (Person p : persons) {
        boolean found = false;
        for (UpdatablePersonView pSub : personSubviews) {
            if (p.getName().equals(pSub.getName())) {
                found = true;
                break;
            }
        }
        if (!found) {
            Assert.fail("Could not find a person subview instance with the name: " + p.getName());
        }
    }
}
Also used : UpdatablePersonView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView) Person(com.blazebit.persistence.testsuite.entity.Person)

Example 3 with UpdatablePersonView

use of com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView in project blaze-persistence by Blazebit.

the class EntityViewUpdateListenerCollectionsTest method testUpdateAddToCollectionAndModify.

@Test
public void testUpdateAddToCollectionAndModify() {
    // Given
    final UpdatableDocumentWithCollectionsView docView = getDoc1View();
    UpdatablePersonView newPerson = getP2View(UpdatablePersonView.class);
    // When
    newPerson.setName("newPerson");
    docView.getPeople().add(newPerson);
    update(docView);
    // Then
    clearPersistenceContextAndReload();
    assertSubviewEquals(doc1.getPeople(), docView.getPeople());
    assertEquals("newPerson", p2.getName());
}
Also used : UpdatableDocumentWithCollectionsView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentWithCollectionsView) UpdatablePersonView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView) Test(org.junit.Test) AbstractEntityViewUpdateDocumentTest(com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateDocumentTest)

Example 4 with UpdatablePersonView

use of com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView in project blaze-persistence by Blazebit.

the class EntityViewUpdateListenerMapsTest method testUpdateAddToCollection.

@Test
public void testUpdateAddToCollection() {
    // Given
    final UpdatableDocumentWithMapsView docView = getDoc1View();
    UpdatablePersonView newPerson = getP2View(UpdatablePersonView.class);
    // When
    docView.getContacts().put(2, newPerson);
    saveWith(docView, flushOperationBuilder -> {
        flushOperationBuilder.onPreUpdate(UpdatableDocumentWithMapsView.class, view -> {
            MapChangeModel<UpdatableDocumentWithMapsView, UpdatablePersonView> changeModel = (MapChangeModel<UpdatableDocumentWithMapsView, UpdatablePersonView>) (ChangeModel<?>) evm.getChangeModel(view).get("contacts");
            assertEquals(newPerson, changeModel.getAddedElements().get(0).getCurrentState());
        });
    });
    // Then
    clearPersistenceContextAndReload();
    assertSubviewEquals(doc1.getContacts(), docView.getContacts());
}
Also used : MapChangeModel(com.blazebit.persistence.view.change.MapChangeModel) UpdatablePersonView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView) UpdatableDocumentWithMapsView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentWithMapsView) Test(org.junit.Test) AbstractEntityViewUpdateDocumentTest(com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateDocumentTest)

Example 5 with UpdatablePersonView

use of com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView in project blaze-persistence by Blazebit.

the class EntityViewUpdateListenerMapsTest method assertSubviewEquals.

public static void assertSubviewEquals(Map<Integer, Person> persons, Map<Integer, ? extends UpdatablePersonView> personSubviews) {
    if (persons == null) {
        assertNull(personSubviews);
        return;
    }
    assertNotNull(personSubviews);
    assertEquals(persons.size(), personSubviews.size());
    for (Map.Entry<Integer, Person> entry : persons.entrySet()) {
        Person p = entry.getValue();
        boolean found = false;
        UpdatablePersonView pSub = personSubviews.get(entry.getKey());
        if (pSub != null) {
            if (p.getName().equals(pSub.getName())) {
                found = true;
                break;
            }
        }
        if (!found) {
            Assert.fail("Could not find a person subview instance with the name: " + p.getName());
        }
    }
}
Also used : UpdatablePersonView(com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView) HashMap(java.util.HashMap) Map(java.util.Map) Person(com.blazebit.persistence.testsuite.entity.Person)

Aggregations

UpdatablePersonView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView)15 AbstractEntityViewUpdateDocumentTest (com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateDocumentTest)13 Test (org.junit.Test)13 UpdatableDocumentWithCollectionsView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentWithCollectionsView)5 UpdatableDocumentWithMapsView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentWithMapsView)5 UpdatableDocumentView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentView)3 Person (com.blazebit.persistence.testsuite.entity.Person)2 HashMap (java.util.HashMap)2 MapChangeModel (com.blazebit.persistence.view.change.MapChangeModel)1 PluralChangeModel (com.blazebit.persistence.view.change.PluralChangeModel)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1