import cn.reghao.tnb.file.app.FileApplication;
import cn.reghao.tnb.file.app.db.mapper.StoreConfigMapper;
import cn.reghao.tnb.file.app.model.po.StoreConfig;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Applications;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;

/**
 * @author reghao
 * @date 2024-04-17 07:18:59
 */
@Slf4j
@ActiveProfiles("dev")
@SpringBootTest(classes = FileApplication.class)
public class FileTest {
    @Autowired
    StoreConfigMapper storeConfigMapper;

    @Test
    public void test() {
        String endpoint = System.getenv("ENDPOINT");
        String accessKeyId = System.getenv("ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");
        String bucketName = System.getenv("BUCKET");
        StoreConfig storeConfig = new StoreConfig(1, endpoint, accessKeyId, accessKeySecret);
        storeConfig.setBucketName(bucketName);

        String endpoint1 = System.getenv("CONSOLE_ENDPOINT");
        String accessKeyId1 = System.getenv("CONSOLE_ACCESS_KEY_ID");
        String accessKeySecret1 = System.getenv("CONSOLE_ACCESS_KEY_SECRET");
        StoreConfig storeConfig1 = new StoreConfig(1, endpoint1, accessKeyId1, accessKeySecret1);

        String endpoint2 = "sts.cn-chengdu.aliyuncs.com";
        String accessKeyId2 = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKeySecret2 = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        String bucket = System.getenv("BUCKET");
        String region = System.getenv("REGION");
        String roleArn = System.getenv("ROLE_ARN");
        StoreConfig storeConfig2 = new StoreConfig(1, endpoint2, accessKeyId2, accessKeySecret2);
        storeConfig2.setBucketName(bucket);
        storeConfig2.setRegion(region);
        storeConfig2.setRoleArn(roleArn);

        List<StoreConfig> list = List.of(storeConfig, storeConfig1, storeConfig2);
        storeConfigMapper.saveAll(list);
    }

    @Autowired
    EurekaClient eurekaClient;
    @Test
    public void eurekaClientTest() {
        Applications applications = eurekaClient.getApplications();
        applications.getRegisteredApplications().forEach(application -> {
            String name = application.getName();
            application.getInstances().forEach(instanceInfo -> {
                String instanceId = instanceInfo.getInstanceId();
            });
        });
    }
}
