Skip to main content
summaryrefslogtreecommitdiffstats
blob: 726c17ef72a1ab780850737cbd7d046baeac27cc (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
/*******************************************************************************
 * Copyright (c) 2013 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.orcs.api;

import static org.eclipse.osee.framework.core.enums.BranchState.MODIFIED;
import static org.eclipse.osee.framework.core.enums.BranchType.BASELINE;
import static org.eclipse.osee.framework.core.enums.CoreBranches.COMMON;
import static org.eclipse.osee.framework.core.enums.CoreBranches.SYSTEM_ROOT;
import static org.eclipse.osee.framework.core.enums.DemoBranches.CIS_Bld_1;
import static org.eclipse.osee.framework.core.enums.DemoBranches.SAW_Bld_1;
import static org.eclipse.osee.framework.core.enums.DemoBranches.SAW_Bld_2;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.OrcsBranch;
import org.eclipse.osee.orcs.data.ArchiveOperation;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.db.mock.OrcsIntegrationByClassRule;
import org.eclipse.osee.orcs.db.mock.OseeClassDatabase;
import org.eclipse.osee.orcs.db.mock.OsgiService;
import org.eclipse.osee.orcs.search.BranchQuery;
import org.eclipse.osee.orcs.search.QueryFactory;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.rules.TestRule;
import org.junit.runners.MethodSorters;

/**
 * @author Roberto E. Escobar
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class OrcsBranchQueryTest {

   @Rule
   public TestRule db = OrcsIntegrationByClassRule.integrationRule(this);

   @Rule
   public TestName testName = new TestName();

   @OsgiService
   private OrcsApi orcsApi;

   private QueryFactory factory;

   @Before
   public void setup() {
      factory = orcsApi.getQueryFactory();
   }

   @AfterClass
   public static void cleanup() throws Exception {
      OseeClassDatabase.cleanup();
   }

   @Test
   public void testGetAll() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();

      ResultSet<BranchReadable> results = query.getResults();
      assertEquals(8, results.size());
      assertEquals(8, query.getCount());

      List<BranchReadable> list = results.getList();

      // list, IOseeBranch, BranchType, BranchState, isArchived, parentId, baseTx, sourceTx, assocArtId
      assertBranch(list, SYSTEM_ROOT, BranchType.SYSTEM_ROOT, MODIFIED, false, BranchId.SENTINEL, -1);
      assertBranch(list, SAW_Bld_1, BASELINE, MODIFIED, false, SYSTEM_ROOT, -1);
      assertBranch(list, CIS_Bld_1, BASELINE, MODIFIED, false, SYSTEM_ROOT, -1);
      assertBranch(list, SAW_Bld_2, BASELINE, MODIFIED, false, SAW_Bld_1, 11);
      assertBranch(list, COMMON, BASELINE, MODIFIED, false, SYSTEM_ROOT, -1);
   }

   @Test
   public void testGetByType() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();
      query.andIsOfType(BranchType.SYSTEM_ROOT);

      assertEquals(1, query.getCount());
      assertEquals(CoreBranches.SYSTEM_ROOT, query.getResults().getExactlyOne());

      query = factory.branchQuery();
      query.andIsOfType(BASELINE);

      ResultSet<BranchReadable> results = query.getResults();
      assertEquals(4, results.size());
      assertEquals(4, query.getCount());

      Iterator<BranchReadable> iterator = results.iterator();
      BranchReadable branch1 = iterator.next();
      BranchReadable branch2 = iterator.next();
      BranchReadable branch3 = iterator.next();
      BranchReadable branch4 = iterator.next();

      assertEquals(SAW_Bld_1, branch1);
      assertEquals(CIS_Bld_1, branch2);
      assertEquals(SAW_Bld_2, branch3);
      assertEquals(COMMON, branch4);
   }

   @Test
   public void testGetByUuid() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();
      query.andUuids(5, 3);

      ResultSet<BranchReadable> results = query.getResults();
      assertEquals(2, results.size());
      assertEquals(2, query.getCount());

      Iterator<BranchReadable> iterator = results.iterator();
      BranchReadable branch1 = iterator.next();
      BranchReadable branch2 = iterator.next();

      assertEquals(SAW_Bld_1, branch1);
      assertEquals(SAW_Bld_2, branch2);
   }

   @Test
   public void testGetByNameEquals() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();
      query.andNameEquals("CIS_Bld_1");

      assertEquals(CIS_Bld_1, query.getResults().getExactlyOne());
      assertEquals(1, query.getCount());
   }

   @Test
   public void testGetByNamePatternEquals() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();
      query.andNamePattern("SAW.*?_Bld.*");

      ResultSet<BranchReadable> results = query.getResults();
      assertEquals(2, results.size());
      assertEquals(2, query.getCount());

      Iterator<BranchReadable> iterator = results.iterator();
      BranchReadable branch1 = iterator.next();
      BranchReadable branch2 = iterator.next();

      assertEquals(SAW_Bld_1, branch1);
      assertEquals(SAW_Bld_2, branch2);
   }

   @Test
   public void testGetByState() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();
      query.andStateIs(MODIFIED);

      assertEquals(7, query.getCount());
   }

   @Test
   public void testGetByChildOf() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();
      query.andIsChildOf(SAW_Bld_1);

      ResultSet<BranchReadable> children = query.getResults();
      assertEquals(4, query.getCount());
      assertEquals(SAW_Bld_2, children.iterator().next());

      query = factory.branchQuery();
      query.andIsChildOf(SYSTEM_ROOT);

      ResultSet<BranchReadable> results = query.getResults();
      assertEquals(7, results.size());
      assertEquals(7, query.getCount());

      List<BranchReadable> list = results.getList();

      for (IOseeBranch branch : Arrays.asList(SAW_Bld_1, CIS_Bld_1, SAW_Bld_2, COMMON)) {
         assertTrue("Missing expected branch " + branch, list.contains(branch));
      }
   }

   @Test
   public void testGetByAncestorOf() throws OseeCoreException {
      BranchQuery query = factory.branchQuery();
      query.andIsAncestorOf(SAW_Bld_2);

      ResultSet<BranchReadable> results = query.getResults();
      assertEquals(2, results.size());
      assertEquals(2, query.getCount());

      Iterator<BranchReadable> iterator = results.iterator();
      BranchReadable branch1 = iterator.next();
      BranchReadable branch2 = iterator.next();

      assertEquals(SYSTEM_ROOT, branch1);
      assertEquals(SAW_Bld_1, branch2);

      query = factory.branchQuery();
      query.andIsAncestorOf(COMMON);

      results = query.getResults();
      assertEquals(1, results.size());
      assertEquals(1, query.getCount());

      iterator = results.iterator();
      branch1 = iterator.next();

      assertEquals(SYSTEM_ROOT, branch1);
   }

   @Test
   public void zTestGetIncludeArchived() throws Exception {
      IOseeBranch child = TokenFactory.createBranch(testName.getMethodName());

      BranchQuery query = factory.branchQuery();
      query.andIds(CIS_Bld_1);
      BranchReadable cisBranch = query.getResults().getExactlyOne();

      IOseeBranch actual = createBranch(cisBranch, child);
      assertEquals(child, actual);

      getBranchOps().archiveUnarchiveBranch(actual, ArchiveOperation.ARCHIVE).call();

      query = factory.branchQuery();
      query.andIds(child);
      query.excludeArchived();
      assertNull(query.getResults().getOneOrNull());

      query.includeArchived();
      assertEquals(child, query.getResults().getExactlyOne());
   }

   // Do remaining tests last cause they create branches which changes query counts

   @Test
   public void zTestGetIncludeDeleted() throws Exception {
      IOseeBranch child = TokenFactory.createBranch(testName.getMethodName());

      BranchQuery query = factory.branchQuery();
      query.andIds(CIS_Bld_1);
      BranchReadable cisBranch = query.getResults().getExactlyOne();

      IOseeBranch actual = createBranch(cisBranch, child);
      assertEquals(child, actual);

      query = factory.branchQuery();
      query.andIds(child);
      assertEquals(child, query.getResults().getOneOrNull());

      getBranchOps().changeBranchState(actual, BranchState.DELETED).call();

      query = factory.branchQuery();
      query.andIds(child);
      query.excludeDeleted();
      assertNull(query.getResults().getOneOrNull());

      query.includeDeleted();
      assertEquals(child, query.getResults().getExactlyOne());

   }

   @Test
   public void zTestGetWithMultipleConditions1() throws Exception {
      IOseeBranch child = TokenFactory.createBranch(testName.getMethodName());

      BranchId actual = createBranch(SAW_Bld_2, child);

      BranchQuery query = factory.branchQuery();
      query.andIsOfType(BranchType.WORKING).andIsChildOf(SAW_Bld_1).andStateIs(BranchState.CREATED);

      ResultSet<BranchReadable> results = query.getResults();
      assertEquals(actual, results.getExactlyOne());
      assertEquals(1, query.getCount());

   }

   private IOseeBranch createBranch(IOseeBranch parent, IOseeBranch uuid) throws Exception {
      ArtifactReadable author = getSystemUser();
      return getBranchOps().createWorkingBranch(uuid, author, parent, null).call();
   }

   @SuppressWarnings("unchecked")
   private ArtifactReadable getSystemUser() throws OseeCoreException {
      return factory.fromBranch(CoreBranches.COMMON).andIds(SystemUser.OseeSystem).getResults().getExactlyOne();
   }

   private OrcsBranch getBranchOps() {
      return orcsApi.getBranchOps();
   }

   private static void assertBranch(List<BranchReadable> list, IOseeBranch token, BranchType type, BranchState state, boolean isArchived, BranchId parent, int assocArtId) {
      BranchReadable found = null;
      for (BranchReadable branch : list) {
         if (branch.getId().equals(token.getId())) {
            found = branch;
            break;
         }
      }
      Assert.assertNotNull(found);
      assertBranch(found, token.getName(), token.getGuid(), type, state, isArchived, parent, assocArtId);
   }

   private static void assertBranch(BranchReadable actual, String name, Long localId, BranchType type, BranchState state, boolean isArchived, BranchId parent, int assocArtId) {
      assertEquals(name, actual.getName());
      assertEquals(localId, actual.getUuid());

      assertEquals(type, actual.getBranchType());
      assertEquals(state, actual.getBranchState());
      assertEquals(isArchived, actual.getArchiveState().isArchived());
      assertEquals(parent, actual.getParentBranch());
      assertEquals(assocArtId, actual.getAssociatedArtifactId());
   }
}

Back to the top