<commit_before>/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.camunda.bpm.engine.test.bpmn.subprocess.transaction;

import java.util.List;

import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.impl.cmd.GetActivityInstanceCmd;
import org.camunda.bpm.engine.impl.persistence.entity.EventSubscriptionEntity;
import org.camunda.bpm.engine.impl.test.PluggableProcessEngineTestCase;

import org.camunda.bpm.engine.runtime.EventSubscription;
import org.camunda.bpm.engine.runtime.ActivityInstance;import org.camunda.bpm.engine.runtime.Execution;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.test.Deployment;


/**
 * @author <NAME>
 */
public class TransactionSubProcessTest extends PluggableProcessEngineTestCase {

  
  @Deployment(resources={"org/camunda/bpm/engine/test/bpmn/subprocess/transaction/TransactionSubProcessTest.testSimpleCase.bpmn20.xml"})
  public void testSimpleCaseTxSuccessful() {
    
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("transactionProcess");
    
    // after the process is started, we have compensate event subscriptions:
    assertEquals(5, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookHotel").count());
    assertEquals(1, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookFlight").count());
    
    // the task is present:
    Task task = taskService.createTaskQuery().singleResult();
    assertNotNull(task);
    
    // making the tx succeed:
    taskService.setVariable(task.getId(), "confirmed", true);    
    taskService.complete(task.getId());
    
    // now the process instance execution is sitting in the 'afterSuccess' task 
    // -> has left the transaction using the "normal" sequence flow
    List<String> activeActivityIds = runtimeService.getActiveActivityIds(processInstance.getId());
    assertTrue(activeActivityIds.contains("afterSuccess"));
    
    // there is a compensate event subscription for the transaction under the process instance
    EventSubscriptionEntity eventSubscriptionEntity = (EventSubscriptionEntity) runtimeService.createEventSubscriptionQuery()
        .eventType("compensate").activityId("tx").executionId(processInstance.getId()).singleResult();
    
    // there is an event-scope execution associated with the event-subscription:
    assertNotNull(eventSubscriptionEntity.getConfiguration());
    Execution eventScopeExecution = runtimeService.createExecutionQuery().executionId(eventSubscriptionEntity.getConfiguration()).singleResult();
    assertNotNull(eventScopeExecution);
    
    // we still have compensate event subscriptions for the compensation handlers, only now they are part of the event scope
    assertEquals(5, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookHotel").executionId(eventScopeExecution.getId()).count());
    assertEquals(1, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookFlight").executionId(eventScopeExecution.getId()).count());
    assertEquals(1, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoChargeCard").executionId(eventScopeExecution.getId()).count());
    
    // assert that the compensation handlers have not been invoked:
    assertNull(runtimeService.getVariable(processInstance.getId(), "undoBookHotel"));
    assertNull(runtimeService.getVariable(processInstance.getId(), "undoBookFlight"));
    assertNull(runtimeService.getVariable(processInstance.getId(), "undoChargeCard"));
           
    // end the process instance
    runtimeService.signal(processInstance.getId());    
    assertProcessEnded(processInstance.getId());    
    assertEquals(0, runtimeService.createExecutionQuery().count());
    
  }
  
  @Deployment(resources={"org/camunda/bpm/engine/test/bpmn/subprocess/transaction/TransactionSubProcessTest.testWaitstateCompensationHandler.bpmn20.xml"})
  public void testWaitstateCompensationHandler() {

    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("transactionProcess");
    
    // after the process is started, we have compensate event subscriptions:
    assertEquals(5,createEventSubscriptionQuery().eventType("compensate").activityId("undoBookHotel").count());
    assertEquals(1,createEventSubscriptionQuery().eventType("compensate").activityId("undoBookFlight").count());
    
    // the task is present:
    Task task = taskService.createTaskQuery().singleResult();
    assertNotNull(task);
    
    // making the tx fail:
    taskService.setVariable(task.getId(), "confirmed", false);    
    taskService.complete(task.getId());
    
    // now there are two user task instances (the compensation handlers):
    
    List<Task> undoBookHotel = taskService.createTaskQuery().taskDefinitionKey("undoBookHotel").list();
    List<Task> undoBookFlight = taskService.createTaskQuery().taskDefinitionKey("undoBookFlight").list();
    
    assertEquals(5,undoBookHotel.size());
    assertEquals(1,undoBookFlight.size());
    
    ActivityInstance rootActivityInstance = runtimeService.getActivityInstance(processInstance.getId());
    List<ActivityInstance> undoBookHotelInstances = getInstancesForActivitiyId(rootActivityInstance, "undoBookHotel");
    List<ActivityInstance> undoBookFlightInstances = getInstancesForActivitiyId(rootActivityInstance, "undoBookFlight");
    assertEquals(5, undoBookHotelInstances.size());
    assertEquals(1, undoBookFlightInstances.size());
    
    ActivityInstance txActivityInstance = rootActivityInstance.getChildActivityInstances()[0];
    assertEquals("tx", txActivityInstance.getActivityId());
    assertEquals(7, txActivityInstance.getChildActivityInstances().length);
    
    for (Task t : undoBookHotel) {
      taskService.complete(t.getId());      
    }
    taskService.complete(undoBookFlight.get(0).getId());
        
    // now the process instance execution is sitting in the 'afterCancellation' task 
    // -> has left the transaction using the cancel boundary event
    List<String> activeActivityIds = runtimeService.getActiveActivityIds(processInstance.getId());
    assertTrue(activeActivityIds.contains("afterCancellation"));
    
    // we have no more compensate event subscriptions
    assertEquals(0,createEventSubscriptionQuery().eventType("compensate").count());
       
    // end the process instance
    runtimeService.signal(processInstance.getId());    
    assertProcessEnded(processInstance.getId());
    assertEquals(0, runtimeService.createExecutionQuery().count());    
  }
  
  @Deployment(resources={"org/camunda/bpm/engine/test/bpmn/subprocess/transaction/TransactionSubProcessTest.testSimpleCase.bpmn20.xml"})
  public void testSimpleCaseTxCancelled() {
    
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("transactionProcess");
    
    // after the process is started, we have compensate event subscriptions:<commit_msg>chore(merge): fix transaction testcase<commit_after>/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.camunda.bpm.engine.test.bpmn.subprocess.transaction;

import java.util.List;

import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.impl.persistence.entity.EventSubscriptionEntity;
import org.camunda.bpm.engine.impl.test.PluggableProcessEngineTestCase;
import org.camunda.bpm.engine.runtime.ActivityInstance;
import org.camunda.bpm.engine.runtime.EventSubscription;
import org.camunda.bpm.engine.runtime.Execution;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.test.Deployment;


/**
 * @author <NAME>
 */
public class TransactionSubProcessTest extends PluggableProcessEngineTestCase {

  
  @Deployment(resources={"org/camunda/bpm/engine/test/bpmn/subprocess/transaction/TransactionSubProcessTest.testSimpleCase.bpmn20.xml"})
  public void testSimpleCaseTxSuccessful() {
    
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("transactionProcess");
    
    // after the process is started, we have compensate event subscriptions:
    assertEquals(5, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookHotel").count());
    assertEquals(1, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookFlight").count());
    
    // the task is present:
    Task task = taskService.createTaskQuery().singleResult();
    assertNotNull(task);
    
    // making the tx succeed:
    taskService.setVariable(task.getId(), "confirmed", true);    
    taskService.complete(task.getId());
    
    // now the process instance execution is sitting in the 'afterSuccess' task 
    // -> has left the transaction using the "normal" sequence flow
    List<String> activeActivityIds = runtimeService.getActiveActivityIds(processInstance.getId());
    assertTrue(activeActivityIds.contains("afterSuccess"));
    
    // there is a compensate event subscription for the transaction under the process instance
    EventSubscriptionEntity eventSubscriptionEntity = (EventSubscriptionEntity) runtimeService.createEventSubscriptionQuery()
        .eventType("compensate").activityId("tx").executionId(processInstance.getId()).singleResult();
    
    // there is an event-scope execution associated with the event-subscription:
    assertNotNull(eventSubscriptionEntity.getConfiguration());
    Execution eventScopeExecution = runtimeService.createExecutionQuery().executionId(eventSubscriptionEntity.getConfiguration()).singleResult();
    assertNotNull(eventScopeExecution);
    
    // we still have compensate event subscriptions for the compensation handlers, only now they are part of the event scope
    assertEquals(5, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookHotel").executionId(eventScopeExecution.getId()).count());
    assertEquals(1, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookFlight").executionId(eventScopeExecution.getId()).count());
    assertEquals(1, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoChargeCard").executionId(eventScopeExecution.getId()).count());
    
    // assert that the compensation handlers have not been invoked:
    assertNull(runtimeService.getVariable(processInstance.getId(), "undoBookHotel"));
    assertNull(runtimeService.getVariable(processInstance.getId(), "undoBookFlight"));
    assertNull(runtimeService.getVariable(processInstance.getId(), "undoChargeCard"));
           
    // end the process instance
    runtimeService.signal(processInstance.getId());    
    assertProcessEnded(processInstance.getId());    
    assertEquals(0, runtimeService.createExecutionQuery().count());
    
  }
  
  @Deployment(resources={"org/camunda/bpm/engine/test/bpmn/subprocess/transaction/TransactionSubProcessTest.testWaitstateCompensationHandler.bpmn20.xml"})
  public void testWaitstateCompensationHandler() {

    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("transactionProcess");
    
    // after the process is started, we have compensate event subscriptions:
    assertEquals(5, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookHotel").count());
    assertEquals(1, runtimeService.createEventSubscriptionQuery().eventType("compensate").activityId("undoBookFlight").count());
    
    // the task is present:
    Task task = taskService.createTaskQuery().singleResult();
    assertNotNull(task);
    
    // making the tx fail:
    taskService.setVariable(task.getId(), "confirmed", false);    
    taskService.complete(task.getId());
    
    // now there are two user task instances (the compensation handlers):
    
    List<Task> undoBookHotel = taskService.createTaskQuery().taskDefinitionKey("undoBookHotel").list();
    List<Task> undoBookFlight = taskService.createTaskQuery().taskDefinitionKey("undoBookFlight").list();
    
    assertEquals(5,undoBookHotel.size());
    assertEquals(1,undoBookFlight.size());
    
    ActivityInstance rootActivityInstance = runtimeService.getActivityInstance(processInstance.getId());
    List<ActivityInstance> undoBookHotelInstances = getInstancesForActivitiyId(rootActivityInstance, "undoBookHotel");
    List<ActivityInstance> undoBookFlightInstances = getInstancesForActivitiyId(rootActivityInstance, "undoBookFlight");
    assertEquals(5, undoBookHotelInstances.size());
    assertEquals(1, undoBookFlightInstances.size());
    
    ActivityInstance txActivityInstance = rootActivityInstance.getChildActivityInstances()[0];
    assertEquals("tx", txActivityInstance.getActivityId());
    assertEquals(7, txActivityInstance.getChildActivityInstances().length);
    
    for (Task t : undoBookHotel) {
      taskService.complete(t.getId());      
    }
    taskService.complete(undoBookFlight.get(0).getId());
        
    // now the process instance execution is sitting in the 'afterCancellation' task 
    // -> has left the transaction using the cancel boundary event
    List<String> activeActivityIds = runtimeService.getActiveActivityIds(processInstance.getId());
    assertTrue(activeActivityIds.contains("afterCancellation"));
    
    // we have no more compensate event subscriptions
    assertEquals(0, runtimeService.createEventSubscriptionQuery().eventType("compensate").count());
       
    // end the process instance
    runtimeService.signal(processInstance.getId());    
    assertProcessEnded(processInstance.getId());
    assertEquals(0, runtimeService.createExecutionQuery().count());    
  }
  
  @Deployment(resources={"org/camunda/bpm/engine/test/bpmn/subprocess/transaction/TransactionSubProcessTest.testSimpleCase.bpmn20.xml"})
  public void testSimpleCaseTxCancelled() {
    
    ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("transactionProcess");
    
    // after the process is started, we have compensate event subscriptions:
{'fraction_non_alphanumeric': 0.10578566732412886, 'fraction_numerical': 0.0035502958579881655, 'mean_word_length': 5.228910728910729, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '6083216', 'n_tokens_mistral': 3893, 'n_tokens_neox': 3892, 'n_words': 780}