Skip to main content
summaryrefslogtreecommitdiffstats
blob: facf5b3b8e7173679e9105b0c4083960d791239e (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) 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.client.integration.tests.integration.skynet.core;

import static org.eclipse.osee.client.demo.DemoChoice.OSEE_CLIENT_DEMO;
import java.util.List;
import org.eclipse.osee.client.test.framework.OseeClientIntegrationRule;
import org.eclipse.osee.client.test.framework.OseeLogMonitorRule;
import org.eclipse.osee.client.test.framework.TestInfo;
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.BranchArchivedState;
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.StorageState;
import org.eclipse.osee.framework.core.model.Branch;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.Pair;
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.DeleteBranchOperation;
import org.eclipse.osee.framework.skynet.core.conflict.ConflictManagerExternal;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.filter.IEventFilter;
import org.eclipse.osee.framework.skynet.core.event.listener.IBranchEventListener;
import org.eclipse.osee.framework.skynet.core.event.model.BranchEvent;
import org.eclipse.osee.framework.skynet.core.event.model.BranchEventType;
import org.eclipse.osee.framework.skynet.core.event.model.Sender;
import org.eclipse.osee.framework.skynet.core.httpRequests.PurgeBranchHttpRequestOperation;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

/**
 * @author Donald G. Dunne
 */
public class BranchEventTest {

   @Rule
   public OseeClientIntegrationRule integration = new OseeClientIntegrationRule(OSEE_CLIENT_DEMO);

   @Rule
   public OseeLogMonitorRule monitorRule = new OseeLogMonitorRule();

   @Rule
   public TestInfo method = new TestInfo();

   private IOseeBranch mainBranch;
   private IOseeBranch topLevel;

   private BranchEventListener branchEventListener;

   @Before
   public void setup() {
      String topLevelBranchName = String.format("%s_TOP_LEVEL", method.getQualifiedTestName());
      mainBranch = TokenFactory.createBranch(topLevelBranchName);

      branchEventListener = new BranchEventListener();
   }

   @After
   public void tearDown() throws OseeCoreException {
      if (topLevel != null) {
         boolean pending = OseeEventManager.getPreferences().isPendRunning();
         try {
            OseeEventManager.getPreferences().setPendRunning(true);
            Operations.executeWorkAndCheckStatus(new PurgeBranchHttpRequestOperation(mainBranch, true));
         } finally {
            OseeEventManager.getPreferences().setPendRunning(pending);
         }
      }
   }

   @Test
   public void testRegistration() throws Exception {
      OseeEventManager.removeAllListeners();
      Assert.assertEquals(0, OseeEventManager.getNumberOfListeners());

      OseeEventManager.addListener(branchEventListener);
      Assert.assertEquals(1, OseeEventManager.getNumberOfListeners());

      OseeEventManager.removeListener(branchEventListener);
      Assert.assertEquals(0, OseeEventManager.getNumberOfListeners());
   }

   /**
    * If all branch tests take longer than 20seconds, fail test, something went wrong.
    */
   @Test(timeout = 20000)
   public void testEvents() throws Exception {
      OseeEventManager.removeAllListeners();
      OseeEventManager.addListener(branchEventListener);

      Assert.assertEquals(1, OseeEventManager.getNumberOfListeners());
      boolean pending = OseeEventManager.getPreferences().isPendRunning();
      try {
         OseeEventManager.getPreferences().setPendRunning(true);

         topLevel = testEvents__topLevelAdded();

         Branch workingBranch = testEvents__workingAdded();
         testEvents__workingRenamed(workingBranch);
         testEvents__typeChange(workingBranch);
         testEvents__stateChange(workingBranch);
         testEvents__deleted(workingBranch);
         testEvents__purged();
         Branch committedBranch = testEvents__committed();
         testEvents__changeArchiveState(committedBranch);

      } finally {
         OseeEventManager.getPreferences().setPendRunning(pending);
         OseeEventManager.removeListener(branchEventListener);
      }
   }

   private Branch testEvents__changeArchiveState(Branch committedBranch) throws Exception {
      branchEventListener.reset();

      Assert.assertNotNull(committedBranch);
      Assert.assertEquals(BranchArchivedState.ARCHIVED, committedBranch.getArchiveState());
      BranchManager.updateBranchArchivedState(null, committedBranch.getUuid(), BranchArchivedState.UNARCHIVED);

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.ArchiveStateUpdated,
         committedBranch);

      Assert.assertEquals(BranchArchivedState.UNARCHIVED, committedBranch.getArchiveState());
      Assert.assertFalse(BranchManager.isEditable(committedBranch));
      return committedBranch;
   }

   private Branch testEvents__committed() throws Exception {
      Branch workingBranch =
         BranchManager.createWorkingBranch(mainBranch, method.getQualifiedTestName() + " - to commit");

      Assert.assertNotNull(workingBranch);

      Assert.assertNotNull(workingBranch);
      Artifact newArt = ArtifactTypeManager.addArtifact(CoreArtifactTypes.GeneralData, workingBranch);
      newArt.persist(getClass().getSimpleName());
      ConflictManagerExternal conflictManager = new ConflictManagerExternal(mainBranch, workingBranch);
      branchEventListener.reset();
      BranchManager.commitBranch(null, conflictManager, true, true);

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.Committing, workingBranch);
      verifyReceivedBranchStatesEvent(branchEventListener.getSecondResults(), BranchEventType.Committed, workingBranch);

      Assert.assertEquals(BranchState.COMMITTED, workingBranch.getBranchState());
      Assert.assertFalse(BranchManager.isEditable(workingBranch));
      return workingBranch;
   }

   private Branch testEvents__purged() throws Exception {
      Branch workingBranch =
         BranchManager.createWorkingBranch(mainBranch, method.getQualifiedTestName() + " - to purge");

      Assert.assertNotNull(workingBranch);

      Assert.assertNotNull(workingBranch);

      branchEventListener.reset();
      Operations.executeWorkAndCheckStatus(new PurgeBranchHttpRequestOperation(workingBranch, false));

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.Purging, workingBranch);
      verifyReceivedBranchStatesEvent(branchEventListener.getSecondResults(), BranchEventType.Purged, workingBranch);

      Assert.assertEquals(BranchState.PURGED, workingBranch.getBranchState());
      Assert.assertEquals(StorageState.PURGED, workingBranch.getStorageState());
      Assert.assertFalse(BranchManager.isEditable(workingBranch));
      Assert.assertFalse("Branch should not exist", BranchManager.branchExists(workingBranch));
      return workingBranch;
   }

   private Branch testEvents__deleted(Branch workingBranch) throws Exception {
      Assert.assertNotNull(workingBranch);
      Assert.assertNotSame(BranchState.DELETED, workingBranch.getBranchState());

      branchEventListener.reset();
      Operations.executeWorkAndCheckStatus(new DeleteBranchOperation(workingBranch));

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.Deleting, workingBranch);
      verifyReceivedBranchStatesEvent(branchEventListener.getSecondResults(), BranchEventType.Deleted, workingBranch);

      Assert.assertEquals(BranchState.DELETED, workingBranch.getBranchState());
      return workingBranch;
   }

   private Branch testEvents__stateChange(Branch workingBranch) throws Exception {
      branchEventListener.reset();

      Assert.assertNotNull(workingBranch);
      Assert.assertEquals(BranchState.CREATED, workingBranch.getBranchState());
      BranchManager.updateBranchState(null, workingBranch.getUuid(), BranchState.MODIFIED);

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.StateUpdated,
         workingBranch);

      Assert.assertEquals(BranchState.MODIFIED, workingBranch.getBranchState());
      return workingBranch;
   }

   private Branch testEvents__typeChange(Branch workingBranch) throws Exception {
      branchEventListener.reset();
      Assert.assertNotNull(workingBranch);
      Assert.assertEquals(BranchType.WORKING, workingBranch.getBranchType());
      BranchManager.updateBranchType(null, workingBranch.getUuid(), BranchType.BASELINE);

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.TypeUpdated,
         workingBranch);

      Assert.assertEquals(BranchType.BASELINE, workingBranch.getBranchType());
      return workingBranch;
   }

   private Branch testEvents__workingRenamed(Branch workingBranch) throws Exception {
      branchEventListener.reset();

      Assert.assertNotNull(workingBranch);
      String newName = method.getQualifiedTestName() + " - working renamed";
      workingBranch.setName(newName);
      BranchManager.persist(workingBranch);

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.Renamed, workingBranch);

      Assert.assertEquals(newName, workingBranch.getName());
      Assert.assertNotNull(BranchManager.getBranchesByName(newName));
      return workingBranch;
   }

   private Branch testEvents__workingAdded() throws Exception {
      branchEventListener.reset();

      Branch workingBranch =
         BranchManager.createWorkingBranch(mainBranch, method.getQualifiedTestName() + " - working");
      Assert.assertNotNull(workingBranch);

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.Added, null);

      return workingBranch;
   }

   private IOseeBranch testEvents__topLevelAdded() throws Exception {
      branchEventListener.reset();
      IOseeBranch branch = BranchManager.createTopLevelBranch(mainBranch);
      Assert.assertNotNull(branch);

      verifyReceivedBranchStatesEvent(branchEventListener.getFirstResults(), BranchEventType.Added, mainBranch);

      return branch;
   }

   private void verifyReceivedBranchStatesEvent(Pair<Sender, BranchEvent> eventPair, BranchEventType expectedEnumState, BranchId expectedBranch) {
      Sender receivedSender = eventPair.getFirst();
      BranchEvent receivedBranchEvent = eventPair.getSecond();

      Assert.assertEquals(expectedEnumState, receivedBranchEvent.getEventType());
      if (isRemoteTest()) {
         Assert.assertTrue(receivedSender.isRemote());
      } else {
         Assert.assertTrue(receivedSender.isLocal());
      }

      if (expectedBranch != null) {
         Assert.assertEquals(expectedBranch, receivedBranchEvent.getSourceBranch());
      }
   }

   protected boolean isRemoteTest() {
      return false;
   }

   private class BranchEventListener implements IBranchEventListener {
      private BranchEvent firstBranchEvent, secondBranchEvent;
      private Sender firstSender, secondSender;
      private boolean receivedFirstUpdate = false, receivedSecondUpdate = false;

      public synchronized void reset() {
         firstBranchEvent = null;
         secondBranchEvent = null;
         receivedFirstUpdate = false;
         receivedSecondUpdate = false;
      }

      @Override
      public List<? extends IEventFilter> getEventFilters() {
         return null;
      }

      @Override
      public synchronized void handleBranchEvent(Sender sender, BranchEvent branchEvent) {
         if (this.firstBranchEvent == null) {
            this.firstBranchEvent = branchEvent;
            this.firstSender = sender;
            receivedFirstUpdate = true;
         } else if (this.secondBranchEvent == null) {
            this.secondBranchEvent = branchEvent;
            this.secondSender = sender;
            receivedSecondUpdate = true;
         }
         notify();
      }

      public synchronized Pair<Sender, BranchEvent> getFirstResults() throws InterruptedException {
         while (!receivedFirstUpdate) {
            wait();
         }
         receivedFirstUpdate = false;
         return new Pair<Sender, BranchEvent>(firstSender, firstBranchEvent);
      }

      public synchronized Pair<Sender, BranchEvent> getSecondResults() throws InterruptedException {
         while (!receivedSecondUpdate) {
            wait();
         }
         receivedSecondUpdate = false;
         return new Pair<Sender, BranchEvent>(secondSender, secondBranchEvent);
      }
   };
}

Back to the top