/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.shardingsphere.infra.executor.sql.process;

import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
import org.apache.shardingsphere.infra.executor.kernel.model.ExecutorDataMap;
import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.context.SQLUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.execute.engine.raw.RawSQLExecutionUnit;
import org.apache.shardingsphere.infra.executor.sql.process.fixture.ExecuteProcessReporterFixture;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;
import java.util.UUID;

import org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public final class ExecuteProcessEngineTest {

    private ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext;

    @Before
    public void setUp() {
        LogicSQL logicSQL = createLogicSQL();
        executionGroupContext = createMockedExecutionGroups();
        ConfigurationProperties actual = createConfigurationProperties();
        ExecuteProcessEngine.initialize(logicSQL, executionGroupContext, actual);
        assertThat(ExecutorDataMap.getValue().get("EXECUTE_ID"), is(executionGroupContext.getExecutionID()));
        assertThat(ExecuteProcessReporterFixture.ACTIONS.get(0), is("Report the summary of this task."));
    }

    @Test
    public void assertFinish() {
        ExecutionUnit executionUnit = new ExecutionUnit("actualName1", new SQLUnit("sql1", Collections.singletonList("parameter1")));
        RawSQLExecutionUnit rawExecutionUnit = new RawSQLExecutionUnit(executionUnit, ConnectionMode.MEMORY_STRICTLY);
        ExecuteProcessEngine.finish(executionGroupContext.getExecutionID(), rawExecutionUnit);
        assertThat(ExecuteProcessReporterFixture.ACTIONS.get(1), is("Report a unit of this task."));
        ExecuteProcessEngine.finish(executionGroupContext.getExecutionID());
        assertThat(ExecuteProcessReporterFixture.ACTIONS.get(2), is("Report this task on completion."));
    }

    @Test
    public void assertClean() {
        ExecuteProcessEngine.clean();
        assertThat(ExecutorDataMap.getValue().size(), is(0));
    }

    private LogicSQL createLogicSQL() {
        DDLStatement ddlStatement = mock(DDLStatement.class);
        SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class);
        when(sqlStatementContext.getSqlStatement()).thenReturn(ddlStatement);
        LogicSQL result = mock(LogicSQL.class);
        when(result.getSqlStatementContext()).thenReturn(sqlStatementContext);
        return result;
    }

    private ConfigurationProperties createConfigurationProperties() {
        ConfigurationProperties result = mock(ConfigurationProperties.class);
        when(result.getValue(ConfigurationPropertyKey.SQL_SHOW)).thenReturn(Boolean.TRUE);
        when(result.getValue(ConfigurationPropertyKey.SHOW_PROCESS_LIST_ENABLED)).thenReturn(Boolean.TRUE);
        return result;
    }

    private ExecutionGroupContext<? extends SQLExecutionUnit> createMockedExecutionGroups() {
        ExecutionGroupContext<? extends SQLExecutionUnit> result = mock(ExecutionGroupContext.class);
        String uuid = UUID.randomUUID().toString();
        when(result.getExecutionID()).thenReturn(uuid);
        return result;
    }

}

@@ -1323,152 +1323,8 @@
 ap;%0A
-import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;%0Aimport org.apache.shardingsphere.infra.executor.sql.context.SQLUnit;%0A
 impo
@@ -1730,144 +1730,29 @@
 st;%0A
-%0Aimport java.util.Collections;%0Aimport java.util.UUID;%0A%0Aimport org.apache.shardingsphere.infra.executor.sql.execute.engine.ConnectionMode
+import java.util.UUID
 ;%0A%0Ai
@@ -1964,16 +1964,20 @@
 eTest %7B%0A
+    
 %0A    pri
@@ -2054,16 +2054,20 @@
 ontext;%0A
+    
 %0A    @Be
@@ -2101,54 +2101,8 @@
 ) %7B%0A
-        LogicSQL logicSQL = createLogicSQL();%0A
     
@@ -2164,82 +2164,8 @@
 ();%0A
-        ConfigurationProperties actual = createConfigurationProperties();%0A
     
@@ -2204,16 +2204,24 @@
 ize(
-l
+createL
 ogicSQL
+()
 , ex
@@ -2241,22 +2241,47 @@
 ontext, 
-actual
+createConfigurationProperties()
 );%0A     
@@ -2489,32 +2489,36 @@
 task.%22));%0A    %7D%0A
+    
 %0A    @Test%0A    p
@@ -2549,261 +2549,8 @@
 ) %7B%0A
-        ExecutionUnit executionUnit = new ExecutionUnit(%22actualName1%22, new SQLUnit(%22sql1%22, Collections.singletonList(%22parameter1%22)));%0A        RawSQLExecutionUnit rawExecutionUnit = new RawSQLExecutionUnit(executionUnit, ConnectionMode.MEMORY_STRICTLY);%0A
     
@@ -2621,19 +2621,27 @@
 onID(), 
-raw
+mock(RawSQL
 Executio
@@ -2645,16 +2645,23 @@
 tionUnit
+.class)
 );%0A     
@@ -2940,24 +2940,28 @@
 .%22));%0A    %7D%0A
+    
 %0A    @Test%0A 
@@ -3089,32 +3089,36 @@
 , is(0));%0A    %7D%0A
+    
 %0A    private Log
@@ -3146,70 +3146,8 @@
 ) %7B%0A
-        DDLStatement ddlStatement = mock(DDLStatement.class);%0A
     
@@ -3288,19 +3288,24 @@
 nReturn(
-ddl
+mock(DDL
 Statemen
@@ -3305,16 +3305,23 @@
 tatement
+.class)
 );%0A     
@@ -3463,32 +3463,36 @@
 n result;%0A    %7D%0A
+    
 %0A    private Con
@@ -3852,16 +3852,20 @@
 ;%0A    %7D%0A
+    
 %0A    pri
@@ -4057,60 +4057,8 @@
 s);%0A
-        String uuid = UUID.randomUUID().toString();%0A
     
@@ -4106,12 +4106,36 @@
 urn(
-uuid
+UUID.randomUUID().toString()
 );%0A 
@@ -4162,11 +4162,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Refactor ExecuteProcessEngineTest (#11734)