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

import java.util.ArrayList;
import java.util.List;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.ProcessEngineConfiguration;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.impl.ProcessEngineImpl;
import org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.camunda.bpm.engine.impl.test.TestHelper;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

/**
 * @author Svetlana Dorokhova
 *
 */
public class BulkHistoryDeleteDmnDisabledTest {

  private ProcessEngineImpl processEngineImpl;
  private RepositoryService repositoryService;
  private RuntimeService runtimeService;
  private HistoryService historyService;

  @Before
  public void createProcessEngine() {
    processEngineImpl = createProcessEngineImpl(false);
    repositoryService = processEngineImpl.getRepositoryService();
    runtimeService = processEngineImpl.getRuntimeService();
    historyService = processEngineImpl.getHistoryService();

  }

  // make sure schema is dropped
  @After
  public void cleanup() {
    TestHelper.dropSchema(processEngineImpl.getProcessEngineConfiguration());
    processEngineImpl.close();
    processEngineImpl = null;

  }

  @Test
  public void bulkHistoryDeleteWithDisabledDmn() {
    BpmnModelInstance model = Bpmn.createExecutableProcess("someProcess")
        .startEvent()
          .userTask("userTask")
        .endEvent()
        .done();
    repositoryService.createDeployment().addModelInstance("process.bpmn", model).deploy();
    List<String> ids = prepareHistoricProcesses("someProcess");
    runtimeService.deleteProcessInstances(ids, null, true, true);

    //when
    historyService.deleteHistoricProcessInstancesBulk(ids);

    //then
    assertEquals(0, historyService.createHistoricProcessInstanceQuery().processDefinitionKey("someProcess").count());
  }

  private List<String> prepareHistoricProcesses(String businessKey) {
    List<String> processInstanceIds = new ArrayList<String>();

    for (int i = 0; i < 5; i++) {
      ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(businessKey);
      processInstanceIds.add(processInstance.getId());
    }

    return processInstanceIds;
  }

  protected ProcessEngineImpl createProcessEngineImpl(boolean dmnEnabled) {
    StandaloneInMemProcessEngineConfiguration config =
        (StandaloneInMemProcessEngineConfiguration) new StandaloneInMemProcessEngineConfiguration()
               .setProcessEngineName("database-dmn-test-engine")
               .setDatabaseSchemaUpdate("true")
               .setHistory(ProcessEngineConfiguration.HISTORY_FULL)
               .setJdbcUrl("jdbc:h2:mem:DatabaseDmnTest");

    config.setDmnEnabled(dmnEnabled);

    return (ProcessEngineImpl) config.buildProcessEngine();
  }

}

@@ -888,16 +888,20 @@
 ne.impl.
+cfg.
 ProcessE
@@ -901,24 +901,37 @@
 rocessEngine
+Configuration
 Impl;%0Aimport
@@ -958,58 +958,99 @@
 ine.
-impl.cfg.StandaloneInMemProcessEngineConfiguration
+runtime.ProcessInstance;%0Aimport org.camunda.bpm.engine.test.util.ProcessEngineBootstrapRule
 ;%0Aim
@@ -1081,28 +1081,39 @@
 ine.
-impl.test.TestHelper
+test.util.ProcessEngineTestRule
 ;%0Aim
@@ -1140,38 +1140,50 @@
 .engine.
-runtime.ProcessInstanc
+test.util.ProvidedProcessEngineRul
 e;%0Aimpor
@@ -1274,32 +1274,8 @@
 ce;%0A
-import org.junit.After;%0A
 impo
@@ -1312,20 +1312,77 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.RuleChain
 ;%0Aimport
@@ -1515,21 +1515,23 @@
  %7B%0A%0A  pr
-ivate
+otected
  Process
@@ -1540,337 +1540,537 @@
 gine
-Impl processEngineImpl;%0A  private RepositoryService repositoryService;%0A  private RuntimeService runtimeService;%0A  private HistoryService historyService;%0A%0A  @Before%0A  public void createProcessEngine() %7B%0A    processEngineImpl = createProcessEngineImpl(fals
+BootstrapRule bootstrapRule = new ProcessEngineBootstrapRule() %7B%0A    public ProcessEngineConfiguration configureEngine(ProcessEngineConfigurationImpl configuration) %7B%0A      configuration.setDmnEnabled(false);%0A      return configuration;%0A    %7D%0A  %7D;%0A%0A  protected ProvidedProcessEngineRule engineRule = new ProvidedProcessEngineRule(bootstrapRule);%0A  public ProcessEngineTestRule testRule = new ProcessEngineTestRule(engineRul
 e);%0A
+%0A
   
-  repositoryService = processEngineImpl.getRepositoryService();%0A    r
+@Rule%0A  public RuleChain ruleChain = RuleChain.outerRule(engineRule).around(testRule);%0A%0A  private R
 unti
@@ -2083,32 +2083,9 @@
 ice 
-= processEngineImpl.getR
+r
 unti
@@ -2097,17 +2097,21 @@
 vice
-()
 ;%0A  
-  h
+private H
 isto
@@ -2124,32 +2124,9 @@
 ice 
-= processEngineImpl.getH
+h
 isto
@@ -2138,59 +2138,20 @@
 vice
-()
 ;%0A%0A  
-%7D%0A%0A  // make sure schema is dropped%0A  @After
+@Before
 %0A  p
@@ -2166,156 +2166,135 @@
 id c
-leanup() %7B%0A    TestHelper.dropSchema(processEngineImpl.getProcessEngineConfiguration
+reateProcessEngine() %7B%0A    runtimeService = engineRule.getRuntimeService
 ()
-)
 ;%0A    
-processEngineImpl.close();%0A    processEngineImpl = null
+historyService = engineRule.getHistoryService()
 ;%0A%0A 
@@ -2529,92 +2529,29 @@
     
-repositoryService.createDeployment().addModelInstance(%22process.bpmn%22, model).deploy(
+testRule.deploy(model
 );%0A 
@@ -3251,582 +3251,6 @@
  %7D%0A%0A
-  protected ProcessEngineImpl createProcessEngineImpl(boolean dmnEnabled) %7B%0A    StandaloneInMemProcessEngineConfiguration config =%0A        (StandaloneInMemProcessEngineConfiguration) new StandaloneInMemProcessEngineConfiguration()%0A               .setProcessEngineName(%22database-dmn-test-engine%22)%0A               .setDatabaseSchemaUpdate(%22true%22)%0A               .setHistory(ProcessEngineConfiguration.HISTORY_FULL)%0A               .setJdbcUrl(%22jdbc:h2:mem:DatabaseDmnTest%22);%0A%0A    config.setDmnEnabled(dmnEnabled);%0A%0A    return (ProcessEngineImpl) config.buildProcessEngine();%0A  %7D%0A%0A
 %7D%0A

test fix