package com.filestack.util;

import com.filestack.model.FilestackClient;
import com.filestack.model.FileLink;
import okhttp3.OkHttpClient;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import retrofit2.Retrofit;
import retrofit2.mock.BehaviorDelegate;
import retrofit2.mock.MockRetrofit;
import retrofit2.mock.NetworkBehavior;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

public class TestUpload {

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    private static MockUploadService createMockUploadService(NetworkBehavior behavior) {

        Retrofit retrofit = new Retrofit.Builder()
                .client(new OkHttpClient())
                .baseUrl(FilestackService.Upload.URL)
                .build();

        MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit)
                .networkBehavior(behavior)
                .build();

        BehaviorDelegate<FilestackService.Upload> delegate = mockRetrofit.create(FilestackService.Upload.class);
        return new MockUploadService(delegate);
    }

    private static Path createRandomFile(long size) throws IOException{
        Path path = Paths.get("/tmp/" + UUID.randomUUID().toString() + ".txt");
        RandomAccessFile file = new RandomAccessFile(path.toString(), "rw");
        file.writeChars("test content\n");
        file.setLength(size);
        file.close();
        return path;
    }

    @Test
    public void test() throws IOException {
        Path path = createRandomFile(10 * 1024 * 1024);

        NetworkBehavior behavior = NetworkBehavior.create();
        MockUploadService mockUploadService = createMockUploadService(behavior);

        FilestackClient fsClient = new FilestackClient("apiKey");
        UploadOptions options = new UploadOptions.Builder().build();
        Upload upload = new Upload(path.toString(), fsClient, options, mockUploadService, 0);

        behavior.setDelay(0, TimeUnit.SECONDS);

        FileLink fileLink = upload.run();

        Assert.assertTrue(fileLink.getHandle().equals("handle"));

        Files.delete(path);
    }
}

@@ -57,56 +57,261 @@
 File
-stackClient;%0Aimport com.filestack.model.FileLink
+Link;%0Aimport com.filestack.model.FilestackClient;%0Aimport java.io.IOException;%0Aimport java.io.RandomAccessFile;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.UUID;%0Aimport java.util.concurrent.TimeUnit
 ;%0Aim
@@ -597,214 +597,8 @@
 r;%0A%0A
-import java.io.IOException;%0Aimport java.io.RandomAccessFile;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.util.UUID;%0Aimport java.util.concurrent.TimeUnit;%0A%0A
 publ
@@ -1162,16 +1162,32 @@
 legate =
+%0A               
  mockRet
@@ -1356,16 +1356,17 @@
 xception
+ 
 %7B%0A      

Fix linter warnings for TestUpload