Search in sources :

Example 1 with ToOneRelationshipChange

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

the class CommitLogFilter_AllIT method testPostCommit_UpdateToOne.

@Test
public void testPostCommit_UpdateToOne() throws SQLException {
    auditable1.insert(1, "xx");
    auditable1.insert(2, "yy");
    auditableChild1.insert(1, 1, "cc1");
    auditableChild1.insert(2, 2, "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);
    final Auditable1 a2 = SelectById.query(Auditable1.class, 2).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 ac1c = changes.getChanges().get(new ObjectId("AuditableChild1", AuditableChild1.ID_PK_COLUMN, 1));
            assertNotNull(ac1c);
            assertEquals(ObjectChangeType.UPDATE, ac1c.getType());
            ToOneRelationshipChange ac1c1 = ac1c.getToOneRelationshipChanges().get(AuditableChild1.PARENT.getName());
            assertEquals(a1.getObjectId(), ac1c1.getOldValue());
            assertEquals(null, ac1c1.getNewValue());
            ObjectChange ac2c = changes.getChanges().get(new ObjectId("AuditableChild1", AuditableChild1.ID_PK_COLUMN, 2));
            assertNotNull(ac2c);
            assertEquals(ObjectChangeType.UPDATE, ac2c.getType());
            ToOneRelationshipChange ac2c1 = ac2c.getToOneRelationshipChanges().get(AuditableChild1.PARENT.getName());
            assertEquals(a2.getObjectId(), ac2c1.getOldValue());
            assertEquals(a1.getObjectId(), ac2c1.getNewValue());
            ObjectChange ac3c = changes.getChanges().get(new ObjectId("AuditableChild1", AuditableChild1.ID_PK_COLUMN, 3));
            assertNotNull(ac3c);
            assertEquals(ObjectChangeType.UPDATE, ac3c.getType());
            ToOneRelationshipChange ac3c1 = ac3c.getToOneRelationshipChanges().get(AuditableChild1.PARENT.getName());
            assertEquals(null, ac3c1.getOldValue());
            assertEquals(a1.getObjectId(), ac3c1.getNewValue());
            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) ToOneRelationshipChange(org.apache.cayenne.commitlog.model.ToOneRelationshipChange) Auditable1(org.apache.cayenne.commitlog.db.Auditable1) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 ObjectId (org.apache.cayenne.ObjectId)1 Auditable1 (org.apache.cayenne.commitlog.db.Auditable1)1 AuditableChild1 (org.apache.cayenne.commitlog.db.AuditableChild1)1 ChangeMap (org.apache.cayenne.commitlog.model.ChangeMap)1 ObjectChange (org.apache.cayenne.commitlog.model.ObjectChange)1 ToOneRelationshipChange (org.apache.cayenne.commitlog.model.ToOneRelationshipChange)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1