DsmDeleteTest.java 8.99 KB
Newer Older
敖文武 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
import clients.DsmFileStationClient;
import exeptions.DsmDeleteException;
import org.junit.*;
import org.junit.rules.TemporaryFolder;
import requests.DsmAuth;
import requests.filestation.DsmRequestParameters;
import responses.Response;
import responses.filestation.delete.DsmDeleteResponse;
import utils.DsmUtils;

import java.io.File;
import java.io.IOException;

public class DsmDeleteTest extends DsmTest{

    private final String ROOT_FOLDER = "/homes/testResource";
    private DsmFileStationClient client;
    private File fileToDownload;

    @Rule
    public TemporaryFolder folder= new TemporaryFolder();

    @Before
    public void initTest() throws IOException {
        super.initTest();
        fileToDownload = createNewFile();

        client = DsmFileStationClient.login(DsmAuth.fromResource("env.properties"));

        client.upload(ROOT_FOLDER, fileToDownload.getAbsolutePath())
                .createParentFolders(true)
                .overwrite(DsmRequestParameters.OverwriteBehaviour.OVERWRITE)
                .call();
    }

    @After
    public void postTest() {
        client.simpleDelete(ROOT_FOLDER).setRecursive(true).call();
    }

    @Test
    public void deleteOneFileAndSuccess() {
        Assert.assertTrue(client.exists(ROOT_FOLDER+"/"+fileToDownload.getName()));

        Response<DsmDeleteResponse> deleteResponse = client.simpleDelete(ROOT_FOLDER+"/"+fileToDownload.getName())
                .call();

        Assert.assertTrue(deleteResponse.isSuccess());
        Assert.assertFalse(client.exists(ROOT_FOLDER+"/"+fileToDownload.getName()));
    }

    @Test
    public void deleteAdvancedOneFileAndSuccess() {

        Assert.assertTrue(client.exists(ROOT_FOLDER+"/"+fileToDownload.getName()));

        Response<DsmDeleteResponse> deleteResponse = client.advancedDelete()
                .addFileToDelete(ROOT_FOLDER+"/"+fileToDownload.getName())
                .start();

        Assert.assertTrue(deleteResponse.isSuccess());
        Assert.assertNotNull(deleteResponse.getData());
        Assert.assertNotNull(deleteResponse.getData().getTaskid());

        Response<DsmDeleteResponse> statusResponse = client.advancedDelete()
                .taskId(deleteResponse.getData().getTaskid())
                .status();

        Assert.assertTrue(statusResponse.isSuccess());
        Assert.assertNotNull(statusResponse.getData());
        Assert.assertNotNull(statusResponse.getData().getPath());
        Assert.assertNotNull(statusResponse.getData().getProcessed_num());
        Assert.assertNotNull(statusResponse.getData().getProcessing_path());
        Assert.assertNotNull(statusResponse.getData().getTotal());
        Assert.assertNotNull(statusResponse.getData().getProgress());
    }

    @Test
    public void deleteFileNotExistAndFail() throws IOException {
        File fileToUpload = createNewFile();

        Response<DsmDeleteResponse> deleteResponse = client.advancedDelete()
                .addFileToDelete(ROOT_FOLDER+"/"+fileToUpload.getName())
                .start();

        Assert.assertTrue(deleteResponse.isSuccess());
        Assert.assertNotNull(deleteResponse.getData());
        Assert.assertNotNull(deleteResponse.getData().getTaskid());

        Response<DsmDeleteResponse> statusResponse = client.advancedDelete()
                .taskId(deleteResponse.getData().getTaskid())
                .status();

        Assert.assertTrue(statusResponse.isSuccess());
        Assert.assertNotNull(statusResponse.getData());
        Assert.assertEquals("",statusResponse.getData().getPath());
        Assert.assertNotNull(statusResponse.getData().getProcessed_num());
        Assert.assertEquals("",statusResponse.getData().getProcessing_path());
        Assert.assertEquals(Integer.valueOf("-1"), statusResponse.getData().getTotal());
        Assert.assertNotNull(statusResponse.getData().getProgress());
    }

    @Test(expected = DsmDeleteException.class)
    public void deleteFileNotSetAndFail() {
        client.advancedDelete()
                .start();
    }

    @Test(expected = DsmDeleteException.class)
    public void getStatusWithoutTakId() {
                client.advancedDelete()
                .status();
    }

    @Test(expected = DsmDeleteException.class)
    public void getStopWithoutTakId() {
        client.advancedDelete()
                .status();
    }

    @Test
    public void getStatusWithDummyTaskId() {
        Response<DsmDeleteResponse> statusResponse = client.advancedDelete()
                .taskId("TaskId")
                .status();

        Assert.assertFalse(statusResponse.isSuccess());
        Assert.assertNull(statusResponse.getData());
        Assert.assertNotNull(statusResponse.getError());
        Assert.assertNotNull(statusResponse.getError().getCode());
        Assert.assertEquals("No such task of the file operation", DsmUtils.manageErrorMessage(Integer.valueOf(statusResponse.getError().getCode())));
    }

    @Test
    public void deleteAdvancedMultipleFilesAndSuccess() throws IOException {
        File fileToDownload2 = createNewFile();
        client.upload(ROOT_FOLDER, fileToDownload2.getAbsolutePath())
                .createParentFolders(true)
                .overwrite(DsmRequestParameters.OverwriteBehaviour.OVERWRITE)
                .call();

        Assert.assertTrue(client.exists(ROOT_FOLDER+"/"+fileToDownload.getName()));
        Assert.assertTrue(client.exists(ROOT_FOLDER+"/"+fileToDownload2.getName()));

        Response<DsmDeleteResponse> deleteResponse = client.advancedDelete()
                .addFileToDelete(ROOT_FOLDER+"/"+fileToDownload.getName())
                .addFileToDelete(ROOT_FOLDER+"/"+fileToDownload2.getName())
                .start();

        Assert.assertTrue(deleteResponse.isSuccess());
        Assert.assertNotNull(deleteResponse.getData());
        Assert.assertNotNull(deleteResponse.getData().getTaskid());

        Response<DsmDeleteResponse> statusResponse = client.advancedDelete()
                .taskId(deleteResponse.getData().getTaskid())
                .status();

        Assert.assertTrue(statusResponse.isSuccess());
        Assert.assertNotNull(statusResponse.getData());
        Assert.assertNotNull(statusResponse.getData().getPath());
        Assert.assertNotNull(statusResponse.getData().getProcessed_num());
        Assert.assertNotNull(statusResponse.getData().getProcessing_path());
        Assert.assertNotNull(statusResponse.getData().getTotal());
        Assert.assertNotNull(statusResponse.getData().getProgress());
    }

    @Test
    public void deleteFolderAndSuccess() throws IOException {
        File fileToUpload = createNewFile();
        File fileToUpload2 = createNewFile();
        File fileToUpload3 = createNewFile();
        client.upload(ROOT_FOLDER+"/folder1", fileToUpload.getAbsolutePath())
                .createParentFolders(true)
                .overwrite(DsmRequestParameters.OverwriteBehaviour.OVERWRITE)
                .call();
        client.upload(ROOT_FOLDER+"/folder1", fileToUpload2.getAbsolutePath())
                .createParentFolders(true)
                .overwrite(DsmRequestParameters.OverwriteBehaviour.OVERWRITE)
                .call();
        client.upload(ROOT_FOLDER+"/folder1", fileToUpload3.getAbsolutePath())
                .createParentFolders(true)
                .overwrite(DsmRequestParameters.OverwriteBehaviour.OVERWRITE)
                .call();

        Assert.assertTrue(client.exists(ROOT_FOLDER+"/folder1/"+fileToUpload.getName()));
        Assert.assertTrue(client.exists(ROOT_FOLDER+"/folder1/"+fileToUpload2.getName()));
        Assert.assertTrue(client.exists(ROOT_FOLDER+"/folder1/"+fileToUpload3.getName()));

        Response<DsmDeleteResponse> deleteResponse = client.advancedDelete()
                .addFileToDelete(ROOT_FOLDER+"/folder1")
                .start();

        Assert.assertTrue(deleteResponse.isSuccess());
        Assert.assertNotNull(deleteResponse.getData());
        Assert.assertNotNull(deleteResponse.getData().getTaskid());

        Response<DsmDeleteResponse> statusResponse = client.advancedDelete()
                .taskId(deleteResponse.getData().getTaskid())
                .status();

        Assert.assertTrue(statusResponse.isSuccess());
        Assert.assertNotNull(statusResponse.getData());
        Assert.assertNotNull(statusResponse.getData().getPath());
        Assert.assertNotNull(statusResponse.getData().getProcessed_num());
        Assert.assertNotNull(statusResponse.getData().getProcessing_path());
        Assert.assertNotNull(statusResponse.getData().getTotal());
        Assert.assertNotNull(statusResponse.getData().getProgress());
    }

    @Test
    public void checkFileExist() {
        Assert.assertTrue(client.exists(ROOT_FOLDER+"/"+fileToDownload.getName()));
    }

    @Test
    public void checkFileNotExist() {
        Assert.assertFalse(client.exists(ROOT_FOLDER+"/not"+fileToDownload.getName()));
    }

    private File createNewFile() throws IOException {
        String fileSuccess = "dummy-upload-file"+System.currentTimeMillis()+".txt";
        String content = "success content";

        return Utils.makeFile(folder, content, fileSuccess);
    }
}