package org.dasein.cloud.joyent.storage;

import org.dasein.cloud.CloudProvider;
import org.dasein.cloud.examples.ProviderLoader;
import org.dasein.cloud.storage.Blob;
import org.dasein.cloud.storage.BlobStoreSupport;
import static org.junit.Assert.*;

import org.dasein.cloud.storage.FileTransfer;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.util.Date;

/**
 * @author ilya.drabenia
 */
public class MantaStorageTest {
    private static final String SRC_FILE_PATH = "src/test/resources/data/Master-Yoda.jpg";
    private static final String MANTA_FILE_PATH = "/altoros2/stor/1/Master-Yoda.jpg";

    private static BlobStoreSupport storage;

    @BeforeClass
    public static void initializeMantaStore() throws Exception {
        CloudProvider provider = new ProviderLoader().getConfiguredProvider();
        storage = provider.getStorageServices().getOnlineStorageSupport();
    }

    private Blob createFile() throws Exception {
        File sourceFile = new File(SRC_FILE_PATH);
        return storage.upload(sourceFile, null, MANTA_FILE_PATH);
    }

    private void assertThatFileSuccessfullyDownloaded(FileTransfer fileTransfer) {
        Throwable error = fileTransfer.getTransferError();
        assertTrue(error != null ? error.toString() : "", error == null);
    }

    private void waitUntilFileTransferred(FileTransfer fileTransfer) throws InterruptedException {
        while (!fileTransfer.isComplete()) {
            synchronized (fileTransfer) {
                fileTransfer.wait(10000L);
            }
        }
    }

    @Test
    public void testFileUpload() throws Exception {
        Blob result = createFile();

        assertTrue(result != null);
    }

    @Test
    public void testFileDownload() throws Exception {
        createFile();

        File toFile = File.createTempFile(String.valueOf(new Date().getTime()), "");
        FileTransfer fileTransfer = storage.download(null, MANTA_FILE_PATH, toFile);

        waitUntilFileTransferred(fileTransfer);
        assertThatFileSuccessfullyDownloaded(fileTransfer);
        assertEquals(toFile.length(), 892907);
    }

    @Test
    public void testFileRemove() throws Exception {
        createFile();

        storage.removeObject(null, MANTA_FILE_PATH);
    }

    @Test
    public void testFileRename() throws Exception {
        createFile();

        storage.renameObject(null, MANTA_FILE_PATH, "/altoros2/stor/1/Master-Yoda1.jpg");
    }

    @Test
    public void testIsPublicForPublicFile() throws Exception {
        boolean result = storage.isPublic(null, "/altoros2/public/1/Master-Yoda.jpg");

        assertTrue("Public files must be stored in /{login}/public folder", result);
    }

    @Test
    public void testIsPublicForNotPublicFiles() throws Exception {
        boolean result = storage.isPublic(null, "/altoros2/stor/1/Master-Yoda.jpg");

        assertFalse(result);
    }

    @Test
    public void testGetMaxObjectSize() throws Exception {
        double size = storage.getMaxObjectSize().doubleValue();

        assertTrue("Files in Manta has unlimited size", size > 1000000);
    }

}

@@ -724,17 +724,14 @@
 oid 
-initializ
+prepar
 eMan
@@ -937,22 +937,26 @@
 te Blob 
-create
+uploadTest
 File() t
@@ -1235,24 +1235,25 @@
 ferError();%0A
+%0A
         asse
@@ -1258,65 +1258,78 @@
 sert
-True(error != null ? error.toString() : %22%22, error == null
+Null(%22After file download Task object must not contains errors%22, error
 );%0A 
@@ -1364,25 +1364,24 @@
 ntilFile
-Transferr
+Download
 ed(FileT
@@ -1677,22 +1677,26 @@
 esult = 
-create
+uploadTest
 File();%0A
@@ -1714,27 +1714,22 @@
 sert
-True(result != null
+NotNull(result
 );%0A 
@@ -1802,38 +1802,42 @@
 n %7B%0A        
-create
+uploadTest
 File();%0A
 %0A        Fil
@@ -1816,33 +1816,32 @@
 loadTestFile();%0A
-%0A
         File toF
@@ -1905,24 +1905,25 @@
 me()), %22%22);%0A
+%0A
         File
@@ -1995,17 +1995,16 @@
 oFile);%0A
-%0A
         
@@ -2020,17 +2020,16 @@
 File
-Transferr
+Download
 ed(f
@@ -2034,32 +2034,33 @@
 (fileTransfer);%0A
+%0A
         assertTh
@@ -2219,38 +2219,42 @@
 ption %7B%0A        
-create
+uploadTest
 File();%0A%0A       
@@ -2376,22 +2376,26 @@
         
-create
+uploadTest
 File();%0A
@@ -2476,16 +2476,17 @@
 ter-Yoda
+-
 1.jpg%22);
@@ -2578,84 +2578,145 @@
     
-boolean result = storage.isPublic(null, %22/altoros2/public/1/Master-Yoda.jpg%22
+final String PUBLIC_FILE_PATH = %22/altoros2/public/1/Master-Yoda.jpg%22;%0A%0A        boolean isPublic = storage.isPublic(null, PUBLIC_FILE_PATH
 );%0A%0A
@@ -2791,22 +2791,24 @@
 older%22, 
-result
+isPublic
 );%0A    %7D
@@ -2898,82 +2898,145 @@
     
-boolean result = storage.isPublic(null, %22/altoros2/stor/1/Master-Yoda.jpg%22
+final String PRIVATE_FILE_PATH = %22/altoros2/stor/1/Master-Yoda.jpg%22;%0A%0A        boolean isPublic = storage.isPublic(null, PRIVATE_FILE_PATH
 );%0A%0A
@@ -3055,22 +3055,24 @@
 rtFalse(
-result
+isPublic
 );%0A    %7D

Implement some methods