Skip to main content
summaryrefslogtreecommitdiffstats
blob: e2d66e8907916ed8b53dc910385f0648f72fcad9 (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
/*******************************************************************************
 * 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:
 *public static final CoreAttributeTypes   Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core.mocks;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import org.eclipse.osee.framework.core.data.IArtifactType;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.model.test.mocks.MockDataFactory;
import org.eclipse.osee.framework.core.model.type.ArtifactType;
import org.eclipse.osee.framework.core.model.type.RelationType;
import org.eclipse.osee.framework.skynet.core.relation.RelationLink;
import org.eclipse.osee.framework.skynet.core.types.IArtifact;
import org.junit.Assert;

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

   private DataFactory() {
      // Utility Class
   }

   public static ArtifactType fromToken(IArtifactType artifactType) {
      String name = artifactType.getName();
      String guid = artifactType.getGuid();
      return new ArtifactType(guid, name, true);
   }

   public static IArtifact createArtifact(String name, String guid) {
      int uniqueId = randomGenerator.nextInt();
      return createArtifact(uniqueId, name, guid, null, fromToken(CoreArtifactTypes.Artifact));
   }

   public static IArtifact createArtifact(int uniqueId, String name, String guid, Branch branch, ArtifactType artifactType) {
      return new MockIArtifact(uniqueId, name, guid, branch, artifactType);
   }

   public static IArtifact createArtifact(int uniqueId, String name, String guid, Branch branch) {
      return new MockIArtifact(uniqueId, name, guid, branch, fromToken(CoreArtifactTypes.Artifact));
   }

   public static RelationLink createRelationLink(int relationId, int artA, int artB, Branch branch, RelationType relationType) {
      return new RelationLink(new MockLinker("Linker"), artA, artB, branch, relationType, relationId, 0,
         "relation: " + relationId, ModificationType.MODIFIED);
   }

   public static List<RelationLink> createLinks(int total, int artA, int artB, Branch branch) {
      List<RelationLink> links = new ArrayList<RelationLink>();
      for (int index = 0; index < total; index++) {
         RelationType relationType = createRelationType(index);
         RelationLink link = DataFactory.createRelationLink(index, index + 1, index + 2, branch, relationType);
         links.add(link);
      }
      return links;
   }

   public static List<RelationLink> createLinks(int total, Branch branch) {
      List<RelationLink> links = new ArrayList<RelationLink>();
      for (int index = 0; index < total; index++) {
         RelationType relationType = createRelationType(index);
         RelationLink link = DataFactory.createRelationLink(index, index + 1, index + 2, branch, relationType);
         links.add(link);
      }
      return links;
   }

   public static List<RelationLink> createLinks(int total, Branch branch, RelationType relationType) {
      List<RelationLink> links = new ArrayList<RelationLink>();
      for (int index = 0; index < total; index++) {
         RelationLink link = DataFactory.createRelationLink(index, index + 1, index + 2, branch, relationType);
         links.add(link);
      }
      return links;
   }

   public static void setEveryOtherToDeleted(Collection<RelationLink> sourceLinks) {
      int count = 0;
      for (RelationLink link : sourceLinks) {
         if (count % 2 == 0) {
            link.delete(false);
         }
         count++;
      }

      int deletedCounts = 0;
      for (RelationLink link : sourceLinks) {
         if (link.isDeleted()) {
            deletedCounts++;
         }
      }
      int expected = sourceLinks.isEmpty() ? 0 : sourceLinks.size() / 2;
      Assert.assertEquals("Deleted relation link count did not match", expected, deletedCounts);
   }

   public static RelationType createRelationType(int id) {
      ArtifactType dummyArtType = MockDataFactory.createArtifactType(id);
      return MockDataFactory.createRelationType(id, dummyArtType, dummyArtType);
   }
}

Back to the top