/** Create applications and infrastructures to associate the tasks with. */
  @Before
  public final void prepareApplication() {
    infras = new LinkedList<>();
    for (int i = 0; i < 1 + (int) TestDataIT.MAX_ENTITIES_IN_LIST * Math.random(); i++) {
      Entity<Infrastructure> infraEntity =
          Entity.entity(TestDataIT.createInfrastructure(), Constants.INDIGOMIMETYPE);
      Response rs =
          target("/v1.0/infrastructures").request(Constants.INDIGOMIMETYPE).post(infraEntity);
      infras.add(rs.readEntity(Infrastructure.class).getId());
    }

    apps = new LinkedList<>();
    for (int i = 0; i < 1 + (int) TestDataIT.MAX_ENTITIES_IN_LIST * Math.random(); i++) {
      Application app = TestDataIT.createApplication();
      app.setInfrastructureIds(infras);
      Entity<Application> appEntity = Entity.entity(app, Constants.INDIGOMIMETYPE);
      Response rs = target("/v1.0/applications").request(Constants.INDIGOMIMETYPE).post(appEntity);
      apps.add(rs.readEntity(Application.class).getId());
    }
  }
  /** Test task delete. */
  @Test
  public final void testTaskDelete() {
    Response rs;
    rs = target("/v1.0/tasks/" + UUID.randomUUID()).request().delete();
    Assert.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rs.getStatus());

    Task testTask = TestDataIT.createTask();
    testTask.setApplicationId(apps.get((int) (Math.random() * apps.size())));
    rs =
        target("/v1.0/tasks")
            .request(Constants.INDIGOMIMETYPE)
            .post(Entity.entity(testTask, Constants.INDIGOMIMETYPE));
    String id = rs.readEntity(Task.class).getId();
    rs = target("/v1.0/tasks/" + id).request().delete();
    Assert.assertEquals(Response.Status.NO_CONTENT.getStatusCode(), rs.getStatus());
    rs = target("/v1.0/tasks/" + id).request().delete();
    Assert.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), rs.getStatus());
  }
  /** Tests the details retrieval. */
  @Test
  public final void testTaskDetails() {
    Task newTask = TestDataIT.createTask();
    newTask.setApplicationId(apps.get((int) (Math.random() * apps.size())));
    Response rs;
    rs =
        target("/v1.0/tasks")
            .request(Constants.INDIGOMIMETYPE)
            .post(Entity.entity(newTask, Constants.INDIGOMIMETYPE));
    rs =
        target("/v1.0/tasks/" + rs.readEntity(Application.class).getId())
            .request(Constants.INDIGOMIMETYPE)
            .get();

    Assert.assertEquals(Response.Status.OK.getStatusCode(), rs.getStatus());
    Task task = rs.readEntity(Task.class);
    Assert.assertNotNull(task);
    Assert.assertNotNull(task.getId());
    Assert.assertNotNull(task.getDateCreated());
    Assert.assertNotNull(task.getLastChange());
    Assert.assertEquals(newTask.getDescription(), task.getDescription());
    Assert.assertEquals(newTask.getApplicationId(), task.getApplicationId());
    Transformer<TaskFile, String> transformer =
        new Transformer<TaskFile, String>() {
          @Override
          public String transform(final TaskFile file) {
            return file.getName();
          }
        };
    if (newTask.getInputFiles() != null) {
      Assert.assertNotNull(task.getInputFiles());
      Assert.assertEquals(
          IterableUtils.toString(newTask.getInputFiles(), transformer),
          IterableUtils.toString(task.getInputFiles(), transformer));
    }
    if (newTask.getOutputFiles() != null) {
      Assert.assertNotNull(task.getOutputFiles());
      Assert.assertEquals(
          IterableUtils.toString(newTask.getOutputFiles(), transformer),
          IterableUtils.toString(task.getOutputFiles(), transformer));
    }
    target("/v1.0/tasks/" + task.getId()).request().delete();
  }