/*
 * Copyright 2026 RisingWave Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.risingwave.connector.catalog;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;

public class GlueCredentialProviderTest {
    @Test
    public void testStaticCredentialsWithoutAssumeRole() {
        Map<String, String> config = new HashMap<>();
        config.put("glue.access-key-id", "access-key");
        config.put("glue.secret-access-key", "secret-key");

        try (GlueCredentialProvider provider = GlueCredentialProvider.create(config)) {
            assertThat(provider.resolveCredentials())
                    .isEqualTo(AwsBasicCredentials.create("access-key", "secret-key"));
        }
    }

    @Test
    public void testAssumeRoleUsesRefreshableProvider() {
        Map<String, String> config = new HashMap<>();
        config.put("glue.access-key-id", "access-key");
        config.put("glue.secret-access-key", "secret-key");
        config.put("glue.iam-role-arn", "arn:aws:iam::123456789012:role/risingwave-glue");
        config.put("glue.region", "us-east-1");

        try (GlueCredentialProvider provider = GlueCredentialProvider.create(config)) {
            assertThat(provider.credentialsProviderForTest())
                    .isInstanceOf(StsAssumeRoleCredentialsProvider.class);
        }
    }

    @Test
    public void testDefaultCredentialChainDoesNotUseSharedSingleton() {
        Map<String, String> config = new HashMap<>();
        config.put("glue.use-default-credential-chain", "true");

        try (GlueCredentialProvider first = GlueCredentialProvider.create(config);
                GlueCredentialProvider second = GlueCredentialProvider.create(config)) {
            assertThat(first.credentialsProviderForTest())
                    .isInstanceOf(DefaultCredentialsProvider.class)
                    .isNotSameAs(second.credentialsProviderForTest());
        }
    }
}
