package com.sonymusic

import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class AirflowDeployTest extends BaseGlobalVarTest {
    def airflowDeploy

    @BeforeEach
    void setUp() {
        super.setUp()
        airflowDeploy = loadScript('vars/airflowDeploy.groovy')
        helper.registerAllowedMethod('sh', [String])
        helper.registerAllowedMethod('withAWS', [Map, Closure])
        helper.registerAllowedMethod('withEcr', [Map, Closure])
    }

    @Test
    void testCallWithMissingBucket() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "airflowDeploy: Missing required parameter: 'bucket'",
            {
                airflowDeploy.call(
                    environment: 'dev',
                    gitCommit: 'abcdef',
                    kmsKeyId: 'some-uuid',
                    serviceName: 'test-service'
                )
            }
        )
    }

    @Test
    void testCallWithMissingEnvironment() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "airflowDeploy: Missing required parameter: 'environment'",
            {
                airflowDeploy.call(
                    bucket: 'test-bucket',
                    gitCommit: 'abcdef',
                    kmsKeyId: 'some-uuid',
                    serviceName: 'test-service'
                )
            }
        )
    }

    @Test
    void testCallWithMissingGitCommit() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "airflowDeploy: Missing required parameter: 'gitCommit'",
            {
                airflowDeploy.call(
                    bucket: 'test-bucket',
                    environment: 'dev',
                    kmsKeyId: 'some-uuid',
                    serviceName: 'test-service'
                )
            }
        )
    }

    @Test
    void testCallWithMissingKmsKeyId() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "airflowDeploy: Missing required parameter: 'kmsKeyId'",
            {
                airflowDeploy.call(
                    bucket: 'test-bucket',
                    environment: 'dev',
                    gitCommit: 'abcdef',
                    serviceName: 'test-service'
                )
            }
        )
    }

    @Test
    void testCallWithMissingServiceName() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "airflowDeploy: Missing required parameter: 'serviceName'",
            {
                airflowDeploy.call(
                    bucket: 'test-bucket',
                    environment: 'dev',
                    gitCommit: 'abcdef',
                    kmsKeyId: 'some-uuid',
                )
            }
        )
    }

    @Test
    void testDeployWithDefaults() {
        airflowDeploy(
            bucket: 'test-bucket',
            environment: 'dev',
            gitCommit: 'abcdef',
            kmsKeyId: 'some-uuid',
            serviceName: 'test-service'
        )

        assertMethodCalledOnceWith('withEcr', [
            registries: [[accountId: '086679231553', region   : 'us-east-1']]
        ])

        assertMethodCalledOnceWith('sh', [
            " -v \"./dags:/var/app/dags\" ",
            " -v \"./requirements.txt:/var/app/requirements.txt\" ",
            " 086679231553.dkr.ecr.us-east-1.amazonaws.com/airflow-tools:latest ",
            " deploy ",
            " --bucket test-bucket ",
            " --environment dev ",
            " --git-commit abcdef ",
            " --kms-key-id some-uuid ",
            " --service-name test-service ",
            " --bucket-prefix \"\" ",
            " --no-rollback"
        ])

        assertMethodNotCalled('withAWS')
    }

    @Test
    void testDeployWithOverrides() {
        airflowDeploy(
            bucket: 'test-bucket',
            environment: 'dev',
            gitCommit: 'abcdef',
            kmsKeyId: 'some-uuid',
            serviceName: 'test-service',
            airflowToolsImage: '123456789012.dkr.ecr.us-east-2.amazonaws.com/airflow-tools:custom',
            awsDeploymentRoleName: 'custom-role',
            awsDeploymentTargetAccountId: '123456789012',
            bucketPrefix: 'custom-prefix',
            dagsFolder: './custom-dags',
            requirementsFile: './custom-requirements.txt',
        )

        assertMethodCalledOnceWith('withEcr', [
            registries: [[accountId: '123456789012', region: 'us-east-2']]
        ])

        assertMethodCalledOnceWith('sh', [
            " -v \"./custom-dags:/var/app/dags\" ",
            " -v \"./custom-requirements.txt:/var/app/requirements.txt\" ",
            " 123456789012.dkr.ecr.us-east-2.amazonaws.com/airflow-tools:custom ",
            " deploy ",
            " --bucket test-bucket ",
            " --environment dev ",
            " --git-commit abcdef ",
            " --kms-key-id some-uuid ",
            " --service-name test-service ",
            " --bucket-prefix \"custom-prefix\" ",
            " --no-rollback"
        ])

        assertMethodCalledOnceWith('withAWS', [
            role: 'custom-role',
            roleAccount: '123456789012'
        ])
    }

    @Test
    void testRollback() {
        airflowDeploy(
            bucket: 'test-bucket',
            environment: 'dev',
            gitCommit: 'abcdef',
            kmsKeyId: 'some-uuid',
            serviceName: 'test-service',
            rollback: true
        )

        assertMethodCalledOnceWith('sh', [
            " -v \"./dags:/var/app/dags\" ",
            " -v \"./requirements.txt:/var/app/requirements.txt\" ",
            " 086679231553.dkr.ecr.us-east-1.amazonaws.com/airflow-tools:latest ",
            " deploy ",
            " --bucket test-bucket ",
            " --environment dev ",
            " --git-commit abcdef ",
            " --kms-key-id some-uuid ",
            " --service-name test-service ",
            " --bucket-prefix \"\" ",
            " --rollback"
        ])
    }
}
