Search in sources :

Example 1 with ToManyRelationshipChange

use of org.apache.cayenne.commitlog.model.ToManyRelationshipChange in project cayenne by apache.

the class CommitLogFilter_All_FlattenedIT method testPostCommit_UpdateToMany.

@Test
public void testPostCommit_UpdateToMany() throws SQLException {
    e3.insert(1);
    e4.insert(11);
    e4.insert(12);
    e34.insert(1, 11);
    final E3 e3 = SelectById.query(E3.class, 1).selectOne(context);
    final E4 e4_1 = SelectById.query(E4.class, 11).selectOne(context);
    final E4 e4_2 = SelectById.query(E4.class, 12).selectOne(context);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertSame(context, invocation.getArguments()[0]);
            ChangeMap changes = (ChangeMap) invocation.getArguments()[1];
            assertNotNull(changes);
            assertEquals(3, changes.getUniqueChanges().size());
            ObjectChange e3c = changes.getChanges().get(new ObjectId("E3", E3.ID_PK_COLUMN, 1));
            assertNotNull(e3c);
            assertEquals(ObjectChangeType.UPDATE, e3c.getType());
            assertEquals(0, e3c.getAttributeChanges().size());
            assertEquals(1, e3c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange e3c1 = e3c.getToManyRelationshipChanges().get(E3.E4S.getName());
            assertNotNull(e3c1);
            assertEquals(1, e3c1.getAdded().size());
            assertTrue(e3c1.getAdded().contains(e4_2.getObjectId()));
            assertEquals(1, e3c1.getRemoved().size());
            assertTrue(e3c1.getRemoved().contains(e4_1.getObjectId()));
            ObjectChange e41c = changes.getChanges().get(new ObjectId("E4", E4.ID_PK_COLUMN, 11));
            assertNotNull(e41c);
            assertEquals(ObjectChangeType.UPDATE, e41c.getType());
            assertEquals(0, e41c.getAttributeChanges().size());
            assertEquals(1, e41c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange e41c1 = e41c.getToManyRelationshipChanges().get(E4.E3S.getName());
            assertNotNull(e41c);
            assertEquals(0, e41c1.getAdded().size());
            assertEquals(1, e41c1.getRemoved().size());
            assertTrue(e41c1.getRemoved().contains(e3.getObjectId()));
            ObjectChange e42c = changes.getChanges().get(new ObjectId("E4", E4.ID_PK_COLUMN, 12));
            assertNotNull(e42c);
            assertEquals(ObjectChangeType.UPDATE, e42c.getType());
            assertEquals(0, e42c.getAttributeChanges().size());
            assertEquals(1, e42c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange e42c1 = e42c.getToManyRelationshipChanges().get(E4.E3S.getName());
            assertNotNull(e42c);
            assertEquals(0, e42c1.getRemoved().size());
            assertEquals(1, e42c1.getAdded().size());
            assertTrue(e42c1.getAdded().contains(e3.getObjectId()));
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    e3.removeFromE4s(e4_1);
    e3.addToE4s(e4_2);
    context.commitChanges();
    verify(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
}
Also used : ChangeMap(org.apache.cayenne.commitlog.model.ChangeMap) ObjectChange(org.apache.cayenne.commitlog.model.ObjectChange) ObjectId(org.apache.cayenne.ObjectId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ToManyRelationshipChange(org.apache.cayenne.commitlog.model.ToManyRelationshipChange) ObjectContext(org.apache.cayenne.ObjectContext) E3(org.apache.cayenne.commitlog.db.E3) E4(org.apache.cayenne.commitlog.db.E4) Test(org.junit.Test)

Example 2 with ToManyRelationshipChange

use of org.apache.cayenne.commitlog.model.ToManyRelationshipChange in project cayenne by apache.

the class CommitLogFilter_AllIT method testPostCommit_UpdateToMany.

@Test
public void testPostCommit_UpdateToMany() throws SQLException {
    auditable1.insert(1, "xx");
    auditableChild1.insert(1, 1, "cc1");
    auditableChild1.insert(2, null, "cc2");
    auditableChild1.insert(3, null, "cc3");
    final AuditableChild1 ac1 = SelectById.query(AuditableChild1.class, 1).selectOne(context);
    final AuditableChild1 ac2 = SelectById.query(AuditableChild1.class, 2).selectOne(context);
    final AuditableChild1 ac3 = SelectById.query(AuditableChild1.class, 3).selectOne(context);
    final Auditable1 a1 = SelectById.query(Auditable1.class, 1).selectOne(context);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertSame(context, invocation.getArguments()[0]);
            ChangeMap changes = (ChangeMap) invocation.getArguments()[1];
            assertNotNull(changes);
            assertEquals(4, changes.getUniqueChanges().size());
            ObjectChange a1c = changes.getChanges().get(new ObjectId("Auditable1", Auditable1.ID_PK_COLUMN, 1));
            assertNotNull(a1c);
            assertEquals(ObjectChangeType.UPDATE, a1c.getType());
            assertEquals(0, a1c.getAttributeChanges().size());
            assertEquals(1, a1c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange a1c1 = a1c.getToManyRelationshipChanges().get(Auditable1.CHILDREN1.getName());
            assertNotNull(a1c1);
            assertEquals(2, a1c1.getAdded().size());
            assertTrue(a1c1.getAdded().contains(ac2.getObjectId()));
            assertTrue(a1c1.getAdded().contains(ac3.getObjectId()));
            assertEquals(1, a1c1.getRemoved().size());
            assertTrue(a1c1.getRemoved().contains(ac1.getObjectId()));
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    a1.removeFromChildren1(ac1);
    a1.addToChildren1(ac2);
    a1.addToChildren1(ac3);
    context.commitChanges();
    verify(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
}
Also used : AuditableChild1(org.apache.cayenne.commitlog.db.AuditableChild1) ChangeMap(org.apache.cayenne.commitlog.model.ChangeMap) ObjectChange(org.apache.cayenne.commitlog.model.ObjectChange) ObjectId(org.apache.cayenne.ObjectId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ToManyRelationshipChange(org.apache.cayenne.commitlog.model.ToManyRelationshipChange) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 3 with ToManyRelationshipChange

use of org.apache.cayenne.commitlog.model.ToManyRelationshipChange in project cayenne by apache.

the class CommitLogFilter_FilteredIT method testPostCommit_UpdateToMany.

@Test
public void testPostCommit_UpdateToMany() throws SQLException {
    auditable1.insert(1, "xx");
    auditableChild1.insert(1, 1, "cc1");
    auditableChild1.insert(2, null, "cc2");
    auditableChild1.insert(3, null, "cc3");
    final AuditableChild1 ac1 = SelectById.query(AuditableChild1.class, 1).selectOne(context);
    final AuditableChild1 ac2 = SelectById.query(AuditableChild1.class, 2).selectOne(context);
    final AuditableChild1 ac3 = SelectById.query(AuditableChild1.class, 3).selectOne(context);
    final Auditable1 a1 = SelectById.query(Auditable1.class, 1).selectOne(context);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertSame(context, invocation.getArguments()[0]);
            ChangeMap changes = (ChangeMap) invocation.getArguments()[1];
            assertNotNull(changes);
            assertEquals(1, changes.getUniqueChanges().size());
            ObjectChange a1c = changes.getChanges().get(new ObjectId("Auditable1", Auditable1.ID_PK_COLUMN, 1));
            assertNotNull(a1c);
            assertEquals(ObjectChangeType.UPDATE, a1c.getType());
            assertEquals(0, a1c.getAttributeChanges().size());
            assertEquals(1, a1c.getToManyRelationshipChanges().size());
            ToManyRelationshipChange a1c1 = a1c.getToManyRelationshipChanges().get(Auditable1.CHILDREN1.getName());
            assertNotNull(a1c1);
            assertEquals(2, a1c1.getAdded().size());
            assertTrue(a1c1.getAdded().contains(ac2.getObjectId()));
            assertTrue(a1c1.getAdded().contains(ac3.getObjectId()));
            assertEquals(1, a1c1.getRemoved().size());
            assertTrue(a1c1.getRemoved().contains(ac1.getObjectId()));
            return null;
        }
    }).when(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
    a1.removeFromChildren1(ac1);
    a1.addToChildren1(ac2);
    a1.addToChildren1(ac3);
    context.commitChanges();
    verify(mockListener).onPostCommit(any(ObjectContext.class), any(ChangeMap.class));
}
Also used : AuditableChild1(org.apache.cayenne.commitlog.db.AuditableChild1) ChangeMap(org.apache.cayenne.commitlog.model.ChangeMap) ObjectChange(org.apache.cayenne.commitlog.model.ObjectChange) ObjectId(org.apache.cayenne.ObjectId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ToManyRelationshipChange(org.apache.cayenne.commitlog.model.ToManyRelationshipChange) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)3 ObjectId (org.apache.cayenne.ObjectId)3 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)3 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)3 ToManyRelationshipChange (org.apache.cayenne.commitlog.model.ToManyRelationshipChange)3 Test (org.junit.Test)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Auditable1 (org.apache.cayenne.commitlog.db.Auditable1)2 AuditableChild1 (org.apache.cayenne.commitlog.db.AuditableChild1)2 E3 (org.apache.cayenne.commitlog.db.E3)1 E4 (org.apache.cayenne.commitlog.db.E4)1