/*
 * Copyright 2016 camunda services GmbH.
 *
 * 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.qa.rolling.upgrade;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.qa.upgrade.Origin;
import org.camunda.bpm.qa.upgrade.ScenarioUnderTest;
import org.camunda.bpm.qa.upgrade.UpgradeTestRule;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

/**
 * This test ensures that the old engine can complete an
 * existing process instance on the new schema.
 *
 * @author Christopher Zell <christopher.zell@camunda.com>
 */
@ScenarioUnderTest("StartProcessInstance")
@Origin("7.5.0")
public class CompleteProcessInstanceTest {

  public static final String PROCESS_DEF_KEY = "rollingProcess";

  @Rule
  public UpgradeTestRule rule = new UpgradeTestRule();

  @Test
  @ScenarioUnderTest("init.1")
  public void testDeployProcessWithoutIsExecutableAttribute() {
    //given an already started process instance
    RuntimeService runtimeService = rule.getRuntimeService();
    ProcessInstance oldInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(PROCESS_DEF_KEY).singleResult();
    Assert.assertNotNull(oldInstance);

    //which waits on an user task
    TaskService taskService = rule.getTaskService();
    Task userTask = taskService.createTaskQuery().processInstanceId(oldInstance.getId()).singleResult();
    Assert.assertNotNull(userTask);

    //when completing the user task
    taskService.complete(userTask.getId());

    //then there exists no more tasks
    //and the process instance is also completed
    Assert.assertEquals(0, taskService.createTaskQuery().processDefinitionKey(PROCESS_DEF_KEY).count());
    Assert.assertEquals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(PROCESS_DEF_KEY).count());
  }
}

@@ -1331,82 +1331,16 @@
  %7B%0A%0A
-  public static final String PROCESS_DEF_KEY = %22rollingProcess%22;%0A%0A
   @Rule%0A
   pu
@@ -1335,16 +1335,16 @@
   @Rule%0A
+
   public
@@ -1550,194 +1550,58 @@
     
-RuntimeService runtimeService = rule.getRuntimeService();%0A    ProcessInstance oldInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(PROCESS_DEF_KEY).singleResult
+ProcessInstance oldInstance = rule.processInstance
 ();%0A
@@ -2068,74 +2068,23 @@
 (0, 
-taskService.createTaskQuery().processDefinitionKey(PROCESS_DEF_KEY
+rule.taskQuery(
 ).co
@@ -2099,125 +2099,39 @@
     
-Assert
+rule
 .assert
-Equals(0, runtimeService.createProcessInstanceQuery().processDefinitionKey(PROCESS_DEF_KEY).count()
+ScenarioEnded(
 );%0A  %7D%0A
+%0A
 %7D%0A

use upgrade test rule in migration test for queries