All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.harness.diff.DiffCommandTest.groovy Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
package liquibase.harness.diff

import liquibase.database.jvm.JdbcConnection
import liquibase.harness.config.DatabaseUnderTest
import liquibase.harness.config.TestConfig
import liquibase.harness.util.rollback.RollbackStrategy
import org.json.JSONObject
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

import static liquibase.harness.diff.DiffCommandTestHelper.*
import static liquibase.harness.util.TestUtils.*
import static liquibase.harness.util.JSONUtils.*
import static liquibase.harness.util.FileUtils.*

/**
 * Warning! This test might be destructive, meaning it may change the state of targetDatabase according to referenceDatabase
 */
class DiffCommandTest extends Specification {
    @Shared
    RollbackStrategy strategy;
    @Shared
    List databases;

    def setupSpec() {
        databases = TestConfig.instance.getFilteredDatabasesUnderTest()
        strategy = chooseRollbackStrategy()
        strategy.prepareForRollback(databases)
    }

    @Unroll
    def "compare referenceDatabase #testInput.referenceDatabase.name #testInput.referenceDatabase.version to targetDatabase #testInput.targetDatabase.name #testInput.targetDatabase.version"() {
        given: "create arguments map for executing command scope, read expected diff from file"
        Map argsMap = new HashMap()
        argsMap.put("url", testInput.targetDatabase.url)
        argsMap.put("username", testInput.targetDatabase.username)
        argsMap.put("password", testInput.targetDatabase.password)
        argsMap.put("referenceUrl", testInput.referenceDatabase.url)
        argsMap.put("referenceUsername", testInput.referenceDatabase.username)
        argsMap.put("referencePassword", testInput.referenceDatabase.password)
        argsMap.put("changelogFile", testInput.pathToChangelogFile)
        argsMap.put("format", "json")
        JSONObject expectedDiff = getJsonFromResource(getExpectedDiffPath(testInput))
        assert testInput.targetDatabase.database.getConnection() instanceof JdbcConnection: "Target database " +
                "${testInput.targetDatabase.name}${testInput.targetDatabase.version} is offline!"
        assert testInput.referenceDatabase.database.getConnection() instanceof JdbcConnection: "Reference database " +
                "${testInput.referenceDatabase.name}${testInput.referenceDatabase.version} is offline!"

        when: "generate diff changelog, apply changes from generated changelog to target database"
        executeCommandScope("diffChangelog", argsMap)
        executeCommandScope("update", argsMap)

        then: "compare expected diff to generated diff"
        def diffToCompare = createDiffToCompare(executeCommandScope("diff", argsMap))
        compareJSONObjects(expectedDiff, diffToCompare)

        /** Rollback might not take effect in the case generated changelog contains ModifyDataTypeChange
         * or DropDefaultValueChange or others that are not supported by default rollback
         */
        cleanup: "try to rollback changes out from target database, delete generated changelog file"
        tryToRollbackDiff(strategy, argsMap)
        deleteFile(testInput.pathToChangelogFile)

        where:
        testInput << buildTestInput()
    }

    def cleanupSpec() {
        strategy.cleanupDatabase(databases)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy