Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9357ceb194eb0f26ea3571c762b9ce3b2c593332 (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
/*******************************************************************************
 * 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.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.osee.framework.core.data.IArtifactType;
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.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.skynet.core.OseeSystemArtifacts;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * @author Andrew M. Finkbeiner
 */
public class RelationOrderingTest {

   private static final String STATIC_ID_TO_DELETE = "testOrderPersist";

   private Branch branch;
   private Artifact parent;
   private Artifact child1;
   private Artifact child2;
   private Artifact child3;

   @BeforeClass
   @AfterClass
   public static void setupTeardown() throws Exception {
      Set<Artifact> artsToDel = new HashSet<Artifact>();
      artsToDel.addAll(ArtifactQuery.getArtifactListFromTypeAndAttribute(CoreArtifactTypes.Folder,
         CoreAttributeTypes.StaticId, STATIC_ID_TO_DELETE, BranchManager.getCommonBranch()));
      if (artsToDel.size() > 0) {
         new PurgeArtifacts(artsToDel).execute();
         Thread.sleep(5000);
      }
   }

   @Before
   public void setupArtifacts() throws Exception {
      branch = BranchManager.getCommonBranch();
      parent = createArtifact(CoreArtifactTypes.Folder, branch);

      child1 = createArtifact(CoreArtifactTypes.Folder, branch);
      child1.setName("a_child");

      child2 = createArtifact(CoreArtifactTypes.Folder, branch);
      child2.setName("b_child");

      child3 = createArtifact(CoreArtifactTypes.Folder, branch);
      child3.setName("c_child");

      parent.addRelation(CoreRelationTypes.Default_Hierarchical__Child, child1);
      parent.addRelation(CoreRelationTypes.Default_Hierarchical__Child, child2);
      parent.addRelation(CoreRelationTypes.Default_Hierarchical__Child, child3);

   }

   @After
   public void cleanupArtifacts() throws Exception {
      //      parent.deleteAndPersist();
      //      child1.deleteAndPersist();
      //      child2.deleteAndPersist();
      //      child3.deleteAndPersist();
   }

   @org.junit.Test
   public void testSetOrderAndSort() throws Exception {

      checkDesc();

      checkAsc();

      checkUserDefined();

      parent.setRelationOrder(CoreRelationTypes.Default_Hierarchical__Child, RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC);
      Attribute<Object> attribute = parent.getSoleAttribute(CoreAttributeTypes.RelationOrder);
      assertTrue("Setting the attribute back to the default type did not cause an attribute to be deleted",
         (attribute == null || attribute.isDeleted()));

      checkDesc();

      Artifact child4 = createArtifact(CoreArtifactTypes.User, branch);
      child1.setName("a_child");
      Artifact child5 = createArtifact(CoreArtifactTypes.User, branch);
      child2.setName("b_child");
      Artifact child6 = createArtifact(CoreArtifactTypes.User, branch);

      parent.addRelation(CoreRelationTypes.Users_User, child4);
      parent.addRelation(CoreRelationTypes.Users_User, child5);
      parent.addRelation(CoreRelationTypes.Users_User, child6);
      parent.persist(getClass().getSimpleName());

      parent.setRelationOrder(CoreRelationTypes.Users_User, RelationOrderBaseTypes.LEXICOGRAPHICAL_DESC);

      parent.setRelationOrder(CoreRelationTypes.Users_Artifact, RelationOrderBaseTypes.LEXICOGRAPHICAL_DESC);

      parent.setRelationOrder(CoreRelationTypes.Default_Hierarchical__Child, RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC);

      attribute = parent.getSoleAttribute(CoreAttributeTypes.RelationOrder);
      assertTrue("The attribute was deleted even though there was a still a non default sort order on the artifact.",
         (attribute != null));

   }

   private void checkAsc() throws OseeCoreException {
      parent.setRelationOrder(CoreRelationTypes.Default_Hierarchical__Child, RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC);
      List<Artifact> children = parent.getRelatedArtifacts(CoreRelationTypes.Default_Hierarchical__Child);
      Assert.assertEquals(3, children.size());
      Assert.assertEquals(children.get(0).getName(), "a_child");
      Assert.assertEquals(children.get(1).getName(), "b_child");
      Assert.assertEquals(children.get(2).getName(), "c_child");
   }

   private void checkDesc() throws OseeCoreException {
      parent.setRelationOrder(CoreRelationTypes.Default_Hierarchical__Child,
         RelationOrderBaseTypes.LEXICOGRAPHICAL_DESC);
      List<Artifact> children = parent.getRelatedArtifacts(CoreRelationTypes.Default_Hierarchical__Child);
      Assert.assertEquals(3, children.size());
      Assert.assertEquals(children.get(0).getName(), "c_child");
      Assert.assertEquals(children.get(1).getName(), "b_child");
      Assert.assertEquals(children.get(2).getName(), "a_child");
   }

   private void checkUserDefined() throws OseeCoreException {
      List<Artifact> children = new ArrayList<Artifact>();
      children.add(child2);
      children.add(child3);
      children.add(child1);
      parent.setRelationOrder(CoreRelationTypes.Default_Hierarchical__Child, children);
      children = parent.getRelatedArtifacts(CoreRelationTypes.Default_Hierarchical__Child);
      Assert.assertEquals(3, children.size());
      Assert.assertEquals(children.get(0).getName(), "b_child");
      Assert.assertEquals(children.get(1).getName(), "c_child");
      Assert.assertEquals(children.get(2).getName(), "a_child");
   }

   @org.junit.Test
   public void testUserDefinedOrderUpdatesListWhenRelationDeleted() throws OseeCoreException {
      checkUserDefined();

      String artifactGuid = child3.getGuid();

      parent.deleteRelation(CoreRelationTypes.Default_Hierarchical__Child, child3);

      String orderString = parent.getSoleAttributeValue(CoreAttributeTypes.RelationOrder);

      assertFalse(orderString.contains(artifactGuid));

      List<Artifact> children = parent.getRelatedArtifacts(CoreRelationTypes.Default_Hierarchical__Child);
      Assert.assertEquals(2, children.size());
      Assert.assertEquals(children.get(0).getName(), "b_child");
      Assert.assertEquals(children.get(1).getName(), "a_child");
   }

   /**
    * This tests the case where a parent artifact already exists and is persisted in the database with ordered children.
    * Then a new artifact is created and added with a persist() call on the artifact. This persists the new artifact and
    * the new relation, but does not persist the relation order attribute stored on the parent.<br>
    */
   @Test
   public void testOrderPersist() throws OseeCoreException {
      String guid = GUID.create();
      SkynetTransaction transaction = new SkynetTransaction(BranchManager.getCommonBranch(), "Test");
      Artifact mainFolder =
         ArtifactTypeManager.addArtifact(CoreArtifactTypes.Folder, BranchManager.getCommonBranch(),
            "Main Folder - " + guid);
      mainFolder.persist(transaction);
      mainFolder.setSingletonAttributeValue(CoreAttributeTypes.StaticId, STATIC_ID_TO_DELETE);
      OseeSystemArtifacts.getDefaultHierarchyRootArtifact(BranchManager.getCommonBranch()).addChild(mainFolder);
      List<Artifact> children = new ArrayList<Artifact>();
      for (int x = 0; x < 3; x++) {
         Artifact childArt =
            ArtifactTypeManager.addArtifact(CoreArtifactTypes.Folder, BranchManager.getCommonBranch(),
               "New Child " + x + " - " + guid);
         children.add(childArt);
         childArt.setSingletonAttributeValue(CoreAttributeTypes.StaticId, STATIC_ID_TO_DELETE);
         mainFolder.addChild(childArt);
         childArt.persist(transaction);
      }
      mainFolder.setRelationOrder(CoreRelationTypes.Default_Hierarchical__Child, children);
      transaction.execute();

      Artifact newArtifact =
         ArtifactTypeManager.addArtifact(CoreArtifactTypes.Folder, BranchManager.getCommonBranch(),
            "New Artifact " + guid);
      mainFolder.addChild(newArtifact);
      newArtifact.setSingletonAttributeValue(CoreAttributeTypes.StaticId, STATIC_ID_TO_DELETE);
      newArtifact.persist(getClass().getSimpleName());

      for (Artifact child : children) {
         Assert.assertFalse(child.isDirty());
      }
      Assert.assertFalse(newArtifact.isDirty());

      Assert.assertFalse("Artifact should not be dirty.", mainFolder.isDirty());
   }

   private Artifact createArtifact(IArtifactType artifactType, Branch branch) throws OseeCoreException {
      return ArtifactTypeManager.addArtifact(artifactType, branch);
   }
}

Back to the top