@Test
  public void testJobTaskCountersXML() throws Exception {
    WebResource r = resource();
    Map<JobId, Job> jobsMap = appContext.getAllJobs();
    for (JobId id : jobsMap.keySet()) {
      String jobId = MRApps.toString(id);
      for (Task task : jobsMap.get(id).getTasks().values()) {

        String tid = MRApps.toString(task.getID());
        ClientResponse response =
            r.path("ws")
                .path("v1")
                .path("history")
                .path("mapreduce")
                .path("jobs")
                .path(jobId)
                .path("tasks")
                .path(tid)
                .path("counters")
                .accept(MediaType.APPLICATION_XML)
                .get(ClientResponse.class);
        assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
        String xml = response.getEntity(String.class);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xml));
        Document dom = db.parse(is);
        NodeList info = dom.getElementsByTagName("jobTaskCounters");
        verifyHsTaskCountersXML(info, task);
      }
    }
  }
  @Test
  public void testTaskIdCountersDefault() throws JSONException, Exception {
    WebResource r = resource();
    Map<JobId, Job> jobsMap = appContext.getAllJobs();
    for (JobId id : jobsMap.keySet()) {
      String jobId = MRApps.toString(id);
      for (Task task : jobsMap.get(id).getTasks().values()) {

        String tid = MRApps.toString(task.getID());
        ClientResponse response =
            r.path("ws")
                .path("v1")
                .path("history")
                .path("mapreduce")
                .path("jobs")
                .path(jobId)
                .path("tasks")
                .path(tid)
                .path("counters")
                .get(ClientResponse.class);
        assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
        JSONObject json = response.getEntity(JSONObject.class);
        assertEquals("incorrect number of elements", 1, json.length());
        JSONObject info = json.getJSONObject("jobTaskCounters");
        verifyHsJobTaskCounters(info, task);
      }
    }
  }
 @Test
 public void testTasksQueryReduce() throws JSONException, Exception {
   WebResource r = resource();
   Map<JobId, Job> jobsMap = appContext.getAllJobs();
   for (JobId id : jobsMap.keySet()) {
     String jobId = MRApps.toString(id);
     String type = "r";
     ClientResponse response =
         r.path("ws")
             .path("v1")
             .path("history")
             .path("mapreduce")
             .path("jobs")
             .path(jobId)
             .path("tasks")
             .queryParam("type", type)
             .accept(MediaType.APPLICATION_JSON)
             .get(ClientResponse.class);
     assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
     JSONObject json = response.getEntity(JSONObject.class);
     assertEquals("incorrect number of elements", 1, json.length());
     JSONObject tasks = json.getJSONObject("tasks");
     JSONArray arr = tasks.getJSONArray("task");
     assertEquals("incorrect number of elements", 1, arr.length());
     verifyHsTask(arr, jobsMap.get(id), type);
   }
 }
  @Test
  public void testTasksXML() throws JSONException, Exception {

    WebResource r = resource();
    Map<JobId, Job> jobsMap = appContext.getAllJobs();
    for (JobId id : jobsMap.keySet()) {
      String jobId = MRApps.toString(id);
      ClientResponse response =
          r.path("ws")
              .path("v1")
              .path("history")
              .path("mapreduce")
              .path("jobs")
              .path(jobId)
              .path("tasks")
              .accept(MediaType.APPLICATION_XML)
              .get(ClientResponse.class);
      assertEquals(MediaType.APPLICATION_XML_TYPE, response.getType());
      String xml = response.getEntity(String.class);
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      InputSource is = new InputSource();
      is.setCharacterStream(new StringReader(xml));
      Document dom = db.parse(is);
      NodeList tasks = dom.getElementsByTagName("tasks");
      assertEquals("incorrect number of elements", 1, tasks.getLength());
      NodeList task = dom.getElementsByTagName("task");
      verifyHsTaskXML(task, jobsMap.get(id));
    }
  }
 @Test
 public void testTaskIdInvalid() throws JSONException, Exception {
   WebResource r = resource();
   Map<JobId, Job> jobsMap = appContext.getAllJobs();
   for (JobId id : jobsMap.keySet()) {
     String jobId = MRApps.toString(id);
     String tid = "task_0_0000_d_000000";
     try {
       r.path("ws")
           .path("v1")
           .path("history")
           .path("mapreduce")
           .path("jobs")
           .path(jobId)
           .path("tasks")
           .path(tid)
           .get(JSONObject.class);
       fail("should have thrown exception on invalid uri");
     } catch (UniformInterfaceException ue) {
       ClientResponse response = ue.getResponse();
       assertEquals(Status.NOT_FOUND, response.getClientResponseStatus());
       assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
       JSONObject msg = response.getEntity(JSONObject.class);
       JSONObject exception = msg.getJSONObject("RemoteException");
       assertEquals("incorrect number of elements", 3, exception.length());
       String message = exception.getString("message");
       String type = exception.getString("exception");
       String classname = exception.getString("javaClassName");
       WebServicesTestUtils.checkStringMatch(
           "exception message",
           "java.lang.Exception: Bad TaskType identifier. TaskId string : "
               + "task_0_0000_d_000000 is not properly formed.",
           message);
       WebServicesTestUtils.checkStringMatch("exception type", "NotFoundException", type);
       WebServicesTestUtils.checkStringMatch(
           "exception classname", "org.apache.hadoop.yarn.webapp.NotFoundException", classname);
     }
   }
 }
  @Test
  public void testTasksQueryInvalid() throws JSONException, Exception {
    WebResource r = resource();
    Map<JobId, Job> jobsMap = appContext.getAllJobs();
    for (JobId id : jobsMap.keySet()) {
      String jobId = MRApps.toString(id);
      // tasktype must be exactly either "m" or "r"
      String tasktype = "reduce";

      try {
        r.path("ws")
            .path("v1")
            .path("history")
            .path("mapreduce")
            .path("jobs")
            .path(jobId)
            .path("tasks")
            .queryParam("type", tasktype)
            .accept(MediaType.APPLICATION_JSON)
            .get(JSONObject.class);
        fail("should have thrown exception on invalid uri");
      } catch (UniformInterfaceException ue) {
        ClientResponse response = ue.getResponse();
        assertEquals(Status.BAD_REQUEST, response.getClientResponseStatus());
        assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType());
        JSONObject msg = response.getEntity(JSONObject.class);
        JSONObject exception = msg.getJSONObject("RemoteException");
        assertEquals("incorrect number of elements", 3, exception.length());
        String message = exception.getString("message");
        String type = exception.getString("exception");
        String classname = exception.getString("javaClassName");
        WebServicesTestUtils.checkStringMatch(
            "exception message", "java.lang.Exception: tasktype must be either m or r", message);
        WebServicesTestUtils.checkStringMatch("exception type", "BadRequestException", type);
        WebServicesTestUtils.checkStringMatch(
            "exception classname", "org.apache.hadoop.yarn.webapp.BadRequestException", classname);
      }
    }
  }