Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8350db194e2e6e3dd315d7105a1fee1180f3dbbd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * Copyright (c) 2012 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.orcs.db.internal.transaction;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.osee.framework.database.IOseeDatabaseService;
import org.eclipse.osee.framework.database.core.IOseeStatement;
import org.eclipse.osee.framework.database.core.OseeConnection;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.core.ds.OrcsChangeSet;
import org.eclipse.osee.orcs.data.TransactionReadable;
import org.eclipse.osee.orcs.db.internal.sql.join.IdJoinQuery;
import org.eclipse.osee.orcs.db.internal.transaction.TransactionWriter.SqlOrderEnum;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

/**
 * Test Case for {@link TransactionWriter}
 * 
 * @author Roberto E. Escobar
 */
public class TransactionWriterTest {

   private static final long BRANCH_ID = 65L;

   private static final int QUERY_ID_1 = 88;
   private static final int QUERY_ID_2 = 89;

   private static final int TX_1 = 51;
   private static final int TX_2 = 52;

   private static final long GAMMA_1 = 80000L;
   private static final long GAMMA_2 = 80001L;

   //@formatter:off
   @Mock private Log logger;
   @Mock private IOseeDatabaseService dbService;
   
   
   @Mock private TxSqlBuilder builder;
   @Mock private OseeConnection connection;
   @Mock private TransactionReadable tx;
   @Mock private DaoToSql dao1;
   @Mock private DaoToSql dao2;
   
   @Mock private IdJoinQuery join1;
   @Mock private IdJoinQuery join2;
   
   @Mock private IOseeStatement chStmt;
   @Captor private ArgumentCaptor<List<Object[]>> paramCaptor;
   @Mock  private OrcsChangeSet changeSet;
   //@formatter:on

   private TransactionWriter writer;
   private List<DaoToSql> stores;

   @Before
   public void setUp() throws OseeCoreException {
      MockitoAnnotations.initMocks(this);

      writer = new TransactionWriter(logger, dbService, builder);

      stores = Arrays.asList(dao1, dao2);

      final Map<SqlOrderEnum, IdJoinQuery> joins = new LinkedHashMap<SqlOrderEnum, IdJoinQuery>();
      joins.put(SqlOrderEnum.ARTIFACTS, join1);
      joins.put(SqlOrderEnum.ATTRIBUTES, join2);

      when(join1.getQueryId()).thenReturn(QUERY_ID_1);
      when(join2.getQueryId()).thenReturn(QUERY_ID_2);

      when(tx.getBranchId()).thenReturn(BRANCH_ID);
      when(builder.getBinaryStores()).thenReturn(stores);
      when(builder.getTxNotCurrents()).thenAnswer(new Answer<Set<Entry<SqlOrderEnum, IdJoinQuery>>>() {

         @Override
         public Set<Entry<SqlOrderEnum, IdJoinQuery>> answer(InvocationOnMock invocation) throws Throwable {
            Set<Entry<SqlOrderEnum, IdJoinQuery>> values = joins.entrySet();
            return values;
         }
      });
      when(dbService.getStatement(connection)).thenReturn(chStmt);

      when(chStmt.next()).thenReturn(true).thenReturn(true).thenReturn(false);
      when(chStmt.getInt("transaction_id")).thenReturn(TX_1).thenReturn(TX_2);
      when(chStmt.getLong("gamma_id")).thenReturn(GAMMA_1).thenReturn(GAMMA_2);
   }

   @Test
   public void testRollback() throws OseeCoreException {
      TransactionWriter spy = Mockito.spy(writer);

      OseeCoreException expected = new OseeCoreException("Testing");

      when(spy.getBinaryStores()).thenReturn(stores);
      doThrow(expected).when(dao1).rollBack();

      spy.rollback();

      verify(dao1).rollBack();
      verify(dao2).rollBack();

      verify(logger).error(expected, "Error during binary rollback [%s]", dao1);
   }

   @Test
   public void testWrite() throws OseeCoreException {
      InOrder inOrder = inOrder(builder, tx, join1, join2, dao1, dao2, dbService, chStmt);

      writer.write(connection, tx, changeSet);

      inOrder.verify(builder).accept(tx, changeSet);
      inOrder.verify(builder).getBinaryStores();
      inOrder.verify(dao1).persist();
      inOrder.verify(dao2).persist();

      inOrder.verify(builder).getTxNotCurrents();

      inOrder.verify(join1).store();
      inOrder.verify(chStmt).runPreparedQuery(SqlOrderEnum.ARTIFACTS.getTxsNotCurrentQuery(), QUERY_ID_1, BRANCH_ID);
      inOrder.verify(join1).delete();

      inOrder.verify(join2).store();
      inOrder.verify(chStmt).runPreparedQuery(SqlOrderEnum.ATTRIBUTES.getTxsNotCurrentQuery(), QUERY_ID_2, BRANCH_ID);
      inOrder.verify(join2).delete();

      inOrder.verify(builder).getInsertData(SqlOrderEnum.ARTIFACTS);
      inOrder.verify(builder).getInsertData(SqlOrderEnum.ATTRIBUTES);
      inOrder.verify(builder).getInsertData(SqlOrderEnum.RELATIONS);
      inOrder.verify(builder).getInsertData(SqlOrderEnum.TXS_DETAIL);
      inOrder.verify(builder).getInsertData(SqlOrderEnum.TXS);

      inOrder.verify(dbService).runBatchUpdate(eq(connection), eq(TransactionWriter.UPDATE_TXS_NOT_CURRENT),
         paramCaptor.capture());

      inOrder.verify(builder).clear();

      Iterator<Object[]> params = paramCaptor.getValue().iterator();
      int index = 0;
      Object[] data = params.next();
      Assert.assertEquals(BRANCH_ID, data[index++]);
      Assert.assertEquals(TX_1, data[index++]);
      Assert.assertEquals(GAMMA_1, data[index++]);

      index = 0;
      data = params.next();
      Assert.assertEquals(BRANCH_ID, data[index++]);
      Assert.assertEquals(TX_2, data[index++]);
      Assert.assertEquals(GAMMA_2, data[index++]);
   }
}

Back to the top