Skip to main content
summaryrefslogtreecommitdiffstats
blob: 957806d00298f896d88820f5851f10ca1cb6eda7 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*******************************************************************************
 * 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.test.cases;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.Collection;
import junit.framework.Assert;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.data.SystemUser;
import org.eclipse.osee.framework.core.enums.BranchState;
import org.eclipse.osee.framework.core.enums.CoreArtifactTypes;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.skynet.core.User;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.skynet.core.artifact.ArtifactTypeManager;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.artifact.PurgeArtifacts;
import org.eclipse.osee.framework.skynet.core.artifact.operation.FinishUpdateBranchOperation;
import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery;
import org.eclipse.osee.framework.skynet.core.artifact.update.ConflictResolverOperation;
import org.eclipse.osee.framework.skynet.core.conflict.ConflictManagerExternal;
import org.eclipse.osee.support.test.util.DemoSawBuilds;
import org.junit.Before;

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

   @Before
   public void setUp() throws Exception {
      // This test should only be run on test db
      assertFalse(ClientSessionManager.isProductionDataStore());
      BranchManager.refreshBranches();
   }

   @org.junit.Test
   public void testCreateState() throws OseeCoreException {
      String originalBranchName = "Create State Branch";
      Branch workingBranch = null;
      try {
         User user = UserManager.getUser(SystemUser.OseeSystem);
         workingBranch = BranchManager.createWorkingBranch(DemoSawBuilds.SAW_Bld_1, originalBranchName, user);
         assertEquals(BranchState.CREATED, workingBranch.getBranchState());
         assertTrue(workingBranch.isEditable());
      } finally {
         if (workingBranch != null) {
            BranchManager.purgeBranch(workingBranch);
         }
      }
   }

   @org.junit.Test
   public void testModifiedState() throws OseeCoreException {
      String originalBranchName = "Modified State Branch";
      Branch workingBranch = null;
      try {
         User user = UserManager.getUser(SystemUser.OseeSystem);
         workingBranch = BranchManager.createWorkingBranch(DemoSawBuilds.SAW_Bld_1, originalBranchName, user);
         assertEquals(BranchState.CREATED, workingBranch.getBranchState());
         assertTrue(workingBranch.isEditable());

         Artifact change =
            ArtifactTypeManager.addArtifact(CoreArtifactTypes.SoftwareRequirement, workingBranch,
               "Test Object on Working Branch");
         change.persist();

         assertEquals(BranchState.MODIFIED, workingBranch.getBranchState());
         assertTrue(workingBranch.isEditable());
      } finally {
         if (workingBranch != null) {
            BranchManager.purgeBranch(workingBranch);
         }
      }
   }

   @org.junit.Test
   public void testDeleteState() throws OseeCoreException, InterruptedException {
      String originalBranchName = "Deleted State Branch";
      Branch workingBranch = null;
      try {
         User user = UserManager.getUser(SystemUser.OseeSystem);
         workingBranch = BranchManager.createWorkingBranch(DemoSawBuilds.SAW_Bld_1, originalBranchName, user);
         assertEquals(BranchState.CREATED, workingBranch.getBranchState());
         assertTrue(workingBranch.isEditable());

         Job job = BranchManager.deleteBranch(workingBranch);
         job.join();
         assertEquals(BranchState.DELETED, workingBranch.getBranchState());
         assertTrue(workingBranch.getArchiveState().isArchived());
         assertTrue(!workingBranch.isEditable());
         assertTrue(workingBranch.getBranchState().isDeleted());
      } finally {
         if (workingBranch != null) {
            // needed to allow for archiving to occur
            Thread.sleep(5000);
            BranchManager.purgeBranch(workingBranch);
         }
      }
   }

   @org.junit.Test
   public void testCommitState() throws OseeCoreException, InterruptedException {
      String originalBranchName = "Commit State Branch";
      Branch workingBranch = null;
      Artifact change = null;
      try {
         User user = UserManager.getUser(SystemUser.OseeSystem);
         workingBranch = BranchManager.createWorkingBranch(DemoSawBuilds.SAW_Bld_1, originalBranchName, user);
         assertEquals(BranchState.CREATED, workingBranch.getBranchState());
         assertTrue(workingBranch.isEditable());

         change =
            ArtifactTypeManager.addArtifact(CoreArtifactTypes.SoftwareRequirement, workingBranch, "A commit change");
         change.persist();

         assertEquals(BranchState.MODIFIED, workingBranch.getBranchState());
         assertTrue(workingBranch.isEditable());

         ConflictManagerExternal conflictManager =
            new ConflictManagerExternal(BranchManager.getBranch(DemoSawBuilds.SAW_Bld_1), workingBranch);
         BranchManager.commitBranch(null, conflictManager, true, false);

         assertEquals(BranchState.COMMITTED, workingBranch.getBranchState());
         assertTrue(workingBranch.getArchiveState().isArchived());
         assertTrue(!workingBranch.isEditable());
      } finally {
         if (workingBranch != null) {
            // needed to allow for archiving to occur
            Thread.sleep(5000);
            BranchManager.purgeBranch(workingBranch);
         }
      }
   }

   @org.junit.Test
   public void testRebaselineBranchNoChanges() throws Exception {
      String originalBranchName = "UpdateBranch No Changes Test";
      Branch workingBranch = null;
      try {
         User user = UserManager.getUser(SystemUser.OseeSystem);
         workingBranch = BranchManager.createWorkingBranch(DemoSawBuilds.SAW_Bld_1, originalBranchName, user);

         // Update the branch
         ConflictResolverOperation resolverOperation =
            new ConflictResolverOperation("Test 1 Resolver", BranchStateTest.class.getCanonicalName()) {

               @Override
               protected void doWork(IProgressMonitor monitor) throws Exception {
                  assertFalse("This code should not be executed since there shouldn't be any conflicts.", wasExecuted());
               }
            };

         Job job = BranchManager.updateBranch(workingBranch, resolverOperation);
         job.join();

         Assert.assertEquals(BranchState.DELETED, workingBranch.getBranchState());
         Assert.assertEquals(user.getArtId(), BranchManager.getAssociatedArtifact(workingBranch).getArtId());

         Collection<Branch> branches = BranchManager.getBranchesByName(originalBranchName);
         assertEquals("Check only 1 original branch", 1, branches.size());

         Branch newWorkingBranch = branches.iterator().next();
         assertTrue(workingBranch.getId() != newWorkingBranch.getId());
         assertEquals(originalBranchName, newWorkingBranch.getName());
         assertTrue("New Working branch was not editable", newWorkingBranch.isEditable());
         assertFalse("New Working branch was editable", workingBranch.isEditable());
      } finally {
         cleanup(originalBranchName, workingBranch, null);
      }
   }

   @org.junit.Test
   public void testRebaselineWithoutConflicts() throws Exception {
      String originalBranchName = "UpdateBranch Test 1";
      Artifact baseArtifact = null;
      Branch workingBranch = null;
      Artifact change = null;
      try {
         baseArtifact =
            ArtifactTypeManager.addArtifact(CoreArtifactTypes.SoftwareRequirement, DemoSawBuilds.SAW_Bld_1,
               "Test Object");
         baseArtifact.setSoleAttributeFromString(CoreAttributeTypes.Annotation, "This is the base annotation");
         baseArtifact.persist();

         User user = UserManager.getUser(SystemUser.OseeSystem);
         workingBranch = BranchManager.createWorkingBranch(DemoSawBuilds.SAW_Bld_1, originalBranchName, user);

         // Add a new artifact on the working branch
         change =
            ArtifactTypeManager.addArtifact(CoreArtifactTypes.SoftwareRequirement, workingBranch,
               "Test Object on Working Branch");
         change.persist();

         // Make a change on the parent
         baseArtifact.setSoleAttributeFromString(CoreAttributeTypes.Annotation, "This is the updated annotation");
         baseArtifact.persist();

         // Update the branch
         ConflictResolverOperation resolverOperation =
            new ConflictResolverOperation("Test 1 Resolver", BranchStateTest.class.getCanonicalName()) {

               @Override
               protected void doWork(IProgressMonitor monitor) throws Exception {
                  assertFalse("This code should not be executed since there shouldn't be any conflicts.", wasExecuted());
               }
            };

         Job job = BranchManager.updateBranch(workingBranch, resolverOperation);
         job.join();
         assertTrue("UpdateBranch was not successful", job.getResult().isOK());
         assertTrue("Resolver was executed", !resolverOperation.wasExecuted());

         checkBranchWasRebaselined(originalBranchName, workingBranch);
         // Check that the associated artifact remained unchanged
         assertEquals(workingBranch.getAssociatedArtifactId().intValue(), user.getArtId());

         Collection<Branch> branches = BranchManager.getBranchesByName(originalBranchName);
         assertEquals("Check only 1 original branch", 1, branches.size());

         Branch newWorkingBranch = branches.iterator().next();
         assertTrue(workingBranch.getId() != newWorkingBranch.getId());
         assertEquals(originalBranchName, newWorkingBranch.getName());
         assertTrue("New Working branch is editable", newWorkingBranch.isEditable());
      } finally {
         cleanup(originalBranchName, workingBranch, null, change, baseArtifact);
      }
   }

   @org.junit.Test
   public void testRebaselineWithConflicts() throws Exception {
      String originalBranchName = "UpdateBranch Test 2";
      Artifact baseArtifact = null;
      Branch workingBranch = null;
      Branch mergeBranch = null;
      Artifact sameArtifact = null;
      try {
         baseArtifact =
            ArtifactTypeManager.addArtifact(CoreArtifactTypes.SoftwareRequirement,
               BranchManager.getBranch(DemoSawBuilds.SAW_Bld_1), "Test Object");
         baseArtifact.setSoleAttributeFromString(CoreAttributeTypes.Annotation, "This is the base annotation");
         baseArtifact.persist();

         User user = UserManager.getUser(SystemUser.OseeSystem);
         workingBranch = BranchManager.createWorkingBranch(DemoSawBuilds.SAW_Bld_1, originalBranchName, user);

         // Modify same artifact on working branch
         sameArtifact = ArtifactQuery.getArtifactFromId(baseArtifact.getGuid(), workingBranch);
         sameArtifact.setSoleAttributeFromString(CoreAttributeTypes.Annotation,
            "This is the working branch update annotation");
         sameArtifact.persist();

         // Make a change on the parent
         baseArtifact.setSoleAttributeFromString(CoreAttributeTypes.Annotation, "This is the updated annotation");
         baseArtifact.persist();

         ConflictResolverOperation resolverOperation =
            new ConflictResolverOperation("Test 2 Resolver", BranchStateTest.class.getCanonicalName()) {

               @Override
               protected void doWork(IProgressMonitor monitor) throws Exception {
                  assertTrue("This code should have been executed since there should be conflicts.", wasExecuted());
               }
            };

         Job job = BranchManager.updateBranch(workingBranch, resolverOperation);
         job.join();

         IStatus status = getCauseStatus(job.getResult());
         String message =
            String.format("UpdateBranch was not successful\n %s", status.getMessage(), status.getException());
         assertTrue(message, job.getResult().isOK());
         assertTrue("Resolver not executed", resolverOperation.wasExecuted());

         assertTrue("Branch was archived", !workingBranch.getArchiveState().isArchived());
         assertTrue("Branch was not marked as rebaseline in progress",
            workingBranch.getBranchState().isRebaselineInProgress());
         assertTrue("Branch was not editable", workingBranch.isEditable());
         assertTrue("Branch state was set to rebaselined before complete",
            !workingBranch.getBranchState().isRebaselined());

         assertEquals("Branch name was changed before update was complete", originalBranchName, workingBranch.getName());

         // Check that a new destination branch exists
         Branch destinationBranch = resolverOperation.getConflictManager().getDestinationBranch();
         assertTrue("Branch name not set correctly",
            destinationBranch.getName().startsWith(String.format("%s - for update -", originalBranchName)));
         assertTrue("Branch was not editable", destinationBranch.isEditable());

         // Check that we have a merge branch
         mergeBranch = BranchManager.getMergeBranch(workingBranch, destinationBranch);
         assertTrue("MergeBranch was not editable", mergeBranch.isEditable());
         assertEquals("Merge Branch should be in Created State", BranchState.CREATED, mergeBranch.getBranchState());

         // Run FinishBranchUpdate and check
         FinishUpdateBranchOperation finishUpdateOperation =
            new FinishUpdateBranchOperation(resolverOperation.getConflictManager(), true, true);
         Operations.executeWork(finishUpdateOperation, new NullProgressMonitor(), -1);

         IStatus status1 = getCauseStatus(finishUpdateOperation.getStatus());
         message =
            String.format("FinishUpdateBranch was not successful\n %s", status1.getMessage(), status1.getException());
         assertTrue(message, status1.isOK());

         checkBranchWasRebaselined(originalBranchName, workingBranch);

         Collection<Branch> branches = BranchManager.getBranchesByName(originalBranchName);
         assertEquals("Check only 1 original branch", 1, branches.size());

         Branch newWorkingBranch = branches.iterator().next();
         assertTrue(workingBranch.getId() != newWorkingBranch.getId());
         assertEquals(originalBranchName, newWorkingBranch.getName());
         assertTrue("New Working branch is editable", newWorkingBranch.isEditable());

         // Swapped successfully
         assertEquals(destinationBranch.getId(), newWorkingBranch.getId());
      } catch (Exception ex) {
         throw ex;
      } finally {
         cleanup(originalBranchName, workingBranch, mergeBranch, sameArtifact, baseArtifact);

      }
   }

   private IStatus getCauseStatus(IStatus status) {
      IStatus toReturn = status;
      if (!status.isOK() && status.isMultiStatus()) {
         for (IStatus child : status.getChildren()) {
            Throwable error = child.getException();
            if (error != null) {
               toReturn = child;
               break;
            }
         }
      }
      return toReturn;
   }

   private void cleanup(String originalBranchName, Branch workingBranch, Branch mergeBranch, Artifact... toDelete) {
      try {
         if (mergeBranch != null) {
            BranchManager.purgeBranch(mergeBranch);
         }
         if (workingBranch != null) {
            purgeBranchAndChildren(workingBranch);
         }
         for (Branch branch : BranchManager.getBranchesByName(originalBranchName)) {
            purgeBranchAndChildren(branch);
         }
         if (toDelete != null && toDelete.length > 0) {
            new PurgeArtifacts(Arrays.asList(toDelete)).execute();
         }
      } catch (Exception ex) {
         // Do Nothing;
      }
   }

   private void purgeBranchAndChildren(Branch branch) throws OseeCoreException {
      for (Branch child : branch.getChildBranches(true)) {
         BranchManager.purgeBranch(child);
      }
      BranchManager.purgeBranch(branch);
   }

   private void checkBranchWasRebaselined(String originalBranchName, Branch branchToCheck) {
      assertTrue("Branch was not archived", branchToCheck.getArchiveState().isArchived());
      assertTrue("Branch was still editable", !branchToCheck.isEditable());
      assertTrue("Branch state was not set as rebaselined", branchToCheck.getBranchState().isRebaselined());
      assertTrue("Branch name not set correctly",
         branchToCheck.getName().startsWith(String.format("%s - moved by update on -", originalBranchName)));
   }

}

Back to the top