@Test
public void testSort() throws Exception {

    Formatter formatter;
    String resultString;
    LoadRecord rec1, rec2;
    String expected;
    Map<Level, LoadRecord> loadMap;
    LoadRecord summary;
    Load load = new Load(new LevelClass(ServerName.class));
    // iteration 1
    loadMap = new TreeMap<>();
    rec1 = new LoadRecord();
    rec1.put(LoadEntry.Reads, 2);
    loadMap.put(new Level("rec1"), rec1);
    rec2 = new LoadRecord();
    rec2.put(LoadEntry.Reads, 1);
    loadMap.put(new Level("rec2"), rec2);
    summary = new LoadRecord();
    summary.put(LoadEntry.Reads, 3);
    load.setLoadMap(loadMap);
    load.setSummary(summary);
    load.updateChangeMap();
    Assert.assertEquals(SortKey.DEFAULT, load.getSortKey());
    formatter = new Formatter("testTable", load);
    resultString = formatter.buildString(false, Formatter.Type.ANSI);
    System.out.println(resultString);
    expected = "\u001B[32mRegionServer\u001B[0m  \u001B[36mReads\u001B[0m    \u001B[36mWrites\u001B[0m     \u001B[36mRegions\u001B[0m    \u001B[36mFiles\u001B[0m      \u001B[36mFileSize\u001B[0m   \u001B[36mFileSizeUncomp\u001B[0m  \u001B[36mMemstoreSize\u001B[0m  \u001B[36mCompactedKVs\u001B[0m  \n" +
            "\u001B[1m rec1\u001B[0m         2 | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m rec2\u001B[0m         1 | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m Total: 2\u001B[0m     3 | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n";
    Assert.assertEquals(expected, resultString);
    // iteration 2
    loadMap = new TreeMap<>();
    rec1 = new LoadRecord();
    rec1.put(LoadEntry.Reads, 2);
    loadMap.put(new Level("rec1"), rec1);
    rec2 = new LoadRecord();
    rec2.put(LoadEntry.Reads, 1);
    loadMap.put(new Level("rec2"), rec2);
    summary = new LoadRecord();
    summary.put(LoadEntry.Reads, 3);
    load.prepare();
    load.setLoadMap(loadMap);
    load.setSummary(summary);
    load.updateChangeMap();
    load.setSortKey(new SortKey("1"));
    formatter = new Formatter("testTable", load);
    resultString = formatter.buildString(false, Formatter.Type.ANSI);
    System.out.println(resultString);
    expected = "\u001B[36mRegionServer\u001B[0m  \u001B[32mReads\u001B[0m  \u001B[36mWrites\u001B[0m     \u001B[36mRegions\u001B[0m    \u001B[36mFiles\u001B[0m      \u001B[36mFileSize\u001B[0m   \u001B[36mFileSizeUncomp\u001B[0m  \u001B[36mMemstoreSize\u001B[0m  \u001B[36mCompactedKVs\u001B[0m  \n" +
            "\u001B[1m rec2\u001B[0m         1 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m rec1\u001B[0m         2 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m Total: 2\u001B[0m     3 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n";
    Assert.assertEquals(expected, resultString);
    // iteration 3
    loadMap = new TreeMap<>();
    rec1 = new LoadRecord();
    rec1.put(LoadEntry.Reads, 2);
    loadMap.put(new Level("rec1"), rec1);
    rec2 = new LoadRecord();
    rec2.put(LoadEntry.Reads, 1);
    loadMap.put(new Level("rec2"), rec2);
    summary = new LoadRecord();
    summary.put(LoadEntry.Reads, 3);
    load.prepare();
    load.setLoadMap(loadMap);
    load.setSummary(summary);
    load.updateChangeMap();
    load.setSortKey(new SortKey("!"));
    formatter = new Formatter("testTable", load);
    resultString = formatter.buildString(false, Formatter.Type.ANSI);
    System.out.println(resultString);
    expected = "\u001B[36mRegionServer\u001B[0m  \u001B[42mReads\u001B[0m  \u001B[36mWrites\u001B[0m     \u001B[36mRegions\u001B[0m    \u001B[36mFiles\u001B[0m      \u001B[36mFileSize\u001B[0m   \u001B[36mFileSizeUncomp\u001B[0m  \u001B[36mMemstoreSize\u001B[0m  \u001B[36mCompactedKVs\u001B[0m  \n" +
            "\u001B[1m rec1\u001B[0m         2 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m rec2\u001B[0m         1 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m Total: 2\u001B[0m     3 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n";
    Assert.assertEquals(expected, resultString);
    // iteration 4. reset to 0
    loadMap = new TreeMap<>();
    rec1 = new LoadRecord();
    rec1.put(LoadEntry.Reads, 2);
    loadMap.put(new Level("rec1"), rec1);
    rec2 = new LoadRecord();
    rec2.put(LoadEntry.Reads, 1);
    loadMap.put(new Level("rec2"), rec2);
    summary = new LoadRecord();
    summary.put(LoadEntry.Reads, 3);
    load.prepare();
    load.setLoadMap(loadMap);
    load.setSummary(summary);
    load.updateChangeMap();
    load.setSortKey(SortKey.DEFAULT);
    Assert.assertEquals(SortKey.DEFAULT, load.getSortKey());
    formatter = new Formatter("testTable", load);
    resultString = formatter.buildString(false, Formatter.Type.ANSI);
    System.out.println(resultString);
    expected = "\u001B[32mRegionServer\u001B[0m  \u001B[36mReads\u001B[0m  \u001B[36mWrites\u001B[0m     \u001B[36mRegions\u001B[0m    \u001B[36mFiles\u001B[0m      \u001B[36mFileSize\u001B[0m   \u001B[36mFileSizeUncomp\u001B[0m  \u001B[36mMemstoreSize\u001B[0m  \u001B[36mCompactedKVs\u001B[0m  \n" +
            "\u001B[1m rec1\u001B[0m         2 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m rec2\u001B[0m         1 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m Total: 2\u001B[0m     3 | 0  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n";
    Assert.assertEquals(expected, resultString);
    // iteration 5. if value is same then sorted by level string
    loadMap = new TreeMap<>();
    rec1 = new LoadRecord();
    rec1.put(LoadEntry.Reads, 2);
    loadMap.put(new Level("rec1"), rec1);
    rec2 = new LoadRecord();
    rec2.put(LoadEntry.Reads, 1);
    loadMap.put(new Level("rec2"), rec2);
    summary = new LoadRecord();
    summary.put(LoadEntry.Reads, 3);
    load.prepare();
    load.setLoadMap(loadMap);
    load.setSummary(summary);
    load.updateChangeMap();
    load.setSortKey(new SortKey("!"));
    load.toggleShowRate();
    Assert.assertTrue(load.isShowRate());
    formatter = new Formatter("testTable", load);
    resultString = formatter.buildString(false, Formatter.Type.ANSI);
    System.out.println(resultString);
    expected = "\u001B[36mRegionServer\u001B[0m  \u001B[42mReads\u001B[0m    \u001B[36mWrites\u001B[0m     \u001B[36mRegions\u001B[0m    \u001B[36mFiles\u001B[0m      \u001B[36mFileSize\u001B[0m   \u001B[36mFileSizeUncomp\u001B[0m  \u001B[36mMemstoreSize\u001B[0m  \u001B[36mCompactedKVs\u001B[0m  \n" +
            "\u001B[1m rec1\u001B[0m         2 | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m rec2\u001B[0m         1 | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m Total: 2\u001B[0m     3 | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n";
    Assert.assertEquals(expected, resultString);
    // iteration 6
    loadMap = new TreeMap<>();
    rec1 = new LoadRecord();
    rec1.put(LoadEntry.Reads, 3);
    loadMap.put(new Level("rec1"), rec1);
    rec2 = new LoadRecord();
    rec2.put(LoadEntry.Reads, 1);
    loadMap.put(new Level("rec2"), rec2);
    summary = new LoadRecord();
    summary.put(LoadEntry.Reads, 4);
    load.prepare();
    load.setLoadMap(loadMap);
    load.setSummary(summary);
    load.updateChangeMap();
    load.setDuration(500);
    Assert.assertTrue(load.isShowRate());
    formatter = new Formatter("testTable", load);
    resultString = formatter.buildString(false, Formatter.Type.ANSI);
    System.out.println(resultString);
    expected = "\u001B[36mRegionServer\u001B[0m  \u001B[42mReads\u001B[0m      \u001B[36mWrites\u001B[0m     \u001B[36mRegions\u001B[0m    \u001B[36mFiles\u001B[0m      \u001B[36mFileSize\u001B[0m   \u001B[36mFileSizeUncomp\u001B[0m  \u001B[36mMemstoreSize\u001B[0m  \u001B[36mCompactedKVs\u001B[0m  \n" +
            "\u001B[1m rec2\u001B[0m         1 |   0/s  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m rec1\u001B[0m         \u001B[33m3\u001B[0m | \u001B[33m2.0/s\u001B[0m  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n" +
            "\u001B[1m Total: 2\u001B[0m     \u001B[33m4\u001B[0m | \u001B[33m2.0/s\u001B[0m  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A  N/A | N/A       N/A | N/A     N/A | N/A     \n";
    Assert.assertEquals(expected, resultString);
}