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