content: /*
   * Copyright 2010-2025 Hyland Software, Inc. and its affiliates.
   *
   * 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.activiti.engine.test.bpmn.event.error;
  
  import static org.assertj.core.api.Assertions.assertThat;
  
  import org.activiti.engine.impl.test.PluggableActivitiTestCase;
  import org.activiti.engine.task.Task;
  import org.activiti.engine.test.Deployment;
  
  /**
  
   */
  public class ErrorEventSubProcessTest extends PluggableActivitiTestCase {
  
      @Deployment
      // an event subprocesses takes precedence over a boundary event
      public void testEventSubprocessTakesPrecedence() {
          String procId = runtimeService.startProcessInstanceByKey("CatchErrorInEmbeddedSubProcess").getId();
          assertThatErrorHasBeenCaught(procId);
      }
  
      @Deployment
      // an event subprocess with errorCode takes precedence over a catch-all handler
      public void testErrorCodeTakesPrecedence() {
          String procId = runtimeService.startProcessInstanceByKey("CatchErrorInEmbeddedSubProcess").getId();
  
          // The process will throw an error event, which is caught and escalated by a User Task
          assertThat(taskService.createTaskQuery().taskDefinitionKey("taskAfterErrorCatch2").count())
              .as("No tasks found in task list.")
              .isEqualTo(1);
          Task task = taskService.createTaskQuery().singleResult();
          assertThat(task.getName()).isEqualTo("Escalated Task");
  
          // Completing the task will end the process instance
          taskService.complete(task.getId());
          assertProcessEnded(procId);
      }
  
      @Deployment
      public void testCatchErrorInEmbeddedSubProcess() {
          String procId = runtimeService.startProcessInstanceByKey("CatchErrorInEmbeddedSubProcess").getId();
          assertThatErrorHasBeenCaught(procId);
      }
  
      @Deployment
      public void testCatchErrorThrownByScriptTaskInEmbeddedSubProcess() {
          String procId = runtimeService
              .startProcessInstanceByKey("CatchErrorThrownByScriptTaskInEmbeddedSubProcess")
              .getId();
          assertThatErrorHasBeenCaught(procId);
      }
  
      @Deployment
      public void testCatchErrorThrownByScriptTaskInEmbeddedSubProcessWithErrorCode() {
          String procId = runtimeService
              .startProcessInstanceByKey("CatchErrorThrownByScriptTaskInEmbeddedSubProcessWithErrorCode")
              .getId();
          assertThatErrorHasBeenCaught(procId);
      }
  
      @Deployment
      public void testCatchErrorThrownByScriptTaskInTopLevelProcess() {
          String procId = runtimeService
              .startProcessInstanceByKey("CatchErrorThrownByScriptTaskInTopLevelProcess")
              .getId();
          assertThatErrorHasBeenCaught(procId);
      }
  
      @Deployment
      public void testCatchErrorThrownByScriptTaskInsideSubProcessInTopLevelProcess() {
          String procId = runtimeService
              .startProcessInstanceByKey("CatchErrorThrownByScriptTaskInsideSubProcessInTopLevelProcess")
              .getId();
          assertThatErrorHasBeenCaught(procId);
      }
  
      @Deployment(
          resources = {
              "org/activiti/engine/test/bpmn/event/error/ErrorEventSubProcessTest.testThrowErrorInScriptTaskInsideCallActivitiCatchInTopLevelProcess.bpmn20.xml",
              "org/activiti/engine/test/bpmn/event/error/BoundaryErrorEventTest.testCatchErrorThrownByJavaDelegateOnCallActivity-child.bpmn20.xml",
          }
      )
      public void testThrowErrorInScriptTaskInsideCallActivitiCatchInTopLevelProcess() {
          String procId = runtimeService
              .startProcessInstanceByKey("testThrowErrorInScriptTaskInsideCallActivitiCatchInTopLevelProcess")
              .getId();
          assertThatErrorHasBeenCaught(procId);
      }
  
      private void assertThatErrorHasBeenCaught(String procId) {
          // The process will throw an error event,
          // which is caught and escalated by a User Task
          assertThat(taskService.createTaskQuery().count()).as("No tasks found in task list.").isEqualTo(1);
          Task task = taskService.createTaskQuery().singleResult();
          assertThat(task.getName()).isEqualTo("Escalated Task");
  
          // Completing the Task will end the process instance
          taskService.complete(task.getId());
          assertProcessEnded(procId);
      }
  }
  