Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9cc9cdfbfaf3c48cc3217374d1c7d5f3228b8cf3 (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
/*******************************************************************************
 * 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.relation;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;

import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IRelationSorterId;
import org.eclipse.osee.framework.core.data.IRelationType;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.RelationOrderBaseTypes;
import org.eclipse.osee.framework.core.enums.RelationSide;
import org.eclipse.osee.framework.core.enums.RelationTypeMultiplicity;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.RelationTypeSide;
import org.eclipse.osee.framework.core.model.cache.RelationTypeCache;
import org.eclipse.osee.framework.core.model.event.DefaultBasicGuidRelationReorder;
import org.eclipse.osee.framework.core.model.mocks.MockOseeDataAccessor;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.eclipse.osee.framework.skynet.core.mocks.DataFactory;
import org.eclipse.osee.framework.skynet.core.mocks.MockIArtifact;
import org.eclipse.osee.framework.skynet.core.relation.order.IRelationOrderAccessor;
import org.eclipse.osee.framework.skynet.core.relation.order.IRelationSorter;
import org.eclipse.osee.framework.skynet.core.relation.order.RelationOrderData;
import org.eclipse.osee.framework.skynet.core.relation.order.RelationSorterProvider;
import org.eclipse.osee.framework.skynet.core.types.IArtifact;
import org.eclipse.osee.framework.skynet.core.utility.Artifacts;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * @author Roberto E. Escobar
 */
@RunWith(Parameterized.class)
public class RelationTypeSideSorterTest {
   private final static Random randomGenerator = new Random();

   private final RelationType relationType;
   private final RelationSide relationSide;
   private final RelationOrderData orderData;
   private final RelationSorterProvider sorterProvider;
   private final RelationTypeSideSorter sorter;

   public RelationTypeSideSorterTest(RelationSorterProvider sorterProvider, RelationType relationType, RelationSide relationSide, RelationOrderData orderData, List<Object[]> expected) {
      this.relationType = relationType;
      this.relationSide = relationSide;
      this.orderData = orderData;
      this.sorterProvider = sorterProvider;
      this.sorter = new RelationTypeSideSorter(relationType, relationSide, sorterProvider, orderData);
   }

   @Test
   public void testConstruction() {
      Assert.assertNotNull(sorter);
   }

   @Test
   public void testGetIArtifact() throws OseeCoreException {
      Assert.assertNotNull(orderData.getIArtifact());
      Assert.assertEquals(orderData.getIArtifact(), sorter.getIArtifact());

      Assert.assertTrue(sorter.getIArtifact() instanceof MockIArtifact);
      MockIArtifact mockArtifact = (MockIArtifact) sorter.getIArtifact();
      mockArtifact.clear();

      // Test Get Full Artifact is Called from IArtifact
      sorter.getArtifact();
      Assert.assertTrue(mockArtifact.wasGetFullArtifactCalled());
   }

   @Test
   public void testGetRelationType() {
      Assert.assertEquals(relationType, sorter.getRelationType());
   }

   @Test
   public void testGetRelationSide() {
      Assert.assertEquals(relationSide, sorter.getSide());
   }

   @Test
   public void testGetSorterId() throws OseeCoreException {
      String sorterGuid = orderData.getCurrentSorterGuid(relationType, relationSide);
      IRelationSorterId expected = sorterProvider.getRelationOrder(sorterGuid).getSorterId();
      Assert.assertNotNull(sorterGuid);
      Assert.assertEquals(expected, sorter.getSorterId());
      Assert.assertEquals(expected.getGuid(), sorter.getSorterId().getGuid());
      Assert.assertEquals(expected.getName(), sorter.getSorterId().getName());
   }

   @Test
   public void testSorterName() throws OseeCoreException {
      String sorterGuid = orderData.getCurrentSorterGuid(relationType, relationSide);
      IRelationSorterId expected = sorterProvider.getRelationOrder(sorterGuid).getSorterId();
      Assert.assertNotNull(sorterGuid);
      Assert.assertEquals(expected.getName(), sorter.getSorterName());
   }

   @Test
   public void testSetOrder() throws OseeCoreException {
      IArtifact art3 = createArtifact("c", GUID.create());
      IArtifact art4 = createArtifact("d", GUID.create());

      List<IArtifact> relatives = Arrays.asList(art3, art4);
      List<String> expected = Artifacts.toGuids(relatives);

      // set same sorter id
      sorter.setOrder(relatives, sorter.getSorterId());
      List<String> actual = orderData.getOrderList(sorter.getRelationType(), sorter.getSide());
      Assert.assertFalse(actual.equals(expected));

      // Set Different sorter id
      sorter.setOrder(relatives, RelationOrderBaseTypes.USER_DEFINED);
      actual = orderData.getOrderList(sorter.getRelationType(), sorter.getSide());
      expected = Artifacts.toGuids(relatives);
      Assert.assertTrue(actual.equals(expected));
   }

   @Test
   public void testSort() {
      //      sorter.sort(listToOrder)
      //      RelationSorter sorter = null;
      //      List<IArtifact> sorted = sorter.getSortedRelatives(relatives);

   }

   @Test
   public void testEquals() {
      //      RelationSorter a = new RelationSorter(RelationTypeManager.getType(6), RelationSide.SIDE_A);
      //      RelationSorter b = new RelationSorter(RelationTypeManager.getType(7), RelationSide.SIDE_B);
      //      assertFalse(a.equals(b));
      //      assertTrue(a.equals(a));
   }

   @Test
   public void testAddItem() throws OseeCoreException {
      IArtifact itemToAdd = createArtifact("Item to Add", GUID.create());

      List<IArtifact> startingArtifacts = new ArrayList<IArtifact>();
      List<String> startingOrder = orderData.getOrderList(sorter.getRelationType(), sorter.getSide());
      for (int index = 0; index < startingOrder.size(); index++) {
         String artifactGuid = startingOrder.get(index);
         startingArtifacts.add(createArtifact("Dummy" + index, artifactGuid));
      }
      // Set Related Artifact Data
      MockArtifactWithRelations artifactMock = (MockArtifactWithRelations) sorter.getIArtifact();
      artifactMock.setRelatedArtifacts(sorter.getRelationType(), startingArtifacts);

      for (IRelationSorterId sorterId : sorterProvider.getAllRelationOrderIds()) {
         IRelationSorter relationSorter = sorterProvider.getRelationOrder(sorterId.getGuid());

         List<IArtifact> itemsToOrder = new ArrayList<IArtifact>(startingArtifacts);
         itemsToOrder.add(itemToAdd);
         if (RelationOrderBaseTypes.USER_DEFINED != sorterId) {
            relationSorter.sort(itemsToOrder, null);
         }

         // Call twice to ensure that the same items is not duplicated in the list
         sorter.addItem(sorterId, itemToAdd);
         sorter.addItem(sorterId, itemToAdd);

         List<String> currentOrder = orderData.getOrderList(sorter.getRelationType(), sorter.getSide());
         if (RelationOrderBaseTypes.USER_DEFINED != sorterId) {
            Assert.assertTrue(currentOrder.isEmpty());
         } else {
            List<String> expectedOrder = Artifacts.toGuids(itemsToOrder);
            Assert.assertTrue(expectedOrder.equals(currentOrder));
         }
      }
   }

   @Test
   public void testToString() throws OseeCoreException {
      String artGuid = sorter.getIArtifact().getGuid();
      String sorterGuid = orderData.getCurrentSorterGuid(relationType, relationSide);
      IRelationSorterId expectedId = sorterProvider.getRelationOrder(sorterGuid).getSorterId();
      String expectedToString =
         String.format("Relation Sorter {relationType=%s, relationSide=[%s], artifact=[%s], sorterId=%s}",
            relationType, relationSide, artGuid, expectedId);
      Assert.assertEquals(expectedToString, sorter.toString());
   }

   @Parameters
   public static Collection<Object[]> data() throws OseeCoreException {
      RelationSorterProvider provider = new RelationSorterProvider();
      IRelationOrderAccessor accessor = new DoNothingAccessor();

      RelationTypeCache cache = new RelationTypeCache(new MockOseeDataAccessor<RelationType>());

      RelationType relationType1 =
         createRelationType(cache, "Rel 1", RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC.getGuid());
      RelationType relationType2 =
         createRelationType(cache, "Rel 2", RelationOrderBaseTypes.LEXICOGRAPHICAL_DESC.getGuid());
      IArtifact art1 = createArtifact("a", GUID.create());
      IArtifact art2 = createArtifact("b", GUID.create());

      RelationOrderData data1 = new RelationOrderData(accessor, art1);
      RelationOrderData data2 = new RelationOrderData(accessor, art2);

      List<Object[]> expected1 = new ArrayList<Object[]>();
      List<Object[]> expected2 = new ArrayList<Object[]>();

      addData(cache, data1, expected1);
      addData(cache, data2, expected2);

      Collection<Object[]> data = new ArrayList<Object[]>();
      data.add(new Object[] {provider, relationType1, RelationSide.SIDE_A, data1, expected1});
      data.add(new Object[] {provider, relationType2, RelationSide.SIDE_B, data2, expected2});
      return data;
   }

   private static IArtifact createArtifact(String name, String guid) {
      int uniqueId = randomGenerator.nextInt();
      Branch branch = new Branch(GUID.create(), name + " - branch", BranchType.WORKING, BranchState.MODIFIED, false);
      return new MockArtifactWithRelations(uniqueId, name, guid, branch, CoreArtifactTypes.Artifact);
   }

   private static RelationType createRelationType(RelationTypeCache cache, String name, String delationRelationOrderGuid) throws OseeCoreException {
      IArtifactType type1 = new ArtifactType(GUID.create(), "1", false);
      IArtifactType type2 = new ArtifactType(GUID.create(), "2", false);
      RelationType relationType =
         new RelationType(GUID.create(), name, name + "_A", name + "_B", type1, type2,
            RelationTypeMultiplicity.MANY_TO_MANY, delationRelationOrderGuid);
      Assert.assertNotNull(relationType);
      cache.cache(relationType);
      return relationType;
   }

   private static void addData(RelationTypeCache cache, RelationOrderData data, List<Object[]> expected) throws OseeCoreException {
      addData(data, expected, cache.getUniqueByName("Rel 1"), RelationSide.SIDE_A, //
         RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC.getGuid(), "1", "2", "3");
      addData(data, expected, cache.getUniqueByName("Rel 2"), RelationSide.SIDE_B, //
         RelationOrderBaseTypes.UNORDERED.getGuid(), "4", "5", "6");

      checkData(data, expected);
   }

   private static void checkData(RelationOrderData orderData, List<Object[]> expectedValues) {
      int index = 0;
      Assert.assertEquals(expectedValues.size(), orderData.size());
      for (Entry<Pair<String, String>, Pair<String, List<String>>> entry : orderData.getOrderedEntrySet()) {
         Object[] actual =
            new Object[] {
               entry.getKey().getFirst(),
               entry.getKey().getSecond(),
               entry.getValue().getFirst(),
               entry.getValue().getSecond()};
         Object[] expected = expectedValues.get(index++);
         Assert.assertEquals(expected.length, actual.length);
         for (int index2 = 0; index2 < expected.length; index2++) {
            Assert.assertEquals(expected[index2], actual[index2]);
         }
      }
   }

   private static void addData(RelationOrderData orderData, List<Object[]> expectedData, RelationType relationType, RelationSide side, String relationOrderIdGuid, String... guids) {
      List<String> artGuids = new ArrayList<String>();
      if (guids != null && guids.length > 0) {
         artGuids.addAll(Arrays.asList(guids));
      }
      orderData.addOrderList(relationType.getName(), side.name(), relationOrderIdGuid, artGuids);
      expectedData.add(new Object[] {relationType.getName(), side.name(), relationOrderIdGuid, artGuids});
   }

   private static final class MockArtifactWithRelations extends MockIArtifact {
      private final Map<IRelationType, List<? extends IArtifact>> relatedItemsMap;

      public MockArtifactWithRelations(int uniqueId, String name, String guid, Branch branch, IArtifactType artifactType) {
         super(uniqueId, name, guid, branch, DataFactory.fromToken(artifactType));
         this.relatedItemsMap = new HashMap<IRelationType, List<? extends IArtifact>>();
      }

      @Override
      public List<? extends IArtifact> getRelatedArtifacts(RelationTypeSide relationTypeSide) {
         List<? extends IArtifact> related = relatedItemsMap.get(relationTypeSide.getRelationType());
         if (related == null) {
            related = Collections.emptyList();
         }
         return related;
      }

      public void setRelatedArtifacts(IRelationType relationType, List<? extends IArtifact> relatedItems) {
         relatedItemsMap.put(relationType, relatedItems);
      }
   }

   private static final class DoNothingAccessor implements IRelationOrderAccessor {

      @Override
      public void load(IArtifact artifact, RelationOrderData orderData) {
         // do nothing
      }

      @Override
      public void store(IArtifact artifact, RelationOrderData orderData, DefaultBasicGuidRelationReorder reorderRecord) {
         // do nothing
      }
   }
}

Back to the top