Skip to main content
summaryrefslogtreecommitdiffstats
blob: f27545a1ab8d80ee7e8a708672cc08cef4b8f21e (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
/*******************************************************************************
 * 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.test.integration;

import static org.eclipse.osee.framework.core.enums.CoreArtifactTypes.Artifact;
import static org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes.USER_DEFINED;
import java.util.List;
import org.eclipse.osee.framework.core.data.IRelationSorterId;
import org.eclipse.osee.framework.core.data.IRelationTypeSide;
import org.eclipse.osee.framework.core.enums.CoreRelationTypes;
import org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
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.relation.RelationManager;
import org.eclipse.osee.framework.skynet.core.relation.RelationTypeManager;
import org.eclipse.osee.framework.skynet.core.relation.order.RelationOrderData;
import org.eclipse.osee.framework.skynet.core.relation.order.RelationOrderMergeUtility;
import org.eclipse.osee.framework.skynet.core.rule.OseeHousekeepingRule;
import org.eclipse.osee.framework.skynet.core.test.integration.utils.FrameworkTestUtil;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.eclipse.osee.framework.skynet.core.transaction.TransactionManager;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;

/**
 * @author Ryan Schmitt
 */
public class RelationOrderMergeUtilityTest {

   @Rule
   public final MethodRule oseeHousekeeping = new OseeHousekeepingRule();

   private final IRelationTypeSide defaultHierarchy = CoreRelationTypes.Default_Hierarchical__Child;
   private final IRelationSorterId ascOrder = RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC;
   private final IRelationSorterId descOrder = RelationOrderBaseTypes.LEXICOGRAPHICAL_DESC;

   private RelationType hierType;
   private RelationSide hierSide;

   private Branch destBranch;

   @Before
   public void createBranch() throws OseeCoreException {
      destBranch =
         BranchManager.createWorkingBranch(BranchManager.getSystemRootBranch(),
            "RelationOrderMergeUtilityTest.createBranch");
      hierType = RelationTypeManager.getType(defaultHierarchy);
      hierSide = defaultHierarchy.getSide();
   }

   @After
   public void destroyBranch() throws OseeCoreException {
      BranchManager.purgeBranch(destBranch);
   }

   @Test
   public void testTrivialMerge() throws OseeCoreException {
      Artifact parent = FrameworkTestUtil.createSimpleArtifact(Artifact, "Parent", destBranch);
      Artifact[] children =
         FrameworkTestUtil.createSimpleArtifacts(Artifact, 5, "Relative", destBranch).toArray(new Artifact[5]);

      for (int i = 0; i < 5; i++) {
         setAsChild(parent, children[i], ascOrder);
      }
      parent.persist(getClass().getSimpleName() + ".testTrivialMerge()");
      RelationOrderData mergedOrder = RelationOrderMergeUtility.mergeRelationOrder(parent, parent);
      Assert.assertNotNull(mergedOrder);

      String sorter = mergedOrder.getCurrentSorterGuid(hierType, hierSide);
      Assert.assertEquals(ascOrder.getGuid(), sorter);
   }

   @Test
   public void testOrderMerge() throws OseeCoreException {
      Artifact destParent = FrameworkTestUtil.createSimpleArtifact(Artifact, "Parent", destBranch);
      Artifact[] destChildren =
         FrameworkTestUtil.createSimpleArtifacts(Artifact, 5, "Relative", destBranch).toArray(new Artifact[5]);

      for (int i = 0; i <= 3; i++) {
         setAsChild(destParent, destChildren[i], USER_DEFINED);
      }
      destParent.persist(getClass().getSimpleName() + ".testOrderMerge()_1");

      Branch sourceBranch = BranchManager.createWorkingBranch(destBranch, "Source Branch");
      Artifact srcParent = ArtifactQuery.getArtifactFromId(destParent.getGuid(), sourceBranch);
      Artifact srcChild = ArtifactQuery.getArtifactFromId(destChildren[4].getGuid(), sourceBranch);
      setAsChild(srcParent, srcChild, USER_DEFINED);
      srcParent.persist(getClass().getSimpleName() + ".testOrderMerge()_2");
      RelationManager.deleteRelationsAll(destChildren[0], true,
         TransactionManager.createTransaction(destBranch, "Delete the relations"));

      RelationOrderData mergedOrder = RelationOrderMergeUtility.mergeRelationOrder(destParent, srcParent);
      Assert.assertNotNull(mergedOrder);

      List<String> orderList = mergedOrder.getOrderList(hierType, hierSide);
      Assert.assertEquals(orderList.size(), 4);
      Assert.assertTrue(orderList.get(0).equals(destChildren[1].getGuid()));
      Assert.assertTrue(orderList.get(1).equals(destChildren[2].getGuid()));
      Assert.assertTrue(orderList.get(2).equals(destChildren[3].getGuid()));
      Assert.assertTrue(orderList.get(3).equals(destChildren[4].getGuid()));

      BranchManager.purgeBranch(sourceBranch);
   }

   @Test
   public void testStrategyMerge() throws OseeCoreException {

      Artifact ascParent = FrameworkTestUtil.createSimpleArtifact(Artifact, "Parent", destBranch);
      Artifact[] ascRelatives =
         FrameworkTestUtil.createSimpleArtifacts(Artifact, 5, "Relative", destBranch).toArray(new Artifact[5]);
      Artifact descParent = FrameworkTestUtil.createSimpleArtifact(Artifact, "Parent", destBranch);
      Artifact[] descRelatives =
         FrameworkTestUtil.createSimpleArtifacts(Artifact, 5, "Relative", destBranch).toArray(new Artifact[5]);

      for (int i = 0; i < 5; i++) {
         setAsChild(ascParent, ascRelatives[i], ascOrder);
         setAsChild(descParent, descRelatives[i], descOrder);
      }

      SkynetTransaction transaction1 =
         TransactionManager.createTransaction(destBranch, getClass().getSimpleName() + ".testStrategyMerge()_1");
      ascParent.persist(transaction1);
      transaction1.execute();

      SkynetTransaction transaction2 =
         TransactionManager.createTransaction(destBranch, getClass().getSimpleName() + ".testStrategyMerge()_2");
      descParent.persist(transaction2);
      transaction2.execute();

      RelationOrderData mergedOrder = RelationOrderMergeUtility.mergeRelationOrder(ascParent, descParent);
      Assert.assertNull(mergedOrder);
   }

   private void setAsChild(Artifact parent, Artifact child, IRelationSorterId sorter) throws OseeCoreException {
      child.deleteRelations(CoreRelationTypes.Default_Hierarchical__Parent);
      parent.addRelation(sorter, CoreRelationTypes.Default_Hierarchical__Child, child);
   }
}

Back to the top