/* 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.flowable.test.spring.boot.process;

import static org.assertj.core.api.Assertions.assertThat;

import javax.persistence.EntityManagerFactory;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.job.service.impl.asyncexecutor.AsyncExecutor;
import org.flowable.spring.boot.FlowableTransactionAutoConfiguration;
import org.flowable.spring.boot.ProcessEngineAutoConfiguration;
import org.flowable.spring.boot.ProcessEngineServicesAutoConfiguration;
import org.flowable.spring.job.service.SpringAsyncExecutor;
import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.core.task.TaskExecutor;

/**
 * @author Joram Barrez
 */
public class ProcessAsyncHistoryExecutorTest {

    private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
        .withConfiguration(AutoConfigurations.of(
            ProcessEngineServicesAutoConfiguration.class,
            ProcessEngineAutoConfiguration.class,
            FlowableTransactionAutoConfiguration.class,
            DataSourceAutoConfiguration.class,
            TransactionAutoConfiguration.class
        ))
        .withPropertyValues("flowable.process.async-history.enable=true")
        .withClassLoader(new FilteredClassLoader(EntityManagerFactory.class));

    @Test
    public void asyncHistoryExecutorBeanAvailable() {
        contextRunner.run((context -> {
            assertThat(context).hasSingleBean(ProcessEngine.class);
            assertThat(context).hasBean("taskExecutor");
            assertThat(context).hasBean("processAsyncExecutor");
            assertThat(context).hasBean("asyncHistoryExecutor");
            
            AsyncExecutor processAsyncExecutor = context.getBean(ProcessEngine.class).getProcessEngineConfiguration().getAsyncExecutor();
            assertThat(processAsyncExecutor).isNotNull();
            AsyncExecutor processAsyncHistoryExecutor = context.getBean(ProcessEngine.class).getProcessEngineConfiguration().getAsyncHistoryExecutor();
            assertThat(processAsyncHistoryExecutor).isNotNull();

            assertThat(processAsyncExecutor).isNotSameAs(processAsyncHistoryExecutor);

            TaskExecutor taskExecutorBean = context.getBean("taskExecutor", TaskExecutor.class);

            assertThat(((SpringAsyncExecutor) processAsyncExecutor).getTaskExecutor()).isSameAs(taskExecutorBean);
            assertThat(((SpringAsyncExecutor) processAsyncHistoryExecutor).getTaskExecutor()).isSameAs(taskExecutorBean);
            
            assertThat(context.getBean(ProcessEngine.class).getProcessEngineConfiguration().isAsyncExecutorActivate()).isTrue();
            assertThat(context.getBean(ProcessEngine.class).getProcessEngineConfiguration().isAsyncHistoryExecutorActivate()).isTrue();
            
            assertThat(((ProcessEngineConfigurationImpl) context.getBean(ProcessEngine.class).getProcessEngineConfiguration()).isAsyncHistoryEnabled()).isTrue();
            
        }));
    }

}

@@ -2096,82 +2096,8 @@
  ))%0A
-        .withPropertyValues(%22flowable.process.async-history.enable=true%22)%0A
     
@@ -2258,16 +2258,81 @@
 tRunner.
+withPropertyValues(%22flowable.process.async-history.enable=true%22).
 run((con
@@ -3932,10 +3932,2487 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void asyncHistoryExecutorBeanDisabled() %7B%0A        contextRunner%0A            .withPropertyValues(%22flowable.process.async-history.enable=false%22)%0A            .run((context -%3E %7B%0A                assertThat(context).hasSingleBean(ProcessEngine.class);%0A                assertThat(context).hasBean(%22taskExecutor%22);%0A                assertThat(context).hasBean(%22processAsyncExecutor%22);%0A                assertThat(context).doesNotHaveBean(%22asyncHistoryExecutor%22);%0A%0A                AsyncExecutor processAsyncExecutor = context.getBean(ProcessEngine.class).getProcessEngineConfiguration().getAsyncExecutor();%0A                assertThat(processAsyncExecutor).isNotNull();%0A                AsyncExecutor processAsyncHistoryExecutor = context.getBean(ProcessEngine.class).getProcessEngineConfiguration().getAsyncHistoryExecutor();%0A                assertThat(processAsyncHistoryExecutor).isNull();%0A%0A                assertThat(processAsyncExecutor).isNotSameAs(processAsyncHistoryExecutor);%0A%0A                TaskExecutor taskExecutorBean = context.getBean(%22taskExecutor%22, TaskExecutor.class);%0A%0A                assertThat(((SpringAsyncExecutor) processAsyncExecutor).getTaskExecutor()).isSameAs(taskExecutorBean);%0A%0A                assertThat(context.getBean(ProcessEngine.class).getProcessEngineConfiguration().isAsyncExecutorActivate()).isTrue();%0A                assertThat(context.getBean(ProcessEngine.class).getProcessEngineConfiguration().isAsyncHistoryExecutorActivate()).isTrue();%0A%0A                assertThat(((ProcessEngineConfigurationImpl) context.getBean(ProcessEngine.class).getProcessEngineConfiguration()).isAsyncHistoryEnabled())%0A                    .isFalse();%0A%0A            %7D));%0A    %7D%0A%0A    @Test%0A    public void asyncHistoryExecutorBeanWithDefault() %7B%0A        contextRunner%0A            .run((context -%3E %7B%0A                assertThat(context).doesNotHaveBean(%22asyncHistoryExecutor%22);%0A%0A                AsyncExecutor processAsyncHistoryExecutor = context.getBean(ProcessEngine.class).getProcessEngineConfiguration().getAsyncHistoryExecutor();%0A                assertThat(processAsyncHistoryExecutor).isNull();%0A%0A                assertThat(context.getBean(ProcessEngine.class).getProcessEngineConfiguration().isAsyncHistoryExecutorActivate()).isTrue();%0A%0A                assertThat(((ProcessEngineConfigurationImpl) context.getBean(ProcessEngine.class).getProcessEngineConfiguration()).isAsyncHistoryEnabled())%0A                    .isFalse();%0A%0A            %7D));%0A    %7D%0A%0A
 %7D%0A

Add more tests for the async history Spring Boot configuration (#1083)