/** Test the list feed instances api using an orderBy parameter. Check the order. */
 @Test
 public void testFeedOrderBy()
     throws URISyntaxException, OozieClientException, JAXBException, AuthenticationException,
         IOException, InterruptedException {
   SoftAssert softAssert = new SoftAssert();
   // orderBy start time
   InstancesResult r =
       prism.getFeedHelper().listInstances(feedName, "orderBy=startTime&sortOrder=desc", null);
   InstancesResult.Instance[] instances = r.getInstances();
   Date previousDate = new Date();
   for (InstancesResult.Instance instance : instances) {
     Date current = instance.getStartTime();
     if (current != null) { // e.g if instance is WAITING it doesn't have start time
       softAssert.assertTrue(
           current.before(previousDate) || current.equals(previousDate),
           "Wrong order. Current startTime :" + current + " Previous: " + previousDate);
       previousDate = (Date) current.clone();
     }
   }
   // orderBy status
   r =
       prism
           .getFeedHelper()
           .listInstances(
               feedName,
               "start=" + startTime + "&numResults=12&orderBy=status&sortOrder=desc",
               null);
   InstanceUtil.validateResponse(r, 12, 1, 1, 4, 6);
   instances = r.getInstances();
   InstancesResult.WorkflowStatus previousStatus = InstancesResult.WorkflowStatus.WAITING;
   for (InstancesResult.Instance instance : instances) {
     InstancesResult.WorkflowStatus current = instance.getStatus();
     softAssert.assertTrue(
         current.toString().compareTo(previousStatus.toString()) <= 0,
         "Wrong order. Compared " + current + " and " + previousStatus + " statuses.");
     previousStatus = current;
   }
   // sort by endTime
   r =
       prism
           .getFeedHelper()
           .listInstances(
               feedName,
               "start=" + startTime + "&numResults=12&orderBy=endTime&sortOrder=desc",
               null);
   instances = r.getInstances();
   previousDate = new Date();
   for (InstancesResult.Instance instance : instances) {
     Date current = instance.getEndTime();
     if (current != null) { // e.g if instance is WAITING it doesn't have end time
       softAssert.assertTrue(
           current.before(previousDate) || current.equals(previousDate),
           "Wrong order. Current startTime :" + current + " Previous: " + previousDate);
       previousDate = (Date) current.clone();
     }
   }
   softAssert.assertAll();
 }
  /**
   * Test list feed instances using custom filter. Expecting list of feed instances which satisfy
   * custom filters.
   */
  @Test
  public void testFeedCustomFilter()
      throws URISyntaxException, IOException, AuthenticationException, InterruptedException {
    String params = "start=" + startTime + "&filterBy=status:RUNNING";
    InstancesResult r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 1, 1, 0, 0, 0);

    params = "start=" + startTime + "&end=" + endTime + "&filterBy=status:RUNNING&offset=2";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateSuccessWOInstances(r);

    params = "start=" + startTime + "&end=" + endTime + "&filterBy=status:WAITING";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 4, 0, 0, 4, 0);

    params =
        "start="
            + startTime
            + "&end="
            + TimeUtil.addMinsToTime(startTime, 41)
            + "&filterBy=status:WAITING";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 3, 0, 0, 3, 0);

    params = "start=" + startTime + "&offset=1&numResults=1&filterBy=status:WAITING";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 1, 0, 0, 1, 0);

    params = "start=" + TimeUtil.addMinsToTime(startTime, 16) + "&offset=2&numResults=12";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 6, 0, 1, 3, 2);

    String sourceCluster = bundles[0].getClusterNames().get(0);
    String clusterName = bundles[1].getClusterNames().get(0);
    params =
        "start="
            + startTime
            + "&filterBy=STATUS:KILLED,CLUSTER:"
            + clusterName
            + "&numResults=5&orderBy=startTime&sortOrder=desc";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 5, 0, 0, 0, 5);

    // should be ordered by a start time
    SoftAssert softAssert = new SoftAssert();
    InstancesResult.Instance[] instances = r.getInstances();
    Date previousDate = new Date();
    for (InstancesResult.Instance instance : instances) {
      Date current = instance.getStartTime();
      softAssert.assertNotNull(current, "Start time shouldn't be null for KILLED instance.");
      softAssert.assertTrue(
          current.before(previousDate) || current.equals(previousDate),
          "Wrong order. Current startTime :" + current + " Previous: " + previousDate);
      previousDate = (Date) current.clone();
    }
    softAssert.assertAll();

    // missing 1st, 11th, 12th instances, all other instances should be retrieved.
    params = "start=" + TimeUtil.addMinsToTime(startTime, 2) + "&offset=2";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 9, 0, 1, 3, 5);

    // missing the 1st, 11th, 12th instance, all instances which have progressed should be present:
    // 5 killed + 1 suspended, but numResults=5, so expecting 1 suspended and 4 killed instances.
    params =
        "start="
            + TimeUtil.addMinsToTime(startTime, 2)
            + "&filterBy=SOURCECLUSTER:"
            + sourceCluster
            + "&offset=1&numResults=5";
    r = prism.getFeedHelper().listInstances(feedName, params, null);
    InstanceUtil.validateResponse(r, 5, 0, 1, 0, 4);
  }