package com.sonymusic

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

class DatadogSoftwareCatalogValidateTest extends BaseGlobalVarTest {
    def datadogSoftwareCatalogValidate

    @BeforeEach
    void setUp() {
        super.setUp()
        datadogSoftwareCatalogValidate = loadScript('vars/datadogSoftwareCatalogValidate.groovy')
        helper.registerAllowedMethod('sh', [String])
        helper.registerAllowedMethod('withAWS', [Map, Closure])
        helper.registerAllowedMethod('withEcr', [Map, Closure])
        helper.registerAllowedMethod('withSecrets', [Map, Closure])
        helper.registerAllowedMethod('readYaml', [Map], {
            return response
        })
        binding.getVariable('env')['BUILD_TAG'] = 'test-build-tag'
    }

    @Test
    void testCallWithDefaults() {
        datadogSoftwareCatalogValidate()

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

        assertMethodCalledOnceWith('sh', [
            ' -v "./:/var/app/repository" ',
            ' 086679231553.dkr.ecr.us-east-1.amazonaws.com/datadog-tools:latest ',
            ' software-catalog validate-entity ',
            ' --repository-path /var/app/repository ',
            ' --entity-path ""'
        ])

        assertMethodCalledOnceWith('withAWS', [
            roleAccount: '086679231553',
            role: 'shared-datadog-software-catalog-role',
            roleSessionName: 'test-build-tag',
            useNode: true
        ])

        assertMethodCalledOnceWith('withEnv', ['AWS_PROFILE='])
    }

    @Test
    void testCallWithOverrides() {
        datadogSoftwareCatalogValidate(
            repositoryPath: './repo',
            servicePath: 'some/subdir',
            datadogToolsImage: '123456789012.dkr.ecr.us-east-2.amazonaws.com/datadog-tools:custom',
        )

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

        assertMethodCalledOnceWith('sh', [
            ' -v "./repo:/var/app/repository" ',
            ' 123456789012.dkr.ecr.us-east-2.amazonaws.com/datadog-tools:custom ',
            ' software-catalog validate-entity ',
            ' --repository-path /var/app/repository ',
            ' --entity-path \"some/subdir\"'
        ])

        assertMethodCalledOnceWith('withAWS', [
            roleAccount: '086679231553',
            role: 'shared-datadog-software-catalog-role',
            roleSessionName: 'test-build-tag',
            useNode: true
        ])

        assertMethodCalledOnceWith('withEnv', ['AWS_PROFILE='])
    }

    @Test
    void testCallWithServiceDefinitionOverride() {
        datadogSoftwareCatalogValidate(
            serviceDefinitionFilePath: 'path/to/service-definition.yaml'
        )

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

        assertMethodCalledOnceWith('sh', [
            ' -v "./:/var/app/repository" ',
            ' 086679231553.dkr.ecr.us-east-1.amazonaws.com/datadog-tools:latest ',
            ' software-catalog validate-entity ',
            ' --repository-path /var/app/repository ',
            ' --entity-path ""',
            " --entity-definition-file-path 'path/to/service-definition.yaml'"
        ])

        assertMethodCalledOnceWith('withAWS', [
            roleAccount: '086679231553',
            role: 'shared-datadog-software-catalog-role',
            roleSessionName: 'test-build-tag',
            useNode: true
        ])

        assertMethodCalledOnceWith('withEnv', ['AWS_PROFILE='])
    }

    @Test
    void testCallWithLongBuildTag() {
        // Simulate a long build tag to test the truncation logic
        binding.getVariable('env')['BUILD_TAG'] = 'this-is-a-very-long-build-tag-that-exceeds-the-64-character-limit-for-role-session-names'

        datadogSoftwareCatalogValidate()

        assertMethodCalledOnceWith('withAWS', [
            roleAccount: '086679231553',
            role: 'shared-datadog-software-catalog-role',
            roleSessionName: 'd-tag-that-exceeds-the-64-character-limit-for-role-session-names',
            useNode: true
        ])
    }
}
