Search in sources :

Example 1 with DefaultDataDomainFlushAction

use of org.apache.cayenne.access.flush.DefaultDataDomainFlushAction in project cayenne by apache.

the class DefaultDataDomainFlushActionTest method mergeSameObjectIds.

@Test
public void mergeSameObjectIds() {
    ObjectId id1 = ObjectId.of("test2", "id", 1);
    ObjectId id2 = ObjectId.of("test", "id", 2);
    ObjectId id3 = ObjectId.of("test", "id", 2);
    ObjectId id4 = ObjectId.of("test", "id", 3);
    ObjectId id5 = ObjectId.of("test2", "id", 4);
    ObjectId id6 = ObjectId.of("test", "id", 5);
    ObjectId id7 = ObjectId.of("test", "id", 6);
    ObjectId id8 = ObjectId.of("test2", "id", 3);
    ObjectId id9 = ObjectId.of("test2", "id", 4);
    ObjectId id10 = ObjectId.of("test", "id", 6);
    DbEntity test = mockEntity("test");
    DbEntity test2 = mockEntity("test2");
    BaseDbRowOp[] op = new BaseDbRowOp[10];
    // +
    op[0] = new InsertDbRowOp(mockObject(id1), test2, id1);
    // -
    op[1] = new InsertDbRowOp(mockObject(id2), test, id2);
    // -
    op[2] = new DeleteDbRowOp(mockObject(id3), test, id3);
    // +
    op[3] = new UpdateDbRowOp(mockObject(id4), test, id4);
    // -
    op[4] = new InsertDbRowOp(mockObject(id5), test2, id5);
    // +
    op[5] = new DeleteDbRowOp(mockObject(id6), test, id6);
    // -
    op[6] = new InsertDbRowOp(mockObject(id7), test, id7);
    // +
    op[7] = new UpdateDbRowOp(mockObject(id8), test2, id8);
    // -
    op[8] = new DeleteDbRowOp(mockObject(id9), test2, id9);
    // -
    op[9] = new DeleteDbRowOp(mockObject(id10), test, id10);
    DefaultDataDomainFlushAction action = mock(DefaultDataDomainFlushAction.class);
    when(action.mergeSameObjectIds((List<DbRowOp>) any(List.class))).thenCallRealMethod();
    Collection<DbRowOp> merged = action.mergeSameObjectIds(new ArrayList<>(Arrays.asList(op)));
    assertEquals(7, merged.size());
    assertThat(merged, hasItems(op[0], op[3], op[5], op[7]));
    assertThat(merged, not(hasItem(sameInstance(op[1]))));
    assertThat(merged, not(hasItem(sameInstance(op[2]))));
    assertThat(merged, not(hasItem(sameInstance(op[4]))));
    assertThat(merged, not(hasItem(sameInstance(op[6]))));
    assertThat(merged, not(hasItem(sameInstance(op[8]))));
    assertThat(merged, not(hasItem(sameInstance(op[9]))));
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) ObjectId(org.apache.cayenne.ObjectId) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) DefaultDataDomainFlushAction(org.apache.cayenne.access.flush.DefaultDataDomainFlushAction) Test(org.junit.Test)

Example 2 with DefaultDataDomainFlushAction

use of org.apache.cayenne.access.flush.DefaultDataDomainFlushAction in project cayenne by apache.

the class DefaultDataDomainFlushActionTest method createQueries.

@Test
public void createQueries() {
    ObjectId id1 = ObjectId.of("test", "id", 1);
    ObjectId id2 = ObjectId.of("test", "id", 2);
    ObjectId id3 = ObjectId.of("test2", "id", 3);
    ObjectId id4 = ObjectId.of("test2", "id", 4);
    ObjectId id5 = ObjectId.of("test", "id", 5);
    ObjectId id6 = ObjectId.of("test2", "id", 6);
    ObjectId id7 = ObjectId.of("test", "id", 7);
    DbEntity test = mockEntity("test");
    DbEntity test2 = mockEntity("test2");
    List<DbRowOp> ops = new ArrayList<>();
    ops.add(new InsertDbRowOp(mockObject(id1), test, id1));
    ops.add(new InsertDbRowOp(mockObject(id2), test, id2));
    ops.add(new InsertDbRowOp(mockObject(id3), test2, id5));
    ops.add(new InsertDbRowOp(mockObject(id4), test2, id7));
    ops.add(new UpdateDbRowOp(mockObject(id5), test, id3));
    ops.add(new DeleteDbRowOp(mockObject(id6), test2, id6));
    ops.add(new DeleteDbRowOp(mockObject(id7), test, id4));
    DefaultDataDomainFlushAction action = mock(DefaultDataDomainFlushAction.class);
    when(action.createQueries((List<DbRowOp>) any(List.class))).thenCallRealMethod();
    List<? extends Query> queries = action.createQueries(ops);
    assertEquals(4, queries.size());
    assertThat(queries.get(0), instanceOf(InsertBatchQuery.class));
    InsertBatchQuery insert1 = (InsertBatchQuery) queries.get(0);
    assertSame(test, insert1.getDbEntity());
    assertEquals(2, insert1.getRows().size());
    assertThat(queries.get(1), instanceOf(InsertBatchQuery.class));
    InsertBatchQuery insert2 = (InsertBatchQuery) queries.get(1);
    assertSame(test2, insert2.getDbEntity());
    assertEquals(2, insert2.getRows().size());
    assertThat(queries.get(2), instanceOf(DeleteBatchQuery.class));
    DeleteBatchQuery delete1 = (DeleteBatchQuery) queries.get(2);
    assertSame(test2, delete1.getDbEntity());
    assertEquals(1, delete1.getRows().size());
    assertThat(queries.get(3), instanceOf(DeleteBatchQuery.class));
    DeleteBatchQuery delete2 = (DeleteBatchQuery) queries.get(3);
    assertSame(test, delete2.getDbEntity());
    assertEquals(1, delete2.getRows().size());
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) DbEntity(org.apache.cayenne.map.DbEntity) ObjectId(org.apache.cayenne.ObjectId) InsertBatchQuery(org.apache.cayenne.query.InsertBatchQuery) DbRowOp(org.apache.cayenne.access.flush.operation.DbRowOp) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) BaseDbRowOp(org.apache.cayenne.access.flush.operation.BaseDbRowOp) UpdateDbRowOp(org.apache.cayenne.access.flush.operation.UpdateDbRowOp) InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) DeleteBatchQuery(org.apache.cayenne.query.DeleteBatchQuery) ArrayList(java.util.ArrayList) DeleteDbRowOp(org.apache.cayenne.access.flush.operation.DeleteDbRowOp) DefaultDataDomainFlushAction(org.apache.cayenne.access.flush.DefaultDataDomainFlushAction) Test(org.junit.Test)

Aggregations

ObjectId (org.apache.cayenne.ObjectId)2 DefaultDataDomainFlushAction (org.apache.cayenne.access.flush.DefaultDataDomainFlushAction)2 BaseDbRowOp (org.apache.cayenne.access.flush.operation.BaseDbRowOp)2 DbRowOp (org.apache.cayenne.access.flush.operation.DbRowOp)2 DeleteDbRowOp (org.apache.cayenne.access.flush.operation.DeleteDbRowOp)2 InsertDbRowOp (org.apache.cayenne.access.flush.operation.InsertDbRowOp)2 UpdateDbRowOp (org.apache.cayenne.access.flush.operation.UpdateDbRowOp)2 DbEntity (org.apache.cayenne.map.DbEntity)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 DeleteBatchQuery (org.apache.cayenne.query.DeleteBatchQuery)1 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)1