use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class HoldStateTest method testHoldAndResume.
@Test
public void testHoldAndResume() throws Exception {
// Add a listener for paused workflow instances
WorkflowStateListener pauseListener = new WorkflowStateListener(WorkflowState.PAUSED);
service.addWorkflowListener(pauseListener);
Map<String, String> initialProps = new HashMap<String, String>();
initialProps.put("testproperty", "foo");
synchronized (pauseListener) {
workflow = service.start(def, mp, initialProps);
pauseListener.wait();
}
service.removeWorkflowListener(pauseListener);
// The variable "testproperty" should have been replaced by "foo", but not "anotherproperty"
Assert.assertEquals("foo", workflow.getOperations().get(0).getConfiguration("testkey"));
Assert.assertEquals("${anotherproperty}", workflow.getOperations().get(1).getConfiguration("testkey"));
// Simulate a user resuming and submitting new properties (this time, with a value for "anotherproperty") to the
// workflow
Map<String, String> resumeProps = new HashMap<String, String>();
resumeProps.put("anotherproperty", "bar");
WorkflowStateListener succeedListener = new WorkflowStateListener(WorkflowState.SUCCEEDED);
service.addWorkflowListener(succeedListener);
synchronized (succeedListener) {
service.resume(workflow.getId(), resumeProps);
succeedListener.wait();
}
service.removeWorkflowListener(succeedListener);
Assert.assertEquals("Workflow expected to succeed", 1, succeedListener.countStateChanges(WorkflowState.SUCCEEDED));
WorkflowInstance fromDb = service.getWorkflowById(workflow.getId());
logger.info("checking for the existence of 'anotherproperty', which should have been replaced");
Assert.assertEquals("foo", fromDb.getOperations().get(0).getConfiguration("testkey"));
Assert.assertEquals("bar", fromDb.getOperations().get(1).getConfiguration("testkey"));
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class HoldStateTest method testMultipleHolds.
@Test
public void testMultipleHolds() throws Exception {
WorkflowStateListener pauseListener = new WorkflowStateListener(WorkflowState.PAUSED);
service.addWorkflowListener(pauseListener);
synchronized (pauseListener) {
workflow = service.start(def, mp);
pauseListener.wait();
}
// Simulate a user resuming the workflow, but the handler still keeps the workflow in a hold state
holdingOperationHandler.setResumeAction(Action.PAUSE);
// Resume the workflow again. It should quickly reenter the paused state
synchronized (pauseListener) {
service.resume(workflow.getId());
pauseListener.wait();
}
// remove the pause listener
service.removeWorkflowListener(pauseListener);
WorkflowInstance fromDb = service.getWorkflowById(workflow.getId());
Assert.assertEquals(WorkflowState.PAUSED, fromDb.getState());
// Resume the workflow again, and this time continue with the workflow
holdingOperationHandler.setResumeAction(Action.CONTINUE);
WorkflowStateListener succeedListener = new WorkflowStateListener(WorkflowState.SUCCEEDED, WorkflowState.FAILED);
service.addWorkflowListener(succeedListener);
synchronized (succeedListener) {
service.resume(workflow.getId());
succeedListener.wait();
}
service.removeWorkflowListener(succeedListener);
Assert.assertEquals(WorkflowState.SUCCEEDED, service.getWorkflowById(workflow.getId()).getState());
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowInstanceTest method testWorkflowFields.
@Test
public void testWorkflowFields() throws Exception {
// Workflows should obtain information from the definition used in the constructor
WorkflowDefinitionImpl def = new WorkflowDefinitionImpl();
def.setId("123");
def.setPublished(true);
Map<String, String> props = new HashMap<String, String>();
props.put("key1", "value1");
WorkflowInstance instance = new WorkflowInstanceImpl(def, null, null, null, null, props);
Assert.assertEquals(def.getId(), instance.getTemplate());
Assert.assertEquals("value1", instance.getConfiguration("key1"));
def.setTitle("a title");
instance = new WorkflowInstanceImpl(def, null, null, null, null, null);
Assert.assertEquals(def.getTitle(), instance.getTitle());
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowRestService method update.
@POST
@Path("update")
@RestQuery(name = "update", description = "Updates a workflow instance.", returnDescription = "No content.", restParameters = { @RestParameter(name = "workflow", isRequired = true, description = "The XML representation of the workflow instance.", type = TEXT) }, reponses = { @RestResponse(responseCode = SC_NO_CONTENT, description = "Workflow instance updated.") })
public Response update(@FormParam("workflow") String workflowInstance) throws NotFoundException, UnauthorizedException {
try {
WorkflowInstance instance = WorkflowParser.parseWorkflowInstance(workflowInstance);
service.update(instance);
return Response.noContent().build();
} catch (WorkflowException e) {
throw new WebApplicationException(e);
}
}
use of org.opencastproject.workflow.api.WorkflowInstance in project opencast by opencast.
the class WorkflowRestService method resume.
@POST
@Path("replaceAndresume")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "replaceAndresume", description = "Replaces a suspended workflow instance with an updated version, and resumes the workflow.", returnDescription = "An XML representation of the updated and resumed workflow instance", restParameters = { @RestParameter(name = "id", isRequired = true, description = "The workflow instance identifier", type = STRING), @RestParameter(name = "mediapackage", isRequired = false, description = "The new Mediapackage", type = TEXT), @RestParameter(name = "properties", isRequired = false, description = "Properties", type = TEXT) }, reponses = { @RestResponse(responseCode = SC_OK, description = "An XML representation of the updated and resumed workflow instance."), @RestResponse(responseCode = SC_CONFLICT, description = "Can not resume workflow not in paused state"), @RestResponse(responseCode = SC_NOT_FOUND, description = "No suspended workflow instance with that identifier exists."), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "You do not have permission to resume. Maybe you need to authenticate.") })
public Response resume(@FormParam("id") long workflowInstanceId, @FormParam("mediapackage") final String mediaPackage, @FormParam("properties") LocalHashMap properties) throws NotFoundException, UnauthorizedException {
final Map<String, String> map;
if (properties == null) {
map = new HashMap<String, String>();
} else {
map = properties.getMap();
}
final Lock lock = this.lock.get(workflowInstanceId);
lock.lock();
try {
WorkflowInstance workflow = service.getWorkflowById(workflowInstanceId);
if (!WorkflowState.PAUSED.equals(workflow.getState())) {
logger.warn("Can not resume workflow '{}', not in state paused but {}", workflow, workflow.getState());
return Response.status(Status.CONFLICT).build();
}
if (mediaPackage != null) {
MediaPackage newMp = MediaPackageParser.getFromXml(mediaPackage);
MediaPackage oldMp = workflow.getMediaPackage();
// Delete removed elements from workspace
for (MediaPackageElement elem : oldMp.getElements()) {
if (MediaPackageSupport.contains(elem.getIdentifier(), newMp))
continue;
try {
workspace.delete(elem.getURI());
logger.info("Deleted removed mediapackge element {}", elem);
} catch (NotFoundException e) {
logger.info("Removed mediapackage element {} is already deleted", elem);
}
}
workflow.setMediaPackage(newMp);
service.update(workflow);
}
workflow = service.resume(workflowInstanceId, map);
return Response.ok(workflow).build();
} catch (NotFoundException e) {
return Response.status(Status.NOT_FOUND).build();
} catch (UnauthorizedException e) {
return Response.status(Status.UNAUTHORIZED).build();
} catch (IllegalStateException e) {
logger.warn(ExceptionUtils.getMessage(e));
return Response.status(Status.CONFLICT).build();
} catch (WorkflowException e) {
logger.error(ExceptionUtils.getMessage(e), e);
return Response.serverError().build();
} catch (Exception e) {
logger.error(ExceptionUtils.getMessage(e), e);
return Response.serverError().build();
} finally {
lock.unlock();
}
}
Aggregations