henryyan
3/24/2012 - 1:41 PM

LeaveWorkflowTestUT

LeaveWorkflowTestUT

/**
 * 请假流程测试
 * 
 * @author HenryYan
 *
 */
public class LeaveWorkflowTestUT extends BaseWorkflowTest {

	@Autowired
	LeaveWorkflowService workflowService;

	@Before
	public void setUp() throws Exception {
		PropertyFileUtil.init();
		String filePath = getProcessDefinitionXmlDir() + "oa/leave/leave.bpmn20.xml";
		deployprocessDefinition(filePath);
		deleteFromTables("OA_LEAVE");
	}
	
	@Test
	public void verifyProcess() {
		List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave").list();
		Assert.assertEquals(1, list.size());
	}

	/**
	 * 通过审核流程|正常流程
	 * @throws Exception 
	 */
	@Test
	public void pass() throws Exception {
		Leave entity = new Leave();
		setLeaveData(entity);
		User user = UserUtil.getUserFromMemoryCache(4l, true);
		Map<String, Object> startWorkflow = workflowService.startWorkflow(entity, user);
		assertNotNull(startWorkflow);
		workflowService.getEntityManager().flush();
		Assert.assertEquals(1, countRowsInTable("OA_LEAVE"));

		// 验证是否启动成功
		String businessKey = entity.getId().toString();
		long runtimeCounter = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).count();
		Assert.assertEquals(1, runtimeCounter);

		// 领导审核
		Task task = taskService.createTaskQuery()
				.processDefinitionKey(WorkflowUtils.getWorkflowProcessId(JoyingWorkflowConstants.MODULE_OA, workflowService.getProcessDefKey()))
				.taskCandidateUser("3").singleResult();
		assertNotNull(task);
		HashMap<String, Object> variables = new HashMap<String, Object>();
		variables.put("approved", "true");
		variables.put("applyer", entity.getUserId().toString());
		taskService.complete(task.getId(), variables);

		// 销假
		task = taskService.createTaskQuery()
				.processDefinitionKey(WorkflowUtils.getWorkflowProcessId(JoyingWorkflowConstants.MODULE_OA, workflowService.getProcessDefKey()))
				.taskAssignee("4").singleResult();
		taskService.complete(task.getId(), new HashMap<String, Object>());

		// 验证是否已结束|runtime没有数据,history有已经结束的数据
		verifyEndProcess(businessKey);
	}

	/**
	 * 中间出现了领导拒绝然后再重新申请
	 * @throws Exception 
	 */
	@Test
	public void unpassAndReapply() throws Exception {
		Leave entity = new Leave();
		setLeaveData(entity);
		User user = UserUtil.getUserFromMemoryCache(4l, true);
		Map<String, Object> startWorkflow = workflowService.startWorkflow(entity, user);
		workflowService.getEntityManager().flush();
		Assert.assertEquals(1, countRowsInTable("OA_LEAVE"));
		assertNotNull(startWorkflow);

		// 领导审核
		String businessKey = entity.getId().toString();
		String workflowProcessId = WorkflowUtils.getWorkflowProcessId(JoyingWorkflowConstants.MODULE_OA, workflowService.getProcessDefKey());
		TaskQuery taskBillzheng = taskService.createTaskQuery().processDefinitionKey(workflowProcessId).taskCandidateUser("3");
		Assert.assertEquals(1, taskBillzheng.count());
		Task task = taskBillzheng.singleResult();
		assertNotNull(task);
		HashMap<String, Object> variables = new HashMap<String, Object>();
		variables.put("approved", "false");
		variables.put("applyer", entity.getUserId().toString());
		workflowService.complete(task.getId(), user, variables);
		Assert.assertEquals(0, taskBillzheng.count());

		// 重新申请
		task = taskService.createTaskQuery().processDefinitionKey(workflowProcessId).taskAssignee("4").singleResult();
		variables = new HashMap<String, Object>();
		variables.put("reApply", "true");
		workflowService.complete(task.getId(), user, variables);

		// 领导审核
		taskBillzheng = taskService.createTaskQuery().processDefinitionKey(workflowProcessId).taskCandidateUser("3");
		Assert.assertEquals(1, taskBillzheng.count());
		task = taskBillzheng.singleResult();
		assertNotNull(task);
		variables = new HashMap<String, Object>();
		variables.put("approved", "true");
		variables.put("applyer", entity.getUserId().toString());
		workflowService.complete(task.getId(), user, variables);
		Assert.assertEquals(0, taskBillzheng.count());

		// 销假
		task = taskService.createTaskQuery()
				.processDefinitionKey(WorkflowUtils.getWorkflowProcessId(JoyingWorkflowConstants.MODULE_OA, workflowService.getProcessDefKey()))
				.taskAssignee("4").singleResult();
		taskService.complete(task.getId(), new HashMap<String, Object>());

		// 流程结束验证
		verifyEndProcess(businessKey);
	}

	/**
	 * 验证是否已结束|runtime没有数据,history有已经结束的数据
	 */
	protected void verifyEndProcess(String businessKey) {
		long runtimeCounter = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(businessKey).count();
		Assert.assertEquals(0, runtimeCounter);
		long historicCounter = historyService.createHistoricProcessInstanceQuery().finished().processInstanceBusinessKey(businessKey).count();
		Assert.assertEquals(1, historicCounter);
	}

	/**
	 * 删除流程
	 * @throws Exception 
	 */
	@Test
	public void deleteProcess() throws Exception {
		Leave entity = new Leave();
		setLeaveData(entity);
		User user = accountManager.getEntity(4l);
		Map<String, Object> startWorkflow = workflowService.startWorkflow(entity, user);
		workflowService.getEntityManager().flush();
		Assert.assertEquals(1, countRowsInTable("OA_LEAVE"));
		assertNotNull(startWorkflow);
		runtimeService.deleteProcessInstance(startWorkflow.get("pid").toString(), "测试");
		long count = runtimeService.createProcessInstanceQuery().processInstanceBusinessKey(entity.getId().toString()).count();
		Assert.assertEquals(0, count);
	}

	/**
	 * 设置测试数据
	 * @param entity
	 */
	private void setLeaveData(Leave entity) {
		entity.setApplyTime(new Date());
		entity.setEndTime(new Date());
		entity.setStartTime(new Date());
		entity.setLeaveType("公休");
		entity.setReason("测试");
		entity.setUserId(4l);
		entity.setUserName("Henry Yan");
	}

}