Search in sources :

Example 1 with MapChangeModel

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

the class EntityViewUpdateMutableEmbeddableMapsTest method assertMutableChangeModel.

private void assertMutableChangeModel(UpdatableDocumentEmbeddableWithMapsView docView) {
    if (!isFullMode()) {
        SingularChangeModel<UpdatableDocumentEmbeddableWithMapsView> changeModel = evm.getChangeModel(docView);
        MapChangeModel<?, ?> nameMapChange = (MapChangeModel<Object, Object>) changeModel.<Map<Object, Object>>get("nameMap");
        if (registerType) {
            assertFalse(changeModel.isDirty());
            assertEquals(ChangeModel.ChangeKind.NONE, changeModel.getKind());
            assertFalse(nameMapChange.isDirty());
            assertEquals(ChangeModel.ChangeKind.NONE, nameMapChange.getKind());
            assertEquals(0, nameMapChange.getAddedElements().size());
            assertEquals(0, nameMapChange.getRemovedElements().size());
            assertEquals(0, nameMapChange.getMutatedElements().size());
            assertEquals(0, nameMapChange.getElementChanges().size());
            assertEquals(0, nameMapChange.getAddedKeys().size());
            assertEquals(0, nameMapChange.getRemovedKeys().size());
            assertEquals(0, nameMapChange.getMutatedKeys().size());
            assertEquals(0, nameMapChange.getKeyChanges().size());
            assertEquals(0, nameMapChange.getAddedObjects().size());
            assertEquals(0, nameMapChange.getRemovedObjects().size());
            assertEquals(0, nameMapChange.getMutatedObjects().size());
            assertEquals(0, nameMapChange.getObjectChanges().size());
            assertEquals(0, changeModel.getDirtyChanges().size());
        } else {
            assertTrue(changeModel.isDirty());
            assertEquals(ChangeModel.ChangeKind.MUTATED, changeModel.getKind());
            assertTrue(nameMapChange.isDirty());
            assertEquals(ChangeModel.ChangeKind.MUTATED, nameMapChange.getKind());
            assertEquals(0, nameMapChange.getAddedElements().size());
            assertEquals(0, nameMapChange.getRemovedElements().size());
            assertEquals(1, nameMapChange.getMutatedElements().size());
            assertEquals(ChangeModel.ChangeKind.MUTATED, nameMapChange.getMutatedElements().get(0).getKind());
            assertEquals(new NameObject("doc1", "doc1"), nameMapChange.getMutatedElements().get(0).getCurrentState());
            assertEquals(0, nameMapChange.getAddedKeys().size());
            assertEquals(0, nameMapChange.getRemovedKeys().size());
            assertEquals(0, nameMapChange.getMutatedKeys().size());
            assertEquals(0, nameMapChange.getAddedObjects().size());
            assertEquals(0, nameMapChange.getRemovedObjects().size());
            assertEquals(1, nameMapChange.getMutatedObjects().size());
            assertTrue(nameMapChange.getMutatedObjects().contains(nameMapChange.getMutatedElements().get(0)));
            assertEquals(Arrays.asList(nameMapChange), changeModel.getDirtyChanges());
        }
    }
}
Also used : MapChangeModel(com.blazebit.persistence.view.change.MapChangeModel) UpdatableDocumentEmbeddableWithMapsView(com.blazebit.persistence.view.testsuite.update.embeddable.simple.mutable.model.UpdatableDocumentEmbeddableWithMapsView) NameObject(com.blazebit.persistence.testsuite.entity.NameObject) NameObject(com.blazebit.persistence.testsuite.entity.NameObject)

Example 2 with MapChangeModel

use of com.blazebit.persistence.view.change.MapChangeModel 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 3 with MapChangeModel

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

the class EntityViewUpdateMutableEmbeddableMapsTest method assertChangesUpdateAndFlush.

protected void assertChangesUpdateAndFlush(UpdatableDocumentEmbeddableWithMapsView docView) {
    if (!isFullMode()) {
        SingularChangeModel<UpdatableDocumentEmbeddableWithMapsView> changeModel = evm.getChangeModel(docView);
        assertTrue(changeModel.isDirty());
        assertEquals(ChangeModel.ChangeKind.MUTATED, changeModel.getKind());
        MapChangeModel<?, ?> nameMapChange = (MapChangeModel<Object, Object>) changeModel.<Map<Object, Object>>get("nameMap");
        assertTrue(nameMapChange.isDirty());
        assertEquals(ChangeModel.ChangeKind.MUTATED, nameMapChange.getKind());
        if (registerType) {
            assertEquals(1, nameMapChange.getAddedElements().size());
            assertEquals(0, nameMapChange.getRemovedElements().size());
            assertEquals(0, nameMapChange.getMutatedElements().size());
            assertEquals(1, nameMapChange.getElementChanges().size());
            assertEquals(ChangeModel.ChangeKind.UPDATED, nameMapChange.getAddedElements().get(0).getKind());
            assertNull(nameMapChange.getAddedElements().get(0).getInitialState());
            assertEquals(new NameObject("newPrimaryName", "newSecondaryName"), nameMapChange.getAddedElements().get(0).getCurrentState());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedElements().get(0)), nameMapChange.getElementChanges());
            assertEquals(1, nameMapChange.getAddedKeys().size());
            assertEquals(0, nameMapChange.getRemovedKeys().size());
            assertEquals(0, nameMapChange.getMutatedKeys().size());
            assertEquals(1, nameMapChange.getKeyChanges().size());
            assertEquals(ChangeModel.ChangeKind.UPDATED, nameMapChange.getAddedKeys().get(0).getKind());
            assertEquals("newPrimaryName", nameMapChange.getAddedKeys().get(0).getCurrentState());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedKeys().get(0)), nameMapChange.getKeyChanges());
            assertEquals(2, nameMapChange.getAddedObjects().size());
            assertEquals(0, nameMapChange.getRemovedObjects().size());
            assertEquals(0, nameMapChange.getMutatedObjects().size());
            assertEquals(2, nameMapChange.getObjectChanges().size());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedKeys().get(0), nameMapChange.getAddedElements().get(0)), nameMapChange.getAddedObjects());
            assertEquals(0, nameMapChange.getMutatedObjects().size());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedKeys().get(0), nameMapChange.getAddedElements().get(0)), nameMapChange.getObjectChanges());
        } else {
            assertEquals(1, nameMapChange.getAddedElements().size());
            assertEquals(0, nameMapChange.getRemovedElements().size());
            assertEquals(1, nameMapChange.getMutatedElements().size());
            assertEquals(2, nameMapChange.getElementChanges().size());
            assertEquals(ChangeModel.ChangeKind.UPDATED, nameMapChange.getAddedElements().get(0).getKind());
            assertNull(nameMapChange.getAddedElements().get(0).getInitialState());
            assertEquals(new NameObject("newPrimaryName", "newSecondaryName"), nameMapChange.getAddedElements().get(0).getCurrentState());
            assertEquals(ChangeModel.ChangeKind.MUTATED, nameMapChange.getMutatedElements().get(0).getKind());
            assertEquals(new NameObject("doc1", "doc1"), nameMapChange.getMutatedElements().get(0).getCurrentState());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedElements().get(0), nameMapChange.getMutatedElements().get(0)), nameMapChange.getElementChanges());
            assertEquals(1, nameMapChange.getAddedKeys().size());
            assertEquals(0, nameMapChange.getRemovedKeys().size());
            assertEquals(0, nameMapChange.getMutatedKeys().size());
            assertEquals(1, nameMapChange.getKeyChanges().size());
            assertEquals(ChangeModel.ChangeKind.UPDATED, nameMapChange.getAddedKeys().get(0).getKind());
            assertEquals("newPrimaryName", nameMapChange.getAddedKeys().get(0).getCurrentState());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedKeys().get(0)), nameMapChange.getKeyChanges());
            assertEquals(2, nameMapChange.getAddedObjects().size());
            assertEquals(0, nameMapChange.getRemovedObjects().size());
            assertEquals(1, nameMapChange.getMutatedObjects().size());
            assertEquals(3, nameMapChange.getObjectChanges().size());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedKeys().get(0), nameMapChange.getAddedElements().get(0)), nameMapChange.getAddedObjects());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getMutatedElements().get(0)), nameMapChange.getMutatedObjects());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getAddedKeys().get(0), nameMapChange.getAddedElements().get(0), nameMapChange.getMutatedElements().get(0)), nameMapChange.getObjectChanges());
        }
        assertUnorderedEquals(Arrays.asList(nameMapChange), changeModel.getDirtyChanges());
    }
    update(docView);
    if (!isFullMode()) {
        SingularChangeModel<UpdatableDocumentEmbeddableWithMapsView> changeModel = evm.getChangeModel(docView);
        MapChangeModel<?, ?> nameMapChange = (MapChangeModel<Object, Object>) changeModel.<Map<Object, Object>>get("nameMap");
        if (registerType) {
            assertFalse(changeModel.isDirty());
            assertEquals(ChangeModel.ChangeKind.NONE, changeModel.getKind());
            assertFalse(nameMapChange.isDirty());
            assertEquals(ChangeModel.ChangeKind.NONE, nameMapChange.getKind());
            assertEquals(0, nameMapChange.getAddedElements().size());
            assertEquals(0, nameMapChange.getRemovedElements().size());
            assertEquals(0, nameMapChange.getMutatedElements().size());
            assertEquals(0, nameMapChange.getElementChanges().size());
            assertEquals(0, nameMapChange.getAddedKeys().size());
            assertEquals(0, nameMapChange.getRemovedKeys().size());
            assertEquals(0, nameMapChange.getMutatedKeys().size());
            assertEquals(0, nameMapChange.getKeyChanges().size());
            assertEquals(0, nameMapChange.getAddedObjects().size());
            assertEquals(0, nameMapChange.getRemovedObjects().size());
            assertEquals(0, nameMapChange.getMutatedObjects().size());
            assertEquals(0, nameMapChange.getObjectChanges().size());
            assertEquals(0, changeModel.getDirtyChanges().size());
        } else {
            assertTrue(changeModel.isDirty());
            assertEquals(ChangeModel.ChangeKind.MUTATED, changeModel.getKind());
            assertTrue(nameMapChange.isDirty());
            assertEquals(ChangeModel.ChangeKind.MUTATED, nameMapChange.getKind());
            assertEquals(0, nameMapChange.getAddedElements().size());
            assertEquals(0, nameMapChange.getRemovedElements().size());
            assertEquals(2, nameMapChange.getMutatedElements().size());
            assertEquals(ChangeModel.ChangeKind.MUTATED, nameMapChange.getMutatedElements().get(0).getKind());
            assertEquals(ChangeModel.ChangeKind.MUTATED, nameMapChange.getMutatedElements().get(1).getKind());
            assertEquals(2, nameMapChange.getElementChanges().size());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getMutatedElements().get(0), nameMapChange.getMutatedElements().get(1)), nameMapChange.getElementChanges());
            assertUnorderedEquals(Arrays.asList(new NameObject("doc1", "doc1"), new NameObject("newPrimaryName", "newSecondaryName")), Arrays.asList(nameMapChange.getMutatedElements().get(0).getCurrentState(), nameMapChange.getMutatedElements().get(1).getCurrentState()));
            assertEquals(0, nameMapChange.getAddedKeys().size());
            assertEquals(0, nameMapChange.getRemovedKeys().size());
            assertEquals(0, nameMapChange.getMutatedKeys().size());
            assertEquals(0, nameMapChange.getKeyChanges().size());
            assertEquals(0, nameMapChange.getAddedObjects().size());
            assertEquals(0, nameMapChange.getRemovedObjects().size());
            assertEquals(2, nameMapChange.getMutatedObjects().size());
            assertEquals(2, nameMapChange.getObjectChanges().size());
            assertUnorderedEquals(Arrays.asList(nameMapChange.getMutatedObjects().get(0), nameMapChange.getMutatedObjects().get(1)), nameMapChange.getObjectChanges());
            assertUnorderedEquals(Arrays.asList(nameMapChange), changeModel.getDirtyChanges());
        }
    }
}
Also used : MapChangeModel(com.blazebit.persistence.view.change.MapChangeModel) UpdatableDocumentEmbeddableWithMapsView(com.blazebit.persistence.view.testsuite.update.embeddable.simple.mutable.model.UpdatableDocumentEmbeddableWithMapsView) NameObject(com.blazebit.persistence.testsuite.entity.NameObject) NameObject(com.blazebit.persistence.testsuite.entity.NameObject)

Aggregations

MapChangeModel (com.blazebit.persistence.view.change.MapChangeModel)3 NameObject (com.blazebit.persistence.testsuite.entity.NameObject)2 UpdatableDocumentEmbeddableWithMapsView (com.blazebit.persistence.view.testsuite.update.embeddable.simple.mutable.model.UpdatableDocumentEmbeddableWithMapsView)2 AbstractEntityViewUpdateDocumentTest (com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateDocumentTest)1 UpdatableDocumentWithMapsView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatableDocumentWithMapsView)1 UpdatablePersonView (com.blazebit.persistence.view.testsuite.update.listener.model.UpdatablePersonView)1 Test (org.junit.Test)1