Skip to main content
summaryrefslogtreecommitdiffstats
blob: 482975abd927c54d63a52ad883e908ab53c2b734 (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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/*******************************************************************************
 * 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.client.integration.tests.integration.ui.skynet;

import static org.eclipse.osee.client.demo.DemoChoice.OSEE_CLIENT_DEMO;
import static org.eclipse.osee.framework.core.enums.DemoBranches.SAW_Bld_1;
import static org.eclipse.osee.framework.core.enums.RelationSorter.LEXICOGRAPHICAL_ASC;
import static org.eclipse.osee.framework.core.enums.RelationSorter.LEXICOGRAPHICAL_DESC;
import static org.eclipse.osee.framework.core.enums.RelationSorter.USER_DEFINED;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.osee.client.test.framework.OseeClientIntegrationRule;
import org.eclipse.osee.client.test.framework.OseeLogMonitorRule;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.RelationTypeSide;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.enums.RelationSorter;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.Attribute;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.change.Change;
import org.eclipse.osee.framework.skynet.core.httpRequests.PurgeBranchHttpRequestOperation;
import org.eclipse.osee.framework.skynet.core.relation.RelationManager;
import org.eclipse.osee.framework.skynet.core.revision.ChangeManager;
import org.eclipse.osee.framework.skynet.core.revision.LoadChangeType;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.eclipse.osee.framework.ui.skynet.blam.operation.ReplaceArtifactWithBaselineOperation;
import org.eclipse.osee.framework.ui.skynet.blam.operation.ReplaceAttributeWithBaselineOperation;
import org.eclipse.osee.framework.ui.skynet.update.InterArtifactExplorerDropHandlerOperation;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * <pre>
 *                   New   |  Deleted  | Modified  |  Moved |  Introduced | RelationOrderAttr
 *                 -------------------------------------------------------------------------
 *     Artifact   |  0           1            3                  2
 *     Attribute  |  4           5            6
 *     Relation   |  7           8                     9*                        **
 *                 -------------------------------------------------------------------------
 *
 *
 *    Legend:
 *    * -   artifact will be left with a DELETED attribute change and a SYNTHETIC
 *          modified artifact change. CASE_10_EXPECTED. i.e.:
 *
 *          A           C-
 *          `- B   ->   `- B        B is moved to C
 *                                  C gets a new user defined order attribute
 *
 *                                  Revert on B causes A to have a new relation order attribute because
 *                                  B was the only child of A and when the link was deleted the relation order
 *                                  attribute was also deleted. (link.deleteEmptyRelationOrder()) After the B
 *                                  link is added back from the revert of B. A will get a new relation order attribute.
 *
 *    ** - creates a parent artifact A on the baseline branch and adds 5 children. On the working branch
 *                                  the same artifact A gets additional 5 children. The revert is called A.
 *                                  Which will set the parent (A) back to the baseline state but leaves
 *                                  the new artifacts orphaned on the working branch.
 *
 *    Case 10 is a collection of 1-9, all cases.
 * </pre>
 */
@RunWith(Parameterized.class)
public final class ReplaceWithBaselineTest {

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

   @Rule
   public OseeLogMonitorRule monitorRule = new OseeLogMonitorRule();

   private static final RelationSorter ascOrder = LEXICOGRAPHICAL_ASC;
   private static final RelationSorter descOrder = LEXICOGRAPHICAL_DESC;

   /**
    * Refer to table for Case 10*
    */
   private static final int CASE_10_EXPECTED = 2;
   private static final int CASE_0_EXPECTED = 10;
   private static final IArtifactType DOC = CoreArtifactTypes.GeneralDocument;
   private static final IProgressMonitor MONITOR = new NullProgressMonitor();

   //@formatter:off
   private static enum Item { ARTIFACT, ATTRBUTE, RELATION, };
   private static enum ChangeItem { NEW, DELETED, MODIFIED, MOVED, INTRODUCED, RELATION_ORDER_ATTR };
   //@formatter:on

   private BranchId workingBranch;
   private BranchId baselineBranch;

   private final String testName;
   private final int expectedChangesLeft;
   private final List<TestData> testDatas;

   public ReplaceWithBaselineTest(String testCaseName, List<TestData> testDatas, int expectedChangesLeft) {
      this.testName = String.format("%s : %s", ReplaceWithBaselineTest.class.getSimpleName(), testCaseName);
      this.testDatas = testDatas;
      this.expectedChangesLeft = expectedChangesLeft;
   }

   @After
   public void tearDown() throws Exception {
      Operations.executeWorkAndCheckStatus(new PurgeBranchHttpRequestOperation(workingBranch, true));
   }

   @Test
   public void testReplaceWithBaselineVersion() throws Exception {
      runTest();
      Collection<Change> changes = getBranchChanges(workingBranch);
      switch (expectedChangesLeft) {
         case CASE_10_EXPECTED:
            Assert.assertTrue(changes.size() == CASE_10_EXPECTED);
            break;
         case CASE_0_EXPECTED:
            Assert.assertTrue(changes.size() == CASE_0_EXPECTED);
            break;
         case CASE_0_EXPECTED + CASE_10_EXPECTED:
            Assert.assertTrue(changes.size() == CASE_0_EXPECTED + CASE_10_EXPECTED);
            break;
         default:
            Assert.assertTrue(changes.isEmpty());
            break;
      }
   }

   @Parameters
   public static List<Object[]> getData() {

      List<Object[]> data = new LinkedList<>();

      data.add(new Object[] {"Case 0", Arrays.asList(new TestData(Item.ARTIFACT, ChangeItem.NEW, false)), 0});
      data.add(new Object[] {"Case 1", Arrays.asList(new TestData(Item.ARTIFACT, ChangeItem.DELETED, true)), 0});
      data.add(new Object[] {"Case 2", Arrays.asList(new TestData(Item.ARTIFACT, ChangeItem.INTRODUCED, false)), 0});
      data.add(new Object[] {"Case 3", Arrays.asList(new TestData(Item.ARTIFACT, ChangeItem.MODIFIED, true)), 0});

      data.add(new Object[] {"Case 4", Arrays.asList(new TestData(Item.ATTRBUTE, ChangeItem.NEW, true)), 0});
      data.add(new Object[] {"Case 5", Arrays.asList(new TestData(Item.ATTRBUTE, ChangeItem.DELETED, true)), 0});
      data.add(new Object[] {"Case 6", Arrays.asList(new TestData(Item.ATTRBUTE, ChangeItem.MODIFIED, true)), 0});

      List<TestData> combinedCases = new ArrayList<>(data.size());

      for (Object[] objects : data) {
         @SuppressWarnings("unchecked")
         List<TestData> caseTestDatas = (List<TestData>) objects[1];
         combinedCases.addAll(caseTestDatas);
      }
      data.add(new Object[] {"Case 7", combinedCases, 0});
      Collections.reverse(combinedCases);
      data.add(new Object[] {"Case 8", combinedCases, 0});

      return data;
   }

   /**
    * Sets up the test based on <code>ReplaceWithBaselineTest.testItem</code> and performs a revert.
    */
   private void runTest() throws Exception {
      setupBackgroundForScenario();

      makeChange(testDatas);

      Collection<Change> changesAfterRevert = getBranchChanges(workingBranch);

      List<Artifact> artifactsToRevert = new ArrayList<>();
      Artifact artifactToRevert;
      for (TestData testData : testDatas) {
         artifactToRevert =
            ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch, DeletionFlag.INCLUDE_DELETED);

         switch (testData.item) {
            case ARTIFACT:
               artifactsToRevert.add(artifactToRevert);
               break;

            case ATTRBUTE:
               revertAttributes(changesAfterRevert, artifactToRevert.getAttributeById(testData.getAttrId(), true));
               break;

            case RELATION:
               artifactsToRevert.add(artifactToRevert);
               break;
         }
      }

      revertArtifacts(changesAfterRevert, artifactsToRevert); //Only if not attributes
   }

   private void setupBackgroundForScenario() throws Exception {
      baselineBranch = SAW_Bld_1;

      //Setup data before the working branch has been created
      for (TestData testData : this.testDatas) {

         if (testData.isBaseline) {
            testData.setArtifactId(createNewArtifact(baselineBranch, GUID.create()).getArtId());
         }

         switch (testData.item) {
            case ARTIFACT:
               break;
            case ATTRBUTE:
               break;
            case RELATION:
               switch (testData.changeItem) {
                  case NEW:
                     //setup a and b artifacts so a new relation can be created.
                     testData.setbArtifactId(createNewArtifact(baselineBranch, GUID.create()).getArtId());
                     testData.setRelationType(CoreRelationTypes.SupportingInfo_SupportingInfo);
                     break;
                  case DELETED:
                     //set up relation to be deleted
                     Artifact c = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), baselineBranch);
                     Artifact d = createNewArtifact(baselineBranch, GUID.create());

                     testData.setbArtifactId(d.getArtId());

                     RelationManager.addRelation(CoreRelationTypes.SupportingInfo_SupportingInfo, c, d,
                        "Supporting info for c to d");

                     testData.setRelationType(CoreRelationTypes.SupportingInfo_SupportingInfo);
                     c.persist(testName);
                     break;
                  case MOVED:
                     //setup parent child relation so it can be moved on the working branch
                     Artifact parent = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), baselineBranch);
                     Artifact child = createNewArtifact(baselineBranch, GUID.create());
                     Artifact newParent = createNewArtifact(baselineBranch, GUID.create());

                     testData.setbArtifactId(child.getArtId());
                     testData.setcArtId(newParent.getArtId());

                     parent.addChild(USER_DEFINED, child);

                     parent.persist(testName);

                     testData.setRelationType(CoreRelationTypes.Default_Hierarchical__Child);
                     break;
                  case RELATION_ORDER_ATTR:
                     Artifact parentArtifact = createNewArtifact(baselineBranch, "parent");

                     int numOfChildren = 5;

                     for (int i = 0; i < numOfChildren; i++) {
                        parentArtifact.addRelation(ascOrder, CoreRelationTypes.Default_Hierarchical__Child,
                           createNewArtifact(baselineBranch, GUID.create()));
                     }
                     parentArtifact.persist(testName);
                     testData.setArtifactId(parentArtifact.getArtId());
                     break;
               }
               break;
         }
      }

      workingBranch = BranchManager.createWorkingBranch(baselineBranch, "working branch",
         UserManager.getUser(SystemUser.OseeSystem));

      SkynetTransaction workingBranchTransaction = TransactionManager.createTransaction(workingBranch, testName);

      workingBranchTransaction.execute();

      //Setup data on the working branch for testing
      for (TestData testData : testDatas) {
         switch (testData.item) {
            case ATTRBUTE:
               if (testData.changeItem == ChangeItem.NEW) {
                  Artifact newArtifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                  newArtifact.addAttributeFromString(CoreAttributeTypes.ParagraphNumber, "1.0");
                  newArtifact.persist(testName);
                  testData.setAttrId(newArtifact.getSoleAttribute(CoreAttributeTypes.ParagraphNumber).getId());

               }
               break;
            case ARTIFACT:
               switch (testData.changeItem) {
                  case NEW:
                     testData.setArtifactId(createNewArtifact(workingBranch, GUID.create()).getArtId());
                     break;
                  case INTRODUCED:
                     BranchId anotherBranch = BranchManager.createWorkingBranch(workingBranch, "another working branch",
                        UserManager.getUser(SystemUser.OseeSystem));

                     Artifact artifactToIntroduce = createNewArtifact(anotherBranch, "introduce artifact");

                     InterArtifactExplorerDropHandlerOperation dropHandler =
                        new InterArtifactExplorerDropHandlerOperation(
                           OseeSystemArtifacts.getDefaultHierarchyRootArtifact(workingBranch),
                           new Artifact[] {artifactToIntroduce}, false);
                     Operations.executeWork(dropHandler);

                     testData.setArtifactId(artifactToIntroduce.getArtId());
               }
               break;
            case RELATION:
               if (testData.changeItem == ChangeItem.NEW) {
                  Artifact a = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                  Artifact b = ArtifactQuery.getArtifactFromId(testData.getbArtifactId(), workingBranch);
                  RelationManager.addRelation(testData.getRelationType(), a, b, "Link add for testing");
                  a.persist(testName);
               }
               break;
         }
      }

   }

   private void makeChange(List<TestData> datas) throws Exception {
      Artifact artifact = null;
      Attribute<?> attribute = null;

      for (TestData testData : testDatas) {

         switch (testData.item) {
            case ATTRBUTE:
               switch (testData.changeItem) {
                  case NEW:
                     artifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     attribute = artifact.getAttributeById(testData.getAttrId(), true);
                     artifact.persist(testName);
                     break;
                  case DELETED:
                     artifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     attribute = artifact.getSoleAttribute(CoreAttributeTypes.Name);
                     attribute.delete();
                     artifact.persist(testName);

                     testData.setAttrId(attribute.getId());
                     break;
                  case MODIFIED:
                     artifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     attribute = artifact.getSoleAttribute(CoreAttributeTypes.Name);
                     attribute.setFromString("I am changed");
                     artifact.persist(testName);
                     testData.setAttrId(attribute.getId());
                     break;
               }
               break;
            case ARTIFACT:
               switch (testData.changeItem) {
                  case DELETED:
                     artifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     artifact.delete();
                     artifact.persist(testName);
                     break;
                  case NEW:
                     artifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     artifact.persist(testName);
                     break;
                  case INTRODUCED:
                     artifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     artifact.persist(testName);
                     break;
                  case MODIFIED:
                     artifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     artifact.addAttribute(CoreAttributeTypes.Active, true);
                     artifact.persist(testName);
                     break;
               }
               break;
            case RELATION:
               switch (testData.changeItem) {
                  case NEW:
                     Artifact a = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     Artifact b = ArtifactQuery.getArtifactFromId(testData.getbArtifactId(), workingBranch);

                     RelationManager.addRelation(testData.getRelationType(), a, b, "This is a new relation");
                     a.persist(testName);
                     break;

                  case DELETED:
                     Artifact c = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     Artifact d = ArtifactQuery.getArtifactFromId(testData.getbArtifactId(), workingBranch);

                     RelationManager.deleteRelation(testData.getRelationType(), c, d);
                     c.persist(testName);
                     break;

                  case MOVED:
                     Artifact parent = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);
                     Artifact child = ArtifactQuery.getArtifactFromId(testData.getbArtifactId(), workingBranch);
                     Artifact newParent = ArtifactQuery.getArtifactFromId(testData.getcArtId(), workingBranch);

                     parent.deleteRelations(CoreRelationTypes.Default_Hierarchical__Child);
                     parent.persist(testName);
                     newParent.addChild(USER_DEFINED, child);
                     newParent.persist(testName);

                     //This is to revert the child artifact
                     testData.setArtifactId(child.getArtId());
                     child.persist(testName);
                     break;
                  case RELATION_ORDER_ATTR:
                     Artifact parentArtifact = ArtifactQuery.getArtifactFromId(testData.getArtifactId(), workingBranch);

                     int numOfChildren = 5;

                     for (int i = 0; i < numOfChildren; i++) {
                        parentArtifact.addRelation(descOrder, CoreRelationTypes.Default_Hierarchical__Child,
                           createNewArtifact(workingBranch, GUID.create()));
                     }
                     parentArtifact.persist(testName);
                     break;
               }
               break;
         }
      }

   }

   private Artifact createNewArtifact(BranchId branch, String name) throws Exception {
      Artifact artifact = ArtifactTypeManager.addArtifact(DOC, branch, name);
      artifact.persist(testName + name);
      return artifact;
   }

   private void revertArtifacts(Collection<Change> changes, Collection<Artifact> artifactsToRevert) throws OseeCoreException {
      Operations.executeWorkAndCheckStatus(new ReplaceArtifactWithBaselineOperation(changes, artifactsToRevert));
   }

   private void revertAttributes(Collection<Change> changes, Attribute<?> attributeToRevert) throws OseeCoreException {
      List<Change> attrChanges = new ArrayList<>(1);

      for (Change change : changes) {
         if (change.getChangeType() == LoadChangeType.attribute && change.getItemId().getId().intValue() == attributeToRevert.getId()) {
            attrChanges.add(change);
         }
      }
      Operations.executeWorkAndCheckStatus(new ReplaceAttributeWithBaselineOperation(attrChanges));
   }

   private List<Change> getBranchChanges(BranchId branch) throws OseeCoreException {
      List<Change> changes = new ArrayList<>();
      Operations.executeWorkAndCheckStatus(ChangeManager.comparedToParent(branch, changes), MONITOR);
      return changes;
   }

   void createArtifact(String name) throws OseeCoreException {
      ArtifactTypeManager.addArtifact(DOC, workingBranch, name);
   }

   private static class TestData {
      public final Item item;
      public final ChangeItem changeItem;
      public final boolean isBaseline;

      private int artifactId;
      private RelationTypeSide relationType;
      private int bArtifactId;

      private int attrId;

      private int cArtId;

      public TestData(Item item, ChangeItem changeItem, boolean isBaseline) {
         this.item = item;
         this.changeItem = changeItem;
         this.isBaseline = isBaseline;
      }

      public int getcArtId() {
         return cArtId;
      }

      public void setcArtId(int cArtId) {
         this.cArtId = cArtId;
      }

      public int getAttrId() {
         return attrId;
      }

      public void setAttrId(int attrId) {
         this.attrId = attrId;
      }

      public RelationTypeSide getRelationType() {
         return relationType;
      }

      public void setRelationType(RelationTypeSide relationType) {
         this.relationType = relationType;
      }

      public int getbArtifactId() {
         return bArtifactId;
      }

      public void setbArtifactId(int bArtifactId) {
         this.bArtifactId = bArtifactId;
      }

      public int getArtifactId() {
         return artifactId;
      }

      public void setArtifactId(int artId) {
         artifactId = artId;
      }

      @Override
      public String toString() {
         String message =
            "Item: %s ChangeItem %s isBaseline %s ArtifactId %s RelationType %s artifactId %s bArtifactId %s";
         return String.format(message, item, changeItem, isBaseline, artifactId, relationType, artifactId, bArtifactId);
      }
   }
}

Back to the top