测试activiti中的taskService

my-process-task.bpnm20.xml文件内容如下:




    
    
    
              activiti:candidateUsers="shr, user1, user2">
        some task ${message}}
    
    
    


测试taskService中的指定候选人完成任务,测试类内容如下:

@Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskServiceUser() {

        //构建参数变量,方便查看描述
        Map var = Maps.newHashMap();
        var.put("message", "this is my test message");
        //加载时加载描述信息
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", var);
        //获取task服务
        TaskService taskService = activitiRule.getTaskService();
        //因为在这里只部署了一个流程定义,所以获取单例
        Task task = taskService.createTaskQuery().singleResult();
        //输出查看内容
        LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));
        LOGGER.info("task.description = {}", task.getDescription());
        //指定该任务拥有者 user1
        taskService.setOwner(task.getId(), "user1");
        //查找候选人为shr的task,但无代办人的流程
        List tasks = taskService.createTaskQuery()
                .taskCandidateUser("shr").taskUnassigned().listPage(0, 100);

        //指定代办人为shr
        tasks.forEach(t -> {
            try {
                taskService.claim(task.getId(), "shr");
            } catch (Exception e) {
                LOGGER.error(e.getMessage());
            }
        });
        //查询都是谁关联了这个任务
        List identityLinksForTask = taskService.getIdentityLinksForTask(task.getId());
        identityLinksForTask.forEach(i -> {
            LOGGER.info("{}关联了这个任务", i);
        });

        //查询代办人关联的任务,并且完成它
        List shr = taskService.createTaskQuery().taskAssignee("shr").listPage(0, 100);
        shr.forEach(s -> {
            Map variables = Maps.newHashMap();
            variables.put("完成key1", "完成key1");
            taskService.complete(s.getId(), variables);
        });

        LOGGER.info("流程是否存在 {}", CollectionUtils.isEmpty(shr));
    }

测试任务中添加附件

   /**
     * 添加附件
     */
    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskAttachment() {

        //构建参数变量
        Map var = Maps.newHashMap();
        var.put("message", "this is my test message");
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", var);
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();
        //添加附件,并列出附件 附件类型 任务id 流程实例id 描述 地址
        Attachment attachment = taskService.createAttachment("url", task.getId(), task.getProcessInstanceId(), "name", "desc", "/url/test.png");
        List taskAttachments = taskService.getTaskAttachments(task.getId());
        taskAttachments.forEach(taskAttachment -> {
            LOGGER.info("taskAttachment = {}", ToStringBuilder.reflectionToString(taskAttachment, ToStringStyle.JSON_STYLE));
        });
    }

测试任务中添加评论,event会记录评论变化:

 @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskComment() {

        //构建参数变量
        Map var = Maps.newHashMap();
        var.put("message", "this is my test message");
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", var);
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();

        //为了展示events和comment的不同,特意添加这两个信息在任务中
        taskService.setOwner(task.getId(), "user1");
        taskService.setAssignee(task.getId(), "shr");

        //添加评论信息
        taskService.addComment(task.getId(), task.getProcessInstanceId(), "我是评论");
        taskService.addComment(task.getId(), task.getProcessInstanceId(), "我是评论2");

        //获取评论信息
        List taskComments = taskService.getTaskComments(task.getId());
        taskComments.forEach(comment -> {
            LOGGER.info("评论 = {}", ToStringBuilder.reflectionToString(comment, ToStringStyle.JSON_STYLE));
        });

        //Event会记录task的变化,
        List taskEvents = taskService.getTaskEvents(task.getId());
        taskEvents.forEach(taskEvent -> {
            LOGGER.info("Events评论 = {}", ToStringBuilder.reflectionToString(taskEvent, ToStringStyle.JSON_STYLE));
        });
    }
 /** 结果
         * 11:35:52.791[main][INFO ]评论 = {"type":"comment","userId":null,"time":"Mon Sep 16 11:35:52 CST 2019","taskId":"32509","processInstanceId":"32504","action":"AddComment","message":"我是评论","fullMessage":"我是评论","id":"32518","isInserted":false,"isUpdated":false,"isDeleted":false} ProcessDefinitionId=     executionId= mdcProcessInstanceID= mdcBusinessKey= c.e.a.s.RepositoryServiceTest.lambda$testTaskComment$4:154
         * 11:35:52.792[main][INFO ]评论 = {"type":"comment","userId":null,"time":"Mon Sep 16 11:35:52 CST 2019","taskId":"32509","processInstanceId":"32504","action":"AddComment","message":"我是评论2","fullMessage":"我是评论2","id":"32519","isInserted":false,"isUpdated":false,"isDeleted":false} ProcessDefinitionId=     executionId= mdcProcessInstanceID= mdcBusinessKey= c.e.a.s.RepositoryServiceTest.lambda$testTaskComment$4:154
         * 11:35:52.796[main][INFO ]Events评论 = {"type":"event","userId":null,"time":"Mon Sep 16 11:35:52 CST 2019","taskId":"32509","processInstanceId":null,"action":"AddUserLink","message":"user1_|_owner","fullMessage":null,"id":"32516","isInserted":false,"isUpdated":false,"isDeleted":false} ProcessDefinitionId=     executionId= mdcProcessInstanceID= mdcBusinessKey= c.e.a.s.RepositoryServiceTest.lambda$testTaskComment$5:160
         * 11:35:52.796[main][INFO ]Events评论 = {"type":"event","userId":null,"time":"Mon Sep 16 11:35:52 CST 2019","taskId":"32509","processInstanceId":null,"action":"AddUserLink","message":"shr_|_assignee","fullMessage":null,"id":"32517","isInserted":false,"isUpdated":false,"isDeleted":false} ProcessDefinitionId=     executionId= mdcProcessInstanceID= mdcBusinessKey= c.e.a.s.RepositoryServiceTest.lambda$testTaskComment$5:160
         * 11:35:52.797[main][INFO ]Events评论 = {"type":"comment","userId":null,"time":"Mon Sep 16 11:35:52 CST 2019","taskId":"32509","processInstanceId":"32504","action":"AddComment","message":"我是评论","fullMessage":"我是评论","id":"32518","isInserted":false,"isUpdated":false,"isDeleted":false} ProcessDefinitionId=     executionId= mdcProcessInstanceID= mdcBusinessKey= c.e.a.s.RepositoryServiceTest.lambda$testTaskComment$5:160
         * 11:35:52.797[main][INFO ]Events评论 = {"type":"comment","userId":null,"time":"Mon Sep 16 11:35:52 CST 2019","taskId":"32509","processInstanceId":"32504","action":"AddComment","message":"我是评论2","fullMessage":"我是评论2","id":"32519","isInserted":false,"isUpdated":false,"isDeleted":false} ProcessDefinitionId=     executionId= mdcProcessInstanceID= mdcBusinessKey= c.e.a.s.RepositoryServiceTest.lambda$testTaskComment$5:160
         */

你可能感兴趣的:(后端开发成长记)