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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.camunda.bpm.engine.impl.cmmn.behavior.CmmnActivityBehavior;
import org.camunda.bpm.engine.impl.cmmn.entity.repository.CaseDefinitionEntity;
import org.camunda.bpm.engine.impl.cmmn.handler.CaseHandler;
import org.camunda.bpm.engine.impl.cmmn.handler.CmmnHandlerContext;
import org.camunda.bpm.engine.impl.cmmn.model.CmmnActivity;
import org.camunda.bpm.engine.impl.persistence.entity.DeploymentEntity;
import org.junit.Before;
import org.junit.Test;

/**
 * @author Roman Smirnov
 *
 */
public class CaseHandlerTest extends CmmnElementHandlerTest {

  protected CaseHandler handler = new CaseHandler();
  protected CmmnHandlerContext context;

  @Before
  public void setUp() {
    context = new CmmnHandlerContext();

    DeploymentEntity deployment = new DeploymentEntity();
    deployment.setId("aDeploymentId");

    context.setDeployment(deployment);
    context.setModel(modelInstance);
  }

  @Test
  public void testCaseActivityName() {
    // given:
    // the case has a name "A Case"
    String name = "A Case";
    caseDefinition.setName(name);

    // when
    CmmnActivity activity = handler.handleElement(caseDefinition, context);

    // then
    assertEquals(name, activity.getName());
  }

  @Test
  public void testActivityBehavior() {
    // given: a case

    // when
    CmmnActivity activity = handler.handleElement(caseDefinition, context);

    // then
    CmmnActivityBehavior behavior = activity.getActivityBehavior();
    assertNull(behavior);
  }

  @Test
  public void testCaseHasNoParent() {
    // given: a caseDefinition

    // when
    CmmnActivity activity = handler.handleElement(caseDefinition, context);

    // then
    assertNull(activity.getParent());
  }

  @Test
  public void testCaseDefinitionKey() {
    // given: a caseDefinition

    // when
    CaseDefinitionEntity activity = (CaseDefinitionEntity) handler.handleElement(caseDefinition, context);

    // then
    assertEquals(caseDefinition.getId(), activity.getKey());
  }

  @Test
  public void testDeploymentId() {
    // given: a caseDefinition

    // when
    CaseDefinitionEntity activity = (CaseDefinitionEntity) handler.handleElement(caseDefinition, context);

    // then
    String deploymentId = context.getDeployment().getId();
    assertEquals(deploymentId, activity.getDeploymentId());
  }

  @Test
  public void testHistoryTimeToLiveNull() {
    // given: a caseDefinition

    // when
    CaseDefinitionEntity activity = (CaseDefinitionEntity) handler.handleElement(caseDefinition, context);

    // then
    assertNull(activity.getHistoryTimeToLive());
  }

  @Test
  public void testHistoryTimeToLive() {
    // given: a caseDefinition
    Integer historyTimeToLive = 6;
    caseDefinition.setCamundaHistoryTimeToLive(historyTimeToLive);

    // when
    CaseDefinitionEntity activity = (CaseDefinitionEntity) handler.handleElement(caseDefinition, context);

    // then
    assertEquals(Integer.valueOf(historyTimeToLive), activity.getHistoryTimeToLive());
  }

  @Test
  public void testHistoryTimeToLiveNegative() {
    // given: a caseDefinition
    Integer historyTimeToLive = -6;
    caseDefinition.setCamundaHistoryTimeToLive(historyTimeToLive);

    // when
    CaseDefinitionEntity activity = (CaseDefinitionEntity) handler.handleElement(caseDefinition, context);

    // then
    assertEquals(Integer.valueOf(historyTimeToLive), activity.getHistoryTimeToLive());
  }

}

@@ -692,16 +692,155 @@
 rtNull;%0A
+import static org.junit.Assert.assertTrue;%0Aimport static org.junit.Assert.fail;%0A%0Aimport org.camunda.bpm.engine.exception.NotValidException;
 %0Aimport 
@@ -4025,74 +4025,33 @@
     
-// when%0A    CaseDefinitionEntity activity = (CaseDefinitionEntity)
+try %7B%0A      // when%0A     
  han
@@ -4099,107 +4099,184 @@
 t);%0A
-%0A
     
-// then%0A    assertEquals(Integer.valueOf(historyTimeToLive), activity.getHistoryTimeToLive());
+  fail(%22Exception is expected, that negative value is not allowed.%22);%0A    %7D catch (NotValidException ex) %7B%0A      assertTrue(ex.getMessage().contains(%22greater than%22));%0A    %7D
 %0A  %7D

fix a test case