Skip to main content
summaryrefslogtreecommitdiffstats
blob: e51a334e43fc87b2e060cd2800df6bcbbb5ef33a (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*******************************************************************************
 * 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.ats.core.internal.state;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.osee.ats.api.IAtsServices;
import org.eclipse.osee.ats.api.IAtsWorkItem;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.workdef.IAtsStateDefinition;
import org.eclipse.osee.ats.api.workdef.IAtsWorkDefinition;
import org.eclipse.osee.ats.api.workdef.StateType;
import org.eclipse.osee.ats.api.workflow.log.IAtsLogFactory;
import org.eclipse.osee.ats.core.AbstractUserTest;
import org.eclipse.osee.ats.core.mock.MockWorkItem;
import org.eclipse.osee.ats.core.model.impl.WorkStateImpl;
import org.eclipse.osee.ats.core.users.AtsCoreUsers;
import org.eclipse.osee.framework.jdk.core.type.OseeArgumentException;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;

/**
 * @author Donald G. Dunne
 */
public class StateManagerTest extends AbstractUserTest {

   private StateManager stateMgr;

   // @formatter:off
   @Mock private IAtsWorkDefinition workDef;
   @Mock private IAtsStateDefinition endorseStateDef;
   @Mock private IAtsStateDefinition analyzeStateDef;
   @Mock private IAtsLogFactory logFactory;
   @Mock private IAtsServices services;
   // @formatter:on

   @Override
   @Before
   public void setup() {
      super.setup();

      MockWorkItem workItem = Mockito.spy(new MockWorkItem("mock work item", "Endorse", StateType.Working));
      stateMgr = Mockito.spy(new StateManager(workItem, logFactory, services));
      when(workItem.getWorkDefinition()).thenReturn(workDef);
      when(workDef.getStateByName("endorse")).thenReturn(endorseStateDef);
      when(endorseStateDef.getStateType()).thenReturn(StateType.Working);

      when(workDef.getStateByName("analyze")).thenReturn(analyzeStateDef);
      when(analyzeStateDef.getStateType()).thenReturn(StateType.Working);

   }

   @Test
   public void testSetNotificationListener() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.addAssignee(steve);

      stateMgr.removeAssignee(steve);

      stateMgr.setAssignee(steve);
      Assert.assertEquals(1, stateMgr.getAssigneesAdded().size());
      Assert.assertEquals(steve, stateMgr.getAssigneesAdded().iterator().next());

   }

   @Test
   public void testAddAssignee() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");

      Assert.assertTrue(stateMgr.getAssignees().isEmpty());
      stateMgr.addAssignees("endorse", null);
      Assert.assertTrue(stateMgr.getAssignees().isEmpty());

      List<IAtsUser> users = new ArrayList<>();
      stateMgr.addAssignees("endorse", users);
      Assert.assertTrue(stateMgr.getAssignees().isEmpty());

      Assert.assertTrue(stateMgr.getAssignees().isEmpty());
      stateMgr.addAssignee(joe);
      Assert.assertEquals(1, stateMgr.getAssignees().size());

      stateMgr.addAssignee(joe);
      Assert.assertEquals(1, stateMgr.getAssignees().size());

   }

   @Test(expected = OseeArgumentException.class)
   public void testAddAssignee_exception() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");

      stateMgr.addAssignee(AtsCoreUsers.GUEST_USER);
   }

   @Test
   public void testSetAssignee() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");

      Assert.assertTrue(stateMgr.getAssignees().isEmpty());
      stateMgr.setAssignee("endorse", null);
      Assert.assertTrue(stateMgr.getAssignees().isEmpty());
      stateMgr.setAssignee(null);
      Assert.assertTrue(stateMgr.getAssignees().isEmpty());

      List<IAtsUser> users = new ArrayList<>();
      stateMgr.setAssignees("endorse", users);
      Assert.assertTrue(stateMgr.getAssignees().isEmpty());

      stateMgr.setAssignees("endorse", null);
      Assert.assertTrue(stateMgr.getAssignees().isEmpty());

      Assert.assertTrue(stateMgr.getAssignees().isEmpty());
      stateMgr.setAssignee(joe);
      Assert.assertEquals(1, stateMgr.getAssignees().size());

      stateMgr.setAssignee(joe);
      Assert.assertEquals(1, stateMgr.getAssignees().size());
   }

   @Test
   public void testSetAssignee_removeUnassigned() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");

      stateMgr.setAssignee(AtsCoreUsers.UNASSIGNED_USER);
      Assert.assertEquals(1, stateMgr.getAssignees().size());
      Assert.assertEquals(AtsCoreUsers.UNASSIGNED_USER, stateMgr.getAssignees().iterator().next());

      stateMgr.setAssignees(Arrays.asList(joe, AtsCoreUsers.UNASSIGNED_USER));
      Assert.assertEquals(1, stateMgr.getAssignees().size());
      Assert.assertEquals(joe, stateMgr.getAssignees().iterator().next());
   }

   @Test
   public void testSetAssignees_nextStateNotification() throws OseeCoreException {
      // create state with two assignees
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      List<IAtsUser> currentAssignees = Arrays.asList(joe, steve);
      stateMgr.setAssignees(currentAssignees);
      stateMgr.getInitialAssignees().addAll(currentAssignees);

      // create next state with no assignees
      stateMgr.addState(new WorkStateImpl("analyze"));

      stateMgr.setAssignees("analyze", currentAssignees);
      Assert.assertTrue(stateMgr.getAssignees().contains(joe));
      Assert.assertTrue(stateMgr.getAssignees().contains(steve));
      Assert.assertEquals("shouldn't notify anyone previously assigned", 0, stateMgr.getAssigneesAdded().size());
   }

   @Test
   public void testSetAssignees_sameStateNotification() throws OseeCoreException {
      // create state with two assignees
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      List<IAtsUser> currentAssignees = Arrays.asList(joe);
      stateMgr.setAssignees(currentAssignees);
      stateMgr.getInitialAssignees().addAll(currentAssignees);

      List<IAtsUser> newAssignees = Arrays.asList(joe, steve);
      stateMgr.setAssignees("endorse", newAssignees);
      Assert.assertTrue(stateMgr.getAssignees().contains(joe));
      Assert.assertTrue(stateMgr.getAssignees().contains(steve));
      Assert.assertEquals("should notify new assignee steve", 1, stateMgr.getAssigneesAdded().size());
   }

   @Test
   public void testAddState_exception() {
      stateMgr.addState(new WorkStateImpl("endorse"));

      stateMgr.addState(new WorkStateImpl("endorse"), false);
   }

   @Test
   public void testAddState_exception2() {
      stateMgr.addState(new WorkStateImpl("endorse"));

      stateMgr.addState("endorse", new LinkedList<IAtsUser>(), 34, 23, false);
   }

   @Test(expected = OseeArgumentException.class)
   public void testSetAssignee_exception() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");

      stateMgr.setAssignee(AtsCoreUsers.GUEST_USER);
   }

   @Test
   public void testIsUnAssigned() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      Assert.assertFalse(stateMgr.isUnAssigned());
      stateMgr.addAssignee(AtsCoreUsers.UNASSIGNED_USER);
      Assert.assertTrue(stateMgr.isUnAssigned());
   }

   @Test
   public void testIsUnAssignedSolely() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      Assert.assertFalse(stateMgr.isUnAssignedSolely());
      stateMgr.addAssignee(AtsCoreUsers.UNASSIGNED_USER);
      Assert.assertTrue(stateMgr.isUnAssignedSolely());
      stateMgr.addAssignee(joe);
      Assert.assertFalse(stateMgr.isUnAssignedSolely());
      stateMgr.removeAssignee(AtsCoreUsers.UNASSIGNED_USER);
      Assert.assertFalse(stateMgr.isUnAssignedSolely());
   }

   @Test
   public void testGetAssigneesStrString() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.addAssignee(steve);
      stateMgr.addAssignee(joe);
      Assert.assertEquals("steve; joe", stateMgr.getAssigneesStr());
      Assert.assertEquals("steve; joe", stateMgr.getAssigneesStr("endorse"));
      Assert.assertEquals("", stateMgr.getAssigneesStr("analyze"));
      Assert.assertEquals("stev...", stateMgr.getAssigneesStr("endorse", 5));
      Assert.assertEquals("steve; joe", stateMgr.getAssigneesStr("endorse", 50));
   }

   @Test
   public void testAddAssignees() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.addAssignees(Arrays.asList(joe, alice));
      Assert.assertEquals("joe; alice", stateMgr.getAssigneesStr());
   }

   @Test
   public void testAddAssignees_state() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.addAssignees("endorse", Arrays.asList(joe, alice));
      Assert.assertEquals("joe; alice", stateMgr.getAssigneesStr());
   }

   @Test
   public void testSetAssigneesIAtsUser() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.setAssignee(joe);
      Assert.assertEquals("joe", stateMgr.getAssigneesStr());
      stateMgr.setAssignee(alice);
      Assert.assertEquals("alice", stateMgr.getAssigneesStr());
   }

   @Test
   public void testSetAssigneeStringIAtsUser() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.setAssignee("endorse", joe);
      Assert.assertEquals("joe", stateMgr.getAssigneesStr());
      stateMgr.setAssignee("endorse", alice);
      Assert.assertEquals("alice", stateMgr.getAssigneesStr());
   }

   @Test
   public void testSetAssigneesListOfQextendsIAtsUser() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.addAssignees(Arrays.asList(joe, alice));
      Assert.assertEquals("joe; alice", stateMgr.getAssigneesStr());
      stateMgr.setAssignees(Arrays.asList(steve));
      Assert.assertEquals("steve", stateMgr.getAssigneesStr());
   }

   @Test
   public void testSetAssigneesStringListOfQextendsIAtsUser() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.setAssignee("endorse", joe);
      Assert.assertEquals("joe", stateMgr.getAssigneesStr());
      stateMgr.setAssignee("endorse", alice);
      Assert.assertEquals("alice", stateMgr.getAssigneesStr());
   }

   @Test
   public void testRemoveAssigneeIAtsUser() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.setAssignees("endorse", Arrays.asList(joe, alice));
      Assert.assertEquals("joe; alice", stateMgr.getAssigneesStr());
      stateMgr.removeAssignee(alice);
      Assert.assertEquals("joe", stateMgr.getAssigneesStr());
   }

   @Test
   public void testClearAssignees() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.setAssignees("endorse", Arrays.asList(joe, alice));
      Assert.assertEquals("joe; alice", stateMgr.getAssigneesStr());
      stateMgr.clearAssignees();
      Assert.assertTrue(stateMgr.getAssignees().isEmpty());
   }

   @Test
   public void testIsStateVisited() {
      Assert.assertFalse(stateMgr.isStateVisited("endorse"));
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      Assert.assertTrue(stateMgr.isStateVisited("endorse"));
      stateMgr.addState(new WorkStateImpl("analyze"));
      Assert.assertTrue(stateMgr.isStateVisited("analyze"));

   }

   @Test
   public void testCreateState() {
      stateMgr.createState("endorse");
      Assert.assertTrue(stateMgr.isStateVisited("endorse"));
      stateMgr.createState("endorse");
      Assert.assertEquals(1, stateMgr.getVisitedStateNames().size());
   }

   @Test
   public void testAddStateStringListOfQextendsIAtsUserDoubleInt() {
      stateMgr.addState("endorse", Arrays.asList(joe), 4.2, 4);
      Assert.assertTrue(stateMgr.isStateVisited("endorse"));
      Assert.assertEquals(1, stateMgr.getVisitedStateNames().size());
      Assert.assertEquals(1, stateMgr.getAssignees("endorse").size());
      Assert.assertEquals(4.2, stateMgr.getHoursSpent("endorse"), 0.0);
      Assert.assertEquals(4, stateMgr.getPercentComplete("endorse"));
   }

   @Test(expected = OseeStateException.class)
   public void testGetAssigneesForState() throws OseeStateException {
      stateMgr.getAssignees();
   }

   @Test
   public void testAddStateWorkState() {
      stateMgr.addState("endorse", Arrays.asList(joe));
      Assert.assertTrue(stateMgr.isStateVisited("endorse"));
      Assert.assertEquals(1, stateMgr.getVisitedStateNames().size());
      Assert.assertEquals(1, stateMgr.getAssignees("endorse").size());
      Assert.assertEquals(0.0, stateMgr.getHoursSpent("endorse"), 0.0);
      Assert.assertEquals(0, stateMgr.getPercentComplete("endorse"));
   }

   @Test
   public void getPercentComplete() {
      Assert.assertEquals(0, stateMgr.getPercentComplete("endorse"));
   }

   @Test(expected = OseeStateException.class)
   public void setPercentComplete_exception() throws OseeStateException {
      stateMgr.setPercentComplete("endorse", 34);
   }

   @Test
   public void setPercentComplete() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.setPercentComplete("endorse", 34);

      Assert.assertEquals(34, stateMgr.getPercentComplete("endorse"));
   }

   @Test
   public void getHoursSpent() {
      Assert.assertEquals(0.0, stateMgr.getHoursSpent("endorse"), 0.0);
   }

   @Test(expected = OseeStateException.class)
   public void setHoursSpent_exception() throws OseeStateException {
      stateMgr.setHoursSpent("endorse", 8.0);
   }

   @Test
   public void setHoursSpent() throws OseeCoreException {
      stateMgr.addState(new WorkStateImpl("endorse"));
      stateMgr.setCurrentStateName("endorse");
      stateMgr.setHoursSpent("endorse", 8.0);

      Assert.assertEquals(8.0, stateMgr.getHoursSpent("endorse"), 0.0);
   }

   @Test(expected = OseeStateException.class)
   public void removeAssignee_exception() throws OseeCoreException {
      stateMgr.removeAssignee(joe);
   }

   @Test
   public void testIsHoursEqual() {
      IAtsWorkItem awa = Mockito.mock(IAtsWorkItem.class);
      StateManager mgr = new StateManager(awa, logFactory, services);

      assertTrue(mgr.isHoursEqual(1.0, 1.0));
      assertTrue(mgr.isHoursEqual(01.0, 1.0));
      assertTrue(mgr.isHoursEqual(01.0, 1.000));
      assertTrue(mgr.isHoursEqual(1.0, 1.001));

      assertFalse(mgr.isHoursEqual(1.0, 1.01));
      assertFalse(mgr.isHoursEqual(1.0, -1.001));
      assertFalse(mgr.isHoursEqual(-1.0, 1.01));
      assertFalse(mgr.isHoursEqual(2, 4));
   }

   @Test
   public void testSetMetrics() throws OseeCoreException {
      IAtsWorkItem awa = mock(IAtsWorkItem.class);
      StateManager mgr = new StateManager(awa, logFactory, services);

      IAtsStateDefinition state = mock(IAtsStateDefinition.class);
      when(state.getName()).thenReturn("Endorse");

      List<IAtsUser> assignees = Collections.emptyList();
      mgr.addState("Endorse", assignees);
      mgr.setHoursSpent("Endorse", 1.0);
      mgr.setPercentComplete("Endorse", 46);

      assertFalse(mgr.setMetricsIfChanged(state, 1.0, 46));
      assertFalse(mgr.setMetricsIfChanged(state, 1.001, 46));

      assertTrue(mgr.setMetricsIfChanged(state, 1.1, 46));
      assertTrue(mgr.setMetricsIfChanged(state, 1.0, 47));
   }

}

Back to the top