package org.camunda.bpm.extension.process_test_coverage.rules;

import org.camunda.bpm.engine.ProcessEngineException;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.extension.process_test_coverage.junit.rules.TestCoverageProcessEngineRule;
import org.camunda.bpm.extension.process_test_coverage.junit.rules.TestCoverageProcessEngineRuleBuilder;
import org.junit.Rule;
import org.junit.Test;

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

import static org.junit.Assert.*;

/**
 * This test should register a still uncompleted/running flow node as such. The
 * color of the flow node should differ from completed flow nodes in the
 * coverage diagram.
 * 
 * @author z0rbas
 *
 */
public class RunningFlowNodeCoverageTest {

    public static final String BPMN_PATH = "processStillRunning.bpmn";

    @Rule
    public TestCoverageProcessEngineRule rule = TestCoverageProcessEngineRuleBuilder.create().build();

    // TODO implement incident coverage handling and refactor this test to a
    // separate class
    @Test(expected = ProcessEngineException.class)
    @Deployment(resources = { RunningFlowNodeCoverageTest.BPMN_PATH })
    public void shouldCoverGatewayAsStillRunningWithException() {

        rule.getRuntimeService().startProcessInstanceByKey(CoverageTestProcessConstants.PROCESS_DEFINITION_KEY);

    }

    @Test
    @Deployment(resources = { RunningFlowNodeCoverageTest.BPMN_PATH })
    public void shouldCoverManualTaskAsStillRunning() {

        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("path", "B");
        final ProcessInstance processInstance = rule.getRuntimeService().startProcessInstanceByKey(
                CoverageTestProcessConstants.PROCESS_DEFINITION_KEY, variables);

        assertFalse("The process instance should still be running!", processInstance.isEnded());

        final Task runningTask = rule.getTaskService().createTaskQuery().active().taskDefinitionKey(
                "UserTask_B").singleResult();
        assertNotNull("One task instance should still be running!", runningTask);

    }

}

@@ -1101,23 +1101,16 @@
 est to a
-%0A    //
  separat
@@ -1117,16 +1117,16 @@
 e class%0A
+
     @Tes
@@ -1301,24 +1301,139 @@
 ception() %7B%0A
+        Map%3CString, Object%3E variables = new HashMap%3CString, Object%3E();%0A        variables.put(%22path%22, %22UNEXPECTED%22);
 %0A        rul
@@ -1531,20 +1531,30 @@
 TION_KEY
+, variables
 );%0A
-%0A
     %7D%0A%0A 

improve the reason for process exception during test coverage