Skip to main content
summaryrefslogtreecommitdiffstats
blob: 692c54d478051acfc6d5aa7e0858edca165732fe (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
/*******************************************************************************
 * 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.core.model.mocks;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Assert;
import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
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.AbstractOseeType;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.MergeBranch;
import org.eclipse.osee.framework.core.model.OseeEnumEntry;
import org.eclipse.osee.framework.core.model.access.AccessDetail;
import org.eclipse.osee.framework.core.model.cache.AbstractOseeCache;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.cache.IOseeTypeFactory;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.core.model.type.OseeEnumType;
import org.eclipse.osee.framework.core.model.type.RelationType;

/**
 * @author Roberto E. Escobar
 */
public class ModelAsserts {

   private ModelAsserts() {
      // Utility Class
   }

   public static void assertTypeSetGet(AbstractOseeType type, String fieldName, String getMethodName, String setMethodName, Object expectedValue, Object newValue) throws Exception {
      Method getMethod = type.getClass().getMethod(getMethodName);
      Method setMethod = type.getClass().getMethod(setMethodName, expectedValue.getClass());

      Assert.assertEquals(expectedValue, getMethod.invoke(type));

      type.clearDirty();
      Assert.assertFalse(type.isDirty());
      Assert.assertFalse(type.areFieldsDirty(fieldName));

      // Check reassign doesn't mark as dirty
      setMethod.invoke(type, expectedValue);
      Assert.assertFalse(type.areFieldsDirty(fieldName));

      setMethod.invoke(type, newValue);
      Assert.assertEquals(newValue, getMethod.invoke(type));

      Assert.assertTrue(type.isDirty());
      Assert.assertTrue(type.areFieldsDirty(fieldName));

      type.clearDirty();
      Assert.assertFalse(type.isDirty());
      Assert.assertFalse(type.areFieldsDirty(fieldName));

      setMethod.invoke(type, expectedValue);
      type.clearDirty();
   }

   public static void createAlias(BranchCache cache, String branchGuid, String... aliases) throws OseeCoreException {
      Branch branch = cache.getByGuid(branchGuid);
      Assert.assertNotNull(branch);
      //      cache.setAliases(branch, Arrays.asList(aliases));
   }

   public static void checkMergeBranch(BranchCache cache, String expectedMergeBranchGuid, String sourceBranchGuid, String destinationBranchGuid) throws OseeCoreException {
      Branch sourceBranch = cache.getByGuid(sourceBranchGuid);
      Assert.assertNotNull(sourceBranch);
      Branch destionationBranch = cache.getByGuid(destinationBranchGuid);
      Assert.assertNotNull(destionationBranch);

      Branch actualMergeBranch = cache.findMergeBranch(sourceBranch, destionationBranch);
      if (expectedMergeBranchGuid == null) {
         Assert.assertNull(actualMergeBranch);
      } else {
         Branch mergeBranch = cache.getByGuid(expectedMergeBranchGuid);
         Assert.assertNotNull(mergeBranch);
         Assert.assertEquals(mergeBranch, actualMergeBranch);
      }
   }

   public static void createMergeBranch(BranchCache cache, String mergeBranchGuid, String sourceBranchGuid, String destinationBranchGuid) throws OseeCoreException {
      Branch mergeBranch = cache.getByGuid(mergeBranchGuid);
      Assert.assertNotNull(mergeBranch);
      Branch sourceBranch = cache.getByGuid(sourceBranchGuid);
      Assert.assertNotNull(sourceBranch);
      Branch destionationBranch = cache.getByGuid(destinationBranchGuid);
      Assert.assertNotNull(destionationBranch);
      Assert.assertTrue(mergeBranch instanceof MergeBranch);
      MergeBranch mBranch = (MergeBranch) mergeBranch;
      mBranch.setSourceBranch(sourceBranch);
      mBranch.setDestinationBranch(destionationBranch);
   }

   public static void checkHierarchy(BranchCache cache, String parentGuid, String... expected) throws OseeCoreException {
      Branch parentBranch = cache.getByGuid(parentGuid);
      Assert.assertNotNull(parentBranch);
      Collection<Branch> children = parentBranch.getChildren();
      Assert.assertEquals(expected.length, children.size());
      int index = 0;
      for (Branch child : children) {
         Branch expectedBranch = cache.getByGuid(expected[index]);
         Assert.assertNotNull(expectedBranch);
         Assert.assertEquals(expectedBranch, child);
         Assert.assertEquals(parentBranch, child.getParentBranch());
         index++;
      }
   }

   public static void createBranchHierarchy(BranchCache cache, String parentGuid, String... childrenGuids) throws OseeCoreException {
      Branch parentBranch = cache.getByGuid(parentGuid);
      Assert.assertNotNull(parentBranch);
      Assert.assertNotNull(childrenGuids);
      Assert.assertTrue(childrenGuids.length > 0);
      for (String childGuid : childrenGuids) {
         Branch childBranch = cache.getByGuid(childGuid);
         Assert.assertNotNull(childBranch);
         childBranch.setParentBranch(parentBranch);
      }
   }

   public static RelationType createRelationType(AbstractOseeCache<ArtifactType> artCache, String guid, String name, String aGUID, String bGUID, RelationTypeMultiplicity multiplicity) throws OseeCoreException {
      IArtifactType type1 = artCache.getByGuid(aGUID);
      IArtifactType type2 = artCache.getByGuid(bGUID);
      RelationType relationType =
         new RelationType(guid, name, name + "_A", name + "_B", type1, type2, multiplicity, "");
      Assert.assertNotNull(relationType);
      return relationType;
   }

   public static Branch createBranch(String guid, String name, BranchType branchType, BranchState branchState, boolean isArchived) {
      Branch branch;
      if (branchType.isMergeBranch()) {
         branch = new MergeBranch(guid, name, branchType, branchState, isArchived);
      } else {
         branch = new Branch(guid, name, branchType, branchState, isArchived);
      }
      Assert.assertNotNull(branch);
      return branch;
   }

   public static AttributeType createAttributeType(String guid, String name) {
      AttributeType attributeType =
         new AttributeType(guid, name, "DummyBase", "DummyProvider", "none", "none", 1, 1, "test data", null);
      Assert.assertNotNull(attributeType);
      return attributeType;
   }

   public static void checkOseeEnumEntries(OseeEnumEntry[] actual, Object... entries) {
      Assert.assertEquals(entries.length / 2, actual.length);
      int index2 = 0;
      for (int index = 0; index < entries.length; index++, index2++) {
         String itemName = (String) entries[index];
         Integer ordinal = (Integer) entries[++index];

         Assert.assertEquals(itemName, actual[index2].getName());
         Assert.assertEquals((int) ordinal, actual[index2].ordinal());
      }
   }

   public static OseeEnumType createEnumType(IOseeTypeFactory factory, String guid, String name, Object... entries) throws OseeCoreException {
      OseeEnumType type = new OseeEnumType(guid, name);
      if (entries != null && entries.length > 0) {
         List<OseeEnumEntry> items = new ArrayList<OseeEnumEntry>();
         for (int index = 0; index < entries.length; index++) {
            String itemName = (String) entries[index];
            Integer ordinal = (Integer) entries[++index];
            items.add(new OseeEnumEntry(null, itemName, ordinal));
         }
         type.setEntries(items);
      }
      return type;
   }

   public static void checkEnumType(String expectedName, String[] expectedEntries, Integer[] expectedOrdinals, OseeEnumType actualEnumType) throws OseeCoreException {
      Assert.assertEquals(expectedName, actualEnumType.getName());
      OseeEnumEntry[] enumEntries = actualEnumType.values();
      Assert.assertEquals(expectedEntries.length, enumEntries.length);
      for (int index = 0; index < expectedEntries.length && index < expectedOrdinals.length; index++) {
         checkEnumEntry(expectedEntries[index], expectedOrdinals[index], actualEnumType, enumEntries[index]);
      }
   }

   public static void checkEnumEntry(String expectedName, int expectedOrdinal, OseeEnumType parent, OseeEnumEntry entry) {
      Assert.assertEquals(expectedName, entry.getName());
      Assert.assertEquals(expectedOrdinal, entry.ordinal());
      Assert.assertEquals(parent.getId(), entry.getId());
      Assert.assertEquals(parent.getName(), entry.getName());
   }

   public static void checkEnumType(OseeEnumType expected, OseeEnumType actual) throws OseeCoreException {
      OseeEnumEntry[] expectedValues = expected.values();
      OseeEnumEntry[] actualValues = actual.values();
      Assert.assertEquals(expectedValues.length, actualValues.length);

      for (int index = 0; index < expectedValues.length; index++) {
         checkEnumEntry(expectedValues[index], actualValues[index]);
      }
   }

   public static void checkEnumEntry(OseeEnumEntry expected, OseeEnumEntry actual) {
      Assert.assertEquals(expected.getName(), actual.getName());
      Assert.assertEquals(expected.ordinal(), actual.ordinal());
      Assert.assertEquals(expected.getGuid(), actual.getGuid());
   }

   public static void checkInheritance(AbstractOseeCache<ArtifactType> artCache, String artTypeGuid, String... superTypeGuids) throws OseeCoreException {
      ArtifactType target = artCache.getByGuid(artTypeGuid);
      Assert.assertNotNull(target);

      List<ArtifactType> expectedSuperTypes = new ArrayList<ArtifactType>();
      for (String superTyperGuid : superTypeGuids) {
         ArtifactType superArtifactType = artCache.getByGuid(superTyperGuid);
         Assert.assertNotNull(superArtifactType);
         expectedSuperTypes.add(superArtifactType);
      }

      for (ArtifactType testAgainstType : artCache.getAll()) {
         boolean result = target.inheritsFrom(testAgainstType);
         if (expectedSuperTypes.contains(testAgainstType) || target.equals(testAgainstType)) {
            Assert.assertTrue(
               String.format("[%s] does not inherit from [%s]", target.getName(), testAgainstType.getName()), result);
         } else {
            Assert.assertFalse(
               String.format("[%s] should not inherit from [%s]", target.getName(), testAgainstType.getName()), result);
         }
      }
   }

   public static void checkDescendants(AbstractOseeCache<ArtifactType> artCache, String artTypeGuid, boolean isAllLevels, String... descendantGuids) throws OseeCoreException {
      ArtifactType target = artCache.getByGuid(artTypeGuid);
      Assert.assertNotNull(target);

      List<ArtifactType> expectedDescendants = new ArrayList<ArtifactType>();
      for (String type : descendantGuids) {
         ArtifactType childType = artCache.getByGuid(type);
         Assert.assertNotNull(childType);
         expectedDescendants.add(childType);
      }

      Collection<ArtifactType> descendants =
         isAllLevels ? target.getAllDescendantTypes() : target.getFirstLevelDescendantTypes();

      Assert.assertEquals(expectedDescendants.size(), descendants.size());
      for (ArtifactType child : descendants) {
         boolean result = expectedDescendants.contains(target);
         if (result) {
            Assert.assertTrue(String.format("[%s] inherits from [%s]", child.getName(), target.getName()), result);
         } else {
            Assert.assertFalse(String.format("[%s] does not inherit from [%s]", child.getName(), target.getName()),
               result);
         }
         if (target.hasSuperArtifactTypes()) {
            Assert.assertEquals(true, child.inheritsFrom(target));
         }
      }
   }

   public static void checkAttributes(AbstractOseeCache<ArtifactType> artCache, AbstractOseeCache<AttributeType> attrCache, String artTypeGuid, Branch branch, String... attributeGuids) throws OseeCoreException {
      ArtifactType artifactType = artCache.getByGuid(artTypeGuid);
      Assert.assertNotNull(artifactType);

      List<IAttributeType> expectedAttributes = new ArrayList<IAttributeType>();
      for (String attrGuid : attributeGuids) {
         IAttributeType attributeType = attrCache.getByGuid(attrGuid);
         Assert.assertNotNull(attributeType);
         expectedAttributes.add(attributeType);
      }

      Collection<IAttributeType> actualTypes = artifactType.getAttributeTypes(branch);
      Assert.assertEquals(String.format("ArtifactType [%s] - incorrect number of attributes actual - %s expected - %s",
         artTypeGuid, actualTypes, expectedAttributes), expectedAttributes.size(), actualTypes.size());

      Collection<IAttributeType> typesNotFound =
         org.eclipse.osee.framework.jdk.core.util.Collections.setComplement(expectedAttributes, actualTypes);
      Assert.assertTrue(
         String.format("Artifact [%s] for branch [%s] did not have the following attributes [%s]",
            artifactType.getName(), branch.getName(), typesNotFound), typesNotFound.isEmpty());

      typesNotFound =
         org.eclipse.osee.framework.jdk.core.util.Collections.setComplement(actualTypes, expectedAttributes);
      Assert.assertTrue(String.format("Artifact [%s] for branch [%s] the following additional attributes [%s]",
         artifactType.getName(), branch.getName(), typesNotFound), typesNotFound.isEmpty());
   }

   public static void checkRelationTypeInheritance(AbstractOseeCache<RelationType> cache, AbstractOseeCache<ArtifactType> artCache, String relGuid, RelationSide relationSide, int maxValue, String... artifactTypesAllowed) throws OseeCoreException {
      RelationType relationType = cache.getByGuid(relGuid);
      Assert.assertNotNull(relationType);

      Assert.assertEquals(maxValue, relationType.getMultiplicity().getLimit(relationSide));
      Assert.assertEquals(maxValue == Integer.MAX_VALUE ? "n" : "1",
         relationType.getMultiplicity().asLimitLabel(relationSide));

      List<ArtifactType> allowedTypes = new ArrayList<ArtifactType>();
      for (String guid : artifactTypesAllowed) {
         ArtifactType type = artCache.getByGuid(guid);
         Assert.assertNotNull(type);
         allowedTypes.add(type);
      }

      for (ArtifactType artifactType : artCache.getAll()) {
         boolean result = relationType.isArtifactTypeAllowed(relationSide, artifactType);
         if (allowedTypes.contains(artifactType)) {
            Assert.assertTrue(String.format("ArtifactType [%s] was not allowed", artifactType), result);
         } else {
            Assert.assertFalse(
               String.format("ArtifactType [%s] was allowed even though it should not have been", artifactType), result);
         }
      }
   }

   public static void assertEquals(AccessDetail<?> expected, AccessDetail<?> actual) {
      Assert.assertEquals(expected, actual);
      Assert.assertEquals(expected.getPermission(), actual.getPermission());
      Assert.assertEquals(expected.getAccessObject(), actual.getAccessObject());
      Assert.assertEquals(expected.getReason(), actual.getReason());
   }
}

Back to the top