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

import java.io.File;
import java.util.List;
import java.util.Map;

import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.engine.core.service.ProcessModelService;
import com.blueland.ebpm.engine.BaseWfServiceTest;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.validation.ProcessValidator;
import org.activiti.validation.ProcessValidatorFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import com.blueland.bpm.engine.core.model.SearchProcessListDTO;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ProcessModelServiceTest extends BaseWfServiceTest {
	@Autowired
	protected ProcessModelService processModelService;
	@Autowired
	protected ProcessDefinitionService processDefinitionService;

	@Test
	public void syncBpmnModelToDb() {
		ProcessDefinitionModel processDefinitionModel = processDefinitionService
				.getProcessDef("cbCompanySend:1:27620");
		System.out.println(processModelService
				.syncBpmnModelToDb(processDefinitionModel));
	}

	@Test
	public void findProcessDefsByResource() {
		String resourceType = "role";
		String resourceVal = "JZSP_FLOW_CBMLD";
		String tenantId = "JZSP_SYS";
		String procDefKey = "";
		//map返回结果,tenantId、procDefName、activityId、activityName
		List<Map<String,String>> inProcess = processDefinitionService.findProcessDefsByResource(tenantId,procDefKey,resourceType,resourceVal);
		System.out.println("查询到出的流程定义列表:"+inProcess);
	}
	
	@Test
	public void findProcessDefsByTenantId() {
		String tenantId="DEMO_SYS";
		SearchProcessListDTO searchProcessListDTO = new SearchProcessListDTO();
		searchProcessListDTO.setTenantId(tenantId);
		//查询符合条件的流程定义列表
		List<ProcessDefinitionModel> processDefinitions = processDefinitionService
				.findProcessDefsNotCascide(searchProcessListDTO);
		System.out.println("查询到出的流程定义列表:"+processDefinitions.size());
	}
	
	
	

	@Test
	public void validateBpmnModel() throws Exception {
		String bpmnFile = "D:\\工作\\学习工程\\工作流\\myBpms\\代码\\ebpm-project5-14-newstyle\\ebpm-process-manage\\src\\main\\resources\\2bpmn2__.bpmn";
		BpmnModel bpmnModel = processModelService
				.convertFileToBpmnModel(new java.io.FileInputStream(new File(
						bpmnFile)));
		ProcessValidator processValidator = new ProcessValidatorFactory()
				.createDefaultProcessValidator();
		System.out.println(processValidator.validate(bpmnModel));
		// System.out.println(processModelService.syncBpmnModelToDb(processDefinitionModel));
	}

}
