Skip to main content
summaryrefslogtreecommitdiffstats
blob: a492711d6c61fb9f02bab5c50c9b5226967295a8 (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
/*******************************************************************************
 * Copyright (c) 2014 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.rest.internal.notify;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.osee.ats.api.ai.IAtsActionableItem;
import org.eclipse.osee.ats.api.data.AtsAttributeTypes;
import org.eclipse.osee.ats.api.data.AtsRelationTypes;
import org.eclipse.osee.ats.api.notify.AtsNotificationCollector;
import org.eclipse.osee.ats.api.notify.AtsNotificationEvent;
import org.eclipse.osee.ats.api.notify.AtsNotifyType;
import org.eclipse.osee.ats.api.notify.AtsWorkItemNotificationEvent;
import org.eclipse.osee.ats.api.review.IAtsPeerToPeerReview;
import org.eclipse.osee.ats.api.team.IAtsTeamDefinition;
import org.eclipse.osee.ats.api.team.IAtsWorkItemFactory;
import org.eclipse.osee.ats.api.user.IAtsUser;
import org.eclipse.osee.ats.api.user.IAtsUserService;
import org.eclipse.osee.ats.api.workdef.IAtsStateDefinition;
import org.eclipse.osee.ats.api.workdef.IAttributeResolver;
import org.eclipse.osee.ats.api.workdef.IRelationResolver;
import org.eclipse.osee.ats.api.workdef.StateType;
import org.eclipse.osee.ats.api.workflow.IAtsTeamWorkflow;
import org.eclipse.osee.ats.api.workflow.state.IAtsStateManager;
import org.eclipse.osee.ats.rest.IAtsServer;
import org.eclipse.osee.framework.core.data.ArtifactToken;
import org.eclipse.osee.framework.core.enums.CoreAttributeTypes;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsApi;
import org.junit.Assert;
import org.junit.Before;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**
 * Test unit for {@link WorkItemNotificationProcessor}
 *
 * @author Donald G. Dunne
 */
public class WorkItemNotificationProcessorTest {

   // @formatter:off
   @Mock IAtsUser joeSmith_CurrentUser, kay_ValidEmail, jason_ValidEmail, alex_NoValidEmail, inactiveSteve;
   @Mock ArtifactToken kayArtifact;
   @Mock IAtsTeamWorkflow teamWf;
   @Mock ArtifactToken teamWfArt;
   @Mock IAtsPeerToPeerReview peerReview;
   @Mock IAtsStateManager stateMgr;
   @Mock Log logger;
   @Mock IAtsServer atsServer;
   @Mock OrcsApi orcsApi;
   @Mock IAtsWorkItemFactory workItemFactory;
   @Mock IAtsUserService userService;
   @Mock IAttributeResolver attrResolver;
   @Mock IRelationResolver relResolver;
   @Mock IAtsStateDefinition stateDef;
   @Mock IAtsTeamDefinition teamDef;
   @Mock IAtsActionableItem ai;

   // @formatter:on

   @Before
   public void setup() {
      MockitoAnnotations.initMocks(this);

      setupUser(joeSmith_CurrentUser, 61106791L, "Joe Smith", "joe@boeing.com", "3333", true, false);
      setupUser(kay_ValidEmail, 5896672L, "Kay Jones", "kay@boeing.com", "4444", true, false);
      setupUser(jason_ValidEmail, 277990L, "Jason Michael", "jason@boeing.com", "5555", true, false);
      setupUser(alex_NoValidEmail, 8006939L, "Alex Kay", "", "6666", true, false);
      setupUser(inactiveSteve, 5808093L, "Inactive Steve", "insactiveSteve@boeing.com", "7777", false, false);

      when(teamWf.getName()).thenReturn(WorkItemNotificationProcessorTest.class.getSimpleName() + "-testNotify");
      List<IAtsUser> assignees = new ArrayList<>();
      assignees.addAll(
         Arrays.asList(inactiveSteve, alex_NoValidEmail, jason_ValidEmail, kay_ValidEmail, joeSmith_CurrentUser));
      String atsId = "ATS003";
      when(teamWf.getAtsId()).thenReturn(atsId);
      when(workItemFactory.getWorkItemByAtsId(atsId)).thenReturn(teamWf);
      when(attrResolver.getSoleAttributeValue(teamWf, AtsAttributeTypes.LegacyPcrId, "")).thenReturn(atsId);
      when(atsServer.getConfigValue(eq("ActionUrl"))).thenReturn("http://ats/action/UUID/");
      when(teamWf.getId()).thenReturn(98L);
      when(teamWf.getArtifactTypeName()).thenReturn("Team Workflow");
      when(teamWf.getStateMgr()).thenReturn(stateMgr);
      when(stateMgr.getCurrentStateName()).thenReturn("Analyze");
      when(stateMgr.getAssignees()).thenReturn(assignees);

      when(teamWf.getStateDefinition()).thenReturn(stateDef);
      when(teamWf.getTeamDefinition()).thenReturn(teamDef);
      Set<IAtsActionableItem> ais = new HashSet<>();
      ais.add(ai);
      when(teamWf.getActionableItems()).thenReturn(ais);

   }

   @org.junit.Test
   public void testNotifyOriginator() throws OseeCoreException {

      WorkItemNotificationProcessor processor =
         new WorkItemNotificationProcessor(logger, atsServer, workItemFactory, userService, attrResolver);
      AtsNotificationCollector notifications = new AtsNotificationCollector();
      AtsWorkItemNotificationEvent event = new AtsWorkItemNotificationEvent();
      event.setFromUserId(joeSmith_CurrentUser.getUserId());
      event.setNotifyType(AtsNotifyType.Originator);
      event.getAtsIds().add(teamWf.getAtsId());
      when(teamWf.getCreatedBy()).thenReturn(kay_ValidEmail);

      processor.run(notifications, event);

      Assert.assertEquals(1, notifications.getNotificationEvents().size());
      AtsNotificationEvent notifyEvent = notifications.getNotificationEvents().get(0);
      Assert.assertEquals(AtsNotifyType.Originator.name(), notifyEvent.getType());
      Assert.assertEquals(kay_ValidEmail.getUserId(), notifyEvent.getUserIds().iterator().next());
      Assert.assertEquals(
         "You have been set as the originator of [Team Workflow] state [Analyze] titled [WorkItemNotificationProcessorTest-testNotify]",
         notifyEvent.getDescription());

      notifications = new AtsNotificationCollector();
      when(teamWf.getCreatedBy()).thenReturn(inactiveSteve);
      processor.run(notifications, event);
      Assert.assertEquals(0, notifications.getNotificationEvents().size());

   }

   @org.junit.Test
   public void testNotifyAssignee() throws OseeCoreException {

      WorkItemNotificationProcessor processor =
         new WorkItemNotificationProcessor(logger, atsServer, workItemFactory, userService, attrResolver);
      AtsNotificationCollector notifications = new AtsNotificationCollector();
      AtsWorkItemNotificationEvent event = new AtsWorkItemNotificationEvent();
      event.setFromUserId(joeSmith_CurrentUser.getUserId());
      event.setNotifyType(AtsNotifyType.Assigned);
      event.getAtsIds().add(teamWf.getAtsId());
      event.getUserIds().add(kay_ValidEmail.getUserId());

      processor.run(notifications, event);
      Assert.assertEquals(1, notifications.getNotificationEvents().size());
      AtsNotificationEvent notifyEvent = notifications.getNotificationEvents().get(0);
      Assert.assertEquals(AtsNotifyType.Assigned.name(), notifyEvent.getType());

      // joe smith should be removed from list cause it's current user
      // alex should be removed cause not valid email
      List<String> expectedUserIds = new ArrayList<>();
      expectedUserIds.add(jason_ValidEmail.getUserId());
      expectedUserIds.add(kay_ValidEmail.getUserId());
      List<IAtsUser> users = new ArrayList<>();
      for (String userId : event.getUserIds()) {
         users.add(userService.getUserById(userId));
      }
      event.getUserIds().clear();

      notifications = new AtsNotificationCollector();
      processor.run(notifications, event);
      Assert.assertEquals(1, notifications.getNotificationEvents().size());
      notifyEvent = notifications.getNotificationEvents().get(0);

      Assert.assertTrue(
         org.eclipse.osee.framework.jdk.core.util.Collections.isEqual(expectedUserIds, notifyEvent.getUserIds()));
      Assert.assertEquals(
         "You have been set as the assignee of [Team Workflow] in state [Analyze] titled [WorkItemNotificationProcessorTest-testNotify]",
         notifyEvent.getDescription());

      notifications = new AtsNotificationCollector();
      event.getUserIds().add(jason_ValidEmail.getUserId());
      processor.run(notifications, event);
      Assert.assertEquals(1, notifications.getNotificationEvents().size());
      notifyEvent = notifications.getNotificationEvents().get(0);

      Assert.assertEquals(AtsNotifyType.Assigned.name(), notifyEvent.getType());
      // only alex should be emailed cause sent in list
      Assert.assertEquals(1, notifyEvent.getUserIds().size());
      Assert.assertEquals(jason_ValidEmail.getUserId(), notifyEvent.getUserIds().iterator().next());
      Assert.assertEquals(
         "You have been set as the assignee of [Team Workflow] in state [Analyze] titled [WorkItemNotificationProcessorTest-testNotify]",
         notifyEvent.getDescription());
   }

   @org.junit.Test
   public void testNotifySubscribe() throws OseeCoreException {
      when(teamWf.getStoreObject()).thenReturn(teamWfArt);
      when(atsServer.getRelationResolver()).thenReturn(relResolver);
      when(atsServer.getAttributeResolver()).thenReturn(attrResolver);
      when(relResolver.getRelated(teamWf.getStoreObject(), AtsRelationTypes.SubscribedUser_User)).thenReturn(
         Arrays.asList(kayArtifact));
      when(attrResolver.getSoleAttributeValue(kayArtifact, CoreAttributeTypes.UserId, null)).thenReturn("4444");
      when(userService.getUserById(eq("4444"))).thenReturn(kay_ValidEmail);
      AtsWorkItemNotificationEvent event = new AtsWorkItemNotificationEvent();
      event.setFromUserId(joeSmith_CurrentUser.getUserId());
      event.setNotifyType(AtsNotifyType.Subscribed);
      event.getAtsIds().add(teamWf.getAtsId());

      WorkItemNotificationProcessor processor =
         new WorkItemNotificationProcessor(logger, atsServer, workItemFactory, userService, attrResolver);
      AtsNotificationCollector notifications = new AtsNotificationCollector();
      processor.run(notifications, event);

      Assert.assertEquals(1, notifications.getNotificationEvents().size());
      AtsNotificationEvent notifyEvent = notifications.getNotificationEvents().get(0);

      Assert.assertEquals(AtsNotifyType.Subscribed.name(), notifyEvent.getType());
      // only alex should be emailed cause sent in list
      Assert.assertEquals(1, notifyEvent.getUserIds().size());
      Assert.assertEquals(kay_ValidEmail.getUserId(), notifyEvent.getUserIds().iterator().next());
      Assert.assertEquals(
         "[Team Workflow] titled [WorkItemNotificationProcessorTest-testNotify] transitioned to [Analyze] and you subscribed for notification.",
         notifyEvent.getDescription());

   }

   @org.junit.Test
   public void testNotifyCompleted() throws OseeCoreException {

      AtsWorkItemNotificationEvent event = new AtsWorkItemNotificationEvent();
      event.setFromUserId(joeSmith_CurrentUser.getUserId());
      event.setNotifyType(AtsNotifyType.Completed);
      event.getAtsIds().add(teamWf.getAtsId());
      when(teamWf.isTask()).thenReturn(false);
      when(stateDef.getStateType()).thenReturn(StateType.Completed);
      when(teamWf.getCreatedBy()).thenReturn(inactiveSteve);
      when(stateMgr.getCurrentStateName()).thenReturn("Completed");

      WorkItemNotificationProcessor processor =
         new WorkItemNotificationProcessor(logger, atsServer, workItemFactory, userService, attrResolver);
      AtsNotificationCollector notifications = new AtsNotificationCollector();

      processor.run(notifications, event);
      Assert.assertEquals(0, notifications.getNotificationEvents().size());

      notifications = new AtsNotificationCollector();
      when(teamWf.getCreatedBy()).thenReturn(kay_ValidEmail);
      processor.run(notifications, event);
      Assert.assertEquals(1, notifications.getNotificationEvents().size());
      AtsNotificationEvent notifyEvent = notifications.getNotificationEvents().get(0);
      Assert.assertEquals(AtsNotifyType.Completed.name(), notifyEvent.getType());
      Assert.assertEquals(kay_ValidEmail.getUserId(), notifyEvent.getUserIds().iterator().next());
      Assert.assertEquals("[Team Workflow] titled [WorkItemNotificationProcessorTest-testNotify] is [Completed]",
         notifyEvent.getDescription());

      notifications = new AtsNotificationCollector();
      when(teamWf.getCreatedBy()).thenReturn(inactiveSteve);
      Assert.assertEquals(0, notifications.getNotificationEvents().size());

   }

   @org.junit.Test
   public void testNotifyCancelled() throws OseeCoreException {

      AtsWorkItemNotificationEvent event = new AtsWorkItemNotificationEvent();
      event.setFromUserId(joeSmith_CurrentUser.getUserId());
      event.setNotifyType(AtsNotifyType.Cancelled);
      event.getAtsIds().add(teamWf.getAtsId());
      when(teamWf.isTask()).thenReturn(false);
      when(stateDef.getStateType()).thenReturn(StateType.Cancelled);
      when(teamWf.getCreatedBy()).thenReturn(inactiveSteve);
      when(teamWf.getCancelledReason()).thenReturn("this is the reason");
      when(teamWf.getCancelledFromState()).thenReturn("Analyze");
      when(stateMgr.getCurrentStateName()).thenReturn("Cancelled");

      WorkItemNotificationProcessor processor =
         new WorkItemNotificationProcessor(logger, atsServer, workItemFactory, userService, attrResolver);
      AtsNotificationCollector notifications = new AtsNotificationCollector();

      processor.run(notifications, event);
      Assert.assertEquals(0, notifications.getNotificationEvents().size());

      notifications = new AtsNotificationCollector();
      when(teamWf.getCreatedBy()).thenReturn(kay_ValidEmail);
      processor.run(notifications, event);
      Assert.assertEquals(1, notifications.getNotificationEvents().size());
      AtsNotificationEvent notifyEvent = notifications.getNotificationEvents().get(0);
      Assert.assertEquals(AtsNotifyType.Cancelled.name(), notifyEvent.getType());
      Assert.assertEquals(kay_ValidEmail.getUserId(), notifyEvent.getUserIds().iterator().next());
      Assert.assertTrue(notifyEvent.getDescription().startsWith(
         "[Team Workflow] titled [WorkItemNotificationProcessorTest-testNotify] was [Cancelled] from the [Analyze] state on"));
      Assert.assertTrue(notifyEvent.getDescription().endsWith(".<br>Reason: [this is the reason]"));

      notifications = new AtsNotificationCollector();
      when(teamWf.getCreatedBy()).thenReturn(inactiveSteve);
      Assert.assertEquals(0, notifications.getNotificationEvents().size());

   }

   @org.junit.Test
   public void testNotifySubscribedTeamOrAi() throws OseeCoreException {

      AtsWorkItemNotificationEvent event = new AtsWorkItemNotificationEvent();
      event.setFromUserId(joeSmith_CurrentUser.getUserId());
      event.setNotifyType(AtsNotifyType.SubscribedTeamOrAi);
      event.getAtsIds().add(teamWf.getAtsId());
      when(teamWf.isTeamWorkflow()).thenReturn(true);
      when(stateDef.getStateType()).thenReturn(StateType.Working);
      when(stateMgr.getCurrentStateName()).thenReturn(StateType.Working.name());

      WorkItemNotificationProcessor processor =
         new WorkItemNotificationProcessor(logger, atsServer, workItemFactory, userService, attrResolver);

      AtsNotificationCollector notifications = new AtsNotificationCollector();
      processor.run(notifications, event);
      when(teamDef.getSubscribed()).thenReturn(new ArrayList<IAtsUser>());
      when(ai.getSubscribed()).thenReturn(new ArrayList<IAtsUser>());
      Assert.assertEquals(0, notifications.getNotificationEvents().size());

      notifications = new AtsNotificationCollector();
      when(teamDef.getSubscribed()).thenReturn(Arrays.asList(kay_ValidEmail));
      when(ai.getSubscribed()).thenReturn(new ArrayList<IAtsUser>());
      processor.run(notifications, event);
      Assert.assertEquals(1, notifications.getNotificationEvents().size());

      notifications = new AtsNotificationCollector();
      when(teamDef.getSubscribed()).thenReturn(new ArrayList<IAtsUser>());
      when(ai.getSubscribed()).thenReturn(Arrays.asList(kay_ValidEmail));
      processor.run(notifications, event);
      Assert.assertEquals(1, notifications.getNotificationEvents().size());

      notifications = new AtsNotificationCollector();
      when(teamDef.getSubscribed()).thenReturn(Arrays.asList(jason_ValidEmail));
      when(ai.getSubscribed()).thenReturn(Arrays.asList(kay_ValidEmail));
      processor.run(notifications, event);
      Assert.assertEquals(2, notifications.getNotificationEvents().size());

   }

   private void setupUser(IAtsUser user, long uuid, String name, String email, String userId, boolean active, boolean admin) {
      when(user.getId()).thenReturn(uuid);
      when(user.getName()).thenReturn(name);
      when(user.getEmail()).thenReturn(email);
      when(user.isActive()).thenReturn(active);
      when(user.getUserId()).thenReturn(userId);

      when(userService.getUserById(userId)).thenReturn(user);
   }

}

Back to the top