@VisibleForTesting
  void showTopScreen() {
    List<ApplicationInformation> appsInfo = new ArrayList<>();
    List<ApplicationReport> apps;
    try {
      apps = fetchAppReports();
    } catch (Exception e) {
      LOG.error("Unable to get application information", e);
      return;
    }

    for (ApplicationReport appReport : apps) {
      ApplicationInformation appInfo = new ApplicationInformation(appReport);
      appsInfo.add(appInfo);
    }
    if (ascendingSort) {
      Collections.sort(appsInfo, comparator);
    } else {
      Collections.sort(appsInfo, Collections.reverseOrder(comparator));
    }
    NodesInformation nodesInfo = getNodesInfo();
    QueueMetrics queueMetrics = getQueueMetrics();
    String header = getHeader(queueMetrics, nodesInfo);
    String appsStr = getPrintableAppInformation(appsInfo);
    synchronized (lock) {
      printHeader(header);
      printApps(appsStr);
      System.out.print(SET_CURSOR_LINE_7_COLUMN_0);
      System.out.print(CLEAR_LINE);
    }
  }
@Test
  public void testHeaderNodeManagers() throws Exception {
    YarnClusterMetrics ymetrics = mock(YarnClusterMetrics.class);
    when(ymetrics.getNumNodeManagers()).thenReturn(0);
    when(ymetrics.getNumDecommissioningNodeManagers()).thenReturn(1);
    when(ymetrics.getNumDecommissionedNodeManagers()).thenReturn(2);
    when(ymetrics.getNumActiveNodeManagers()).thenReturn(3);
    when(ymetrics.getNumLostNodeManagers()).thenReturn(4);
    when(ymetrics.getNumUnhealthyNodeManagers()).thenReturn(5);
    when(ymetrics.getNumRebootedNodeManagers()).thenReturn(6);
    when(ymetrics.getNumShutdownNodeManagers()).thenReturn(7);

    YarnClient client = mock(YarnClient.class);
    when(client.getYarnClusterMetrics()).thenReturn(ymetrics);

    TopCLI topcli = new TopCLI() {
      @Override protected void createAndStartYarnClient() {
      }
    };
    topcli.setClient(client);
    topcli.terminalWidth = 200;

    String actual;
    try (ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        PrintStream out = new PrintStream(outStream)) {
      System.setOut(out);
      System.setErr(out);
      topcli.showTopScreen();
      out.flush();
      actual = outStream.toString(StandardCharsets.UTF_8.name());
    }

    String expected = "NodeManager(s)"
        + ": 0 total, 3 active, 5 unhealthy, 1 decommissioning,"
        + " 2 decommissioned, 4 lost, 6 rebooted, 7 shutdown";
    Assert.assertTrue(
        String.format("Expected output to contain [%s], actual output was [%s].", expected, actual),
        actual.contains(expected));
  }