/* 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.cmmn.required;

import java.util.Collections;

import org.camunda.bpm.engine.impl.test.CmmnProcessEngineTestCase;
import org.camunda.bpm.engine.runtime.CaseExecution;
import org.camunda.bpm.engine.test.Deployment;

/**
 * @author Roman Smirnov
 *
 */
public class RequiredRuleTest extends CmmnProcessEngineTestCase {

  @Deployment(resources = "org/camunda/bpm/engine/test/cmmn/required/RequiredRuleTest.testVariableBasedRule.cmmn")
  public void testRequiredRuleEvaluatesToTrue() {
    caseService.createCaseInstanceByKey("case", Collections.<String, Object>singletonMap("required", true));

    CaseExecution taskExecution = caseService
        .createCaseExecutionQuery()
        .activityId("PI_HumanTask_1")
        .singleResult();

    assertNotNull(taskExecution);
    assertTrue(taskExecution.isRequired());
  }

  @Deployment(resources = "org/camunda/bpm/engine/test/cmmn/required/RequiredRuleTest.testVariableBasedRule.cmmn")
  public void testRequiredRuleEvaluatesToFalse() {
    caseService.createCaseInstanceByKey("case", Collections.<String, Object>singletonMap("required", false));

    CaseExecution taskExecution = caseService
        .createCaseExecutionQuery()
        .activityId("PI_HumanTask_1")
        .singleResult();

    assertNotNull(taskExecution);
    assertFalse(taskExecution.isRequired());
  }

  @Deployment(resources = "org/camunda/bpm/engine/test/cmmn/required/RequiredRuleTest.testDefaultVariableBasedRule.cmmn")
  public void testDefaultRequiredRuleEvaluatesToTrue() {
    caseService.createCaseInstanceByKey("case", Collections.<String, Object>singletonMap("required", true));

    CaseExecution taskExecution = caseService
        .createCaseExecutionQuery()
        .activityId("PI_HumanTask_1")
        .singleResult();

    assertNotNull(taskExecution);
    assertTrue(taskExecution.isRequired());
  }

  @Deployment(resources = "org/camunda/bpm/engine/test/cmmn/required/RequiredRuleTest.testDefaultVariableBasedRule.cmmn")
  public void testDefaultRequiredRuleEvaluatesToFalse() {
    caseService.createCaseInstanceByKey("case", Collections.<String, Object>singletonMap("required", false));

    CaseExecution taskExecution = caseService
        .createCaseExecutionQuery()
        .activityId("PI_HumanTask_1")
        .singleResult();

    assertNotNull(taskExecution);
    assertFalse(taskExecution.isRequired());
  }

}

@@ -636,16 +636,77 @@
 tions;%0A%0A
+import org.camunda.bpm.engine.exception.NotAllowedException;%0A
 import o
@@ -764,16 +764,16 @@
 stCase;%0A
-
 import o
@@ -813,24 +813,76 @@
 eExecution;%0A
+import org.camunda.bpm.engine.runtime.CaseInstance;%0A
 import org.c
@@ -1177,32 +1177,68 @@
 uatesToTrue() %7B%0A
+    CaseInstance caseInstance =%0A    
     caseService.
@@ -1322,32 +1322,32 @@
 uired%22, true));%0A
-
 %0A    CaseExecuti
@@ -1468,33 +1468,32 @@
 singleResult();%0A
-%0A
     assertNotNul
@@ -1545,32 +1545,239 @@
 n.isRequired());
+%0A%0A    try %7B%0A      caseService.completeCaseExecution(caseInstance.getId());%0A      fail(%22completing the containing stage should not be allowed%22);%0A    %7D catch (NotAllowedException e) %7B%0A      // happy path%0A    %7D
 %0A  %7D%0A%0A  @Deploym
@@ -1924,32 +1924,68 @@
 atesToFalse() %7B%0A
+    CaseInstance caseInstance =%0A    
     caseService.
@@ -2070,32 +2070,32 @@
 ired%22, false));%0A
-
 %0A    CaseExecuti
@@ -2295,32 +2295,139 @@
 n.isRequired());
+%0A%0A    // completing manually should be allowed%0A    caseService.completeCaseExecution(caseInstance.getId());
 %0A  %7D%0A%0A  @Deploym
@@ -2591,24 +2591,60 @@
 sToTrue() %7B%0A
+    CaseInstance caseInstance =%0A    
     caseServ
@@ -2732,32 +2732,32 @@
 uired%22, true));%0A
-
 %0A    CaseExecuti
@@ -2960,24 +2960,231 @@
 Required());
+%0A%0A    try %7B%0A      caseService.completeCaseExecution(caseInstance.getId());%0A      fail(%22completing the containing stage should not be allowed%22);%0A    %7D catch (NotAllowedException e) %7B%0A      // happy path%0A    %7D
 %0A  %7D%0A%0A  @Dep
@@ -3353,24 +3353,60 @@
 ToFalse() %7B%0A
+    CaseInstance caseInstance =%0A    
     caseServ
@@ -3495,32 +3495,32 @@
 ired%22, false));%0A
-
 %0A    CaseExecuti
@@ -3676,32 +3676,32 @@
 taskExecution);%0A
-
     assertFalse(
@@ -3724,20 +3724,127 @@
 Required());
+%0A%0A    // completing manually should be allowed%0A    caseService.completeCaseExecution(caseInstance.getId());
 %0A  %7D%0A%0A%7D%0A

test manual completion with required rule