-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ProcessesPanelPresenterTest.java
638 lines (529 loc) · 28.4 KB
/
ProcessesPanelPresenterTest.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
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*******************************************************************************
* Copyright (c) 2012-2017 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.extension.machine.client.processes.panel;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwtmockito.GwtMockitoTestRunner;
import com.google.web.bindery.event.shared.EventBus;
import org.eclipse.che.api.core.model.machine.MachineStatus;
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.MachineProcessDto;
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.promises.client.PromiseError;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto;
import org.eclipse.che.api.workspace.shared.dto.WorkspaceRuntimeDto;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.command.CommandType;
import org.eclipse.che.ide.api.command.CommandTypeRegistry;
import org.eclipse.che.ide.api.dialogs.ConfirmDialog;
import org.eclipse.che.ide.api.dialogs.DialogFactory;
import org.eclipse.che.ide.api.machine.DevMachine;
import org.eclipse.che.ide.api.machine.ExecAgentCommandManager;
import org.eclipse.che.ide.api.machine.MachineEntity;
import org.eclipse.che.ide.api.machine.MachineManager;
import org.eclipse.che.ide.api.machine.events.MachineStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.macro.MacroProcessor;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.notification.StatusNotification;
import org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode;
import org.eclipse.che.ide.api.outputconsole.OutputConsole;
import org.eclipse.che.ide.api.parts.PartStack;
import org.eclipse.che.ide.api.parts.PartStackType;
import org.eclipse.che.ide.api.parts.WorkspaceAgent;
import org.eclipse.che.ide.api.ssh.SshServiceClient;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant;
import org.eclipse.che.ide.extension.machine.client.MachineResources;
import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory;
import org.eclipse.che.ide.extension.machine.client.inject.factories.TerminalFactory;
import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandConsoleFactory;
import org.eclipse.che.ide.extension.machine.client.outputspanel.console.CommandOutputConsole;
import org.eclipse.che.ide.extension.machine.client.perspective.terminal.TerminalPresenter;
import org.eclipse.che.ide.extension.machine.client.processes.ProcessFinishedEvent;
import org.eclipse.che.ide.extension.machine.client.processes.ProcessTreeNode;
import org.eclipse.che.ide.extension.machine.client.processes.actions.ConsoleTreeContextMenuFactory;
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 java.util.ArrayList;
import java.util.List;
import static org.eclipse.che.ide.extension.machine.client.processes.ProcessTreeNode.ProcessNodeType.COMMAND_NODE;
import static org.eclipse.che.ide.extension.machine.client.processes.ProcessTreeNode.ProcessNodeType.MACHINE_NODE;
import static org.eclipse.che.ide.extension.machine.client.processes.ProcessTreeNode.ProcessNodeType.ROOT_NODE;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
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.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Roman Nikitenko
*/
@RunWith(GwtMockitoTestRunner.class)
public class ProcessesPanelPresenterTest {
private static final String MACHINE_ID = "machineID";
private static final String MACHINE_NAME = "machineName";
private static final String WORKSPACE_ID = "workspaceID";
private static final String PROCESS_ID = "processID";
private static final String PROCESS_NAME = "processName";
private static final String OUTPUT_CHANNEL = "outputChannel";
private static final int PID = 101;
@Mock
private CommandConsoleFactory commandConsoleFactory;
@Mock
private ConsoleTreeContextMenuFactory consoleTreeContextMenuFactory;
@Mock
private CommandTypeRegistry commandTypeRegistry;
@Mock
private DialogFactory dialogFactory;
@Mock
private WorkspaceAgent workspaceAgent;
@Mock
private NotificationManager notificationManager;
@Mock
private MachineLocalizationConstant localizationConstant;
@Mock
private TerminalFactory terminalFactory;
@Mock
private ProcessesPanelView view;
@Mock
private MachineResources resources;
@Mock
private AppContext appContext;
@Mock
private SshServiceClient sshService;
@Mock
private EventBus eventBus;
@Mock
private WorkspaceDto workspace;
@Mock
private OutputConsole outputConsole;
@Mock
private MachineManager machineManager;
@Mock
private EntityFactory entityFactory;
@Mock
private WorkspaceRuntimeDto workspaceRuntime;
@Mock
private PartStack partStack;
@Mock
private ExecAgentCommandManager execAgentCommandManager;
@Mock
private MacroProcessor macroProcessor;
@Mock
private DtoFactory dtoFactory;
@Mock
private Promise<List<MachineProcessDto>> processesPromise;
@Mock
private Promise<List<GetProcessesResponseDto>> promise;
@Mock
private Widget widget;
@Captor
private ArgumentCaptor<AcceptsOneWidget> acceptsOneWidgetCaptor;
@Captor
private ArgumentCaptor<Operation<List<MachineProcessDto>>> processesCaptor;
@Captor
private ArgumentCaptor<MachineStateEvent.Handler> machineStateHandlerCaptor;
@Captor
private ArgumentCaptor<Operation<PromiseError>> errorOperation;
private ProcessesPanelPresenter presenter;
@Before
public void setUp() {
DevMachine devMachine = mock(DevMachine.class);
when(devMachine.getId()).thenReturn(WORKSPACE_ID);
when(appContext.getDevMachine()).thenReturn(devMachine);
when(appContext.getWorkspace()).thenReturn(workspace);
when(workspace.getRuntime()).thenReturn(workspaceRuntime);
when(processesPromise.then(Matchers.<Operation<List<MachineProcessDto>>>anyObject())).thenReturn(processesPromise);
when(commandConsoleFactory.create(anyString())).thenReturn(mock(OutputConsole.class));
when(appContext.getWorkspaceId()).thenReturn(WORKSPACE_ID);
when(workspaceAgent.getPartStack(eq(PartStackType.INFORMATION))).thenReturn(partStack);
when(execAgentCommandManager.getProcesses(anyString(), anyBoolean())).thenReturn(promise);
when(promise.then(any(Operation.class))).thenReturn(promise);
presenter = new ProcessesPanelPresenter(view,
localizationConstant,
resources,
eventBus,
workspaceAgent,
appContext,
notificationManager,
entityFactory,
terminalFactory,
commandConsoleFactory,
dialogFactory,
consoleTreeContextMenuFactory,
commandTypeRegistry,
sshService,
execAgentCommandManager,
macroProcessor,
dtoFactory);
}
@Test
public void shouldAddMachineWhenMachineCreating() throws Exception {
MachineEntity machine = mock(MachineEntity.class);
MachineConfigDto machineConfigDto = mock(MachineConfigDto.class);
OutputConsole outputConsole = mock(OutputConsole.class);
when(machineConfigDto.getName()).thenReturn(MACHINE_NAME);
when(machine.getConfig()).thenReturn(machineConfigDto);
when(appContext.getWorkspaceId()).thenReturn(WORKSPACE_ID);
when(commandConsoleFactory.create(eq(MACHINE_NAME))).thenReturn(outputConsole);
MachineStateEvent machineStateEvent = mock(MachineStateEvent.class);
when(machineStateEvent.getMachine()).thenReturn(machine);
verify(eventBus, times(8)).addHandler(anyObject(), machineStateHandlerCaptor.capture());
MachineStateEvent.Handler machineStateHandler = machineStateHandlerCaptor.getAllValues().get(0);
machineStateHandler.onMachineCreating(machineStateEvent);
verify(outputConsole).go(acceptsOneWidgetCaptor.capture());
IsWidget widget = mock(IsWidget.class);
acceptsOneWidgetCaptor.getValue().setWidget(widget);
verify(commandConsoleFactory).create(eq(MACHINE_NAME));
verify(view).addWidget(anyString(), anyString(), anyObject(), anyObject(), anyBoolean());
verify(view).setProcessesData(eq(presenter.rootNode));
}
@Test
public void shouldShowErrorWhenMachineNodeIsNull() throws Exception {
List<ProcessTreeNode> children = new ArrayList<>();
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
OutputConsole outputConsole = mock(OutputConsole.class);
presenter.addCommandOutput(MACHINE_ID, outputConsole);
verify(notificationManager).notify(anyString(), anyString(), any(StatusNotification.Status.class), any(DisplayMode.class));
verify(localizationConstant, times(2)).machineNotFound(eq(MACHINE_ID));
}
@Test
public void shouldAddCommand() throws Exception {
ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
when(machineNode.getId()).thenReturn(MACHINE_ID);
List<ProcessTreeNode> children = new ArrayList<>();
children.add(machineNode);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
presenter.addCommandOutput(MACHINE_ID, outputConsole);
verify(view, never()).hideProcessOutput(anyString());
verify(outputConsole).go(acceptsOneWidgetCaptor.capture());
IsWidget widget = mock(IsWidget.class);
acceptsOneWidgetCaptor.getValue().setWidget(widget);
verify(view).addProcessNode(anyObject());
verify(view).addWidget(anyString(), anyString(), anyObject(), eq(widget), anyBoolean());
verify(view, times(2)).selectNode(anyObject());
verify(view).setProcessesData(anyObject());
verify(view).getNodeById(anyString());
verify(view).setStopButtonVisibility(anyString(), anyBoolean());
}
@Test
public void shouldDisplayStopProcessButtonAtAddingCommand() throws Exception {
ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
when(machineNode.getId()).thenReturn(MACHINE_ID);
List<ProcessTreeNode> children = new ArrayList<>();
children.add(machineNode);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
ProcessTreeNode selectedCommandNode = new ProcessTreeNode(COMMAND_NODE, null, PROCESS_NAME, null, children);
children.add(selectedCommandNode);
when(outputConsole.isFinished()).thenReturn(false);
presenter.consoles.clear();
presenter.addCommandOutput(MACHINE_ID, outputConsole);
verify(view, never()).hideProcessOutput(anyString());
verify(outputConsole).go(acceptsOneWidgetCaptor.capture());
IsWidget widget = mock(IsWidget.class);
acceptsOneWidgetCaptor.getValue().setWidget(widget);
verify(view).addProcessNode(anyObject());
verify(view).addWidget(anyString(), anyString(), anyObject(), eq(widget), anyBoolean());
verify(view, times(2)).selectNode(anyObject());
verify(view).setProcessesData(anyObject());
verify(view).getNodeById(anyString());
verify(view).setStopButtonVisibility(anyString(), eq(true));
}
@Test
public void shouldHideStopProcessButtonAtAddingCommand() throws Exception {
ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
when(machineNode.getId()).thenReturn(MACHINE_ID);
List<ProcessTreeNode> children = new ArrayList<>();
children.add(machineNode);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
ProcessTreeNode selectedCommandNode = new ProcessTreeNode(COMMAND_NODE, null, PROCESS_NAME, null, children);
children.add(selectedCommandNode);
when(outputConsole.isFinished()).thenReturn(true);
presenter.consoles.clear();
presenter.addCommandOutput(MACHINE_ID, outputConsole);
verify(view, never()).hideProcessOutput(anyString());
verify(outputConsole).go(acceptsOneWidgetCaptor.capture());
IsWidget widget = mock(IsWidget.class);
acceptsOneWidgetCaptor.getValue().setWidget(widget);
verify(view).addProcessNode(anyObject());
verify(view).addWidget(anyString(), anyString(), anyObject(), eq(widget), anyBoolean());
verify(view, times(2)).selectNode(anyObject());
verify(view).setProcessesData(anyObject());
verify(view).getNodeById(anyString());
verify(view).setStopButtonVisibility(anyString(), eq(false));
}
@Test
public void shouldHideStopProcessButtonAtAddingTerminal() throws Exception {
MachineDto machineDto = mock(MachineDto.class);
MachineEntity machine = mock(MachineEntity.class);
when(machine.getId()).thenReturn(MACHINE_ID);
MachineConfigDto machineConfigDto = mock(MachineConfigDto.class);
when(machine.getConfig()).thenReturn(machineConfigDto);
when(machineConfigDto.isDev()).thenReturn(true);
when(machine.getStatus()).thenReturn(MachineStatus.RUNNING);
List<MachineDto> machines = new ArrayList<>(1);
machines.add(machineDto);
when(workspaceRuntime.getMachines()).thenReturn(machines);
when(entityFactory.createMachine(machineDto)).thenReturn(machine);
ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
when(machineNode.getId()).thenReturn(MACHINE_ID);
List<ProcessTreeNode> children = new ArrayList<>();
children.add(machineNode);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
TerminalPresenter terminal = mock(TerminalPresenter.class);
when(terminalFactory.create(machine, presenter)).thenReturn(terminal);
IsWidget terminalWidget = mock(IsWidget.class);
when(terminal.getView()).thenReturn(terminalWidget);
when(terminalWidget.asWidget()).thenReturn(widget);
presenter.addCommandOutput(MACHINE_ID, outputConsole);
presenter.onAddTerminal(MACHINE_ID, presenter);
verify(terminalFactory).create(eq(machine), eq(presenter));
verify(terminal).getView();
verify(view, times(2)).setProcessesData(anyObject());
verify(view, times(2)).selectNode(anyObject());
verify(view).addWidget(anyString(), anyString(), anyObject(), eq(terminalWidget), anyBoolean());
verify(view, times(1)).addProcessNode(anyObject());
verify(terminal).setVisible(eq(true));
verify(terminal).connect();
verify(terminal).setListener(anyObject());
verify(view).setStopButtonVisibility(anyString(), eq(false));
}
@Test
public void shouldReplaceCommandOutput() throws Exception {
MachineEntity machine = mock(MachineEntity.class);
when(machine.getId()).thenReturn(MACHINE_ID);
MachineConfigDto machineConfigDto = mock(MachineConfigDto.class);
when(machine.getConfig()).thenReturn(machineConfigDto);
List<ProcessTreeNode> children = new ArrayList<>();
ProcessTreeNode commandNode = new ProcessTreeNode(COMMAND_NODE, null, PROCESS_NAME, null, children);
children.add(commandNode);
ProcessTreeNode machineNode = new ProcessTreeNode(MACHINE_NODE, null, machine, null, children);
children.add(machineNode);
when(machineNode.getId()).thenReturn(MACHINE_ID);
String commandId = commandNode.getId();
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
presenter.consoles.put(commandId, outputConsole);
when(outputConsole.isFinished()).thenReturn(true);
when(outputConsole.getTitle()).thenReturn(PROCESS_NAME);
presenter.addCommandOutput(MACHINE_ID, outputConsole);
verify(outputConsole).go(acceptsOneWidgetCaptor.capture());
IsWidget widget = mock(IsWidget.class);
acceptsOneWidgetCaptor.getValue().setWidget(widget);
verify(view).hideProcessOutput(eq(commandId));
verify(view).addWidget(anyString(), anyString(), anyObject(), eq(widget), anyBoolean());
verify(view, times(2)).selectNode(anyObject());
verify(view).getNodeById(anyString());
}
@Test
public void shouldAddTerminal() throws Exception {
MachineDto machineDto = mock(MachineDto.class);
MachineEntity machine = mock(MachineEntity.class);
when(machine.getId()).thenReturn(MACHINE_ID);
MachineConfigDto machineConfigDto = mock(MachineConfigDto.class);
when(machine.getConfig()).thenReturn(machineConfigDto);
when(machineConfigDto.isDev()).thenReturn(true);
when(machine.getStatus()).thenReturn(MachineStatus.RUNNING);
List<MachineDto> machines = new ArrayList<>(1);
machines.add(machineDto);
when(workspaceRuntime.getMachines()).thenReturn(machines);
when(entityFactory.createMachine(machineDto)).thenReturn(machine);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, new ArrayList<ProcessTreeNode>());
TerminalPresenter terminal = mock(TerminalPresenter.class);
when(terminalFactory.create(machine, presenter)).thenReturn(terminal);
IsWidget terminalWidget = mock(IsWidget.class);
when(terminal.getView()).thenReturn(terminalWidget);
when(terminalWidget.asWidget()).thenReturn(widget);
presenter.onAddTerminal(MACHINE_ID, presenter);
verify(terminalFactory).create(eq(machine), eq(presenter));
verify(workspaceAgent).setActivePart(presenter);
verify(terminal).getView();
verify(view, times(2)).setProcessesData(anyObject());
verify(view).selectNode(anyObject());
verify(view).addWidget(anyString(), anyString(), anyObject(), eq(terminalWidget), anyBoolean());
verify(view).addProcessNode(anyObject());
verify(terminal).setVisible(eq(true));
verify(terminal).connect();
verify(terminal).setListener(anyObject());
}
@Test
public void shouldShowCommanOutputWhenCommandSelected() throws Exception {
ProcessTreeNode commandNode = mock(ProcessTreeNode.class);
when(commandNode.getId()).thenReturn(PROCESS_ID);
presenter.consoles.put(PROCESS_ID, outputConsole);
presenter.onTreeNodeSelected(commandNode);
verify(view).showProcessOutput(eq(PROCESS_ID));
verify(view).setStopButtonVisibility(anyString(), eq(true));
}
@Test
public void stopButtonShouldBeHiddenWhenConsoleHasFinishedProcess() {
ProcessTreeNode commandNode = mock(ProcessTreeNode.class);
when(commandNode.getId()).thenReturn(PROCESS_ID);
when(outputConsole.isFinished()).thenReturn(true);
presenter.consoles.put(PROCESS_ID, outputConsole);
presenter.onTreeNodeSelected(commandNode);
verify(view).setStopButtonVisibility(PROCESS_ID, false);
}
@Test
public void stopButtonStateShouldBeRefreshedWhenConsoleHasRunningProcess() {
ProcessTreeNode commandNode = mock(ProcessTreeNode.class);
when(commandNode.getId()).thenReturn(PROCESS_ID);
when(outputConsole.isFinished()).thenReturn(false);
presenter.consoles.put(PROCESS_ID, outputConsole);
presenter.onTreeNodeSelected(commandNode);
verify(view).setStopButtonVisibility(PROCESS_ID, true);
}
@Test
public void stopButtonShouldBeHiddenWhenProcessFinished() {
when(outputConsole.isFinished()).thenReturn(true);
presenter.consoles.put(PROCESS_ID, outputConsole);
presenter.onProcessFinished(new ProcessFinishedEvent(PID));
verify(view).setStopButtonVisibility(PROCESS_ID, false);
}
@Test
public void shouldStopProcessWithoutCloseCommandOutput() throws Exception {
ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
ProcessTreeNode commandNode = mock(ProcessTreeNode.class);
when(machineNode.getId()).thenReturn(MACHINE_ID);
List<ProcessTreeNode> children = new ArrayList<>();
children.add(machineNode);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
when(outputConsole.isFinished()).thenReturn(false);
presenter.consoles.put(PROCESS_ID, outputConsole);
//noinspection ConstantConditions
machineNode.getChildren().add(commandNode);
when(commandNode.getId()).thenReturn(PROCESS_ID);
when(view.getNodeIndex(anyString())).thenReturn(0);
when(machineNode.getChildren()).thenReturn(children);
when(commandNode.getParent()).thenReturn(machineNode);
presenter.onStopCommandProcess(commandNode);
verify(outputConsole).stop();
verify(view, never()).hideProcessOutput(eq(PROCESS_ID));
verify(view, never()).removeProcessNode(eq(commandNode));
}
@Test
public void shouldCloseCommandOutputWhenCommandHasFinished() throws Exception {
ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
ProcessTreeNode commandNode = mock(ProcessTreeNode.class);
when(machineNode.getId()).thenReturn(MACHINE_ID);
List<ProcessTreeNode> children = new ArrayList<>();
children.add(machineNode);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
when(outputConsole.isFinished()).thenReturn(true);
presenter.consoles.put(PROCESS_ID, outputConsole);
machineNode.getChildren().add(commandNode);
when(commandNode.getId()).thenReturn(PROCESS_ID);
when(view.getNodeIndex(anyString())).thenReturn(0);
when(machineNode.getChildren()).thenReturn(children);
when(commandNode.getParent()).thenReturn(machineNode);
presenter.onCloseCommandOutputClick(commandNode);
verify(commandNode, times(3)).getId();
verify(commandNode).getParent();
verify(view).getNodeIndex(eq(PROCESS_ID));
verify(view).hideProcessOutput(eq(PROCESS_ID));
verify(view).removeProcessNode(eq(commandNode));
verify(view).setProcessesData(anyObject());
}
@Test
public void shouldShowConfirmDialogWhenCommandHasNotFinished() throws Exception {
ConfirmDialog confirmDialog = mock(ConfirmDialog.class);
ProcessTreeNode commandNode = mock(ProcessTreeNode.class);
when(outputConsole.isFinished()).thenReturn(false);
presenter.consoles.put(PROCESS_ID, outputConsole);
when(commandNode.getId()).thenReturn(PROCESS_ID);
when(dialogFactory.createConfirmDialog(anyString(), anyString(), anyObject(), anyObject())).thenReturn(confirmDialog);
presenter.onCloseCommandOutputClick(commandNode);
verify(commandNode).getId();
verify(view, never()).hideProcessOutput(anyString());
verify(view, never()).removeProcessNode(anyObject());
verify(dialogFactory).createConfirmDialog(anyString(), anyString(), anyObject(), anyObject());
verify(confirmDialog).show();
}
@Test
public void shouldShowTerminalWhenTerminalNodeSelected() throws Exception {
TerminalPresenter terminal = mock(TerminalPresenter.class);
presenter.terminals.put(PROCESS_ID, terminal);
ProcessTreeNode terminalNode = mock(ProcessTreeNode.class);
when(terminalNode.getId()).thenReturn(PROCESS_ID);
presenter.onTreeNodeSelected(terminalNode);
verify(view).showProcessOutput(eq(PROCESS_ID));
verify(view, never()).setStopButtonVisibility(PROCESS_ID, true);
}
@Test
public void shouldCloseTerminal() throws Exception {
ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
when(machineNode.getId()).thenReturn(MACHINE_ID);
TerminalPresenter terminal = mock(TerminalPresenter.class);
ProcessTreeNode terminalNode = mock(ProcessTreeNode.class);
when(terminalNode.getId()).thenReturn(PROCESS_ID);
List<ProcessTreeNode> children = new ArrayList<>();
children.add(machineNode);
presenter.rootNode = new ProcessTreeNode(ROOT_NODE, null, null, null, children);
presenter.terminals.put(PROCESS_ID, terminal);
when(view.getNodeIndex(anyString())).thenReturn(0);
when(machineNode.getChildren()).thenReturn(children);
when(terminalNode.getParent()).thenReturn(machineNode);
presenter.onCloseTerminal(terminalNode);
verify(terminal).stopTerminal();
verify(terminalNode, times(3)).getId();
verify(terminalNode).getParent();
verify(view).getNodeIndex(eq(PROCESS_ID));
verify(view).hideProcessOutput(eq(PROCESS_ID));
verify(view).removeProcessNode(eq(terminalNode));
verify(view).setProcessesData(anyObject());
}
@Test
public void testGo() throws Exception {
AcceptsOneWidget container = mock(AcceptsOneWidget.class);
presenter.go(container);
verify(container).setWidget(eq(view));
}
@Test
public void commandShouldBeRestoredWhenWsAgentIsStarted() throws Exception {
WsAgentStateEvent event = mock(WsAgentStateEvent.class);
MachineEntity machineEntity = mock(MachineEntity.class);
MachineDto machine = mock(MachineDto.class);
when(machineEntity.getId()).thenReturn(MACHINE_ID);
when(machineEntity.getWorkspaceId()).thenReturn(WORKSPACE_ID);
when(entityFactory.createMachine(machine)).thenReturn(machineEntity);
MachineConfigDto machineConfigDto = mock(MachineConfigDto.class);
when(machine.getConfig()).thenReturn(machineConfigDto);
when(machineConfigDto.isDev()).thenReturn(true);
when(machine.getStatus()).thenReturn(MachineStatus.RUNNING);
List<MachineDto> machines = new ArrayList<>(2);
machines.add(machine);
when(workspaceRuntime.getMachines()).thenReturn(machines);
MachineProcessDto machineProcessDto = mock(MachineProcessDto.class);
when(machineProcessDto.getOutputChannel()).thenReturn(OUTPUT_CHANNEL);
when(machineProcessDto.getPid()).thenReturn(PID);
List<MachineProcessDto> processes = new ArrayList<>(1);
processes.add(machineProcessDto);
CommandOutputConsole outputConsole = mock(CommandOutputConsole.class);
CommandType commandType = mock(CommandType.class);
when(commandTypeRegistry.getCommandTypeById(anyString())).thenReturn(commandType);
when(commandConsoleFactory.create(anyObject(),
any(org.eclipse.che.api.core.model.machine.Machine.class))).thenReturn(outputConsole);
presenter.onWsAgentStarted(event);
}
}