Skip to main content
summaryrefslogtreecommitdiffstats
blob: 32dc3e8684cff733b8be30b4ab6437faafd72c67 (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
/*******************************************************************************
 * Copyright (c) 2012 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.data.ArtifactId.SENTINEL;
import static org.eclipse.osee.orcs.OrcsIntegrationRule.integrationRule;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import org.eclipse.osee.framework.core.data.ArtifactId;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TransactionId;
import org.eclipse.osee.framework.core.data.TransactionToken;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.BranchType;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.enums.CoreBranches;
import org.eclipse.osee.framework.core.enums.ModificationType;
import org.eclipse.osee.framework.core.enums.SystemUser;
import org.eclipse.osee.framework.core.model.change.ChangeItem;
import org.eclipse.osee.orcs.OrcsApi;
import org.eclipse.osee.orcs.OrcsBranch;
import org.eclipse.osee.orcs.data.ArtifactReadable;
import org.eclipse.osee.orcs.data.BranchReadable;
import org.eclipse.osee.orcs.db.mock.OsgiService;
import org.eclipse.osee.orcs.search.QueryFactory;
import org.eclipse.osee.orcs.transaction.TransactionBuilder;
import org.eclipse.osee.orcs.transaction.TransactionFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.rules.TestRule;

/**
 * @author David W. Miller
 */
public class OrcsBranchTest {

   private static final String ARTIFACT_NAME = "Joe Smith";

   @Rule
   public TestRule osgi = integrationRule(this);

   @Rule
   public TestName testName = new TestName();

   @OsgiService
   private OrcsApi orcsApi;

   private OrcsBranch branchOps;
   private QueryFactory query;
   private TransactionFactory txFactory;

   @Before
   public void setUp() throws Exception {
      branchOps = orcsApi.getBranchOps();
      query = orcsApi.getQueryFactory();
      txFactory = orcsApi.getTransactionFactory();
   }

   @Test
   public void testCreateBranch() throws Exception {
      int SOURCE_TX_ID = 8; // Chosen starting transaction on Common Branch
      int CHANGED_TX_ID = 9; // Transaction containing tested change

      // set up the initial branch
      IOseeBranch branch = IOseeBranch.create("PriorBranch");

      ArtifactId author = SystemUser.OseeSystem;

      TransactionId tx = TransactionId.valueOf(SOURCE_TX_ID);
      Callable<BranchReadable> callable = branchOps.createCopyTxBranch(branch, author, tx, ArtifactId.SENTINEL);

      assertNotNull(callable);
      BranchReadable priorBranch = callable.call();

      // in the database, on the common branch, the users are all created in transaction 9
      // the common branch will have one user named Joe Smith

      int coreResult = query.fromBranch(CoreBranches.COMMON).andNameEquals(ARTIFACT_NAME).getResults().size();
      assertEquals(1, coreResult);

      // we copied the branch at transaction 8, so, on the copied branch there will not be any
      // user Joe Smith

      int priorResult = query.fromBranch(priorBranch).andNameEquals(ARTIFACT_NAME).getResults().size();
      assertEquals(0, priorResult);

      // finally, we copy another branch at transaction id 9, this is the transaction that added the
      // user Joe Smith, so if the code is correct, and the copy includes the final
      // transaction, then this will produce the same result as the query of the common branch
      // create the branch with the copied transaction
      IOseeBranch postbranch = IOseeBranch.create("PostBranch");

      TransactionId tx1 = TransactionId.valueOf(CHANGED_TX_ID);
      Callable<BranchReadable> postCallable =
         branchOps.createCopyTxBranch(postbranch, author, tx1, ArtifactId.SENTINEL);

      assertNotNull(postCallable);
      BranchReadable postBranch = postCallable.call();

      int postResult = query.fromBranch(postBranch).andNameEquals(ARTIFACT_NAME).getResults().size();
      assertEquals(1, postResult);
   }

   @Test
   public void testCreateBranchCopyFromTx() throws Exception {
      // this test shows that the change report for a transaction for the newly copied branch is
      // the same as the change report on the branch the transaction is copied from
      TransactionId PRIOR_TX_ID = TransactionId.valueOf(15);
      TransactionId SOURCE_TX_ID = TransactionId.valueOf(16);

      // get the list of changes from the original branch
      TransactionToken priorTx = query.transactionQuery().andTxId(PRIOR_TX_ID).getResults().getExactlyOne();
      TransactionToken sourceTx = query.transactionQuery().andTxId(SOURCE_TX_ID).getResults().getExactlyOne();
      Callable<List<ChangeItem>> callable = branchOps.compareBranch(priorTx, sourceTx);
      List<ChangeItem> priorItems = callable.call();

      // create the branch with the copied transaction
      IOseeBranch branch = IOseeBranch.create("CopiedBranch");

      ArtifactId author = SystemUser.OseeSystem;

      Callable<BranchReadable> callableBranch =
         branchOps.createCopyTxBranch(branch, author, SOURCE_TX_ID, ArtifactId.SENTINEL);

      // the new branch will contain two transactions - these should have the same change report as the original branch
      BranchReadable postBranch = callableBranch.call();

      callable = branchOps.compareBranch(postBranch);
      List<ChangeItem> newItems = callable.call();
      compareBranchChanges(priorItems, newItems);
   }

   @Test
   public void testCommitBranchMissingArtifactsOnDestination() throws Exception {
      ArtifactId author = SystemUser.OseeSystem;
      // set up the initial branch
      IOseeBranch branch = IOseeBranch.create("BaseBranch");

      Callable<BranchReadable> callableBranch = branchOps.createTopLevelBranch(branch, author);
      BranchReadable base = callableBranch.call();
      // put some changes on the base branch
      TransactionBuilder tx = txFactory.createTransaction(base, author, "add some changes");
      ArtifactId folder = tx.createArtifact(CoreArtifactTypes.Folder, "BaseFolder");
      tx.commit();

      // create working branch off of base to make some changes
      // set up the child branch
      IOseeBranch branchName = IOseeBranch.create("ChildBranch");
      Callable<BranchReadable> callableChildBranch =
         branchOps.createWorkingBranch(branchName, author, base, ArtifactId.SENTINEL);

      BranchReadable childBranch = callableChildBranch.call();

      TransactionBuilder tx2 = txFactory.createTransaction(childBranch, author, "modify and make new arts");
      ArtifactReadable readableFolder = query.fromBranch(childBranch).andIds(folder).getResults().getExactlyOne();

      tx2.setName(readableFolder, "New Folder Name");
      tx2.setSoleAttributeFromString(readableFolder, CoreAttributeTypes.StaticId, "test id");

      // new artifacts should come across as new
      tx2.createArtifact(CoreArtifactTypes.Folder, "childBranch folder");
      tx2.commit();

      List<ChangeItem> expectedChanges = branchOps.compareBranch(childBranch).call();

      // create a disjoint working branch from common

      IOseeBranch commonName = IOseeBranch.create("ChildFromCommonBranch");
      Callable<BranchReadable> callableBranchFromCommon =
         branchOps.createWorkingBranch(commonName, author, CoreBranches.COMMON, ArtifactId.SENTINEL);
      BranchReadable commonChildBranch = callableBranchFromCommon.call();

      branchOps.commitBranch(author, childBranch, commonChildBranch).call();

      List<ChangeItem> actualChanges = branchOps.compareBranch(commonChildBranch).call();
      ensureExpectedAreInActual(expectedChanges, actualChanges);
   }

   @Test
   public void testBranchUpdateFields() throws Exception {
      String branchName = testName.getMethodName();

      IOseeBranch branch = IOseeBranch.create(branchName);

      branchOps.createBaselineBranch(branch, SystemUser.OseeSystem, CoreBranches.SYSTEM_ROOT, SENTINEL).call();

      BranchReadable actual = getBranch(branch);
      Long id = actual.getUuid();
      assertBranch(actual, id, branchName, BranchState.CREATED, BranchType.BASELINE, SENTINEL);

      branchName = "another-name";
      branchOps.changeBranchName(branch, branchName).call();

      actual = getBranch(branch);
      assertBranch(actual, id, branchName, BranchState.CREATED, BranchType.BASELINE, SENTINEL);

      BranchState branchState = BranchState.DELETED;
      branchOps.changeBranchState(branch, branchState).call();

      actual = getBranch(branch);
      assertBranch(actual, id, branchName, branchState, BranchType.BASELINE, SENTINEL);

      BranchType branchType = BranchType.WORKING;
      branchOps.changeBranchType(branch, branchType).call();

      actual = getBranch(branch);
      assertBranch(actual, id, branchName, branchState, branchType, SENTINEL);

      ArtifactId assocArtifact = SystemUser.OseeSystem;
      branchOps.associateBranchToArtifact(branch, assocArtifact).call();

      actual = getBranch(branch);
      assertBranch(actual, id, branchName, branchState, branchType, assocArtifact);

      branchOps.unassociateBranch(branch).call();

      actual = getBranch(branch);
      assertBranch(actual, id, branchName, branchState, branchType, SENTINEL);
   }

   private void assertBranch(BranchReadable branch, Long id, String name, BranchState state, BranchType type, ArtifactId assocArtId) {
      assertEquals(id, branch.getGuid());
      assertEquals(id, branch.getUuid());

      assertEquals(name, branch.getName());
      assertEquals(state, branch.getBranchState());
      assertEquals(type, branch.getBranchType());
      assertEquals(assocArtId, branch.getAssociatedArtifact());
   }

   private BranchReadable getBranch(BranchId branch) {
      return query.branchQuery().andId(branch).getResults().getExactlyOne();
   }

   private void ensureExpectedAreInActual(List<ChangeItem> expected, List<ChangeItem> actual) {
      for (ChangeItem expect : expected) {
         boolean contains = actual.contains(expect);
         if (!contains) {
            for (ChangeItem act : actual) {
               if (act.getItemId().equals(expect.getItemId()) && act.getArtId().equals(
                  expect.getArtId()) && act.getCurrentVersion().getModType().matches(ModificationType.INTRODUCED)) {
                  contains = true;
                  break;
               }
            }
            assertTrue(contains);
         }
      }
   }

   private void compareBranchChanges(List<ChangeItem> priorItems, List<ChangeItem> newItems) {
      Collections.sort(priorItems);
      Collections.sort(newItems);
      assertEquals(priorItems, newItems);
   }
}

Back to the top