/*
 * 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.sql.parser.integrate.engine;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.antlr.v4.runtime.tree.ParseTree;
import org.apache.shardingsphere.spi.NewInstanceServiceLoader;
import org.apache.shardingsphere.sql.parser.core.parser.SQLParserFactory;
import org.apache.shardingsphere.sql.parser.core.visitor.SQLVisitorEngine;
import org.apache.shardingsphere.sql.parser.integrate.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.sql.parser.integrate.asserts.statement.SQLStatementAssert;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.SQLParserTestCasesRegistry;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.VisitorSQLParserTestCasesRegistryFactory;
import org.apache.shardingsphere.sql.parser.integrate.jaxb.domain.statement.SQLParserTestCase;
import org.apache.shardingsphere.sql.parser.spi.SQLParserEntry;
import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
import org.apache.shardingsphere.test.sql.SQLCaseType;
import org.apache.shardingsphere.test.sql.loader.SQLCasesLoader;
import org.apache.shardingsphere.test.sql.loader.SQLCasesRegistry;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;

import static org.junit.Assert.fail;

@RunWith(Parameterized.class)
@RequiredArgsConstructor
@Slf4j
public final class VisitorParameterizedParsingTest {
    
    private static final SQLCasesLoader SQL_CASES_LOADER = SQLCasesRegistry.getInstance().getSqlCasesLoader();
    
    private static final SQLParserTestCasesRegistry SQL_PARSER_TEST_CASES_REGISTRY = VisitorSQLParserTestCasesRegistryFactory.getInstance().getRegistry();
    
    private static final Properties PROPS = new Properties();
    
    private final String sqlCaseId;
    
    private final String databaseType;
    
    private final SQLCaseType sqlCaseType;
    
    static {
        try {
            PROPS.load(new FileInputStream(SQLParserParameterizedTest.class.getProtectionDomain().getCodeSource().getLocation().getPath() + "/runtime-config.properties"));
        } catch (final IOException ex) {
            log.warn("Can not find file `runtime-config.properties`, use default properties configuration.", ex);
        }
    }
    
    @Before
    public void setUp() {
        NewInstanceServiceLoader.register(SQLParserEntry.class);
    }
    
    @Parameters(name = "{0} ({2}) -> {1}")
    public static Collection<Object[]> getTestParameters() {
        // TODO resume me after all test cases passed
        //        checkTestCases();
        return SQL_CASES_LOADER.getSQLTestParameters();
    }
    
    private static void checkTestCases() {
        Collection<String> allSQLCaseIDs = new HashSet<>(SQL_CASES_LOADER.getAllSQLCaseIDs());
        if (allSQLCaseIDs.size() != SQL_PARSER_TEST_CASES_REGISTRY.getAllSQLCaseIDs().size()) {
            allSQLCaseIDs.removeAll(SQL_PARSER_TEST_CASES_REGISTRY.getAllSQLCaseIDs());
            fail(String.format("The count of SQL cases and SQL parser cases are mismatched, missing cases are: %s", allSQLCaseIDs));
        }
    }
    
    @Test
    public void assertSupportedSQL() {
        // TODO remove me after all test cases passed
        if (!"H2".equals(databaseType) && !"MySQL".equals(databaseType)) {
            return;
        }
        String sql;
        try {
            sql = SQL_CASES_LOADER.getSQL(sqlCaseId, sqlCaseType, SQL_PARSER_TEST_CASES_REGISTRY.get(sqlCaseId).getParameters());
            // TODO remove me after all test cases passed
        } catch (final IllegalStateException ex) {
            return;
        }
        SQLParserTestCase expected = VisitorSQLParserTestCasesRegistryFactory.getInstance().getRegistry().get(sqlCaseId);
        if (expected.isLongSQL() && Boolean.parseBoolean(PROPS.getProperty("long.sql.skip", Boolean.TRUE.toString()))) {
            return;
        }
        String databaseTypeName = "H2".equals(databaseType) ? "MySQL" : databaseType;
        ParseTree parseTree = SQLParserFactory.newInstance(databaseTypeName, sql).execute().getChild(0);
        SQLCaseAssertContext assertContext = new SQLCaseAssertContext(sqlCaseId, sqlCaseType);
        SQLStatement actual = (SQLStatement) new SQLVisitorEngine(databaseTypeName, parseTree).parse();
        if (!expected.isLongSQL()) {
            SQLStatementAssert.assertIs(assertContext, actual, expected);
        }
    }
}

@@ -2259,16 +2259,45 @@
 ashSet;%0A
+import java.util.LinkedList;%0A
 import j
@@ -3647,54 +3647,1110 @@
     
-return SQL_CASES_LOADER.getSQLTestParameters()
+// TODO remove me after all test cases passed%0A        return getSQLTestParameters(SQL_CASES_LOADER.getSQLTestParameters());%0A        // TODO resume me after all test cases passed %0A//        return SQL_CASES_LOADER.getSQLTestParameters();%0A    %7D%0A    %0A    // TODO remove me after all test cases passed%0A    private static Collection%3CObject%5B%5D%3E getSQLTestParameters(final Collection%3CObject%5B%5D%3E sqlTestParameters) %7B%0A        Collection%3CObject%5B%5D%3E result = new LinkedList%3C%3E();%0A        for (Object%5B%5D each : sqlTestParameters) %7B%0A            String sqlCaseId = each%5B0%5D.toString();%0A            String databaseType = each%5B1%5D.toString();%0A            SQLCaseType sqlCaseType = (SQLCaseType) each%5B2%5D;%0A            if (!%22H2%22.equals(databaseType) && !%22MySQL%22.equals(databaseType)) %7B%0A                continue;%0A            %7D%0A            try %7B%0A                SQL_CASES_LOADER.getSQL(sqlCaseId, sqlCaseType, SQL_PARSER_TEST_CASES_REGISTRY.get(sqlCaseId).getParameters());%0A            %7D catch (final IllegalStateException ex) %7B%0A                continue;%0A            %7D%0A            result.add(each);%0A        %7D%0A        return result
 ;%0A  

remove empty test cases for VisitorParameterizedParsingTest (#4198)