/* 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.flowable.cmmn.test.runtime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.flowable.cmmn.api.runtime.PlanItemInstanceState.ACTIVE;

import java.util.List;

import org.flowable.cmmn.api.runtime.CaseInstance;
import org.flowable.cmmn.api.runtime.PlanItemInstance;
import org.flowable.cmmn.engine.test.CmmnDeployment;
import org.flowable.cmmn.engine.test.FlowableCmmnTestCase;
import org.flowable.common.engine.api.FlowableIllegalArgumentException;
import org.junit.Test;

public class ExitSentryCompletionExceptionTest extends FlowableCmmnTestCase {

    @Test
    @CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/ExitSentryCompletionExceptionTestCase.cmmn")
    public void testStageExitSentryCompletionFailureMessage() {
        CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
            .caseDefinitionKey("exitSentryCompletionExceptionTestCase")
            .start();

        List<PlanItemInstance> planItemInstances = getPlanItemInstances(caseInstance.getId());

        assertThat(planItemInstances).hasSize(4);
        assertPlanItemInstanceState(planItemInstances, "Task B", ACTIVE);

        try {
            cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task B"));

            // must fail
            fail("Triggering Task B must fail as the stage is not yet completable.");
        } catch (FlowableIllegalArgumentException e) {
            assertThat(e.getMessage()).endsWith("The plan item 'Task A (humanTask1)' prevented it from completion.");
        }

        cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task A"));
        cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task B"));
        cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task C"));

        assertThat(cmmnRuntimeService.createPlanItemInstanceQuery().count()).isEqualTo(0);
        assertThat(cmmnRuntimeService.createCaseInstanceQuery().count()).isEqualTo(0);
        assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().finished().count()).isEqualTo(1);
    }

    @Test
    @CmmnDeployment(resources = "org/flowable/cmmn/test/runtime/ExitSentryCompletionExceptionTestCase.cmmn")
    public void testCaseExitSentryCompletionFailureMessage() {
        CaseInstance caseInstance = cmmnRuntimeService.createCaseInstanceBuilder()
            .caseDefinitionKey("exitSentryCompletionExceptionTestCase")
            .start();

        List<PlanItemInstance> planItemInstances = getPlanItemInstances(caseInstance.getId());

        assertThat(planItemInstances).hasSize(4);
        assertPlanItemInstanceState(planItemInstances, "Task C", ACTIVE);

        try {
            cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task C"));

            // must fail
            fail("Triggering Task B must fail as the stage is not yet completable.");
        } catch (FlowableIllegalArgumentException e) {
            assertThat(e.getMessage()).endsWith("The plan item 'Stage (expandedStage1)' prevented it from completion.");
        }

        cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task A"));
        cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task B"));
        cmmnRuntimeService.triggerPlanItemInstance(getPlanItemInstanceIdByName(planItemInstances, "Task C"));

        assertThat(cmmnRuntimeService.createPlanItemInstanceQuery().count()).isEqualTo(0);
        assertThat(cmmnRuntimeService.createCaseInstanceQuery().count()).isEqualTo(0);
        assertThat(cmmnHistoryService.createHistoricCaseInstanceQuery().finished().count()).isEqualTo(1);
    }
}

@@ -698,20 +698,34 @@
 ertions.
-fail
+assertThatThrownBy
 ;%0Aimport
@@ -1480,32 +1480,36 @@
 r()%0A            
+    
 .caseDefinitionK
@@ -1544,32 +1544,36 @@
 ptionTestCase%22)%0A
+    
             .sta
@@ -1808,33 +1808,116 @@
         
-try %7B%0A           
+// Triggering Task B must fail as the stage is not yet completable.%0A        assertThatThrownBy(() -%3E
  cmmnRun
@@ -2009,18 +2009,17 @@
 ask B%22))
-;%0A
+)
 %0A       
@@ -2027,123 +2027,32 @@
     
-// must fail%0A            fail(%22Triggering Task B must fail as the stage is not yet completable.%22);%0A        %7D catch 
+    .isExactlyInstanceOf
 (Flo
@@ -2072,38 +2072,43 @@
 rgumentException
- e) %7B%0A
+.class)%0A   
             asse
@@ -2107,43 +2107,30 @@
     
-assertThat(e.getMessage()).endsWith
+ .hasMessageContaining
 (%22Th
@@ -2191,34 +2191,24 @@
 mpletion.%22);
-%0A        %7D
 %0A%0A        cm
@@ -3088,32 +3088,36 @@
 r()%0A            
+    
 .caseDefinitionK
@@ -3152,32 +3152,36 @@
 ptionTestCase%22)%0A
+    
             .sta
@@ -3420,25 +3420,108 @@
     
-try %7B%0A           
+// Triggering Task C must fail as the stage is not yet completable.%0A        assertThatThrownBy(() -%3E
  cmm
@@ -3609,34 +3609,33 @@
 nces, %22Task C%22))
-;%0A
+)
 %0A            // 
@@ -3635,123 +3635,32 @@
     
-// must fail%0A            fail(%22Triggering Task B must fail as the stage is not yet completable.%22);%0A        %7D catch 
+    .isExactlyInstanceOf
 (Flo
@@ -3692,14 +3692,19 @@
 tion
- e) %7B%0A
+.class)%0A   
     
@@ -3715,43 +3715,30 @@
     
-assertThat(e.getMessage()).endsWith
+ .hasMessageContaining
 (%22Th
@@ -3810,18 +3810,8 @@
 .%22);
-%0A        %7D
 %0A%0A  

Use assertj fluent assertThatThrownBy() instead of fail()