package com.sonymusic

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

class RetagEcrImageTest extends BaseGlobalVarTest {
    def retagEcrImage

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

    @Test
    void testCallWithMissingAwsRegions() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "retagEcrImage: Missing required parameter: 'awsRegions'",
            {
                retagEcrImage.call(
                    ecrAccountId: '123456789012',
                    imageName: 'test-image',
                    imageTag: 'old-tag',
                    newTag: 'new-tag'
                )
            }
        )
    }

    @Test
    void testCallWithMissingEcrAccountId() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "retagEcrImage: Missing required parameter: 'ecrAccountId'",
            {
                retagEcrImage.call(
                    awsRegions: ['us-east-1'],
                    imageName: 'test-image',
                    imageTag: 'old-tag',
                    newTag: 'new-tag'
                )
            }
        )
    }

    @Test
    void testCallWithMissingImageName() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "retagEcrImage: Missing required parameter: 'imageName'",
            {
                retagEcrImage.call(
                    awsRegions: ['us-east-1'],
                    ecrAccountId: '123456789012',
                    imageTag: 'old-tag',
                    newTag: 'new-tag'
                )
            }
        )
    }

    @Test
    void testCallWithMissingImageTag() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "retagEcrImage: Missing required parameter: 'imageTag'",
            {
                retagEcrImage.call(
                    awsRegions: ['us-east-1'],
                    ecrAccountId: '123456789012',
                    imageName: 'test-image',
                    newTag: 'new-tag'
                )
            }
        )
    }

    @Test
    void testCallWithMissingNewTag() {
        assertThrowsWithMessage(
            IllegalArgumentException.class,
            "retagEcrImage: Missing required parameter: 'newTag'",
            {
                retagEcrImage.call(
                    awsRegions: ['us-east-1'],
                    ecrAccountId: '123456789012',
                    imageName: 'test-image',
                    imageTag: 'old-tag'
                )
            }
        )
    }

    @Test
    void testCallSuccess() {
        retagEcrImage(
            awsRegions: ['us-east-1'],
            ecrAccountId: '123456789012',
            imageName: 'test-image',
            imageTag: 'old-tag',
            newTag: 'new-tag'
        )

        assertMethodCalledTimes('sh', 1)
        assertMethodCalledOnceWith('withEnv', [
            'AWS_DEFAULT_REGION=us-east-1',
            'ECR_ACCOUNT_ID=123456789012',
            'IMAGE_NAME=test-image',
            'IMAGE_TAG=old-tag',
            'NEW_TAG=new-tag'
        ])
    }
}
