Skip to main content
summaryrefslogtreecommitdiffstats
blob: 54eda19cb651fa5b9ef4a94ecef3b560a942e417 (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
/*******************************************************************************
 * 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.List;

import org.eclipse.osee.framework.core.enums.DeletionFlag;
import org.eclipse.osee.framework.core.enums.RelationSide;
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.RelationType;
import org.eclipse.osee.framework.skynet.core.mocks.DataFactory;
import org.eclipse.osee.framework.skynet.core.relation.RelationFilterUtil.RelationMatcher;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Test Case for {@link RelationFilterUtil}
 * 
 * @author Roberto E. Escobar
 */
public class RelationFilterUtilTest {

   private static Branch branch1;
   private static RelationType relationType;

   @BeforeClass
   public static void setUp() {
      branch1 = MockDataFactory.createBranch(100);
      relationType = DataFactory.createRelationType(5);
   }

   @AfterClass
   public static void tearDown() {
      branch1 = null;
      relationType = null;
   }

   @Test
   public void testFilter() {
      List<RelationLink> sourceLinks = DataFactory.createLinks(4, branch1);
      Assert.assertEquals(4, sourceLinks.size());

      List<RelationLink> destination = new ArrayList<RelationLink>();
      RelationFilterUtil.filter(null, destination, null);
      Assert.assertEquals(0, destination.size());

      RelationFilterUtil.filter(sourceLinks, destination, null);
      Assert.assertEquals(4, destination.size());
   }

   @Test
   public void testIncludeExcludeDeletedFilter() {
      List<RelationLink> sourceLinks = DataFactory.createLinks(10, branch1);
      Assert.assertEquals(10, sourceLinks.size());

      RelationMatcher excludeDeleteds = RelationFilterUtil.createMatcher(DeletionFlag.EXCLUDE_DELETED);

      List<RelationLink> destination = new ArrayList<RelationLink>();
      RelationFilterUtil.filter(sourceLinks, destination, excludeDeleteds);
      Assert.assertEquals(10, destination.size());

      destination.clear();
      Assert.assertEquals(0, destination.size());

      DataFactory.setEveryOtherToDeleted(sourceLinks);
      RelationFilterUtil.filter(sourceLinks, destination, excludeDeleteds);
      Assert.assertEquals(5, destination.size());

      destination.clear();
      Assert.assertEquals(0, destination.size());

      RelationMatcher includeDeleteds = RelationFilterUtil.createMatcher(DeletionFlag.INCLUDE_DELETED);
      RelationFilterUtil.filter(sourceLinks, destination, includeDeleteds);
      Assert.assertEquals(10, destination.size());
   }

   @Test
   public void testFindFirstRelationLinkIdFilter() {
      List<RelationLink> sourceLinks = DataFactory.createLinks(4, branch1);
      List<RelationLink> destination = new ArrayList<RelationLink>();

      int relationLinkToFind = 2;
      RelationMatcher relationLinkIdMatcher =
         RelationFilterUtil.createFindFirstRelationLinkIdMatcher(relationLinkToFind);

      RelationFilterUtil.filter(sourceLinks, destination, relationLinkIdMatcher);

      // Found 1 -- Check we found what we expected
      Assert.assertEquals(1, destination.size());
      Assert.assertEquals(sourceLinks.get(2), destination.iterator().next());

      // Add two more Relations with RelationId == 2 so we have more than one match 
      // Check we only return the first match
      sourceLinks.add(DataFactory.createRelationLink(2, 55, 66, branch1, relationType));
      sourceLinks.add(DataFactory.createRelationLink(2, 77, 88, branch1, relationType));
      Assert.assertEquals(6, sourceLinks.size());

      destination.clear();
      Assert.assertEquals(0, destination.size());
      RelationFilterUtil.filter(sourceLinks, destination, relationLinkIdMatcher);

      Assert.assertEquals(1, destination.size());
      Assert.assertEquals(sourceLinks.get(2), destination.iterator().next());
   }

   @Test
   public void testFindFirstArtifactIdFilter() {
      RelationLink link1 = DataFactory.createRelationLink(0, 55, 66, branch1, relationType);
      RelationLink link2 = DataFactory.createRelationLink(1, 77, 55, branch1, relationType);

      List<RelationLink> sourceLinks = Arrays.asList(link1, link2);

      // Test Side A Match
      RelationMatcher sideAmatcher = RelationFilterUtil.createFindFirstRelatedArtIdMatcher(55, RelationSide.SIDE_A);

      List<RelationLink> destination = new ArrayList<RelationLink>();
      RelationFilterUtil.filter(sourceLinks, destination, sideAmatcher);

      Assert.assertEquals(1, destination.size());
      Assert.assertEquals(link1, destination.iterator().next());

      // Test Side B Match
      RelationMatcher sideBmatcher = RelationFilterUtil.createFindFirstRelatedArtIdMatcher(55, RelationSide.SIDE_B);
      List<RelationLink> destination2 = new ArrayList<RelationLink>();
      RelationFilterUtil.filter(sourceLinks, destination2, sideBmatcher);

      Assert.assertEquals(1, destination2.size());
      Assert.assertEquals(link2, destination2.iterator().next());
   }

   @Test
   public void testCompositeFilter() {
      RelationLink link1 = DataFactory.createRelationLink(0, 55, 66, branch1, relationType);
      RelationLink link2 = DataFactory.createRelationLink(1, 77, 55, branch1, relationType);
      link2.delete(false);
      RelationLink link3 = DataFactory.createRelationLink(3, 88, 55, branch1, relationType);
      RelationLink link4 = DataFactory.createRelationLink(4, 99, 55, branch1, relationType);

      List<RelationLink> sourceLinks = Arrays.asList(link1, link2, link3, link4);

      RelationMatcher sideBmatcher = RelationFilterUtil.createFindFirstRelatedArtIdMatcher(55, RelationSide.SIDE_B);

      // Find 3rd link since 2nd link is excluded because it is deleted
      RelationMatcher matcher1 = RelationFilterUtil.createMatcher(DeletionFlag.EXCLUDE_DELETED, sideBmatcher);

      List<RelationLink> destination = new ArrayList<RelationLink>();
      RelationFilterUtil.filter(sourceLinks, destination, matcher1);

      Assert.assertEquals(1, destination.size());
      Assert.assertEquals(link3, destination.iterator().next());

      // Find 2nd link since we are including deleteds
      RelationMatcher matcher2 = RelationFilterUtil.createMatcher(DeletionFlag.INCLUDE_DELETED, sideBmatcher);
      List<RelationLink> destination2 = new ArrayList<RelationLink>();
      RelationFilterUtil.filter(sourceLinks, destination2, matcher2);

      Assert.assertEquals(1, destination2.size());
      Assert.assertEquals(link2, destination2.iterator().next());
   }

}

Back to the top