use of io.automatiko.engine.api.workflow.VariableViolationException in project automatiko-engine by automatiko-io.
the class VariableTagsTest method testProcessWithCustomVariableTag.
@Test
public void testProcessWithCustomVariableTag() throws Exception {
TestWorkItemHandler workItemHandler = new TestWorkItemHandler("Human Task");
DefaultProcessEventListener listener = new DefaultProcessEventListener() {
@Override
public void beforeVariableChanged(ProcessVariableChangedEvent event) {
if (event.hasTag("onlyAdmin")) {
throw new VariableViolationException(event.getProcessInstance().getId(), event.getVariableId(), "Variable can only be set by admins");
}
}
};
ProcessConfig config = config(Collections.singletonList(workItemHandler), Collections.singletonList(listener));
BpmnProcess process = create(config, "variable-tags/approval-with-custom-variable-tags.bpmn2");
Map<String, Object> parameters = new HashMap<>();
parameters.put("approver", "john");
assertThrows(VariableViolationException.class, () -> process.createInstance(BpmnVariables.create(parameters)));
}
use of io.automatiko.engine.api.workflow.VariableViolationException in project automatiko-engine by automatiko-io.
the class VariableViolationExceptionMapper method toResponse.
@Override
public Response toResponse(VariableViolationException ex) {
VariableViolationException exception = (VariableViolationException) ex;
Map<String, String> response = new HashMap<>();
response.put(MESSAGE, exception.getMessage() + " : " + exception.getErrorMessage());
response.put(PROCESS_INSTANCE_ID, exception.getProcessInstanceId());
response.put(VARIABLE, exception.getVariableName());
return badRequest(response);
}
use of io.automatiko.engine.api.workflow.VariableViolationException in project automatiko-engine by automatiko-io.
the class VariableScopeInstance method setVariable.
@SuppressWarnings("unchecked")
public void setVariable(NodeInstance nodeInstance, String name, Object value) {
if (name == null) {
throw new IllegalArgumentException("The name of a variable may not be null!");
}
Variable var = getVariableScope().findVariable(name);
if (var != null) {
name = var.getName();
}
Object oldValue = getVariable(name);
if (oldValue == null) {
if (value == null) {
return;
}
}
// check if variable that is being set is readonly and has already been set
if (oldValue != null && getVariableScope().isReadOnly(name)) {
throw new VariableViolationException(getProcessInstance().getId(), name, "Variable '" + name + "' is already set and is marked as read only");
}
// in case variable is marked as notnull (via tag) then null values should be ignored
if (value == null && getVariableScope().isNullable(name)) {
return;
}
// in case variable is versioned store the old value into versioned variable by variable name
if (var != null && var.hasTag(Variable.VERSIONED_TAG)) {
Map<String, List<Object>> versions = (Map<String, List<Object>>) variables.computeIfAbsent(VariableScope.VERSIONED_VARIABLES, key -> new ConcurrentHashMap<>());
List<Object> varVersions = versions.computeIfAbsent(name, k -> new ArrayList<>());
int versionLimit = Integer.parseInt(var.getMetaData().getOrDefault(Variable.VAR_VERSIONS_LIMIT, "10").toString());
if (oldValue != null) {
varVersions.add(oldValue);
// and remove the oldest if exceeding
if (varVersions.size() > versionLimit) {
varVersions.remove(0);
}
}
}
if (getProcessInstance().getProcessRuntime().getVariableInitializer() != null) {
for (VariableAugmentor augmentor : getProcessInstance().getProcessRuntime().getVariableInitializer().augmentors()) {
if (augmentor.accept(var, value)) {
// run any of the available augmentors on the value
if (oldValue != null) {
value = augmentor.augmentOnUpdate(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, value);
} else if (value == null) {
augmentor.augmentOnDelete(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, oldValue);
} else {
value = augmentor.augmentOnCreate(getProcessInstance().getProcess().getId(), getProcessInstance().getProcess().getVersion(), getProcessInstance().getId(), var, value);
}
}
}
}
ProcessInstance processInstance = getProcessInstance();
if (nodeInstance != null) {
processInstance = nodeInstance.getProcessInstance();
}
ProcessEventSupport processEventSupport = ((InternalProcessRuntime) getProcessInstance().getProcessRuntime()).getProcessEventSupport();
processEventSupport.fireBeforeVariableChanged((variableIdPrefix == null ? "" : variableIdPrefix + ":") + name, (variableInstanceIdPrefix == null ? "" : variableInstanceIdPrefix + ":") + name, oldValue, value, getVariableScope().tags(name), processInstance, nodeInstance, getProcessInstance().getProcessRuntime());
internalSetVariable(name, value);
processEventSupport.fireAfterVariableChanged((variableIdPrefix == null ? "" : variableIdPrefix + ":") + name, (variableInstanceIdPrefix == null ? "" : variableInstanceIdPrefix + ":") + name, oldValue, value, getVariableScope().tags(name), processInstance, nodeInstance, getProcessInstance().getProcessRuntime());
processInstance.signalEvent("variableChanged", value);
}
use of io.automatiko.engine.api.workflow.VariableViolationException in project automatiko-engine by automatiko-io.
the class BaseExceptionHandlerTest method testMapVariableViolationException.
@Test
void testMapVariableViolationException() {
Object response = tested.mapException(new VariableViolationException("processInstanceId", "variable", "message"));
assertThat(response).isEqualTo(badRequestResponse);
}
Aggregations