Skip to main content
summaryrefslogtreecommitdiffstats
blob: 394310f1607b36fd1c9256ba83b0c84f11f0d107 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*******************************************************************************
 * 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.client.integration.tests.integration.skynet.core;

import static org.eclipse.osee.client.demo.DemoChoice.OSEE_CLIENT_DEMO;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Collection;
import java.util.LinkedList;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.osee.client.integration.tests.integration.skynet.core.utils.Asserts;
import org.eclipse.osee.client.integration.tests.integration.skynet.core.utils.ConflictTestManager;
import org.eclipse.osee.client.test.framework.OseeClientIntegrationRule;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.enums.TxChange;
import org.eclipse.osee.framework.core.exception.OseeDataStoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.IOperation;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.logging.SevereLoggingMonitor;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.relation.RelationLink;
import org.eclipse.osee.framework.skynet.core.utility.ConnectionHandler;
import org.eclipse.osee.framework.skynet.core.utility.PurgeTransactionOperationWithListener;
import org.eclipse.osee.jdbc.JdbcStatement;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;

/**
 * @author Theron Virgin
 */
@Ignore("This test does not work")
public class ConflictDeletionTest {

   @Rule
   public OseeClientIntegrationRule integration = new OseeClientIntegrationRule(OSEE_CLIENT_DEMO);

   private static final String CHECK_FOR_ZERO_TX_CURRENT =
      "SELECT txs.tx_current, txs.transaction_id FROM osee_txs txs, osee_artifact art WHERE txs.branch_id = ? AND txs.transaction_id < ? AND txs.tx_current != 0 AND txs.gamma_id = art.gamma_id and art.art_id = ?";
   private static final String CHECK_FOR_DELETED_TX_CURRENT =
      "SELECT txs.tx_current, txs.transaction_id FROM osee_txs txs, osee_artifact art WHERE txs.branch_id = ? AND txs.tx_current = 2 AND txs.gamma_id = art.gamma_id and art.art_id = ?";

   private static final String CHECK_FOR_ZERO_TX_CURRENT_ATTRIBUTE =
      "SELECT txs.tx_current, txs.transaction_id FROM osee_txs txs, osee_attribute att WHERE txs.branch_id = ? AND txs.transaction_id < ? AND txs.tx_current != 0 AND txs.gamma_id = att.gamma_id and att.attr_id = ?";
   private static final String CHECK_FOR_DELETED_TX_CURRENT_ATTRIBUTE =
      "SELECT txs.tx_current, txs.transaction_id FROM osee_txs txs, osee_attribute att WHERE txs.branch_id = ? AND txs.tx_current = ? AND txs.gamma_id = att.gamma_id and att.attr_id = ?";

   private static final String CHECK_FOR_ZERO_TX_CURRENT_RELATION =
      "SELECT txs.tx_current, txs.transaction_id FROM osee_txs txs, osee_relation_link rel WHERE txs.branch_id = ? AND txs.transaction_id < ? AND txs.tx_current != 0 AND txs.gamma_id = rel.gamma_id and rel.rel_link_id = ?";
   private static final String CHECK_FOR_DELETED_TX_CURRENT_RELATION =
      "SELECT txs.tx_current, txs.transaction_id FROM osee_txs txs, osee_relation_link rel WHERE txs.branch_id = ? AND txs.tx_current = ? AND txs.gamma_id = rel.gamma_id and rel.rel_link_id = ?";

   private static final String GET_DELETED_TRANSACTION = "SELECT * FROM osee_txs WHERE transaction_id = ?";

   private static final String GET_ARTIFACT_DEBUG =
      "select txs.branch_id, txs.transaction_id, txs.tx_current, txs.mod_type, txs.gamma_id, art.art_id FROM osee_txs txs, osee_artifact art WHERE txs.branch_id = ? AND txs.gamma_id = art.gamma_id AND art.art_id = ?";

   private static final String GET_ATTRIBUTE_DEBUG =
      "select txs.branch_id, txs.transaction_id, txs.tx_current, txs.mod_type, txs.gamma_id, att.art_id, att.attr_id FROM osee_txs txs, osee_attribute att WHERE txs.branch_id = ? AND txs.gamma_id = att.gamma_id AND att.attr_id = ?";

   private static final String GET_RELATION_DEBUG =
      "select txs.branch_id, txs.transaction_id, txs.tx_current, txs.mod_type, txs.gamma_id, rel.rel_link_id, rel.a_art_id, rel.b_art_id FROM osee_txs txs, osee_relation_link rel WHERE txs.branch_id = ? AND txs.gamma_id = rel.gamma_id AND rel.rel_link_id = ?";

   private static final boolean DEBUG = false;
   private static final boolean DELETE_TRANSACTION_TEST = true;
   private static final boolean INDIVIDUAL_DELETE_TEST = true;

   /**
    * Test method for
    * {@link org.eclipse.osee.framework.skynet.core.artifact.BranchManager#getMergeBranch(Branch, Branch)} .
    */
   @Test
   public void deleteAndCheckTXCurrents() throws OseeCoreException {
      SevereLoggingMonitor monitorLog = new SevereLoggingMonitor();
      OseeLog.registerLoggerListener(monitorLog);
      Collection<Artifact> artifacts = ConflictTestManager.getArtifacts(true, ConflictTestManager.DELETION_TEST_QUERY);
      Collection<Artifact> artifactsToCheck = new LinkedList<Artifact>();
      int deletionTransaction = 0;
      for (Artifact artifact : artifacts) {
         artifactsToCheck.add(artifact);
         artifactsToCheck.addAll(artifact.getDescendants());
      }
      if (DEBUG) {
         System.err.println("Initial Status artifacts");
         for (Artifact artifact : artifactsToCheck) {
            dumpArtifact(artifact);
            for (Attribute<?> attribute : artifact.getAttributes()) {
               dumpAttribute(attribute);
            }
            for (RelationLink relation : artifact.getRelationsAll(DeletionFlag.INCLUDE_DELETED)) {
               dumpRelation(relation, artifact);
            }
         }
         System.err.println("Deleting the first set of artifacts");
      }
      for (Artifact artifact : artifacts) {
         artifact.deleteAndPersist();
         if (DEBUG) {
            System.err.println("Deleting Artifact " + artifact.getArtId());
         }
      }

      JdbcStatement chStmt = ConnectionHandler.getStatement();
      //Let's check both through the API and the SQL to make sure the Artifact is internally deleted
      //and deleted in the Database, That we don't get some bad data case.

      //Check that artifacts are deleted
      for (Artifact artifact : artifactsToCheck) {
         deletionTransaction = artifact.getTransactionNumber();
         assertTrue("Artifact " + artifact.getArtId() + " should be deleted, but isn't", artifact.isDeleted());
         //Now Check Artifact in the DB tx_currents etc

         if (DEBUG) {
            dumpArtifact(artifact);
         } else {
            try {
               chStmt.runPreparedQuery(CHECK_FOR_ZERO_TX_CURRENT, artifact.getFullBranch().getUuid(),
                  artifact.getTransactionNumber(), artifact.getArtId());
               if (chStmt.next()) {
                  fail("Artifact " + artifact.getArtId() + " old Transaction < " + artifact.getTransactionNumber() + "  is set to " + chStmt.getInt("tx_current") + " , should be 0 on branch " + artifact.getBranch().getGuid());
               }
            } finally {
               chStmt.close();
            }
            try {
               chStmt.runPreparedQuery(CHECK_FOR_DELETED_TX_CURRENT, artifact.getFullBranch().getUuid(),
                  artifact.getArtId());
               if (chStmt.next()) {
                  assertTrue(
                     "Artifact " + artifact.getArtId() + " Transaction: " + artifact.getTransactionNumber() + " should be 3 on branch " + artifact.getBranch().getGuid(),
                     chStmt.getInt("transaction_id") == artifact.getTransactionNumber());
               } else {
                  fail("Artifact " + artifact.getArtId() + " was not given a tx_current value of 2 when it was deleted on branch " + artifact.getBranch().getGuid() + " on transaction " + artifact.getTransactionNumber());
               }
            } finally {
               chStmt.close();
            }
         }

         //Check that attributes are Artifact Deleted
         for (Attribute<?> attribute : artifact.getAttributes(true)) {
            if (DEBUG) {
               dumpAttribute(attribute);
            } else {
               checkAttribute(artifact, attribute, TxChange.ARTIFACT_DELETED.getValue());
            }
         }
         //Check that relations are deleted.
         for (RelationLink relation : artifact.getRelationsAll(DeletionFlag.INCLUDE_DELETED)) {
            if (DEBUG) {
               dumpRelation(relation, artifact);
            } else {
               checkRelation(artifact, relation, TxChange.ARTIFACT_DELETED.getValue());
            }
         }
      }

      //OK now lets delete the transaction and check for the same thing

      if (DELETE_TRANSACTION_TEST) {
         IOperation operation = PurgeTransactionOperationWithListener.getPurgeTransactionOperation(deletionTransaction);
         Asserts.assertOperation(operation, IStatus.OK);
         if (DEBUG) {
            System.err.println("Deleting the Transaction");
         }
         //This is only a DB deletion so it won't be reflected in the
         for (Artifact artifact : artifactsToCheck) {
            if (DEBUG) {
               dumpArtifact(artifact);
            } else {
               try {
                  chStmt.runPreparedQuery(CHECK_FOR_ZERO_TX_CURRENT, artifact.getFullBranch().getUuid(),
                     deletionTransaction, artifact.getArtId());
                  if (chStmt.next()) {
                     if (deletionTransaction == chStmt.getInt("transaction_id")) {
                        fail("Artifact " + artifact.getArtId() + " tx_current set on  " + chStmt.getInt("transaction_id") + " when it should be < " + deletionTransaction + " on branch " + artifact.getBranch().getGuid());
                     }
                     if (chStmt.next()) {
                        fail("Artifact " + artifact.getArtId() + " has multiple tx_current set on " + artifact.getBranch().getGuid());
                     }
                  } else {
                     fail("Artifact " + artifact.getArtId() + " has no tx_current set on " + artifact.getBranch().getGuid());
                  }
               } finally {
                  chStmt.close();
               }
            }

            //Check that attributes are Artifact Deleted
            for (Attribute<?> attribute : artifact.getAttributes(true)) {
               if (DEBUG) {
                  dumpAttribute(attribute);
               } else {
                  try {
                     chStmt.runPreparedQuery(CHECK_FOR_ZERO_TX_CURRENT_ATTRIBUTE, artifact.getFullBranch().getUuid(),
                        deletionTransaction, attribute.getId());
                     if (chStmt.next()) {
                        if (deletionTransaction == chStmt.getInt("transaction_id")) {
                           fail("Attribute " + attribute.getId() + " tx_current set on  " + chStmt.getInt("transaction_id") + " when it should be < " + deletionTransaction + " on branch " + artifact.getBranch().getGuid());
                        }
                        if (chStmt.next()) {
                           fail("Attribute " + attribute.getId() + " has multiple tx_current set on " + artifact.getBranch().getGuid());
                        }
                     } else {
                        fail("Attribute " + attribute.getId() + " has no tx_current set on " + artifact.getBranch().getGuid());
                     }
                  } finally {
                     chStmt.close();
                  }
               }
            }
            for (RelationLink relation : artifact.getRelationsAll(DeletionFlag.INCLUDE_DELETED)) {
               if (DEBUG) {
                  dumpRelation(relation, artifact);
               } else {
                  try {
                     chStmt.runPreparedQuery(CHECK_FOR_ZERO_TX_CURRENT_RELATION, artifact.getFullBranch().getUuid(),
                        deletionTransaction, relation.getId());
                     if (chStmt.next()) {
                        if (deletionTransaction == chStmt.getInt("transaction_id")) {
                           fail("Relation " + relation.getId() + " tx_current set on  " + chStmt.getInt("transaction_id") + " when it should be < " + deletionTransaction + " on branch " + artifact.getBranch().getGuid());
                        }
                        if (chStmt.next()) {
                           fail("Relation " + relation.getId() + " has multiple tx_current set on " + artifact.getBranch().getGuid());
                        }
                     } else {
                        fail("Relation " + relation.getId() + " has no tx_current set on " + artifact.getBranch().getGuid());
                     }
                  } finally {
                     chStmt.close();
                  }
               }
            }

         }

         try {
            chStmt.runPreparedQuery(GET_DELETED_TRANSACTION, deletionTransaction);
            assertTrue(
               "Trancsaction " + deletionTransaction + " should be deleted and should not be found in the database",
               !chStmt.next());
         } finally {
            chStmt.close();
         }
      }
      if (INDIVIDUAL_DELETE_TEST) {

         //Check deleting an attribute and deleting a relation directly create the desired effect.

         if (ConflictTestManager.getArtifacts(true, ConflictTestManager.DELETION_ATTRIBUTE_TEST_QUERY).size() > 0) {

            Artifact artifactForDeletionCheck =
               ConflictTestManager.getArtifacts(true, ConflictTestManager.DELETION_ATTRIBUTE_TEST_QUERY).get(0);

            if (artifactForDeletionCheck != null) {
               Attribute<?> attribute = artifactForDeletionCheck.getAttributes().get(0);
               RelationLink relation =
                  artifactForDeletionCheck.getRelations(CoreRelationTypes.Default_Hierarchical__Child).get(0);
               attribute.delete();
               relation.delete(true);
               artifactForDeletionCheck.persist(getClass().getSimpleName());
               //check for internal deletions and then check the database

               assertTrue("Attribute " + attribute.getId() + " should be deleted but isn't", attribute.isDeleted());
               assertTrue("Relation " + relation.getId() + " should be deleted but isn't", relation.isDeleted());

               checkAttribute(artifactForDeletionCheck, attribute, TxChange.DELETED.getValue());
               checkRelation(artifactForDeletionCheck, relation, TxChange.DELETED.getValue());
            }

         }
      }

      assertTrue(String.format("%d SevereLogs during test.", monitorLog.getAllLogs().size()),
         monitorLog.getAllLogs().isEmpty());
      if (DEBUG) {
         fail("Deletion Test was run with tracing enabled to prevent stopping at a failure so no conditions were checked.");
      }
      if (!DELETE_TRANSACTION_TEST) {
         fail("The Delete Transaction Test was not run. Check the flag");
      }
      if (!INDIVIDUAL_DELETE_TEST) {
         fail("The Individual Deletion Test was not run. Check the flag");
      }
   }

   private void checkAttribute(Artifact artifact, Attribute<?> attribute, int value) throws OseeCoreException {
      JdbcStatement chStmt = ConnectionHandler.getStatement();
      try {
         chStmt.runPreparedQuery(CHECK_FOR_ZERO_TX_CURRENT_ATTRIBUTE, artifact.getFullBranch().getUuid(),
            artifact.getTransactionNumber(), attribute.getId());
         if (chStmt.next()) {
            fail("Attribute " + attribute.getId() + " old Transaction < : " + artifact.getTransactionNumber() + "  is set to " + chStmt.getInt("tx_current") + " , should be 0 on branch " + artifact.getBranch().getGuid());
         }
      } finally {
         chStmt.close();
      }
      try {
         chStmt.runPreparedQuery(CHECK_FOR_DELETED_TX_CURRENT_ATTRIBUTE, artifact.getFullBranch().getUuid(), value,
            attribute.getId());
         if (chStmt.next()) {
            assertTrue(
               "Attribute " + attribute.getId() + " Transaction: " + artifact.getTransactionNumber() + " should be 3 on branch " + artifact.getBranch().getGuid(),
               chStmt.getInt("transaction_id") == artifact.getTransactionNumber());
         } else {
            fail("Attribute " + attribute.getId() + " was not given a tx_current value of 3 when it was deleted on branch " + artifact.getBranch().getGuid());
         }
      } finally {
         chStmt.close();
      }
   }

   public void checkRelation(Artifact artifact, RelationLink relation, int value) throws OseeCoreException {
      JdbcStatement chStmt = ConnectionHandler.getStatement();
      assertTrue(
         "Relation should be deleted between Parent: " + relation.getAArtifactId() + " and child " + relation.getBArtifactId(),
         relation.isDeleted());
      try {
         chStmt.runPreparedQuery(CHECK_FOR_ZERO_TX_CURRENT_RELATION, artifact.getFullBranch().getUuid(),
            artifact.getTransactionNumber(), relation.getId());
         if (chStmt.next()) {
            fail("Relation " + relation.getId() + " old Transaction < : " + artifact.getTransactionNumber() + "  is set to " + chStmt.getInt("tx_current") + " , should be 0 on branch " + artifact.getBranch().getGuid());
         }
      } finally {
         chStmt.close();
      }
      try {
         chStmt.runPreparedQuery(CHECK_FOR_DELETED_TX_CURRENT_RELATION, artifact.getBranch().getGuid(), value,
            relation.getId());
         if (chStmt.next()) {
            assertTrue(
               "Relation " + relation.getId() + " Transaction: " + artifact.getTransactionNumber() + " should be " + value + " on branch " + artifact.getBranch().getGuid(),
               chStmt.getInt("transaction_id") >= artifact.getTransactionNumber());
            if (chStmt.next()) {
               fail("Relation " + relation.getId() + " has multiple tx_current values of " + value + " when it was deleted on branch " + artifact.getBranch().getGuid() + " on transaction " + artifact.getTransactionNumber());
            }
         } else {
            fail("Relation " + relation.getId() + " was not given a tx_current value of " + value + " when it was deleted on branch " + artifact.getBranch().getGuid() + " on transaction " + artifact.getTransactionNumber());
         }
      } finally {
         chStmt.close();
      }
   }

   public static void dumpArtifact(Artifact artifact) throws OseeCoreException {
      JdbcStatement chStmt = ConnectionHandler.getStatement();
      try {
         if (DEBUG) {
            System.out.println("  Artifact Dump : " + artifact.getName());
            chStmt.runPreparedQuery(GET_ARTIFACT_DEBUG, artifact.getFullBranch().getUuid(), artifact.getArtId());
            while (chStmt.next()) {
               System.out.println(String.format(
                  "      Art Id = %d  Branch Uuid = %d TX_Current = %d mod_type = %d Transaction_id = %d Gamma_id = %d",
                  chStmt.getInt("art_id"), chStmt.getLong("branch_id"), chStmt.getInt("tx_current"),
                  chStmt.getInt("mod_type"), chStmt.getInt("transaction_id"), chStmt.getInt("gamma_id")));
            }
         }
      } finally {
         chStmt.close();
      }
   }

   public static void dumpAttribute(Attribute<?> attribute) throws OseeDataStoreException, OseeCoreException {
      JdbcStatement chStmt = ConnectionHandler.getStatement();
      try {
         if (DEBUG) {
            System.out.println("  Attribute Dump");
            chStmt.runPreparedQuery(GET_ATTRIBUTE_DEBUG, attribute.getArtifact().getFullBranch().getUuid(),
               attribute.getId());
            while (chStmt.next()) {
               System.out.println(String.format(
                  "        Attribute Id = %d  Art_id = %d Branch Uuid = %d TX_Current = %d mod_type = %d Transaction_id = %d Gamma_id = %d",
                  chStmt.getInt("attr_id"), chStmt.getInt("art_id"), chStmt.getLong("branch_id"),
                  chStmt.getInt("tx_current"), chStmt.getInt("mod_type"), chStmt.getInt("transaction_id"),
                  chStmt.getInt("gamma_id")));
            }
         }
      } finally {
         chStmt.close();
      }
   }

   public static void dumpRelation(RelationLink relation, Artifact artifact) throws OseeCoreException {
      JdbcStatement chStmt = ConnectionHandler.getStatement();
      try {
         if (DEBUG) {
            System.out.println("  Relation Dump");
            chStmt.runPreparedQuery(GET_RELATION_DEBUG, artifact.getFullBranch().getUuid(), relation.getId());
            while (chStmt.next()) {
               System.out.println(String.format(
                  "        Relation Id = %d  a_art_id = %d b_art_id = %d Branch Uuid = %d TX_Current = %d mod_type = %d Transaction_id = %d Gamma_id = %d",
                  chStmt.getInt("rel_link_id"), chStmt.getInt("a_art_id"), chStmt.getInt("b_art_id"),
                  chStmt.getLong("branch_id"), chStmt.getInt("tx_current"), chStmt.getInt("mod_type"),
                  chStmt.getInt("transaction_id"), chStmt.getInt("gamma_id")));
            }
         }
      } finally {
         chStmt.close();
      }
   }
}

Back to the top