Search in sources :

Example 1 with PluralChangeModel

use of com.blazebit.persistence.view.change.PluralChangeModel in project blaze-persistence by Blazebit.

the class EntityViewUpdateSubviewInverseUmappedTest method testRemoveReadOnlyElementFromCollection.

@Test
public void testRemoveReadOnlyElementFromCollection() {
    // Given
    UpdatableDocumentView newDocument = evm.create(UpdatableDocumentView.class);
    UpdatablePersonView owner = evm.create(UpdatablePersonView.class);
    owner.setName("test");
    newDocument.setName("doc");
    newDocument.setOwner(owner);
    UpdatableVersionView version = evm.create(UpdatableVersionView.class);
    newDocument.getVersions().add(version);
    update(newDocument);
    // When
    em.clear();
    newDocument = evm.applySetting(EntityViewSetting.create(UpdatableDocumentView.class), cbf.create(em, Document.class)).getSingleResult();
    newDocument.getVersions().remove(newDocument.getVersions().iterator().next());
    PluralChangeModel<Object, Object> positionsChangeModel = (PluralChangeModel<Object, Object>) evm.getChangeModel(newDocument).get("versions");
    Assert.assertEquals(1, positionsChangeModel.getRemovedElements().size());
    update(newDocument);
    // Then
    em.clear();
    Document doc = em.find(Document.class, newDocument.getId());
    Assert.assertEquals(0, doc.getVersions().size());
}
Also used : UpdatableDocumentView(com.blazebit.persistence.view.testsuite.update.subview.inverse.unmapped.model.UpdatableDocumentView) UpdatablePersonView(com.blazebit.persistence.view.testsuite.update.subview.inverse.unmapped.model.UpdatablePersonView) PluralChangeModel(com.blazebit.persistence.view.change.PluralChangeModel) UpdatableVersionView(com.blazebit.persistence.view.testsuite.update.subview.inverse.unmapped.model.UpdatableVersionView) Document(com.blazebit.persistence.testsuite.entity.Document) AbstractEntityViewUpdateTest(com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateTest) Test(org.junit.Test)

Example 2 with PluralChangeModel

use of com.blazebit.persistence.view.change.PluralChangeModel in project blaze-persistence by Blazebit.

the class EntityViewUpdateSubviewInverseEmbeddedSimpleTest method testRemoveReadOnlyElementFromCollection.

@Test
public void testRemoveReadOnlyElementFromCollection() {
    // Given
    UpdatableLegacyOrderView newOrder = evm.create(UpdatableLegacyOrderView.class);
    UpdatableLegacyOrderPositionView position = evm.create(UpdatableLegacyOrderPositionView.class);
    position.getId().setPositionId(0);
    position.setArticleNumber("123");
    newOrder.getPositions().add(position);
    update(newOrder);
    // When
    em.clear();
    newOrder = evm.applySetting(EntityViewSetting.create(UpdatableLegacyOrderView.class), cbf.create(em, LegacyOrder.class)).getSingleResult();
    newOrder.getPositions().remove(newOrder.getPositions().iterator().next());
    PluralChangeModel<Object, Object> positionsChangeModel = (PluralChangeModel<Object, Object>) evm.getChangeModel(newOrder).get("positions");
    Assert.assertEquals(1, positionsChangeModel.getRemovedElements().size());
    update(newOrder);
    // Then
    em.clear();
    LegacyOrder legacyOrder = em.find(LegacyOrder.class, newOrder.getId());
    Assert.assertEquals(0, legacyOrder.getPositions().size());
}
Also used : PluralChangeModel(com.blazebit.persistence.view.change.PluralChangeModel) UpdatableLegacyOrderPositionView(com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.simple.model.UpdatableLegacyOrderPositionView) LegacyOrder(com.blazebit.persistence.view.testsuite.entity.LegacyOrder) UpdatableLegacyOrderView(com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.simple.model.UpdatableLegacyOrderView) AbstractEntityViewUpdateTest(com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateTest) Test(org.junit.Test)

Example 3 with PluralChangeModel

use of com.blazebit.persistence.view.change.PluralChangeModel in project blaze-persistence by Blazebit.

the class EntityViewUpdateMutableEmbeddableCollectionsTest method assertChangesUpdateAndFlush.

protected void assertChangesUpdateAndFlush(UpdatableDocumentEmbeddableWithCollectionsView docView) {
    if (!isFullMode()) {
        SingularChangeModel<UpdatableDocumentEmbeddableWithCollectionsView> changeModel = evm.getChangeModel(docView);
        PluralChangeModel<?, ?> namesChange = (PluralChangeModel<?, ?>) changeModel.get("names");
        assertTrue(changeModel.isDirty());
        assertEquals(ChangeModel.ChangeKind.MUTATED, changeModel.getKind());
        assertTrue(namesChange.isDirty());
        assertEquals(ChangeModel.ChangeKind.MUTATED, namesChange.getKind());
        if (registerType) {
            assertEquals(1, namesChange.getAddedElements().size());
            assertEquals(0, namesChange.getRemovedElements().size());
            assertEquals(0, namesChange.getMutatedElements().size());
            assertUnorderedEquals(Arrays.asList(namesChange.getAddedElements().get(0)), namesChange.getElementChanges());
        } else {
            assertEquals(2, namesChange.getAddedElements().size());
            assertEquals(1, namesChange.getRemovedElements().size());
            assertEquals(0, namesChange.getMutatedElements().size());
            assertEquals(ChangeModel.ChangeKind.UPDATED, namesChange.getAddedElements().get(0).getKind());
            assertEquals(new NameObject("doc1", "doc1"), namesChange.getAddedElements().get(0).getCurrentState());
            assertEquals(ChangeModel.ChangeKind.UPDATED, namesChange.getAddedElements().get(1).getKind());
            assertEquals(new NameObject("newPrimaryName", "newSecondaryName"), namesChange.getAddedElements().get(1).getCurrentState());
            assertEquals(ChangeModel.ChangeKind.UPDATED, namesChange.getRemovedElements().get(0).getKind());
            assertEquals(new NameObject("doc1", "doc1"), namesChange.getRemovedElements().get(0).getInitialState());
            assertUnorderedEquals(Arrays.asList(namesChange.getAddedElements().get(0), namesChange.getAddedElements().get(1), namesChange.getRemovedElements().get(0)), namesChange.getElementChanges());
        }
        assertEquals(ChangeModel.ChangeKind.UPDATED, namesChange.getAddedElements().get(namesChange.getAddedElements().size() - 1).getKind());
        assertNull(namesChange.getAddedElements().get(namesChange.getAddedElements().size() - 1).getInitialState());
        assertEquals(new NameObject("newPrimaryName", "newSecondaryName"), namesChange.getAddedElements().get(namesChange.getAddedElements().size() - 1).getCurrentState());
        assertEquals(Arrays.asList(namesChange), changeModel.getDirtyChanges());
    }
    update(docView);
    if (!isFullMode()) {
        SingularChangeModel<UpdatableDocumentEmbeddableWithCollectionsView> changeModel = evm.getChangeModel(docView);
        PluralChangeModel<?, ?> namesChange = (PluralChangeModel<?, ?>) changeModel.get("names");
        if (registerType) {
            assertFalse(changeModel.isDirty());
            assertEquals(ChangeModel.ChangeKind.NONE, changeModel.getKind());
            assertFalse(namesChange.isDirty());
            assertEquals(ChangeModel.ChangeKind.NONE, namesChange.getKind());
            assertEquals(0, namesChange.getAddedElements().size());
            assertEquals(0, namesChange.getRemovedElements().size());
            assertEquals(0, namesChange.getMutatedElements().size());
            assertEquals(0, namesChange.getElementChanges().size());
            assertEquals(0, changeModel.getDirtyChanges().size());
        } else {
            assertTrue(changeModel.isDirty());
            assertEquals(ChangeModel.ChangeKind.MUTATED, changeModel.getKind());
            assertTrue(namesChange.isDirty());
            assertEquals(ChangeModel.ChangeKind.MUTATED, namesChange.getKind());
            assertEquals(2, namesChange.getAddedElements().size());
            assertEquals(2, namesChange.getRemovedElements().size());
            assertEquals(0, namesChange.getMutatedElements().size());
            assertEquals(ChangeModel.ChangeKind.UPDATED, namesChange.getAddedElements().get(0).getKind());
            assertEquals(ChangeModel.ChangeKind.UPDATED, namesChange.getAddedElements().get(1).getKind());
            assertUnorderedEquals(Arrays.asList(namesChange.getAddedElements().get(0), namesChange.getAddedElements().get(1), namesChange.getRemovedElements().get(0), namesChange.getRemovedElements().get(1)), namesChange.getElementChanges());
            assertEquals(new NameObject("doc1", "doc1"), namesChange.getAddedElements().get(0).getCurrentState());
            assertEquals(new NameObject("newPrimaryName", "newSecondaryName"), namesChange.getAddedElements().get(1).getCurrentState());
            assertEquals(new NameObject("doc1", "doc1"), namesChange.getRemovedElements().get(0).getInitialState());
            assertEquals(new NameObject("newPrimaryName", "newSecondaryName"), namesChange.getRemovedElements().get(1).getInitialState());
            assertEquals(Arrays.asList(namesChange), changeModel.getDirtyChanges());
        }
    }
}
Also used : UpdatableDocumentEmbeddableWithCollectionsView(com.blazebit.persistence.view.testsuite.update.embeddable.simple.mutable.model.UpdatableDocumentEmbeddableWithCollectionsView) NameObject(com.blazebit.persistence.testsuite.entity.NameObject) PluralChangeModel(com.blazebit.persistence.view.change.PluralChangeModel)

Example 4 with PluralChangeModel

use of com.blazebit.persistence.view.change.PluralChangeModel in project blaze-persistence by Blazebit.

the class EntityViewUpdateSubviewInverseEmbeddedComplexTest method testRemoveReadOnlyElementFromCollection.

@Test
public void testRemoveReadOnlyElementFromCollection() {
    // Given
    UpdatableLegacyOrderView newOrder = evm.create(UpdatableLegacyOrderView.class);
    UpdatableLegacyOrderPositionView position = evm.create(UpdatableLegacyOrderPositionView.class);
    position.getId().setPositionId(0);
    position.setArticleNumber("123");
    newOrder.getPositions().add(position);
    update(newOrder);
    // When
    em.clear();
    newOrder = evm.applySetting(EntityViewSetting.create(UpdatableLegacyOrderView.class), cbf.create(em, LegacyOrder.class)).getSingleResult();
    newOrder.getPositions().remove(newOrder.getPositions().iterator().next());
    PluralChangeModel<Object, Object> positionsChangeModel = (PluralChangeModel<Object, Object>) evm.getChangeModel(newOrder).get("positions");
    Assert.assertEquals(1, positionsChangeModel.getRemovedElements().size());
    update(newOrder);
    // Then
    em.clear();
    LegacyOrder legacyOrder = em.find(LegacyOrder.class, newOrder.getId());
    Assert.assertEquals(0, legacyOrder.getPositions().size());
}
Also used : PluralChangeModel(com.blazebit.persistence.view.change.PluralChangeModel) UpdatableLegacyOrderPositionView(com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.complex.model.UpdatableLegacyOrderPositionView) LegacyOrder(com.blazebit.persistence.view.testsuite.entity.LegacyOrder) UpdatableLegacyOrderView(com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.complex.model.UpdatableLegacyOrderView) AbstractEntityViewUpdateTest(com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateTest) Test(org.junit.Test)

Example 5 with PluralChangeModel

use of com.blazebit.persistence.view.change.PluralChangeModel in project blaze-persistence by Blazebit.

the class EntityViewUpdateListenerCollectionsTest method testUpdateAddToCollection.

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

Aggregations

PluralChangeModel (com.blazebit.persistence.view.change.PluralChangeModel)7 Test (org.junit.Test)5 AbstractEntityViewUpdateTest (com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateTest)4 LegacyOrder (com.blazebit.persistence.view.testsuite.entity.LegacyOrder)3 NameObject (com.blazebit.persistence.testsuite.entity.NameObject)2 UpdatableDocumentEmbeddableWithCollectionsView (com.blazebit.persistence.view.testsuite.update.embeddable.simple.mutable.model.UpdatableDocumentEmbeddableWithCollectionsView)2 Document (com.blazebit.persistence.testsuite.entity.Document)1 AbstractEntityViewUpdateDocumentTest (com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateDocumentTest)1 UpdatableDocumentWithCollectionsView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentWithCollectionsView)1 UpdatablePersonView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView)1 UpdatableLegacyOrderPositionView (com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.complex.model.UpdatableLegacyOrderPositionView)1 UpdatableLegacyOrderView (com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.complex.model.UpdatableLegacyOrderView)1 UpdatableLegacyOrderPositionView (com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.entityid.model.UpdatableLegacyOrderPositionView)1 UpdatableLegacyOrderView (com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.entityid.model.UpdatableLegacyOrderView)1 UpdatableLegacyOrderPositionView (com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.simple.model.UpdatableLegacyOrderPositionView)1 UpdatableLegacyOrderView (com.blazebit.persistence.view.testsuite.update.subview.inverse.embedded.simple.model.UpdatableLegacyOrderView)1 UpdatableDocumentView (com.blazebit.persistence.view.testsuite.update.subview.inverse.unmapped.model.UpdatableDocumentView)1 UpdatablePersonView (com.blazebit.persistence.view.testsuite.update.subview.inverse.unmapped.model.UpdatablePersonView)1 UpdatableVersionView (com.blazebit.persistence.view.testsuite.update.subview.inverse.unmapped.model.UpdatableVersionView)1