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

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.io.File;
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 {
        OssStore store = new OssStore(new OssConfig(config_cloud, server_cloud), System.getProperty("java.io.tmpdir"));
        String content = RandomStringUtils.random(128, true, true);
        ByteArray data = ByteArray.fromString(content);
        store.write("/test/randome", new ByteArrayInputStream(data.value()), ByteArray.md5(data.value()).toHexString());
        Assert.assertTrue(store.exist("test/randome"));
        Assert.assertEquals(content, ByteArray.fromInputStream(store.read("test/randome")).toString());

        data = ByteArray.fromFile(new File("/Users/luoshi/Downloads/test.pdf"));
        store.write("/test/test.pdf", new ByteArrayInputStream(data.value()),
                ByteArray.md5(data.value()).toHexString());
        data = ByteArray.fromFile(new File("/Users/luoshi/Downloads/test.json"));
        store.write("/test/test.json", new ByteArrayInputStream(data.value()),
                ByteArray.md5(data.value()).toHexString());
        data = ByteArray.fromFile(new File("/Users/luoshi/Downloads/test.zip"));
        store.write("/test/测试.zip", new ByteArrayInputStream(data.value()), ByteArray.md5(data.value()).toHexString());
        Assert.assertTrue(store.exist("test/测试.zip"));
        String url = store.getServerUrl("/test/测试.zip", Duration.ofMinutes(5));
        //System.out.println(url);
        Assert.assertEquals(data.toHexString(), ByteArray.fromUrl(url).toHexString());

        store.close();
    }

}
