<commit_before>package org.jbpm.persistence.timer;

import static org.drools.runtime.EnvironmentName.ENTITY_MANAGER_FACTORY;
import static org.jbpm.test.JBPMHelper.*;

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

import javax.persistence.EntityManagerFactory;

import org.drools.KnowledgeBase;
import org.drools.process.instance.WorkItemHandler;
import org.drools.runtime.EnvironmentName;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.jbpm.test.JBPMHelper;
import org.jbpm.test.JbpmJUnitTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * See JBPM-3170/JBPM-3391
 */
public class TimerPersistenceTest extends JbpmJUnitTestCase {

    // General setup
    private static Logger testLogger = LoggerFactory.getLogger(TimerPersistenceTest.class);

    // Test processses
    private final static String DELAY_TIMER_FILE = "delayTimerEventProcess.bpmn";
    private final static String DELAY_TIMER_PROCESS = "DelayTimerEventProcess";
    
    private final static String PROCESS_FILE_NAME = "boundaryTimerProcess.bpmn";
    private final static String PROCESS_NAME = "BoundaryTimerEventProcess";
    private final static String WORK_ITEM_HANLDER_TASK = "Human Task";
    
    private final static String TIMER_FIRED_PROP = "timerFired";
    private final static String TIMER_FIRED_TIME_PROP = "afterTimerTime";

    public TimerPersistenceTest() { 
        super(true);
        this.setPersistence(true);
    }
    
    @Before
    public void setup() { 
        System.clearProperty(TIMER_FIRED_PROP);
        System.clearProperty(TIMER_FIRED_TIME_PROP);
    }
    
    @Test
    public void timerEventOutsideOfKnowledgeSessionTest() throws InterruptedException {
        /**
         * First we set up everything and start the process
         */
        KnowledgeBase kbase = createKnowledgeBase(DELAY_TIMER_FILE);
        StatefulKnowledgeSession ksession = JBPMHelper.newStatefulKnowledgeSession(kbase);
        int ksessionId = ksession.getId();

        // Start the process
        ProcessInstance process = ksession.startProcess(DELAY_TIMER_PROCESS);
        long processId = process.getId();
        Assert.assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());

        /**
         * Then we close down the session (imagine that the timer is for a week,
         * for example.)
         */
        EntityManagerFactory emf = (EntityManagerFactory) ksession.getEnvironment().get(EnvironmentName.ENTITY_MANAGER_FACTORY);
        ksession.dispose();
        ksession = null;
        emf.close();
        emf = null;

        assertTrue("sesssion id has not been saved", ksessionId > 0);
        assertTrue("process id has not been saved", processId > 0);
        kbase = null;
        process = null;

        // The timer fires..
        int sleep = 2000;
        testLogger.debug("Sleeping " + sleep / 1000 + " seconds.");
        Thread.sleep(sleep);
        testLogger.debug("Awake!");

        assertTrue("The timer has not fired!", timerHasFired());
        assertTrue("The did not fire at the right time!", System.currentTimeMillis() > timerFiredTime());
        
        /**
         * Now we recreate a knowledge base and sesion and retrieve the process
         * to see what has happened
         */
        kbase = createKnowledgeBase(DELAY_TIMER_FILE);
        ksession = JBPMHelper.loadStatefulKnowledgeSession(kbase, ksessionId);
        assertNotNull("Could not retrieve session " + ksessionId, ksession);

        process = ksession.getProcessInstance(processId);
        assertNotNull("Process " + processId + "should have completed and been deleted", process);
    }
    
    @Test
    public void boundaryEventTimerAndCompleteHumanTaskWithoutPersistence() throws InterruptedException {
        // Setup session
        KnowledgeBase kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
       
        // Do stuff
        HumanTaskMockHandler humanTaskMockHandler = new HumanTaskMockHandler();
        ProcessInstance process = registerHTHandlerAndStartProcess(ksession, humanTaskMockHandler);
        
        sleepAndVerifyTimerRuns(process.getState());
        completeWork(ksession, humanTaskMockHandler);
   
        // The process reaches the end node
        int processState = process.getState();
        assertTrue("Expected process state to be " + processStateName[2] + " not " + processStateName[processState],
                ProcessInstance.STATE_COMPLETED == process.getState());
    }
    
    @Test
    public void boundaryEventTimerAndCompleteHumanTask() throws InterruptedException {
        /**
         * First we set up everything and start the process
         */
        KnowledgeBase kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        StatefulKnowledgeSession ksession = newStatefulKnowledgeSession(kbase);
        int ksessionId = ksession.getId();
        assertTrue("session id not saved.", ksessionId > 0);
        
        HumanTaskMockHandler humanTaskMockHandler = new HumanTaskMockHandler();
        ProcessInstance process = registerHTHandlerAndStartProcess(ksession, humanTaskMockHandler);
        
        sleepAndVerifyTimerRuns(process.getState());
        completeWork(ksession, humanTaskMockHandler);
    
        // The process reaches the end node
        process = ksession.getProcessInstance(process.getId());
        assertNull("Expected process to have been completed and removed", process);
    }

    @Test
    public void boundaryEventTimerFiresAndCompleteHumanTaskWithoutKSession() throws InterruptedException {
        /**
         * First we set up everything and start the process
         */
        KnowledgeBase kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        StatefulKnowledgeSession ksession = newStatefulKnowledgeSession(kbase);
        int ksessionId = ksession.getId();
        assertTrue("session id not saved.", ksessionId > 0);
        
        HumanTaskMockHandler humanTaskMockHandler = new HumanTaskMockHandler();
        ProcessInstance process = registerHTHandlerAndStartProcess(ksession, humanTaskMockHandler);
        
        int processState = process.getState();
        /**
         * DISPOSE OF THE KSESSION AND THE EMF
         * (imagine that the timer is for 1 week.. we can't keep the ksession and emf open that long.. 
         */
        EntityManagerFactory emf = (EntityManagerFactory) ksession.getEnvironment().get(ENTITY_MANAGER_FACTORY);
        ksession.dispose();
        ksession = null;
        emf.close();
        emf = null;
        
        sleepAndVerifyTimerRuns(processState);
    
        kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        ksession = loadStatefulKnowledgeSession(kbase, ksessionId);
        
        completeWork(ksession, humanTaskMockHandler);
    }

    private ProcessInstance registerHTHandlerAndStartProcess(StatefulKnowledgeSession ksession, HumanTaskMockHandler humanTaskMockHandler) { 
        // Register Human Task Handler
        ksession.getWorkItemManager().registerWorkItemHandler(WORK_ITEM_HANLDER_TASK, humanTaskMockHandler);
    
        // Start the process 
        ProcessInstance process = ksession.startProcess(PROCESS_NAME);
        long processId = process.getId();
        assertTrue("process id not saved", processId > 0);
        
        // The process is in the Human Task waiting for its completion
        int processState = process.getState();
        assertEquals("Expected process state to be " + processStateName[ProcessInstance.STATE_ACTIVE] + " not "
                + processStateName[processState], ProcessInstance.STATE_ACTIVE, processState);<commit_msg>Ignoring tests that test not-yet-supported functionality.<commit_after>package org.jbpm.persistence.timer;

import static org.drools.runtime.EnvironmentName.ENTITY_MANAGER_FACTORY;
import static org.jbpm.test.JBPMHelper.*;

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

import javax.persistence.EntityManagerFactory;

import org.drools.KnowledgeBase;
import org.drools.process.instance.WorkItemHandler;
import org.drools.runtime.EnvironmentName;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.process.ProcessInstance;
import org.jbpm.test.JBPMHelper;
import org.jbpm.test.JbpmJUnitTestCase;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * See JBPM-3170/JBPM-3391
 */
public class TimerPersistenceTest extends JbpmJUnitTestCase {

    // General setup
    private static Logger testLogger = LoggerFactory.getLogger(TimerPersistenceTest.class);

    // Test processses
    private final static String DELAY_TIMER_FILE = "delayTimerEventProcess.bpmn";
    private final static String DELAY_TIMER_PROCESS = "DelayTimerEventProcess";
    
    private final static String PROCESS_FILE_NAME = "boundaryTimerProcess.bpmn";
    private final static String PROCESS_NAME = "BoundaryTimerEventProcess";
    private final static String WORK_ITEM_HANLDER_TASK = "Human Task";
    
    private final static String TIMER_FIRED_PROP = "timerFired";
    private final static String TIMER_FIRED_TIME_PROP = "afterTimerTime";

    public TimerPersistenceTest() { 
        super(true);
        this.setPersistence(true);
    }
    
    @Before
    public void setup() { 
        System.clearProperty(TIMER_FIRED_PROP);
        System.clearProperty(TIMER_FIRED_TIME_PROP);
    }
    
    @Test
    public void boundaryEventTimerAndCompleteHumanTaskWithoutPersistence() throws InterruptedException {
        // Setup session
        KnowledgeBase kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
       
        // Do stuff
        HumanTaskMockHandler humanTaskMockHandler = new HumanTaskMockHandler();
        ProcessInstance process = registerHTHandlerAndStartProcess(ksession, humanTaskMockHandler);
        
        sleepAndVerifyTimerRuns(process.getState());
        completeWork(ksession, humanTaskMockHandler);
   
        // The process reaches the end node
        int processState = process.getState();
        assertTrue("Expected process state to be " + processStateName[2] + " not " + processStateName[processState],
                ProcessInstance.STATE_COMPLETED == process.getState());
    }
    
    @Test
    public void boundaryEventTimerAndCompleteHumanTask() throws InterruptedException {
        /**
         * First we set up everything and start the process
         */
        KnowledgeBase kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        StatefulKnowledgeSession ksession = newStatefulKnowledgeSession(kbase);
        int ksessionId = ksession.getId();
        assertTrue("session id not saved.", ksessionId > 0);
        
        HumanTaskMockHandler humanTaskMockHandler = new HumanTaskMockHandler();
        ProcessInstance process = registerHTHandlerAndStartProcess(ksession, humanTaskMockHandler);
        
        sleepAndVerifyTimerRuns(process.getState());
        completeWork(ksession, humanTaskMockHandler);
    
        // The process completes
        process = ksession.getProcessInstance(process.getId());
        assertNull("Expected process to have been completed and removed", process);
    }

    @Test
    @Ignore
    public void timerEventOutsideOfKnowledgeSessionTest() throws InterruptedException {
        /**
         * First we set up everything and start the process
         */
        KnowledgeBase kbase = createKnowledgeBase(DELAY_TIMER_FILE);
        StatefulKnowledgeSession ksession = JBPMHelper.newStatefulKnowledgeSession(kbase);
        int ksessionId = ksession.getId();
    
        // Start the process
        ProcessInstance process = ksession.startProcess(DELAY_TIMER_PROCESS);
        long processId = process.getId();
        Assert.assertEquals(ProcessInstance.STATE_ACTIVE, process.getState());
    
        /**
         * Then we close down the session (imagine that the timer is for a week,
         * for example.)
         */
        EntityManagerFactory emf = (EntityManagerFactory) ksession.getEnvironment().get(EnvironmentName.ENTITY_MANAGER_FACTORY);
        ksession.dispose();
        ksession = null;
        emf.close();
        emf = null;
    
        assertTrue("sesssion id has not been saved", ksessionId > 0);
        assertTrue("process id has not been saved", processId > 0);
        kbase = null;
        process = null;
    
        // The timer fires..
        int sleep = 2000;
        testLogger.debug("Sleeping " + sleep / 1000 + " seconds.");
        Thread.sleep(sleep);
        testLogger.debug("Awake!");
    
        assertTrue("The timer has not fired!", timerHasFired());
        assertTrue("The did not fire at the right time!", System.currentTimeMillis() > timerFiredTime());
        
        /**
         * Now we recreate a knowledge base and sesion and retrieve the process
         * to see what has happened
         */
        kbase = createKnowledgeBase(DELAY_TIMER_FILE);
        ksession = JBPMHelper.loadStatefulKnowledgeSession(kbase, ksessionId);
        assertNotNull("Could not retrieve session " + ksessionId, ksession);
    
        process = ksession.getProcessInstance(processId);
        assertNotNull("Process " + processId + "should have completed and been deleted", process);
    }

    @Test
    @Ignore
    public void boundaryEventTimerFiresAndCompleteHumanTaskWithoutKSession() throws InterruptedException {
        /**
         * First we set up everything and start the process
         */
        KnowledgeBase kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        StatefulKnowledgeSession ksession = newStatefulKnowledgeSession(kbase);
        int ksessionId = ksession.getId();
        assertTrue("session id not saved.", ksessionId > 0);
        
        HumanTaskMockHandler humanTaskMockHandler = new HumanTaskMockHandler();
        ProcessInstance process = registerHTHandlerAndStartProcess(ksession, humanTaskMockHandler);
        
        int processState = process.getState();
        /**
         * DISPOSE OF THE KSESSION AND THE EMF
         * (imagine that the timer is for 1 week.. we can't keep the ksession and emf open that long.. 
         */
        EntityManagerFactory emf = (EntityManagerFactory) ksession.getEnvironment().get(ENTITY_MANAGER_FACTORY);
        ksession.dispose();
        ksession = null;
        emf.close();
        emf = null;
        
        sleepAndVerifyTimerRuns(processState);
    
        kbase = createKnowledgeBase(PROCESS_FILE_NAME);
        ksession = loadStatefulKnowledgeSession(kbase, ksessionId);
        
        completeWork(ksession, humanTaskMockHandler);
        
        // The process completes
        process = ksession.getProcessInstance(process.getId());
        assertNull("Expected process to have been completed and removed", process);
    }

    private ProcessInstance registerHTHandlerAndStartProcess(StatefulKnowledgeSession ksession, HumanTaskMockHandler humanTaskMockHandler) { 
        // Register Human Task Handler
        ksession.getWorkItemManager().registerWorkItemHandler(WORK_ITEM_HANLDER_TASK, humanTaskMockHandler);
    
        // Start the process 
        ProcessInstance process = ksession.startProcess(PROCESS_NAME);
        long processId = process.getId();
        assertTrue("process id not saved", processId > 0);
        
        // The process is in the Human Task waiting for its completion
        int processState = process.getState();
        assertEquals("Expected process state to be " + processStateName[ProcessInstance.STATE_ACTIVE] + " not "
                + processStateName[processState], ProcessInstance.STATE_ACTIVE, processState);
{'fraction_non_alphanumeric': 0.079476861167002, 'fraction_numerical': 0.003143863179074447, 'mean_word_length': 3.1193991193991195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6348625', 'n_tokens_mistral': 4261, 'n_tokens_neox': 4175, 'n_words': 1007}