/** * Register a new task. * * @param task The task to register * @return The task registered */ @POST @Status(Response.Status.CREATED) @Consumes({MediaType.APPLICATION_JSON, Constants.INDIGOMIMETYPE}) @Produces(Constants.INDIGOMIMETYPE) public final Task createTask(final Task task) { if (task.getApplicationId() == null) { throw new BadRequestException("A valid application for the task" + " must be provided"); } task.setDateCreated(new Date()); task.setUserName(getUser()); task.setState(Task.STATE.PENDING); EntityManager em = getEntityManager(); EntityTransaction et = null; try { et = em.getTransaction(); et.begin(); Application app = em.find(Application.class, task.getApplicationId()); if (app == null) { throw new BadRequestException("Application id not valid"); } task.setApplicationDetail(app); em.persist(task); et.commit(); log.debug("New task registered: " + task.getId()); } catch (BadRequestException bre) { throw bre; } catch (RuntimeException re) { log.error("Impossible to create a task"); log.debug(re); throw re; } finally { if (et != null && et.isActive()) { et.rollback(); } em.close(); } log.debug("Created cache storate for the task"); getStorage().createCache(Storage.RESOURCE.TASKS, task.getId()); log.debug("Adding the observer"); task.addObserver( new TaskObserver( getEntityManagerFactory(), getSubmissionThreadPool(), getStorage(), getMonitorQueue())); log.debug("Task in waiting for the next step"); task.setState(Task.STATE.WAITING); return task; }
/** 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(); }