@Test
public void testSortNodeByFields() throws Exception {

  DatanodeID dnId1 = new DatanodeID("127.0.0.1", "localhost1", "datanode1",
      1234, 2345, 3456, 4567);
  DatanodeID dnId2 = new DatanodeID("127.0.0.2", "localhost2", "datanode2",
      1235, 2346, 3457, 4568);
  // Setup DatanodeDescriptors with one storage each.
  DatanodeDescriptor dnDesc1 = new DatanodeDescriptor(dnId1, "rack1");
  DatanodeDescriptor dnDesc2 = new DatanodeDescriptor(dnId2, "rack2");
  // Update the DatanodeDescriptors with their attached storages.
  BlockManagerTestUtil.updateStorage(dnDesc1, new DatanodeStorage("dnStorage1"));
  BlockManagerTestUtil.updateStorage(dnDesc2, new DatanodeStorage("dnStorage2"));
  DatanodeStorage dns1 = new DatanodeStorage("dnStorage1");
  DatanodeStorage dns2 = new DatanodeStorage("dnStorage2");
  StorageReport[] report1 = new StorageReport[] {
      new StorageReport(dns1, false, 1024, 100, 924, 100)
  };
  StorageReport[] report2 = new StorageReport[] {
      new StorageReport(dns2, false, 2500, 200, 1848, 200)
  };
  dnDesc1.updateHeartbeat(report1, 5L, 3L, 10, 2);
  dnDesc2.updateHeartbeat(report2, 10L, 2L, 20, 1);
  ArrayList<DatanodeDescriptor> live = new ArrayList<DatanodeDescriptor>();
  live.add(dnDesc1);
  live.add(dnDesc2);
    
  JspHelper.sortNodeList(live, "unexists", "ASC");
  Assert.assertEquals(dnDesc1, live.get(0));
  Assert.assertEquals(dnDesc2, live.get(1));    
  JspHelper.sortNodeList(live, "unexists", "DSC");
  Assert.assertEquals(dnDesc2, live.get(0));
  Assert.assertEquals(dnDesc1, live.get(1));  
  
  // test sorting by capacity
  JspHelper.sortNodeList(live, "capacity", "ASC");
  Assert.assertEquals(dnDesc1, live.get(0));
  Assert.assertEquals(dnDesc2, live.get(1));    
  JspHelper.sortNodeList(live, "capacity", "DSC");
  Assert.assertEquals(dnDesc2, live.get(0));
  Assert.assertEquals(dnDesc1, live.get(1));
  // test sorting by used
  JspHelper.sortNodeList(live, "used", "ASC");
  Assert.assertEquals(dnDesc1, live.get(0));
  Assert.assertEquals(dnDesc2, live.get(1));    
  JspHelper.sortNodeList(live, "used", "DSC");
  Assert.assertEquals(dnDesc2, live.get(0));
  Assert.assertEquals(dnDesc1, live.get(1)); 
  
  // test sorting by nondfsused
  JspHelper.sortNodeList(live, "nondfsused", "ASC");
  Assert.assertEquals(dnDesc1, live.get(0));
  Assert.assertEquals(dnDesc2, live.get(1));
  
  JspHelper.sortNodeList(live, "nondfsused", "DSC");
  Assert.assertEquals(dnDesc2, live.get(0));
  Assert.assertEquals(dnDesc1, live.get(1));
 
  // test sorting by remaining
  JspHelper.sortNodeList(live, "remaining", "ASC");
  Assert.assertEquals(dnDesc1, live.get(0));
  Assert.assertEquals(dnDesc2, live.get(1));
  
  JspHelper.sortNodeList(live, "remaining", "DSC");
  Assert.assertEquals(dnDesc2, live.get(0));
  Assert.assertEquals(dnDesc1, live.get(1));
}