package org.dbflute.intro.app.web.alter;

import org.dbflute.intro.unit.UnitIntroTestCase;
import org.lastaflute.web.validation.exception.ValidationErrorException;

/**
 * @author cabos
 */
public class AlterActionTest extends UnitIntroTestCase {

    // ===================================================================================
    //                                                                               Test
    //                                                                              ======
    // -----------------------------------------------------
    //                                              Validate
    //                                              --------
    public void test_create_validateInvalidPrefix() {
        // ## Arrange ##
        AlterAction alterAction = new AlterAction();
        inject(alterAction);

        AlterCreateBody body = new AlterCreateBody();
        body.alterFileName = "alter_schema_sample_business.sql";

        // ## Act, Assert ##
        assertException(ValidationErrorException.class, () -> {
            alterAction.create(TEST_CLIENT_PROJECT, body);
        });
    }

    public void test_create_validateNotSQLFile() {
        // ## Arrange ##
        AlterAction alterAction = new AlterAction();
        inject(alterAction);

        AlterCreateBody body = new AlterCreateBody();
        body.alterFileName = "alter-schema_sample_business.txt";

        // ## Act, Assert ##
        assertException(ValidationErrorException.class, () -> {
            alterAction.create(TEST_CLIENT_PROJECT, body);
        });
    }

    public void test_create_validateSameFileName() {
        // ## Arrange ##
        AlterAction alterAction = new AlterAction();
        inject(alterAction);

        AlterCreateBody body = new AlterCreateBody();
        body.alterFileName = "alter-schema_sample.sql"; // see test/resource/playsql/mygrate/alter/alter-schema_sample.sql

        // ## Act, Assert ##
        assertException(ValidationErrorException.class, () -> {
            alterAction.create(TEST_CLIENT_PROJECT, body);
        });
    }

    public void test_create_validateInvalidChar() {
        // ## Arrange ##
        AlterAction alterAction = new AlterAction();
        inject(alterAction);

        AlterCreateBody body = new AlterCreateBody();
        body.alterFileName = "alter-schema_/\\<>*?\"|:;\0 .sql";

        // ## Act, Assert ##
        assertException(ValidationErrorException.class, () -> {
            alterAction.create(TEST_CLIENT_PROJECT, body);
        });
    }

    public void test_create_validateEmptyBusiness() {
        // ## Arrange ##
        AlterAction alterAction = new AlterAction();
        inject(alterAction);

        AlterCreateBody body = new AlterCreateBody();
        body.alterFileName = "alter-schema_.sql";

        // ## Act, Assert ##
        assertException(ValidationErrorException.class, () -> {
            alterAction.create(TEST_CLIENT_PROJECT, body);
        });
    }

}

@@ -35,16 +35,106 @@
 alter;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A%0Aimport org.apache.commons.io.FileUtils;%0A
 import o
@@ -506,285 +506,1846 @@
     
-     Test%0A    //                                                                              ======%0A    // -----------------------------------------------------%0A    //                                              Validate%0A    //                                              --
+Definition%0A    //                                                                          ==========%0A    private static final String ALTER_DIR = TEST_CLIENT_PATH + PLAYSQL_DIR_PATH + %22/migration/alter%22;%0A%0A    // ===================================================================================%0A    //                                                                          Initialize%0A    //                                                                          ==========%0A    @Override%0A    public void setUp() throws Exception %7B%0A        super.setUp();%0A        preparePlaysqlFiles();%0A    %7D%0A%0A    // ===================================================================================%0A    //                                                                               Test%0A    //                                                                              ======%0A    // -----------------------------------------------------%0A    //                                               Prepare%0A    //                                               -------%0A    public void test_prepare_existsAlterDir() %7B%0A        // ## Arrange ##%0A        AlterAction alterAction = new AlterAction();%0A        inject(alterAction);%0A%0A        // ## Act ##%0A        alterAction.prepare(TEST_CLIENT_PROJECT);%0A%0A        // ## Assert ##%0A        // do nothing%0A    %7D%0A%0A    public void test_prepare_notExistsAlterDir() throws IOException %7B%0A        // ## Arrange ##%0A        AlterAction alterAction = new AlterAction();%0A        inject(alterAction);%0A%0A        removeAlterDir();%0A%0A        // ## Act ##%0A        alterAction.prepare(TEST_CLIENT_PROJECT);%0A%0A        // ## Assert ##%0A        // do nothing%0A    %7D%0A%0A    // -----------------------------------------------------%0A    //                                                create%0A    //                                                
 ----
@@ -4649,10 +4649,417 @@
 %0A    %7D%0A%0A
+    // ===================================================================================%0A    //                                                                       Client Helper%0A    //                                                                       =============%0A    private void removeAlterDir() throws IOException %7B%0A        FileUtils.deleteDirectory(new File(getProjectDir(), ALTER_DIR));%0A    %7D%0A
 %7D%0A

add prepare test