@Test
  public void shouldCloseCommanOutputWhenCommandHasFinished() 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(2)).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 shouldStopProcessWithoutCloseCommanOutput() 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 shouldCloseTerminal() throws Exception {
    TerminalPresenter terminal = mock(TerminalPresenter.class);
    ProcessTreeNode machineNode = mock(ProcessTreeNode.class);
    ProcessTreeNode terminalNode = 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.terminals.put(PROCESS_ID, terminal);

    when(terminalNode.getId()).thenReturn(PROCESS_ID);
    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(2)).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());
  }
 private boolean isTerminalNameExist(ProcessTreeNode machineNode, String terminalName) {
   for (ProcessTreeNode node : machineNode.getChildren()) {
     if (TERMINAL_NODE == node.getType() && node.getName().equals(terminalName)) {
       return true;
     }
   }
   return false;
 }
 private ProcessTreeNode findProcessTreeNodeById(@NotNull String id) {
   for (ProcessTreeNode processTreeNode : rootNode.getChildren()) {
     if (id.equals(processTreeNode.getId())) {
       return processTreeNode;
     }
   }
   return null;
 }
 private ProcessTreeNode getProcessTreeNodeByName(
     String processName, ProcessTreeNode machineTreeNode) {
   for (ProcessTreeNode processTreeNode : machineTreeNode.getChildren()) {
     if (processTreeNode.getName().equals(processName)) {
       return processTreeNode;
     }
   }
   return null;
 }
  @Override
  public void onWorkspaceStopped(WorkspaceStoppedEvent event) {
    for (ProcessTreeNode processTreeNode : rootNode.getChildren()) {
      if (processTreeNode.getType() == MACHINE_NODE) {
        onCloseTerminal(processTreeNode);
        processTreeNode.setRunning(false);
        if (processTreeNode.getChildren() != null) {
          processTreeNode.getChildren().clear();
        }
      }
    }

    rootNode.getChildren().clear();
    rootChildren.clear();

    view.clear();
    view.selectNode(null);
    view.setProcessesData(rootNode);
  }
 private void removeChildFromMachineNode(
     ProcessTreeNode childNode, ProcessTreeNode machineTreeNode) {
   view.removeProcessNode(childNode);
   machineTreeNode.getChildren().remove(childNode);
   view.setProcessesData(rootNode);
 }
 private void addChildToMachineNode(ProcessTreeNode childNode, ProcessTreeNode machineTreeNode) {
   machineTreeNode.getChildren().add(childNode);
   view.setProcessesData(rootNode);
   view.selectNode(childNode);
 }