package liquibase.change;

import liquibase.database.Database;
import liquibase.database.FirebirdDatabase;
import liquibase.database.SQLiteDatabase;
import liquibase.statement.AddDefaultValueStatement;
import liquibase.statement.SqlStatement;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class AddLookupTableChangeTest extends AbstractChangeTest {

    @Override
    public void getRefactoringName() throws Exception {
        assertEquals("Add Lookup Table", new AddLookupTableChange().getChangeMetaData().getDescription());
    }

    @Override
    public void generateStatement() throws Exception {
        AddLookupTableChange change = new AddLookupTableChange();
        change.setExistingTableName("OLD_TABLE_NAME");
        change.setExistingColumnName("OLD_COLUMN_NAME");
        change.setExistingTableSchemaName("OLD_SCHEMA");
        change.setConstraintName("FK_NAME");
        change.setNewColumnDataType("TYPE(255)");
        change.setNewTableName("NEW_TABLE");
        change.setNewColumnName("NEW_COL");
        change.setNewTableSchemaName("NEW_SCHEM");

        testChangeOnAll(change, new GenerateAllValidator() {
            public void validate(SqlStatement[] statements, Database database) {
                assertEquals(4, statements.length);
                AddDefaultValueStatement statement = (AddDefaultValueStatement) statements[0];


                assertEquals("TABLE_NAME", statement.getTableName());
                assertEquals("COLUMN_NAME", statement.getColumnName());
                assertTrue(statement.getDefaultValue() instanceof Boolean);
                assertEquals(Boolean.TRUE, statement.getDefaultValue());
            }
        });
    }

    @Override
    public void getConfirmationMessage() throws Exception {
        AddLookupTableChange change = new AddLookupTableChange();
        change.setExistingTableName("OLD_TABLE_NAME");
        change.setExistingColumnName("OLD_COLUMN_NAME");

        assertEquals("Lookup table added for OLD_TABLE_NAME.OLD_COLUMN_NAME",change.getConfirmationMessage());
    }

    @Override
    protected boolean changeIsUnsupported(Database database) {
        return database instanceof FirebirdDatabase
                || database instanceof SQLiteDatabase;
    }
}

@@ -1282,32 +1282,584 @@
 se database) %7B%0D%0A
+                %0D%0A                // TODO this test should reorganized %0D%0A                // first statement is RawSql to create new (Lookup) Table%0D%0A                // second - Set Not Null Constraint for the Field in the created Lookup table%0D%0A                // third - primary key constraint of the Lookup Table%0D%0A            %09// forth - add foreign key reference from old (base) table to the lookup table%0D%0A            %09// Take into account that different databases could produce different number of the statements.%0D%0A            %09%0D%0A                %0D%0A//
                 
@@ -1891,24 +1891,26 @@
 s.length);%0D%0A
+//
             
@@ -1993,24 +1993,26 @@
 ts%5B0%5D;%0D%0A%0D%0A%0D%0A
+//
             
@@ -2062,32 +2062,34 @@
 tTableName());%0D%0A
+//
                 
@@ -2141,24 +2141,26 @@
 mnName());%0D%0A
+//
             
@@ -2220,24 +2220,26 @@
  Boolean);%0D%0A
+//
             

add lookup table change test should be repaired