-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
WorkspaceEventsHandlerTest.java
395 lines (336 loc) · 18.8 KB
/
WorkspaceEventsHandlerTest.java
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
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* 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:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.workspace;
import com.google.gwt.core.client.Callback;
import com.google.inject.Provider;
import com.google.web.bindery.event.shared.EventBus;
import org.eclipse.che.api.core.rest.shared.dto.Link;
import org.eclipse.che.api.core.rest.shared.dto.LinkParameter;
import org.eclipse.che.api.machine.shared.dto.MachineConfigDto;
import org.eclipse.che.api.machine.shared.dto.MachineDto;
import org.eclipse.che.api.machine.shared.dto.MachineLogMessageDto;
import org.eclipse.che.api.machine.shared.dto.event.MachineStatusEvent;
import org.eclipse.che.api.machine.shared.dto.execagent.GetProcessesResponseDto;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.api.workspace.shared.dto.EnvironmentDto;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceRuntimeDto;
import org.eclipse.che.api.workspace.shared.dto.event.WorkspaceStatusEvent;
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.actions.WorkspaceSnapshotCreator;
import org.eclipse.che.ide.api.component.Component;
import org.eclipse.che.ide.api.dialogs.ConfirmCallback;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
import org.eclipse.che.ide.api.dialogs.MessageDialog;
import org.eclipse.che.ide.api.machine.ExecAgentCommandManager;
import org.eclipse.che.ide.api.machine.MachineManager;
import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.notification.StatusNotification;
import org.eclipse.che.ide.api.workspace.WorkspaceServiceClient;
import org.eclipse.che.ide.api.workspace.event.EnvironmentOutputEvent;
import org.eclipse.che.ide.api.workspace.event.MachineStatusChangedEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStartedEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStartingEvent;
import org.eclipse.che.ide.api.workspace.event.WorkspaceStoppedEvent;
import org.eclipse.che.ide.rest.AsyncRequestFactory;
import org.eclipse.che.ide.rest.DtoUnmarshallerFactory;
import org.eclipse.che.ide.ui.loaders.LoaderPresenter;
import org.eclipse.che.ide.ui.loaders.request.LoaderFactory;
import org.eclipse.che.ide.ui.loaders.request.MessageLoader;
import org.eclipse.che.ide.websocket.MessageBus;
import org.eclipse.che.ide.websocket.MessageBusProvider;
import org.eclipse.che.ide.websocket.events.MessageHandler;
import org.eclipse.che.ide.workspace.start.StartWorkspaceNotification;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.RUNNING;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.SNAPSHOTTING;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STARTING;
import static org.eclipse.che.api.core.model.workspace.WorkspaceStatus.STOPPED;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Roman Nikitenko
*/
@RunWith(MockitoJUnitRunner.class)
public class WorkspaceEventsHandlerTest {
private static final String MACHINE_NAME = "machineName";
private static final String WORKSPACE_ID = "workspaceId";
private static final String WORKSPACE_STATUS_CHANNEL = "channel";
private static final String WS_AGENT_LOG_CHANNEL = "workspace:" + WORKSPACE_ID + ":ext-server:output";
private static final String ACTIVE_ENV = "activeEnv";
//constructor mocks
@Mock
private EventBus eventBus;
@Mock
private CoreLocalizationConstant locale;
@Mock
private NotificationManager notificationManager;
@Mock
private DialogFactory dialogFactory;
@Mock
private DtoUnmarshallerFactory dtoUnmarshallerFactory;
@Mock
private Provider<MachineManager> machineManagerProvider;
@Mock
private MessageLoader snapshotLoader;
@Mock
private Provider<DefaultWorkspaceComponent> wsComponentProvider;
@Mock
private WorkspaceSnapshotCreator snapshotCreator;
@Mock
private WorkspaceServiceClient workspaceServiceClient;
@Mock
private StartWorkspaceNotification startWorkspaceNotification;
//additional mocks
@Mock
private Callback<Component, Exception> callback;
@Mock
private DefaultWorkspaceComponent workspaceComponent;
@Mock
private MessageBusProvider messageBusProvider;
@Mock
private WorkspaceDto workspace;
@Mock
private Link workspaceEventsLink;
@Mock
private LinkParameter linkParameter;
@Mock
private WorkspaceStatusEvent workspaceStatusEvent;
@Mock
private MachineStatusEvent machineStatusEvent;
@Mock
private LoaderFactory loaderFactory;
@Mock
private MessageBus messageBus;
@Mock
private ExecAgentCommandManager execAgentCommandManager;
@Mock
private Promise<WorkspaceDto> workspacePromise;
@Mock
private Promise<List<WorkspaceDto>> workspacesPromise;
@Mock
private AsyncRequestFactory asyncRequestFactory;
@Mock
private LoaderPresenter loader;
@Mock
private Promise<List<GetProcessesResponseDto>> promise;
@Captor
private ArgumentCaptor<Operation<WorkspaceDto>> workspaceCaptor;
@Captor
private ArgumentCaptor<Operation<List<WorkspaceDto>>> workspacesCaptor;
private WorkspaceEventsHandler workspaceEventsHandler;
@Before
public void setUp() {
when(loaderFactory.newLoader(anyString())).thenReturn(snapshotLoader);
workspaceEventsHandler = new WorkspaceEventsHandler(eventBus,
locale,
dialogFactory,
dtoUnmarshallerFactory,
notificationManager,
messageBusProvider,
machineManagerProvider,
snapshotCreator,
workspaceServiceClient,
startWorkspaceNotification,
wsComponentProvider,
asyncRequestFactory,
execAgentCommandManager,
loader);
when(wsComponentProvider.get()).thenReturn(workspaceComponent);
when(workspace.getId()).thenReturn(WORKSPACE_ID);
when(workspaceStatusEvent.getWorkspaceId()).thenReturn(WORKSPACE_ID);
when(workspaceServiceClient.getWorkspace(WORKSPACE_ID)).thenReturn(workspacePromise);
when(messageBusProvider.getMessageBus()).thenReturn(messageBus);
when(workspace.getLink(anyString())).thenReturn(workspaceEventsLink);
when(workspaceEventsLink.getParameter("channel")).thenReturn(linkParameter);
when(linkParameter.getDefaultValue()).thenReturn(WORKSPACE_STATUS_CHANNEL);
when(execAgentCommandManager.getProcesses(anyString(), anyBoolean())).thenReturn(promise);
}
// @Test disabled because of GWT timer usage
public void shouldSubscribesOnWsAgentOutputWhenWorkspaceIsStarting() throws Exception {
WorkspaceRuntimeDto runtime = mock(WorkspaceRuntimeDto.class);
WorkspaceConfigDto workspaceConfig = mock(WorkspaceConfigDto.class);
when(workspaceStatusEvent.getStatus()).thenReturn(STARTING);
when(workspace.getRuntime()).thenReturn(runtime);
when(runtime.getActiveEnv()).thenReturn(ACTIVE_ENV);
when(workspace.getConfig()).thenReturn(workspaceConfig);
Map<String, EnvironmentDto> environments = new HashMap<>(3);
EnvironmentDto environment = mock(EnvironmentDto.class);
environments.put(ACTIVE_ENV, environment);
when(workspaceConfig.getEnvironments()).thenReturn(environments);
MachineConfigDto devMachineConfig = mock(MachineConfigDto.class);
when(devMachineConfig.getName()).thenReturn(MACHINE_NAME);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(workspacePromise).then(workspaceCaptor.capture());
workspaceCaptor.getValue().apply(workspace);
verify(messageBus, times(2)).subscribe(eq(WS_AGENT_LOG_CHANNEL), (MessageHandler)anyObject());
}
@Test
public void shouldSubscribeOnWsAgentOutputWhenWorkspaceIsRunningAfterRefreshPage() throws Exception {
WorkspaceRuntimeDto runtime = mock(WorkspaceRuntimeDto.class);
WorkspaceConfigDto workspaceConfig = mock(WorkspaceConfigDto.class);
when(workspace.getRuntime()).thenReturn(runtime);
MachineDto devMachine = mock(MachineDto.class);
when(devMachine.getWorkspaceId()).thenReturn(WORKSPACE_ID);
when(devMachine.getId()).thenReturn(MACHINE_NAME);
when(runtime.getDevMachine()).thenReturn(devMachine);
when(runtime.getActiveEnv()).thenReturn(ACTIVE_ENV);
when(workspace.getConfig()).thenReturn(workspaceConfig);
Map<String, EnvironmentDto> environments = new HashMap<>(3);
EnvironmentDto environment = mock(EnvironmentDto.class);
environments.put(ACTIVE_ENV, environment);
when(workspaceConfig.getEnvironments()).thenReturn(environments);
MachineConfigDto devMachineConfig = mock(MachineConfigDto.class);
when(devMachineConfig.getName()).thenReturn(MACHINE_NAME);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
verify(messageBus).subscribe(eq(WS_AGENT_LOG_CHANNEL), (MessageHandler)anyObject());
}
// @Test disabled because of GWT timer usage
public void onWorkspaceStartingTest() throws Exception {
when(workspaceStatusEvent.getStatus()).thenReturn(STARTING);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(workspacePromise).then(workspaceCaptor.capture());
workspaceCaptor.getValue().apply(workspace);
verify(workspaceServiceClient).getWorkspace(WORKSPACE_ID);
verify(workspaceComponent).setCurrentWorkspace(workspace);
verify(machineManagerProvider).get();
verify(loader).show(eq(LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME));
verify(eventBus).fireEvent(Matchers.<WorkspaceStartingEvent>anyObject());
}
@Test
public void onWorkspaceStartedTest() throws Exception {
when(workspaceStatusEvent.getStatus()).thenReturn(RUNNING);
when(workspaceStatusEvent.getPrevStatus()).thenReturn(STARTING);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(workspacePromise).then(workspaceCaptor.capture());
workspaceCaptor.getValue().apply(workspace);
verify(workspaceServiceClient).getWorkspace(WORKSPACE_ID);
verify(workspaceComponent).setCurrentWorkspace(workspace);
verify(loader).setSuccess(eq(LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME));
verify(eventBus).fireEvent(Matchers.<WorkspaceStartedEvent>anyObject());
}
@Test
public void onErrorEventReceivedTest() throws Exception {
WorkspaceConfigDto workspaceConfig = mock(WorkspaceConfigDto.class);
when(workspace.getConfig()).thenReturn(workspaceConfig);
when(workspaceServiceClient.getWorkspaces(anyInt(), anyInt())).thenReturn(workspacesPromise);
List<WorkspaceDto> workspaces = new ArrayList<>(1);
workspaces.add(workspace);
MessageDialog errorDialog = mock(MessageDialog.class);
when(dialogFactory.createMessageDialog(anyString(), anyString(), (ConfirmCallback)anyObject())).thenReturn(errorDialog);
when(workspaceStatusEvent.getStatus()).thenReturn(STOPPED);
when(workspaceStatusEvent.getError()).thenReturn("error");
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(workspacesPromise).then(workspacesCaptor.capture());
workspacesCaptor.getValue().apply(workspaces);
verify(notificationManager).notify(anyString(), eq(StatusNotification.Status.FAIL), eq(FLOAT_MODE));
verify(loader).setError(eq(LoaderPresenter.Phase.STARTING_WORKSPACE_RUNTIME));
verify(eventBus, times(2)).fireEvent(Matchers.<WorkspaceStoppedEvent>anyObject());
verify(eventBus, times(2)).fireEvent(Matchers.<WsAgentStateEvent> anyObject());
verify(errorDialog).show();
}
@Test
public void onWorkspaceStoppedTest() throws Exception {
when(workspaceStatusEvent.getStatus()).thenReturn(STOPPED);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(eventBus, times(2)).fireEvent(Matchers.<WorkspaceStoppedEvent>anyObject());
verify(eventBus, times(2)).fireEvent(Matchers.<WsAgentStateEvent> anyObject());
}
@Test
public void onSnapshotCreatingEventReceivedTest() throws Exception {
when(workspaceStatusEvent.getStatus()).thenReturn(SNAPSHOTTING);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(loader).show(eq(LoaderPresenter.Phase.CREATING_WORKSPACE_SNAPSHOT));
}
@Test
public void onSnapshotCreatedEventReceivedTest() throws Exception {
when(workspaceStatusEvent.getStatus()).thenReturn(RUNNING);
when(workspaceStatusEvent.getPrevStatus()).thenReturn(SNAPSHOTTING);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(loader).setSuccess(eq(LoaderPresenter.Phase.CREATING_WORKSPACE_SNAPSHOT));
verify(snapshotCreator).successfullyCreated();
}
@Test
public void onSnapshotCreationErrorEventReceivedTest() throws Exception {
when(workspaceStatusEvent.getStatus()).thenReturn(RUNNING);
when(workspaceStatusEvent.getPrevStatus()).thenReturn(SNAPSHOTTING);
when(workspaceStatusEvent.getError()).thenReturn("error");
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.workspaceStatusSubscriptionHandler.onMessageReceived(workspaceStatusEvent);
verify(loader).setError(eq(LoaderPresenter.Phase.CREATING_WORKSPACE_SNAPSHOT));
verify(snapshotCreator).creationError(anyString());
}
@Test
public void onEnvironmentStatusEventReceivedTest() throws Exception {
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.environmentStatusSubscriptionHandler.onMessageReceived(machineStatusEvent);
verify(eventBus).fireEvent(Matchers.<MachineStatusChangedEvent>anyObject());
}
@Test
public void onEnvironmentOutputEventReceivedTest() throws Exception {
MachineLogMessageDto machineLogMessage = mock(MachineLogMessageDto.class);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.environmentOutputSubscriptionHandler.onMessageReceived(machineLogMessage);
verify(eventBus).fireEvent(Matchers.<EnvironmentOutputEvent>anyObject());
verify(machineLogMessage).getContent();
verify(machineLogMessage).getMachineName();
}
@Test
public void onWsAgentOutputEventReceivedTest() throws Exception {
WorkspaceRuntimeDto runtime = mock(WorkspaceRuntimeDto.class);
WorkspaceConfigDto workspaceConfig = mock(WorkspaceConfigDto.class);
when(workspace.getRuntime()).thenReturn(runtime);
MachineDto devMachine = mock(MachineDto.class);
when(devMachine.getWorkspaceId()).thenReturn(WORKSPACE_ID);
when(devMachine.getId()).thenReturn(MACHINE_NAME);
when(runtime.getDevMachine()).thenReturn(devMachine);
when(runtime.getActiveEnv()).thenReturn(ACTIVE_ENV);
when(workspace.getConfig()).thenReturn(workspaceConfig);
Map<String, EnvironmentDto> environments = new HashMap<>(3);
EnvironmentDto environment = mock(EnvironmentDto.class);
environments.put(ACTIVE_ENV, environment);
when(workspaceConfig.getEnvironments()).thenReturn(environments);
MachineConfigDto devMachineConfig = mock(MachineConfigDto.class);
when(devMachineConfig.getName()).thenReturn(MACHINE_NAME);
workspaceEventsHandler.trackWorkspaceEvents(workspace, callback);
workspaceEventsHandler.wsAgentLogSubscriptionHandler.onMessageReceived("");
verify(eventBus).fireEvent(Matchers.<EnvironmentOutputEvent> anyObject());
}
}