package loan;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.task.Task;
import org.jbpm.test.JbpmTestCase;
/**
* @author shangtongwei
*/
public class LoanTest extends JbpmTestCase {
String deploymentId;
@Override
protected void setUp() throws Exception {
super.setUp();
deploymentId = repositoryService.createDeployment().addResourceFromClasspath("loan.jpdl.xml")
.deploy();
}
@Override
protected void tearDown() throws Exception {
repositoryService.deleteDeploymentCascade(deploymentId);
super.tearDown();
}
/**
* 测试用例
*
* 张三 申请4000元, 部门经理驳回
*/
public void test4000ButManagerDisapprove() {
String curUser = "Zhang San";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 4000);
curUser = "manager";
managerApprove(piId, curUser, false, "最近资金紧张,暂不同意."); // 部门经理不同意
pi = executionService.findProcessInstanceById(piId);
assertNull(pi); // 流程结束
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "cancel"); // 流程结束状态是cancel , 取消
}
/**
* 测试用例
*
* 李四 申请4000元,部门经理同意
*/
public void test4000AndManagerApprove() {
String curUser = "Li si";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 4000);
curUser = "manager";
managerApprove(piId, curUser, true, "同意."); // 部门经理同意
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi); // 流程未结束
assertNotNull(pi.findActiveExecutionIn("财务拨款")); // 财务拨款处于活动状态
curUser = "finance";
earmark(piId, curUser, piId); // 拨款
pi = executionService.findProcessInstanceById(piId);
assertNull(pi); //流程结束
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "ended"); // 流程结束状态是ended, 正常结束
}
/**
* 测试用例
*
* 王五 申请6000元,部门经理驳回
*/
public void test6000ButManagerDisapprove() {
String curUser = "Wang Wu";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 6000);
curUser = "manager";
managerApprove(piId, curUser, false, "最近资金紧张,暂不同意.");
pi = executionService.findProcessInstanceById(piId);
assertNull(pi);
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "cancel");
}
/**
* 测试用例
*
* 赵六 申请6000元,部门经理同意,总经理驳回
*/
public void test6000AndManagerApproveButCeoDisapprove() {
String curUser = "Zhao Liu";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 6000);
curUser = "manager";
managerApprove(piId, curUser, true, "同意. manager");
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi);
assertNotNull(pi.findActiveExecutionIn("总经理审批"));
curUser = "ceo";
ceoApprove(piId, curUser, false, "不同意.等资金周转过来再说吧");
pi = executionService.findProcessInstanceById(piId);
assertNull(pi);
HistoryProcessInstance hpi = getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "cancel");
}
/**
* 测试用例
*
* 孙七 申请6000元,部门经理同意,总经理也同意
*/
public void test6000AndManagerApproveAdnCeoApprove() {
String curUser = "Sun Qi";
ProcessInstance pi = startLoanProcess(curUser);
String piId = pi.getId();
filloutApplication(piId, curUser, 6000);
curUser = "manager";
managerApprove(piId, curUser, true, "同意. manager");
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi);
assertNotNull(pi.findActiveExecutionIn("总经理审批"));
curUser = "ceo";
ceoApprove(piId, curUser, true, "同意. ceo");
pi = executionService.findProcessInstanceById(piId);
assertNotNull(pi);
assertNotNull(pi.findActiveExecutionIn("财务拨款"));
curUser = "finance";
earmark(piId, curUser, piId);
pi = executionService.findProcessInstanceById(piId);
assertNull(pi);
HistoryProcessInstance hpi =
getHistoryProcessInstanceByProcessInstanceId(piId);
assertEquals(hpi.getState(), "ended");
}
/**
* 用户发起借款申请
*
* @param userId
* @return
*/
protected ProcessInstance startLoanProcess(String user) {
Map
variables.put("proposer", user);
return executionService.startProcessInstanceByKey("loan", variables);
}
/**
* 填写借款申请
*
* @param piId
* @param user
* @param money
*/
protected void filloutApplication(String piId, String user, double money) {
List
Task filloutTask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
filloutTask = task;
break;
}
}
assertNotNull(filloutTask);
Map
variables.put("money", money);
executionService.setVariables(piId, variables);
taskService.completeTask(filloutTask.getId());
}
/**
* 经理审批
*
* @param piId
* @param user
* @param isPass
* @param comment
*/
protected void managerApprove(String piId, String user, boolean isPass, String comment) {
List
Task approveTask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
approveTask = task;
break;
}
}
assertNotNull(approveTask);
taskService.addTaskComment(approveTask.getId(), comment);
taskService.completeTask(approveTask.getId(),
isPass ? "部门经理审批通过" : "部门经理驳回");
}
/**
* 总经理审批
*
* @param piId
* @param user
* @param isPass
* @param comment
*/
protected void ceoApprove(String piId, String user, boolean isPass, String comment) {
List
Task approveTask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
approveTask = task;
break;
}
}
assertNotNull(approveTask);
taskService.addTaskComment(approveTask.getId(), comment);
taskService.completeTask(approveTask.getId(),
isPass ? "总经理审批通过" : "总经理驳回");
}
/**
* 拨款
*
* @param piId
* @param user
* @param isPass
* @param comment
*/
/**
* @param piId
* @param user
* @param comment
*/
protected void earmark(String piId, String user, String comment) {
List
Task earmarkask = null;
for (Task task : tasks) {
if (task.getExecutionId().equals(piId)) {
earmarkask = task;
break;
}
}
assertNotNull(earmarkask);
taskService.addTaskComment(earmarkask.getId(), comment);
// HistoryProcessInstance hpi =
// getHistoryProcessInstanceByProcessInstanceId(piId,"填写借款申请");
String proposer =(String) executionService.getVariable(piId,"proposer");
taskService.completeTask(earmarkask.getId());
// 通知借款申请人
System.err.println(">> to " + proposer +", 财务科已经给你拨款.");
}
public HistoryProcessInstance
getHistoryProcessInstanceByProcessInstanceId(String piId) {
return historyService.createHistoryProcessInstanceQuery().
processInstanceId(piId).uniqueResult();
}
}