package org.mvnsearch.ali.oss.spring.services.impl;

import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.*;
import junit.framework.TestCase;
import org.apache.commons.lang3.StringUtils;
import org.mvnsearch.ali.oss.spring.services.OSSUri;
import org.mvnsearch.ali.oss.spring.services.OssObjectDocument;
import org.springframework.mail.javamail.ConfigurableMimeFileTypeMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * aliyun oss service implementation test case
 *
 * @author linux_china
 */
public class AliyunOssServiceImplTest extends TestCase {
    /**
     * aliyun oss service
     */
    private AliyunOssServiceImpl aliyunOssService;
    /**
     * bucket name
     */
    private String bucketName = "faxianla_temp";
    private static ConfigurableMimeFileTypeMap mimeTypes = new ConfigurableMimeFileTypeMap();


    /**
     * setup logic, please create .aliyunoss.cfg in user home directory and fill access info
     *
     * @throws Exception exception
     */
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        aliyunOssService = new AliyunOssServiceImpl();
        ConfigServiceImpl configService = new ConfigServiceImpl();
        configService.init();
        aliyunOssService.setConfigService(configService);
        aliyunOssService.refreshToken();
    }

    /**
     * test to list buckets
     *
     * @throws Exception exception
     */
    public void testListBuckets() throws Exception {
        List<Bucket> buckets = aliyunOssService.getBuckets();
        for (Bucket bucket : buckets) {
            System.out.println(bucket.getName());
        }
    }

    /**
     * test to list bucket
     *
     * @throws Exception exception
     */
    public void testList() throws Exception {
        SearchManagerImpl searchManager = new SearchManagerImpl();
        long start = System.currentTimeMillis();
        ObjectListing objectListing = aliyunOssService.list(bucketName, "", 1000);
       long   end = System.currentTimeMillis();
        System.out.println(end - start);
        List<OssObjectDocument> documents = new ArrayList<OssObjectDocument>();
        for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            objectSummary.setBucketName(bucketName);
            documents.add(OssObjectDocument.constructFromObjectSummary(objectSummary));
        }
         end = System.currentTimeMillis();
        System.out.println(end - start);
        searchManager.index(documents);
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }

    /**
     * test to get object metadata
     *
     * @throws Exception exception
     */
    public void testGetObjectMetadata() throws Exception {
        ObjectMetadata objectMetadata = aliyunOssService.getObjectMetadata(new OSSUri(bucketName, "demo.jpg"));
        printMetaData(objectMetadata);
    }

    /**
     * print meta data information
     *
     * @param objectMetadata object metadata
     */
    private void printMetaData(ObjectMetadata objectMetadata) {
        System.out.println("---raw metadata-----");
        for (Map.Entry<String, Object> entry : objectMetadata.getRawMetadata().entrySet()) {
            System.out.println(StringUtils.rightPad(entry.getKey(), 20, ' ') + " : " + entry.getValue());
        }
        System.out.println("---user metadata-----");
        for (Map.Entry<String, String> entry : objectMetadata.getUserMetadata().entrySet()) {
            System.out.println(StringUtils.rightPad(entry.getKey(), 20, ' ') + " : " + entry.getValue());
        }
    }

    /**
     * test to put object
     *
     * @throws Exception exception
     */
    public void testPutObject() throws Exception {
        String destFile = "demo.jpg";
        String sourceFile = "/Users/linux_china/demo.jpg";
        aliyunOssService.put(sourceFile, new OSSUri(bucketName, destFile));
        ObjectMetadata objectMetadata = aliyunOssService.getObjectMetadata(new OSSUri(bucketName, destFile));
        printMetaData(objectMetadata);
    }
}

@@ -271,72 +271,8 @@
 ri;%0A
-import org.mvnsearch.ali.oss.spring.services.OssObjectDocument;%0A
 impo
@@ -1784,75 +1784,8 @@
 n %7B%0A
-        SearchManagerImpl searchManager = new SearchManagerImpl();%0A
     
@@ -1923,15 +1923,14 @@
     
+ 
 long 
-  
 end 
@@ -2004,88 +2004,8 @@
 t);%0A
-        List%3COssObjectDocument%3E documents = new ArrayList%3COssObjectDocument%3E();%0A
     
@@ -2149,221 +2149,9 @@
     
-    documents.add(OssObjectDocument.constructFromObjectSummary(objectSummary));%0A        %7D%0A         end = System.currentTimeMillis();%0A        System.out.println(end - start);%0A        searchManager.index(documents);
+%7D
 %0A   

remove search logic