Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kie.remote.services.rest.query;
  
  import static org.kie.remote.services.rest.query.QueryResourceData.QUERY_PARAM_DATE_FORMAT;
  import static org.mockito.Matchers.anyString;
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.when;
  
  import java.util.Calendar;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
 import  org.codehaus.jackson.map.ObjectMapper;
 import  org.codehaus.jackson.map.SerializationConfig.Feature;
 import  org.jbpm.kie.services.impl.UserTaskServiceImpl;
 import  org.jbpm.process.audit.AuditLogService;
 import  org.jbpm.process.audit.JPAAuditLogService;
 import  org.jbpm.process.audit.VariableInstanceLog;
 import  org.jbpm.services.api.DeploymentService;
 import  org.jbpm.test.JbpmJUnitBaseTestCase;
 import  org.junit.After;
 import  org.junit.Before;
 import  org.junit.Test;
 import  org.kie.api.runtime.KieSession;
 import  org.kie.api.runtime.manager.RuntimeEngine;
 import  org.kie.api.runtime.manager.RuntimeManager;
 import  org.kie.api.runtime.process.ProcessInstance;
 import  org.kie.api.task.TaskService;
 import  org.kie.api.task.model.TaskSummary;
 import  org.kie.internal.identity.IdentityProvider;
 import  org.kie.internal.task.api.InternalTaskService;
 import  org.kie.remote.client.jaxb.ClientJaxbSerializationProvider;
 import  org.kie.services.client.serialization.JaxbSerializationProvider;
 import  org.kie.services.client.serialization.jaxb.impl.query.JaxbQueryProcessInstanceInfo;
 import  org.kie.services.client.serialization.jaxb.impl.query.JaxbQueryProcessInstanceResult;
 import  org.kie.services.client.serialization.jaxb.impl.query.JaxbQueryTaskInfo;
 import  org.kie.services.client.serialization.jaxb.impl.query.JaxbQueryTaskResult;
 import  org.kie.services.client.serialization.jaxb.impl.query.JaxbVariableInfo;
 import  org.slf4j.Logger;
 import  org.slf4j.LoggerFactory;
 
 @SuppressWarnings({"null""unchecked"})
 public class QueryResourceTest extends JbpmJUnitBaseTestCase {
 
     private static final Logger logger = LoggerFactory.getLogger(QueryResourceTest.class);
     
     private static final String PROCESS_STRING_VAR_FILE = "BPMN2-HumanTaskWithStringVariables.bpmn2";
     private static final String PROCESS_STRING_VAR_ID = "org.var.human.task.string";
     private static final String PROCESS_OBJ_VAR_FILE = "BPMN2-HumanTaskWithObjectVariables.bpmn2";
     private static final String PROCESS_OBJ_VAR_ID = "org.var.human.task.object";
     private static final String USER_ID = "john";
 
     private KieSession ksession;
     private TaskService taskService;
     private RuntimeManager runtimeManager;
     private RuntimeEngine engine;
 
     private QueryResourceImpl queryResource;
 
     private static ObjectMapper jsonMapper = new ObjectMapper();
     private static JaxbSerializationProvider jaxbClientMapper = ServerJaxbSerializationProvider.newInstance();
     private static JaxbSerializationProvider jaxbServerMapper = ClientJaxbSerializationProvider.newInstance();
    
     public QueryResourceTest() {
         super(truetrue"org.jbpm.domain");
         .enable(Feature.INDENT_OUTPUT);
         .setPrettyPrint(true);
         .setPrettyPrint(true);
     }
 
     @Before
     public void init() {
          = getRuntimeEngine();
          = .getKieSession();
          = .getTaskService();
         
         setupTestData();
         
          = new QueryResourceImpl();
         
         IdentityProvider mockIdProvider = mock(IdentityProvider.class);
         when(mockIdProvider.getName()).thenReturn();
         .setIdentityProvider(mockIdProvider);
         
         ProcessRequestBean processRequestBean = new ProcessRequestBean();
         UserTaskServiceImpl userTaskService = new UserTaskServiceImpl();
         userTaskService.setNonProcessScopedTaskService((InternalTaskService) );
         processRequestBean.setUserTaskService(userTaskService);
         DeploymentService mockDepService = mock(DeploymentService.class);
        when(mockDepService.getRuntimeManager(anyString())).thenReturn();
        userTaskService.setDeploymentService(mockDepService);
        .setProcessRequestBean(processRequestBean);
        
        processRequestBean.setAuditLogService(new JPAAuditLogService(getEmf()));
        
    }
    
    @After
    public void cleanup() {
        if != null ) { 
            .disposeRuntimeEngine();
            .close();
        }
    }
    private static boolean testDataInitialized = false;
    
    private void setupTestData() { 
        if( !  ) { 
            forint i = 0; i < 5; ++i ) { 
                runStringProcess();
                runObjectProcess(i);
            }
             = true;
        }
    }
  
    private <T> T roundTripJson(T inthrows Exception { 
        String jsonStr = .writeValueAsString(in);
        .debug("\n" + jsonStr);
        return (T) .readValue(jsonStrin.getClass());
    }
   
    private <T> T roundTripXml(T inthrows Exception { 
        String xmlStr = .serialize(in);
        .debug("\n" + xmlStr);
        return (T) .deserialize(xmlStr);
    }
    
    private void runStringProcess(KieSession ksession) { 
        Map<StringObjectparams = new HashMap<StringObject>();
        String initValue = UUID.randomUUID().toString();
        params.put("inputStr"initValue );
        params.put("otherStr"initValue ); 
        ProcessInstance processInstance = ksession.startProcess(params);
        assertTrue( processInstance != null && processInstance.getState() == ProcessInstance.STATE_ACTIVE);
        long procInstId = processInstance.getId();
        
        List<LongtaskIds = .getTasksByProcessInstanceId(procInstId);
        assertFalse"No tasks found!"taskIds.isEmpty() );
        long taskId = taskIds.get(0); 
        .start(taskId);
        
        Map<StringObjecttaskResults = new HashMap<StringObject>();
        taskResults.put("taskOutputStr""task-" + procInstId);
        .complete(taskIdtaskResults);
    
        assertNull("Process instance has not been finished."ksession.getProcessInstance(procInstId) );
        
        AuditLogService logService = new JPAAuditLogService(getEmf());
        List<VariableInstanceLog> vils = logService.findVariableInstances(procInstId);
        assertTrue( "No variable instance logs found"vils != null && ! vils.isEmpty() );
        assertTrue( "Too few variable instance logs found"vils.size() > 3 );
    }
    private static Random random = new Random();
    
    private void runObjectProcess(KieSession ksessionint i) { 
        Map<StringObjectparams = new HashMap<StringObject>();
        String initValue = "start-" + i;
        params.put("inputStr"new MyType(initValue.nextInt()));
        params.put("otherStr"new MyType(initValue.nextInt()));
        ProcessInstance processInstance = ksession.startProcess(params);
        assertTrue( processInstance != null && processInstance.getState() == ProcessInstance.STATE_ACTIVE);
        long procInstId = processInstance.getId();
        
        List<LongtaskIds = .getTasksByProcessInstanceId(procInstId);
        assertFalse"No tasks found!"taskIds.isEmpty() );
        long taskId = taskIds.get(0); 
        .start(taskId);
        
        Map<StringObjecttaskResults = new HashMap<StringObject>();
        taskResults.put("taskOutputStr"new MyType("task-" + procInstId.nextInt()));
        .complete(taskIdtaskResults);
    
        assertNull("Process instance has not been finished."ksession.getProcessInstance(procInstId) );
        
        AuditLogService logService = new JPAAuditLogService(getEmf());
        List<VariableInstanceLog> vils = logService.findVariableInstances(procInstId);
        assertTrue( "No variable instance logs found"vils != null && ! vils.isEmpty() );
        assertTrue( "Too few variable instance logs found: " + vils.size(), vils.size() >= 3 );
        
        VariableInstanceLog lastVil = null;
        for( VariableInstanceLog vil : vils ) { 
            if( ! vil.getVariableId().equals("inputStr") ) { 
               continue
            }
            iflastVil == null ) { 
                lastVil = vil;
            }
            iflastVil.getId() < vil.getId() ) { 
                lastVil = vil;
            }
        }
        assertTrue( lastVil.getVariableId() + ": " + lastVil.getValue(), 
                lastVil.getValue().contains("check") || lastVil.getVariableId().equals("otherStr") );
    }
    // TESTS ----------------------------------------------------------------------------------------------------------------------
    @Test
    public void queryTaskRestCallTest() throws Exception  {
        int [] pageInfo = { 0, 10 };
        int maxResults = 1000;
        Map<StringString[]> queryParams = new HashMap<StringString[]>();
        
        // simple (everything) 
        JaxbQueryTaskResult result = .queryTaskOrProcInstAndAssociatedVariables(queryParamspageInfomaxResults);
        assertNotNull( "null result"result );
        assertFalse( "empty result"result.getTaskInfoList().isEmpty() );
        assertTrue( "empty result"result.getTaskInfoList().size() > 2 );
        for( JaxbQueryTaskInfo taskInfo : result.getTaskInfoList() ) { 
           long procInstId = taskInfo.getProcessInstanceId();
           assertEquals( procInstIdtaskInfo.getTaskSummaries().get(0).getProcessInstanceId().longValue() );
        }
      
        roundTripJson(result);
        roundTripXml(result);
        
        // complicated 
        long procInstId = result.getTaskInfoList().get(0).getProcessInstanceId();
        long taskId = result.getTaskInfoList().get(0).getTaskSummaries().get(0).getId();
     
        addParams(queryParams"processinstanceid"procInstId + ""); 
        addParams(queryParams"processid_re".substring(0, 10) + "*");
        addParams(queryParams"taskid_min"taskId + "");
        addParams(queryParams"taskid_max"taskId + "");
        addParams(queryParams"taskowner" );
        addParams(queryParams"tst""Completed" );
       
        Calendar cal = GregorianCalendar.getInstance();
        cal.roll(., +2);
        String tomorowStr = .format(cal.getTime()).substring(0, 8);
        addParams(queryParams"edt_max"tomorowStr);
        String yesterdayStr = "00:00:01";
        addParams(queryParams"startdate_min"yesterdayStr);
        addParams(queryParams"vid""inputStr");
        addParams(queryParams"vv_re""check*");
        
        result = .queryTaskOrProcInstAndAssociatedVariables(queryParamspageInfomaxResults);
        assertNotNull( "null result"result );
        assertFalse( "empty result"result.getTaskInfoList().isEmpty() );
        assertTrue( "more than 1 result"result.getTaskInfoList().size() == 1 );
        JaxbQueryTaskInfo taskInfo = result.getTaskInfoList().get(0);
        assertEquals( "more than 1 task sum", 1, taskInfo.getTaskSummaries().size() );
        assertEquals( "more than 1 variable", 1, taskInfo.getVariables().size() );
        TaskSummary taskSum = taskInfo.getTaskSummaries().get(0);
        assertNotNull( taskSum );
        assertEquals( taskIdtaskSum.getId().longValue() );
        JaxbVariableInfo varInfo = taskInfo.getVariables().get(0);
        assertEquals( "inputStr"varInfo.getName() );
      
        roundTripJson(result);
        roundTripXml(result);
    }
    private static void addParams(Map<StringString[]> paramsString nameString... values ) { 
       params.put(name,  values);
    }
   
    @Test
    public void queryProcessRestCallTest() throws Exception  { 
        forint i = 0; i < 3; ++i ) { 
            runObjectProcess(i);
        }
        
        int [] pageInfo = { 0, 10 };
        int maxResults = 1000;
        Map<StringString[]> queryParams = new HashMap<StringString[]>();
        
        // simple (everything) 
        JaxbQueryProcessInstanceResult result = .queryTasksOrProcInstsAndVariables(queryParamspageInfomaxResults);
        assertNotNull( "null result"result );
        assertFalse( "empty result (all)"result.getProcessInstanceInfoList().isEmpty() );
        assertTrue( "not enough proc info's: " + result.getProcessInstanceInfoList().size(), result.getProcessInstanceInfoList().size() > 2 );
        long procInstMin = .;
        long procInstMax = -1l;
        for( JaxbQueryProcessInstanceInfo procInfo : result.getProcessInstanceInfoList() ) { 
           long procInstId = procInfo.getProcessInstance().getId();
           ifprocInstId > procInstMax ) { 
               procInstMax = procInstId;
           }
           ifprocInstId < procInstMin ) { 
               procInstMin = procInstId;
           }
           String procId = procInfo.getProcessInstance().getProcessId();
           boolean myType = procId.contains("object");
           for( JaxbVariableInfo varInfo : procInfo.getVariables() ) { 
               String varVal = (StringvarInfo.getValue();
               String varName = varInfo.getName();
               assertTrueprocId + ": var value [" + varVal + "]"varVal.contains("{") || ! myType );
               assertTrueprocInstId + ": var value [" + varVal + "]"varVal.contains("check") || ! varName.equals("inputStr"));
           }
        }
       
        roundTripJson(result);
        roundTripXml(result);
       
        // complicated 
        Long procInstId = null;
        String varName = null;
        String varVal = null;
        for( JaxbQueryProcessInstanceInfo procInfo : result.getProcessInstanceInfoList() ) { 
            ifprocInfo.getProcessInstance().getProcessId().equals() ) { 
                procInstId = procInfo.getProcessInstance().getId();
                JaxbVariableInfo varInfo = procInfo.getVariables().get(0);
                if( ! varInfo.getName().equals("inputStr") ) {
                   varInfo = procInfo.getVariables().get(1); 
                }
                varName = varInfo.getName();
                varVal = (StringvarInfo.getValue();
                break;
            }
        }
        assertNotNull"proc inst id"procInstId );
        assertNotNull"var id"varName );
        assertNotNull"var value "varVal );
     
        addParams(queryParams"processinstanceid"procInstId + ""); 
        addParams(queryParams"processid_re""*" + .substring(10));
       
        Calendar cal = GregorianCalendar.getInstance();
        cal.roll(., +2);
        String tomorowStr = .format(cal.getTime()).substring(0, 8);
        addParams(queryParams"enddate_max"tomorowStr);
        String yesterdayStr = "00:00:01";
        addParams(queryParams"stdt_min"yesterdayStr);
        addParams(queryParams"var_" + varNamevarVal );
        
        result = .queryTasksOrProcInstsAndVariables(queryParamspageInfomaxResults);
        assertNotNull( "null result"result );
        assertFalse( "empty result"result.getProcessInstanceInfoList().isEmpty() );
        assertTrue( "more than 1 result"result.getProcessInstanceInfoList().size() == 1 );
        JaxbQueryProcessInstanceInfo procInfo = result.getProcessInstanceInfoList().get(0);
        assertNotNull( "no proc instance"procInfo.getProcessInstance() );
        assertEquals( "num variables", 1, procInfo.getVariables().size() );
      
        roundTripJson(result);
        roundTripXml(result);
       
        // more complicated
        queryParams.clear();
        --procInstMax;
        ++procInstMin;
        addParams(queryParams"piid_min", String.valueOf(procInstMin));
        addParams(queryParams"processinstanceid_max", String.valueOf(procInstMax));
        result = .queryTasksOrProcInstsAndVariables(queryParamspageInfomaxResults);
        assertNotNull( "null result"result );
        assertFalse( "empty result"result.getProcessInstanceInfoList().isEmpty() );
        assertEquals( "number results"procInstMax - procInstMin+1, result.getProcessInstanceInfoList().size() );
        long findMin = .;
        long findMax = -1;
        for( JaxbQueryProcessInstanceInfo jaxbProcInfo : result.getProcessInstanceInfoList() ) { 
           procInstId = jaxbProcInfo.getProcessInstance().getId();
           ifprocInstId > findMax ) { 
               findMax = procInstId;
           }
           ifprocInstId < findMin ) { 
               findMin = procInstId;
           }
        }
        assertEquals"process instance id max"procInstMaxfindMax );
        assertEquals"process instance id min"procInstMinfindMin );
        
        // test bad parameter
        addParams(queryParams"taskowner" );
        boolean exThrown = false;
        try { 
            result = .queryTasksOrProcInstsAndVariables(queryParamspageInfomaxResults);
        } catchException e ) { 
           exThrown = true
        }
        assertTrue"Exception not thrown on invalid parameter 'taskowner'"exThrown);
      
        
        // varregex test
        queryParams.clear();
        addParams(queryParams"processinstanceid"procInstId + ""); 
        addParams(queryParams"processid_re""*" + .substring(10));
       
        addParams(queryParams"vr_" + varName"X" + varVal.substring(0, 3) + "*" );
        
        result = .queryTasksOrProcInstsAndVariables(queryParamspageInfomaxResults);
        assertNotNull( "null result"result );
        assertTrue( "Expected empty result: " + result.getProcessInstanceInfoList().size(), result.getProcessInstanceInfoList().isEmpty() );
    }
New to GrepCode? Check out our FAQ X