package com.blueland.ebpm.engine.core.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.blueland.ebpm.engine.BaseWfServiceTest;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Assert;
import org.junit.Test;

public class ManualTransitionTest extends BaseWfServiceTest {

	//@Deployment(resources = { "com/xsl/oa/com.blueland.bpm.engine/list/service/manual_transition.bpmn20.xml" })
	@Test
	public void testProcess() {
		this.identityService.setAuthenticatedUserId("longwen");
		ProcessInstance processInstance = this.runtimeService
				.startProcessInstanceByKey("process");
		
		Assert.assertNotNull(processInstance);
		List<Task> task = this.taskService.createTaskQuery()
				.taskAssignee("kermit").list();
		this.taskService.complete(task.get(0).getId());
		List<Task> tas1 = this.taskService.createTaskQuery().taskAssignee("lw")
				.list();
		Map params = new HashMap();
		params.put("select", "act1");
		this.taskService.complete(tas1.get(0).getId(), "",params);
		/*
		 * try { turnTransition(tas1.get(0).getId(),"act1",null); } catch
		 * (Exception e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); }
		 */
		List<Task> task2 = this.taskService.createTaskQuery()
				.taskAssignee("kermit").list();
		if (task2 == null || task2.isEmpty()) {
			task2 = this.taskService.createTaskQuery().taskAssignee("fozzie")
					.list();
			Assert.assertEquals("act2", task2.get(0).getTaskDefinitionKey());
		} else {
			Assert.assertEquals("act1", task2.get(0).getTaskDefinitionKey());
		}
		this.taskService.complete(task2.get(0).getId());
		List<HistoricProcessInstance> historicProcessInstance = this.historyService
				.createHistoricProcessInstanceQuery().startedBy("longwen").finished().orderByProcessInstanceStartTime().desc().list();
		Assert.assertEquals(1, historicProcessInstance.size());
	}
	
	@Test
	public void testProcess1() {
		Task task = this.taskService.createTaskQuery().taskId("").includeProcessVariables().singleResult();
		this.taskService.complete(task.getId(),"",task.getProcessVariables());
		
	}

	@Override
	protected void beforeTest() throws Exception {
		/* Deployment deployment = repositoryService.createDeployment().addClasspathResource("com/xsl/oa/com.blueland.bpm.engine/list/service/manual_transition.bpmn20.xml").deploy();    
		 deploymentId = deployment.getId();  */
		 
		 /*if (identityService.createUserQuery().userId("fozzie").singleResult() == null) {
				identityService.saveUser(identityService.newUser("fozzie"));
			}
			if (identityService.createUserQuery().userId("kermit").singleResult() == null) {
				identityService.saveUser(identityService.newUser("kermit"));
			}
			if (identityService.createUserQuery().userId("gonzo").singleResult() == null) {
				identityService.saveUser(identityService.newUser("gonzo"));
			}

			if (identityService.createGroupQuery().groupId("accountancy")
					.singleResult() == null) {
				identityService.saveGroup(identityService.newGroup("accountancy"));
			}
			if (identityService.createGroupQuery().groupId("management")
					.singleResult() == null) {
				identityService.saveGroup(identityService.newGroup("management"));
			}*/
/*
			UserEntity user1 = identityService.createUserQuery()
					.memberOfGroup("management").singleResult();
			if (user1 == null || !"kermit".equals(user1.getId())) {
				identityService.createMembership("kermit", "management");
			}
			UserEntity user2 = identityService.createUserQuery()
					.memberOfGroup("accountancy").singleResult();
			if (user2 == null || !"fozzie".equals(user2.getId())) {
				identityService.createMembership("fozzie", "accountancy");
			}*/

	}

	@Override
	protected void afterTest() throws Exception {
		//repositoryService.deleteDeployment(deploymentId, true); 
		/*identityService.deleteUser("fozzie");
		identityService.deleteUser("kermit");
		identityService.deleteUser("gonzo");
		identityService.deleteGroup("accountancy");
		identityService.deleteGroup("management");*/
		//identityService.deleteMembership("fozzie", "accountancy");
		//identityService.deleteMembership("kermit", "management");

	}

}