package com.qmth.boot.test.core.fss;

import com.qmth.boot.core.fss.config.FileStoreProperty;
import com.qmth.boot.core.fss.store.impl.OssStore;
import com.qmth.boot.core.fss.utils.OssConfig;
import com.qmth.boot.tools.models.ByteArray;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Assert;

import java.io.ByteArrayInputStream;
import java.time.Duration;

public class OssStoreTest {

    private String server_cloud = "https://static-test.qmth.com.cn";

    private String server_local = "http://oss-file.qmth.com.cn/test";

    private String config_cloud = "oss://LTAI4FnJ2pgV6aGceYcCkeEi:ktrMEVE7PfoxRPeJUPDFeygOIH4aU7@qmth-test.oss-cn-shenzhen.aliyuncs.com";

    private String config_local = "oss://key:secret@test.oss-api.qmth.com.cn";

    //@Test
    public void test() throws Exception {
        FileStoreProperty p = new FileStoreProperty();
        p.setConfig(config_local);
        p.setServer(server_local);
        OssStore store = new OssStore(new OssConfig(p));
        String content = RandomStringUtils.random(128, true, true);
        ByteArray data = ByteArray.fromString(content);
        store.write("/test/1.txt", new ByteArrayInputStream(data.value()), ByteArray.md5(data.value()).toHexString());
        Assert.assertTrue(store.exist("test/1.txt"));
        Assert.assertEquals(content, ByteArray.fromInputStream(store.read("test/1.txt")).toString());
        String url = store.getServerUrl("/test/1.txt", Duration.ofMinutes(5));
        Assert.assertEquals(content, ByteArray.fromUrl(url).toString());
        store.close();
    }

}
