/* 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.container.impl.deployment.jobexecutor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

import org.camunda.bpm.container.RuntimeContainerDelegate;
import org.camunda.bpm.container.impl.RuntimeContainerDelegateImpl;
import org.camunda.bpm.container.impl.deployment.Attachments;
import org.camunda.bpm.container.impl.deployment.PlatformXmlStartProcessEnginesStep;
import org.camunda.bpm.container.impl.deployment.StopProcessEnginesStep;
import org.camunda.bpm.container.impl.metadata.BpmPlatformXmlImpl;
import org.camunda.bpm.container.impl.metadata.JobAcquisitionXmlImpl;
import org.camunda.bpm.container.impl.metadata.JobExecutorXmlImpl;
import org.camunda.bpm.container.impl.metadata.ProcessEngineXmlImpl;
import org.camunda.bpm.container.impl.metadata.spi.JobAcquisitionXml;
import org.camunda.bpm.container.impl.metadata.spi.ProcessEnginePluginXml;
import org.camunda.bpm.container.impl.metadata.spi.ProcessEngineXml;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.camunda.bpm.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.camunda.bpm.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @author Daniel Meyer
 *
 */
public class PlatformJobExecutorActivateTest {

  @Test
  public void shouldAutoActivateIfNoPropertySet() {

    // given
    JobExecutorXmlImpl jobExecutorXml = defineJobExecutor();
    ProcessEngineXmlImpl processEngineXml = defineProcessEngine();
    BpmPlatformXmlImpl bpmPlatformXml = new BpmPlatformXmlImpl(jobExecutorXml, Collections.<ProcessEngineXml>singletonList(processEngineXml));

    // when
    deployPlatform(bpmPlatformXml);

    try {
      ProcessEngine processEngine = ProcessEngines.getProcessEngine("default");
      ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
      // then
      assertEquals(true, processEngineConfiguration.getJobExecutor().isActive());
    }
    finally {
      undeployPlatform();
    }


  }

  @Test
  public void shouldNotAutoActivateIfConfigured() {

    // given
    JobExecutorXmlImpl jobExecutorXml = defineJobExecutor();
    ProcessEngineXmlImpl processEngineXml = defineProcessEngine();
    // activate set to false
    processEngineXml.getProperties()
      .put("jobExecutorActivate", "false");
    BpmPlatformXmlImpl bpmPlatformXml = new BpmPlatformXmlImpl(jobExecutorXml, Collections.<ProcessEngineXml>singletonList(processEngineXml));

    // when
    deployPlatform(bpmPlatformXml);

    try {
      ProcessEngine processEngine = ProcessEngines.getProcessEngine("default");
      ProcessEngineConfigurationImpl processEngineConfiguration = (ProcessEngineConfigurationImpl) processEngine.getProcessEngineConfiguration();
      // then
      assertEquals(false, processEngineConfiguration.getJobExecutor().isActive());
    }
    finally {
      undeployPlatform();
    }
  }


  private ProcessEngineXmlImpl defineProcessEngine() {
    ProcessEngineXmlImpl processEngineXml = new ProcessEngineXmlImpl();
    processEngineXml.setProperties(new HashMap<String, String>());
    processEngineXml.setPlugins(new ArrayList<ProcessEnginePluginXml>());
    processEngineXml.setName("default");
    processEngineXml.setJobAcquisitionName("default");
    processEngineXml.setConfigurationClass(StandaloneInMemProcessEngineConfiguration.class.getName());
    processEngineXml.setDefault(true);
    return processEngineXml;
  }


  private JobExecutorXmlImpl defineJobExecutor() {
    JobAcquisitionXmlImpl jobAcquisition = new JobAcquisitionXmlImpl();
    jobAcquisition.setProperties(new HashMap<String, String>());
    jobAcquisition.setName("default");
    JobExecutorXmlImpl jobExecutorXml = new JobExecutorXmlImpl();
    jobExecutorXml.setProperties(new HashMap<String, String>());
    jobExecutorXml.setJobAcquisitions(Collections.<JobAcquisitionXml>singletonList(jobAcquisition));
    return jobExecutorXml;
  }

  private void undeployPlatform() {
    RuntimeContainerDelegateImpl containerDelegate = (RuntimeContainerDelegateImpl) RuntimeContainerDelegate.INSTANCE.get();
    containerDelegate.getServiceContainer().createUndeploymentOperation("deploy BPM platform")
      .addStep(new StopJobExecutorStep())
      .addStep(new StopProcessEnginesStep())
      .addStep(new StopManagedThreadPoolStep())
      .execute();
  }

  private void deployPlatform(BpmPlatformXmlImpl bpmPlatformXml) {
    RuntimeContainerDelegateImpl containerDelegate = (RuntimeContainerDelegateImpl) RuntimeContainerDelegate.INSTANCE.get();
    containerDelegate.getServiceContainer().createDeploymentOperation("deploy BPM platform")
      .addAttachment(Attachments.BPM_PLATFORM_XML, bpmPlatformXml)
      .addStep(new StartManagedThreadPoolStep())
      .addStep(new StartJobExecutorStep())
      .addStep(new PlatformXmlStartProcessEnginesStep())
      .execute();
  }
}

@@ -1922,24 +1922,111 @@
 Test %7B%0A%0A
+  private static final String ENGINE_NAME = %22PlatformJobExecutorActivateTest-engine%22;%0A%0A
   @Test%0A
   public
@@ -2009,32 +2009,32 @@
 gine%22;%0A%0A  @Test%0A
-
   public void sh
@@ -2478,33 +2478,35 @@
 ocessEngine(
-%22default%22
+ENGINE_NAME
 );%0A      Pro
@@ -3380,25 +3380,27 @@
 sEngine(
-%22default%22
+ENGINE_NAME
 );%0A     
@@ -3826,37 +3826,41 @@
 ();%0A    
-processEngineXml.setP
+HashMap%3CString, String%3E p
 ropertie
@@ -3852,33 +3852,35 @@
 ring%3E properties
-(
+ = 
 new HashMap%3CStri
@@ -3888,24 +3888,152 @@
 g, String%3E()
+;%0A    properties.put(%22jdbcUrl%22, %22jdbc:h2:mem:PlatformJobExecutorActivateTest-db%22);%0A    processEngineXml.setProperties(properties
 );%0A    proce
@@ -4122,33 +4122,35 @@
 Xml.setName(
-%22default%22
+ENGINE_NAME
 );%0A    proce

use separate engine name and db for job executor test