Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9efb331b98acdea659ca66bc823322f289e32cee (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 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.framework.skynet.core.artifact;

import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.core.operation.NullOperationLogger;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.database.core.ConnectionHandler;
import org.eclipse.osee.framework.database.operation.PurgeUnusedBackingDataAndTransactions;
import org.eclipse.osee.framework.skynet.core.mocks.Asserts;
import org.eclipse.osee.framework.skynet.core.mocks.DbTestUtil;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.skynet.core.util.FrameworkTestUtil;
import org.eclipse.osee.framework.skynet.core.utility.PurgeTransactionOperationWithListener;
import org.eclipse.osee.support.test.util.DemoSawBuilds;
import org.eclipse.osee.support.test.util.TestUtil;
import org.junit.BeforeClass;

/**
 * @author Ryan Schmitt
 */
public class PurgeTransactionTest {
   private IOseeBranch branch;
   Collection<Artifact> softArts;
   private SkynetTransaction createTransaction;
   private SkynetTransaction modifyTransaction;
   private int createId;
   private int modifyId;
   private Map<String, Integer> preCreateCount;
   private Map<String, Integer> preModifyCount;
   private Map<String, Integer> postModifyPurgeCount;
   private Map<String, Integer> postCreatePurgeCount;
   private static final List<String> tables = Arrays.asList("osee_attribute", "osee_artifact", "osee_relation_link",
      "osee_tx_details", "osee_txs");

   @BeforeClass
   public static void setUpOnce() throws Exception {
      Operations.executeWorkAndCheckStatus(new PurgeUnusedBackingDataAndTransactions(NullOperationLogger.getSingleton()));
   }

   @org.junit.Test
   public void testPurgeTransaction() throws Exception {
      init();

      createArtifacts();
      int initialTxCurrents = getCurrentRows();

      modifyArtifacts();
      purge(modifyId, postModifyPurgeCount);
      TestUtil.checkThatEqual(preModifyCount, postModifyPurgeCount);

      assertEquals("Purge Transaction did not correctly update tx_current.", initialTxCurrents, getCurrentRows());

      purge(createId, postCreatePurgeCount);
      TestUtil.checkThatEqual(preCreateCount, postCreatePurgeCount);
   }

   private void init() throws Exception {
      branch = DemoSawBuilds.SAW_Bld_2;
      preCreateCount = new HashMap<String, Integer>();
      preModifyCount = new HashMap<String, Integer>();
      postModifyPurgeCount = new HashMap<String, Integer>();
      postCreatePurgeCount = new HashMap<String, Integer>();
   }

   private void createArtifacts() throws Exception {
      DbTestUtil.getTableRowCounts(preCreateCount, tables);
      createTransaction = TransactionManager.createTransaction(branch, "Purge Transaction Test");
      softArts =
         FrameworkTestUtil.createSimpleArtifacts(CoreArtifactTypes.SoftwareRequirement, 10, getClass().getSimpleName(),
            branch);
      for (Artifact softArt : softArts) {
         softArt.persist(createTransaction);
      }
      createTransaction.execute();
      createId = createTransaction.getTransactionId();
   }

   private void modifyArtifacts() throws Exception {
      DbTestUtil.getTableRowCounts(preModifyCount, tables);
      modifyTransaction = TransactionManager.createTransaction(branch, "Purge Transaction Test");
      for (Artifact softArt : softArts) {
         softArt.addAttribute(CoreAttributeTypes.StaticId, getClass().getSimpleName());
         softArt.persist(modifyTransaction);
      }
      modifyTransaction.execute();
      modifyId = modifyTransaction.getTransactionId();
   }

   private void purge(int transactionId, Map<String, Integer> dbCount) throws Exception {
      IOperation operation = PurgeTransactionOperationWithListener.getPurgeTransactionOperation(transactionId);
      Asserts.testOperation(operation, IStatus.OK);
      Operations.executeWorkAndCheckStatus(new PurgeUnusedBackingDataAndTransactions(NullOperationLogger.getSingleton()));
      DbTestUtil.getTableRowCounts(dbCount, tables);
   }

   private int getCurrentRows() throws OseeCoreException {
      final String query = "select count(*) from osee_txs where transaction_id=? and tx_current=1";
      return ConnectionHandler.runPreparedQueryFetchInt(-1, query, createId);
   }
}

Back to the top