Skip to content

Commit

Permalink
Basic test for task delegation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Feb 1, 2017
1 parent 3bff827 commit 23413e5
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 20 deletions.
Expand Up @@ -6,6 +6,7 @@
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemDelegationMethodType;

import java.util.List;

Expand Down Expand Up @@ -33,5 +34,6 @@ void stopProcessInstance(String instanceId, String username, OperationResult par
void releaseWorkItem(String workItemId, OperationResult parentResult) throws ObjectNotFoundException, SecurityViolationException;

// TODO check authority in wf manager
void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, OperationResult parentResult) throws ObjectNotFoundException, SecurityViolationException;
void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, WorkItemDelegationMethodType method,
OperationResult parentResult) throws ObjectNotFoundException, SecurityViolationException;
}
Expand Up @@ -2029,8 +2029,9 @@ public void releaseWorkItem(String workItemId, OperationResult parentResult) thr

// TODO check authority in wf manager
@Override
public void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, OperationResult parentResult) throws ObjectNotFoundException, SecurityViolationException {
getWorkflowManagerChecked().delegateWorkItem(workItemId, delegates, parentResult);
public void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, WorkItemDelegationMethodType method,
OperationResult parentResult) throws ObjectNotFoundException, SecurityViolationException {
getWorkflowManagerChecked().delegateWorkItem(workItemId, delegates, method, parentResult);
}
//endregion

Expand Down
Expand Up @@ -79,7 +79,8 @@ void approveOrRejectWorkItem(String taskId, boolean decision, String comment, Ob
void releaseWorkItem(String workItemId, OperationResult result) throws SecurityViolationException, ObjectNotFoundException;

// TODO check authority
void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, OperationResult parentResult) throws SecurityViolationException, ObjectNotFoundException;
void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, WorkItemDelegationMethodType method,
OperationResult parentResult) throws SecurityViolationException, ObjectNotFoundException;

void stopProcessInstance(String instanceId, String username, OperationResult parentResult);

Expand Down
Expand Up @@ -170,8 +170,9 @@ public void releaseWorkItem(String workItemId, OperationResult result) throws Se

// TODO check authority
@Override
public void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, OperationResult parentResult) throws SecurityViolationException, ObjectNotFoundException {
workItemManager.delegateWorkItem(workItemId, delegates, parentResult);
public void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, WorkItemDelegationMethodType method,
OperationResult parentResult) throws SecurityViolationException, ObjectNotFoundException {
workItemManager.delegateWorkItem(workItemId, delegates, method, parentResult);
}

/*
Expand Down
Expand Up @@ -37,6 +37,7 @@
import com.evolveum.midpoint.wf.impl.processes.common.CommonProcessVariableNames;
import com.evolveum.midpoint.wf.impl.util.MiscDataUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemDelegationMethodType;
import com.evolveum.prism.xml.ns._public.types_3.ObjectDeltaType;
import org.activiti.engine.FormService;
import org.activiti.engine.TaskService;
Expand Down Expand Up @@ -211,7 +212,8 @@ public void releaseWorkItem(String workItemId, OperationResult parentResult) thr
}
}

public void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, OperationResult parentResult)
public void delegateWorkItem(String workItemId, List<PrismReferenceValue> delegates, WorkItemDelegationMethodType method,
OperationResult parentResult)
throws ObjectNotFoundException, SecurityViolationException {
OperationResult result = parentResult.createSubresult(OPERATION_DELEGATE_WORK_ITEM);
result.addParam("workItemId", workItemId);
Expand All @@ -228,7 +230,8 @@ public void delegateWorkItem(String workItemId, List<PrismReferenceValue> delega
Task task = taskService.createTaskQuery().taskId(workItemId).singleResult();
// TODO check authority

List<PrismReferenceValue> currentDelegates = new ArrayList<>(workItemProvider.getDelegates(task));
List<PrismReferenceValue> currentDelegates = new ArrayList<>(workItemProvider.getDelegates(task.getTaskLocalVariables(),
"task " + task.getId()));
for (PrismReferenceValue delegate : delegates) {
if (delegate.getTargetType() != null && !QNameUtil.match(UserType.COMPLEX_TYPE, delegate.getTargetType())) {
throw new IllegalArgumentException("Couldn't add non-user reference as a delegate: " + delegate);
Expand Down
Expand Up @@ -122,7 +122,7 @@ public SearchResultList<WorkItemType> searchWorkItems(ObjectQuery query, Collect
}
boolean getAllVariables = true; // TODO implement based on options
// there's no need to fill-in assignee details ; but candidates are necessary to fill-in; TODO implement based on options (resolve)
return tasksToWorkItemsNew(tasks, null, false, false, true, getAllVariables, result);
return tasksToWorkItems(tasks, null, false, false, true, getAllVariables, result);
}

// primitive 'query interpreter'
Expand Down Expand Up @@ -265,15 +265,15 @@ public SearchResultList<WorkItemType> getWorkItemsForProcessInstanceId(String pr
.includeTaskLocalVariables()
.includeProcessVariables()
.list();
return tasksToWorkItemsNew(tasks, null, false, true, true, true, result);
return tasksToWorkItems(tasks, null, false, true, true, true, result);
}

private SearchResultList<WorkItemType> tasksToWorkItemsNew(List<Task> tasks, Map<String, Object> processVariables,
private SearchResultList<WorkItemType> tasksToWorkItems(List<Task> tasks, Map<String, Object> processVariables,
boolean resolveTask, boolean resolveAssignee, boolean resolveCandidates, boolean fetchAllVariables, OperationResult result) {
SearchResultList<WorkItemType> retval = new SearchResultList<>(new ArrayList<WorkItemType>());
for (Task task : tasks) {
try {
retval.add(taskToWorkItemNew(task, processVariables, resolveTask, resolveAssignee, resolveCandidates, fetchAllVariables, result));
retval.add(taskToWorkItem(task, processVariables, resolveTask, resolveAssignee, resolveCandidates, fetchAllVariables, result));
} catch (RuntimeException e) {
// operation result already contains corresponding error record
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't get information on activiti task {}", e, task.getId());
Expand All @@ -282,13 +282,18 @@ private SearchResultList<WorkItemType> tasksToWorkItemsNew(List<Task> tasks, Map
return retval;
}

public List<PrismReferenceValue> getDelegates(Task task) {
public List<PrismReferenceValue> getDelegates(Map<String, Object> variables, String contextDescription) {
List<PrismReferenceValue> rv = new ArrayList<>();
Object o = task.getTaskLocalVariables().get(DELEGATE_VARIABLE_NAME);
Object o = variables.get(DELEGATE_VARIABLE_NAME);
if (o instanceof String) {
String s = (String) o;
for (String oid : s.split(DELEGATE_SEPARATOR)) {
rv.add(ObjectTypeUtil.createObjectRef(oid, ObjectTypes.USER).asReferenceValue());
for (String wrappedOid : s.split(DELEGATE_SEPARATOR)) {
if (!wrappedOid.startsWith("[") || !wrappedOid.endsWith("]")) {
LOGGER.warn("Wrongly-formatted '"+DELEGATE_VARIABLE_NAME+"' variable contents: '{}' in {}", o, contextDescription);
} else {
String oid = wrappedOid.substring(1, wrappedOid.length()-1);
rv.add(ObjectTypeUtil.createObjectRef(oid, ObjectTypes.USER).asReferenceValue());
}
}
}
return rv;
Expand Down Expand Up @@ -415,19 +420,19 @@ public String toString() {
}
}

private WorkItemType taskToWorkItemNew(Task task, Map<String, Object> processVariables, boolean resolveTask, boolean resolveAssignee,
private WorkItemType taskToWorkItem(Task task, Map<String, Object> processVariables, boolean resolveTask, boolean resolveAssignee,
boolean resolveCandidates, boolean fetchAllVariables, OperationResult result) {
TaskExtract taskExtract = new TaskExtract(task, processVariables);
return taskExtractToWorkItemNew(taskExtract, resolveTask, resolveAssignee, resolveCandidates, fetchAllVariables, result);
return taskExtractToWorkItem(taskExtract, resolveTask, resolveAssignee, resolveCandidates, fetchAllVariables, result);
}

public WorkItemType taskEventToWorkItemNew(TaskEvent taskEvent, Map<String, Object> processVariables, boolean resolveTask,
boolean resolveAssignee, boolean resolveCandidates, OperationResult result) {
TaskExtract taskExtract = new TaskExtract(taskEvent);
return taskExtractToWorkItemNew(taskExtract, resolveTask, resolveAssignee, resolveCandidates, false, result);
return taskExtractToWorkItem(taskExtract, resolveTask, resolveAssignee, resolveCandidates, false, result);
}

public WorkItemType taskExtractToWorkItemNew(TaskExtract task, boolean resolveTask, boolean resolveAssignee,
public WorkItemType taskExtractToWorkItem(TaskExtract task, boolean resolveTask, boolean resolveAssignee,
boolean resolveCandidates, boolean fetchAllVariables, OperationResult parentResult) {
OperationResult result = parentResult.createSubresult(OPERATION_ACTIVITI_TASK_TO_WORK_ITEM);
result.addParams(new String [] { "activitiTaskId", "resolveTask", "resolveAssignee", "resolveCandidates" },
Expand Down Expand Up @@ -470,6 +475,7 @@ public WorkItemType taskExtractToWorkItemNew(TaskExtract task, boolean resolveTa
}
}
}
getDelegates(variables, "task " + task.getId()).forEach(prv -> wi.getDelegateRef().add(ObjectTypeUtil.createObjectRef(prv)));
wi.setObjectRef(MiscDataUtil.toObjectReferenceType((LightweightObjectRef) variables.get(CommonProcessVariableNames.VARIABLE_OBJECT_REF)));
wi.setTargetRef(MiscDataUtil.toObjectReferenceType((LightweightObjectRef) variables.get(CommonProcessVariableNames.VARIABLE_TARGET_REF)));

Expand Down
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2010-2017 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.evolveum.midpoint.wf.impl.policy.other;

import com.evolveum.midpoint.model.api.WorkflowService;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismReferenceValue;
import com.evolveum.midpoint.schema.SearchResultList;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.wf.impl.activiti.ActivitiEngine;
import com.evolveum.midpoint.wf.impl.policy.AbstractWfTestPolicy;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

import java.util.Collections;

import static com.evolveum.midpoint.test.util.TestUtil.assertSuccess;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.WorkItemDelegationMethodType.DEPUTY;
import static org.testng.AssertJUnit.assertEquals;

/**
* @author mederly
*/
@ContextConfiguration(locations = {"classpath:ctx-workflow-test-main.xml"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class TestDelegation extends AbstractWfTestPolicy {

@Override
protected PrismObject<UserType> getDefaultActor() {
return userAdministrator;
}

@Autowired
private ActivitiEngine activitiEngine;

@Autowired
private WorkflowService workflowService;

String workItemId;

@Test
public void test100CreateTask() throws Exception {
final String TEST_NAME = "test100CreateTask";
TestUtil.displayTestTile(this, TEST_NAME);
login(userAdministrator);

Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

assignRole(userJackOid, roleRole1aOid, task, result); // should start approval process
assertNotAssignedRole(userJackOid, roleRole1aOid, task, result);

WorkItemType workItem = getWorkItem(task, result);
workItemId = workItem.getWorkItemId();

assertRefEquals("Wrong assigneeRef", ort(userLead1Oid), workItem.getAssigneeRef());
assertEquals("Wrong # of delegates", 0, workItem.getDelegateRef().size());
}

private WorkItemType getWorkItem(Task task, OperationResult result)
throws SchemaException, SecurityViolationException, ConfigurationException, ObjectNotFoundException {
SearchResultList<WorkItemType> itemsAll = modelService.searchContainers(WorkItemType.class, null, null, task, result);
assertEquals("Wrong # of total work items", 1, itemsAll.size());
return itemsAll.get(0);
}

@Test
public void test110DelegateToUser2() throws Exception {
final String TEST_NAME = "test110DelegateToUser2";
TestUtil.displayTestTile(this, TEST_NAME);
login(userAdministrator);

Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

workflowService.delegateWorkItem(workItemId, Collections.singletonList(prv(userLead2Oid)), DEPUTY, result);

result.computeStatus();
assertSuccess(result);

WorkItemType workItem = getWorkItem(task, result);
assertRefEquals("Wrong assigneeRef", ort(userLead1Oid), workItem.getAssigneeRef());
assertEquals("Wrong # of delegates", 1, workItem.getDelegateRef().size());
assertRefEquals("Wrong delegateRef", ort(userLead2Oid), workItem.getDelegateRef().get(0));

}

private ObjectReferenceType ort(String oid) {
return ObjectTypeUtil.createObjectRef(oid, ObjectTypes.USER);
}

private PrismReferenceValue prv(String oid) {
return ObjectTypeUtil.createObjectRef(oid, ObjectTypes.USER).asReferenceValue();
}

}
1 change: 1 addition & 0 deletions model/workflow-impl/testng-integration.xml
Expand Up @@ -57,6 +57,7 @@
<test name="Other" preserve-order="true" parallel="false" verbose="10" enabled="true">
<classes>
<class name="com.evolveum.midpoint.wf.impl.policy.other.TestActivitiQuery"/>
<class name="com.evolveum.midpoint.wf.impl.policy.other.TestDelegation"/>
</classes>
</test>
</suite>

0 comments on commit 23413e5

Please sign in to comment.