Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4d054022251f784e36184b46b7f5112c0d8abc27 (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
/*******************************************************************************
 * 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.order;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Random;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
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.model.Branch;
import org.eclipse.osee.framework.core.model.cache.RelationTypeCache;
import org.eclipse.osee.framework.core.model.event.DefaultBasicUuidRelationReorder;
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.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
import org.eclipse.osee.framework.jdk.core.util.Collections;
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.MockOseeDataAccessor;
import org.eclipse.osee.framework.skynet.core.types.IArtifact;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

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

   private IArtifact artifact;
   private MockRelationOrderAccessor accessor;
   private RelationOrderData data;
   private RelationType relationType1;
   private RelationType relationType2;
   private RelationType relationType3;

   @Before
   public void setUp() throws OseeCoreException {
      artifact = createArtifact("art1", GUID.create());
      accessor = new MockRelationOrderAccessor();
      data = new RelationOrderData(accessor, artifact);

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

      relationType1 = createRelationType(cache, "Rel 1", RelationOrderBaseTypes.USER_DEFINED.getGuid());
      relationType2 = createRelationType(cache, "Rel 2", RelationOrderBaseTypes.UNORDERED.getGuid());
      relationType3 = createRelationType(cache, "Rel 3", RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC.getGuid());

      Assert.assertFalse(data.hasEntries());
      Assert.assertEquals(0, data.size());
   }

   @After
   public void tearDown() {
      artifact = null;
      accessor = null;
      data = null;
   }

   @Test
   public void testGetIArtifact() {
      Assert.assertEquals(artifact, data.getIArtifact());
   }

   @Test
   public void testLoad() throws OseeCoreException {
      accessor.clearLoadCalled();

      data.load();
      Assert.assertTrue(accessor.wasLoadCalled());

      List<Object[]> expected = new ArrayList<Object[]>();
      addData(data, expected, relationType1, RelationSide.SIDE_A, //
         RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC.getGuid(), "1", "2", "3");
      checkData(data, expected);

      accessor.clearLoadCalled();

      Assert.assertEquals(1, data.size());
      data.load();
      Assert.assertTrue(accessor.wasLoadCalled());
      Assert.assertFalse(data.hasEntries());
      Assert.assertEquals(0, data.size());
   }

   @Test
   public void testAddOrderList() {
      List<Object[]> expected = new ArrayList<Object[]>();
      addData(expected);
      data.clear();
      Assert.assertFalse(data.hasEntries());
      Assert.assertEquals(0, data.size());

   }

   @Test
   public void testGetCurrentSorterGuid() throws OseeCoreException {
      List<Object[]> expected = new ArrayList<Object[]>();
      addData(expected);
      try {
         data.getCurrentSorterGuid(relationType1, null);
         Assert.assertNull("This line should not be executed");
      } catch (Exception ex) {
         Assert.assertTrue(ex instanceof OseeArgumentException);
      }

      try {
         data.getCurrentSorterGuid(null, RelationSide.SIDE_A);
         Assert.assertNull("This line should not be executed");
      } catch (Exception ex) {
         Assert.assertTrue(ex instanceof OseeArgumentException);
      }

      String actualGuid = data.getCurrentSorterGuid(relationType1, RelationSide.SIDE_A);
      Assert.assertEquals(RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC.getGuid(), actualGuid);

      actualGuid = data.getCurrentSorterGuid(relationType2, RelationSide.SIDE_B);
      Assert.assertEquals(RelationOrderBaseTypes.UNORDERED.getGuid(), actualGuid);

      // Pair does not exist
      actualGuid = data.getCurrentSorterGuid(relationType2, RelationSide.SIDE_A);
      Assert.assertEquals(relationType2.getDefaultOrderTypeGuid(), actualGuid);

      // Pair does not exist
      actualGuid = data.getCurrentSorterGuid(relationType3, RelationSide.SIDE_B);
      Assert.assertEquals(relationType3.getDefaultOrderTypeGuid(), actualGuid);
   }

   @Test
   public void testGetOrderList() throws OseeCoreException {
      List<Object[]> expected = new ArrayList<Object[]>();
      addData(expected);

      try {
         data.getOrderList(relationType1, null);
         Assert.assertNull("This line should not be executed");
      } catch (Exception ex) {
         Assert.assertTrue(ex instanceof OseeArgumentException);
      }

      try {
         data.getOrderList(null, RelationSide.SIDE_A);
         Assert.assertNull("This line should not be executed");
      } catch (Exception ex) {
         Assert.assertTrue(ex instanceof OseeArgumentException);
      }

      List<String> actualGuids = data.getOrderList(relationType1, RelationSide.SIDE_A);
      Assert.assertTrue(Collections.isEqual(Arrays.asList("1", "2", "3"), actualGuids));

      actualGuids = data.getOrderList(relationType2, RelationSide.SIDE_B);
      Assert.assertTrue(Collections.isEqual(Arrays.asList("4", "5", "6"), actualGuids));

      // Non-existent combination
      actualGuids = data.getOrderList(relationType1, RelationSide.SIDE_B);
      Assert.assertTrue(actualGuids.isEmpty());

      actualGuids = data.getOrderList(relationType2, RelationSide.SIDE_A);
      Assert.assertTrue(actualGuids.isEmpty());

      actualGuids = data.getOrderList(relationType3, RelationSide.SIDE_A);
      Assert.assertTrue(actualGuids.isEmpty());
   }

   @Test
   public void testRemoveFromList() throws OseeCoreException {
      List<Object[]> expected = new ArrayList<Object[]>();
      addData(expected);

      // Remove item that does not exist
      data.removeOrderList(relationType2, RelationSide.SIDE_A);
      checkData(data, expected);

      data.removeOrderList(relationType3, RelationSide.SIDE_A);
      checkData(data, expected);

      // Actual Removal
      data.removeOrderList(relationType1, RelationSide.SIDE_A);
      expected.remove(0);
      checkData(data, expected);

      data.removeOrderList(relationType2, RelationSide.SIDE_B);
      expected.remove(0);
      checkData(data, expected);
   }

   @Test
   public void testStore() throws OseeCoreException {
      List<Object[]> expected = new ArrayList<Object[]>();
      addData(expected);

      accessor.clearStoreCalled();

      // No Change -- Current Sorter Id
      List<IArtifact> emptyList = java.util.Collections.emptyList();
      data.store(relationType1, RelationSide.SIDE_A, RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC, emptyList);
      Assert.assertFalse(accessor.wasStoreCalled());

      // Store
      accessor.clearStoreCalled();
      data.store(relationType1, RelationSide.SIDE_A,
         RelationOrderBaseTypes.getFromGuid(relationType1.getDefaultOrderTypeGuid()), emptyList);
      Assert.assertTrue(accessor.wasStoreCalled());

      // Store
      accessor.clearStoreCalled();
      data.store(relationType1, RelationSide.SIDE_A, RelationOrderBaseTypes.UNORDERED, emptyList);
      Assert.assertTrue(accessor.wasStoreCalled());
   }

   @Test
   public void testToString() {
      Assert.assertEquals("Relation Order Data for artifact:" + artifact.toString(), data.toString());
   }

   private void addData(List<Object[]> expected) {
      addData(data, expected, relationType1, RelationSide.SIDE_A, //
         RelationOrderBaseTypes.LEXICOGRAPHICAL_ASC.getGuid(), "1", "2", "3");

      addData(data, expected, relationType2, RelationSide.SIDE_B, //
         RelationOrderBaseTypes.UNORDERED.getGuid(), "4", "5", "6");

      checkData(data, expected);
   }

   private 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 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 IArtifact createArtifact(String name, String guid) {
      int uniqueId = randomGenerator.nextInt();
      Branch branch = new Branch(GUID.create(), name + " - branch", BranchType.WORKING, BranchState.MODIFIED, false);
      return DataFactory.createArtifact(uniqueId, name, guid, branch);
   }

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

   private final class MockRelationOrderAccessor implements IRelationOrderAccessor {

      private boolean wasLoadCalled;
      private boolean wasStoreCalled;

      public MockRelationOrderAccessor() {
         super();
         clearLoadCalled();
         clearStoreCalled();
      }

      public boolean wasLoadCalled() {
         return wasLoadCalled;
      }

      public void clearLoadCalled() {
         wasLoadCalled = false;
         Assert.assertFalse(wasLoadCalled());
      }

      public boolean wasStoreCalled() {
         return wasStoreCalled;
      }

      public void clearStoreCalled() {
         wasStoreCalled = false;
         Assert.assertFalse(wasStoreCalled());
      }

      @Override
      public void load(IArtifact artifact, RelationOrderData orderData) {
         wasLoadCalled = true;
      }

      @Override
      public void store(IArtifact artifact, RelationOrderData orderData, DefaultBasicUuidRelationReorder reorderRecord) {
         wasStoreCalled = true;
      }

   }
}

Back to the top