package com.filestack;

import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.UUID;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Test;

public class TestBasicFunctions {
  private static final String API_KEY = System.getenv("API_KEY");
  private static final String POLICY = System.getenv("POLICY");
  private static final String SIGNATURE = System.getenv("SIGNATURE");
  private static final Security SECURITY = Security.fromExisting(POLICY, SIGNATURE);

  private static ArrayList<String> handles = new ArrayList<>();
  private static ArrayList<File> files = new ArrayList<>();

  private static File createRandomFile(String uuid) throws IOException {
    return createRandomFile(uuid, null);
  }

  private static File createRandomFile(String uuid, Long size) throws IOException {
    File file  = new File("/tmp/" + uuid + ".txt");
    RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
    randomAccessFile.writeChars(uuid);
    if (size != null) {
      randomAccessFile.setLength(size);
    }
    randomAccessFile.close();
    return file;
  }

  @Test
  public void testUpload() throws Exception {
    FilestackClient client = new FilestackClient(API_KEY, SECURITY);

    String uuid = UUID.randomUUID().toString();
    File file = createRandomFile(uuid, 15L * 1024 * 1024);
    files.add(file);

    FileLink fileLink = client.upload(file.getPath(), "text/plain");
    String handle = fileLink.getHandle();
    handles.add(handle);

    Assert.assertNotNull(handle);
  }

  @Test
  public void testGetContent() throws Exception {
    FilestackClient client = new FilestackClient(API_KEY, SECURITY);

    String uuid = UUID.randomUUID().toString();
    File file = createRandomFile(uuid);
    files.add(file);

    FileLink fileLink = client.upload(file.getPath(), "text/plain");
    String handle = fileLink.getHandle();
    handles.add(handle);
    byte[] bytes = fileLink.getContent().bytes();
    String content = new String(bytes, "utf-16");

    Assert.assertEquals(uuid, content);
  }

  @Test
  public void testDownload() throws Exception {
    FilestackClient client = new FilestackClient(API_KEY, SECURITY);

    String uploadUuid = UUID.randomUUID().toString();
    File uploadFile = createRandomFile(uploadUuid);
    files.add(uploadFile);

    FileLink fileLink = client.upload(uploadFile.getPath(), "text/plain");
    String handle = fileLink.getHandle();
    handles.add(handle);

    String downloadUuid = UUID.randomUUID().toString();
    File downloadFile = new File("/tmp/" + downloadUuid + ".txt");
    files.add(downloadFile);
    fileLink.download("/tmp/", downloadFile.getName());

    Assert.assertTrue(downloadFile.isFile());
    byte[] bytes = Files.asByteSource(downloadFile).read();
    String content = new String(bytes, "utf-16");
    Assert.assertEquals(uploadUuid, content);
  }

  @Test
  public void testOverwrite() throws Exception {
    FilestackClient client = new FilestackClient(API_KEY, SECURITY);

    String uploadUuid = UUID.randomUUID().toString();
    File uploadFile = createRandomFile(uploadUuid);
    files.add(uploadFile);

    String overwriteUuid = UUID.randomUUID().toString();
    File overwriteFile = createRandomFile(overwriteUuid);
    files.add(overwriteFile);

    FileLink fileLink = client.upload(uploadFile.getPath(), "text/plain");
    String handle = fileLink.getHandle();
    handles.add(handle);

    fileLink.overwrite(overwriteFile.getPath());

    byte[] bytes = fileLink.getContent().bytes();
    String content = new String(bytes, "utf-16");
    Assert.assertEquals(overwriteUuid, content);
  }

  /** Deletes any files uploaded to Filestack during tests. Also tests delete function. */
  @AfterClass
  public static void cleanupHandles() {
    for (String handle : handles) {
      FileLink fileLink = new FileLink(API_KEY, handle, SECURITY);
      try {
        fileLink.delete();
      } catch (Exception e) {
        Assert.fail("FileLink delete failed");
      }
    }
  }

  /** Deletes any local files created during tests. */
  @AfterClass
  public static void cleanupFiles() {
    for (File file : files) {
      if (!file.delete()) {
        Assert.fail("Unable to cleanup resource");
      }
    }
  }
}

@@ -17,16 +17,71 @@
 stack;%0A%0A
+import com.filestack.errors.ResourceNotFoundException;%0A
 import c
@@ -311,20 +311,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0A%0Apubli
@@ -824,24 +824,95 @@
 yList%3C%3E();%0A%0A
+  @Rule%0A  public ExpectedException thrown = ExpectedException.none();%0A%0A
   private st
@@ -3917,92 +3917,493 @@
 %0A%0A  
-/** Deletes any files uploaded to Filestack during tests. Also tests delete function
+@Test%0A  public void testDelete() throws Exception %7B%0A    FilestackClient client = new FilestackClient(API_KEY, SECURITY);%0A%0A    String uploadUuid = UUID.randomUUID().toString();%0A    File uploadFile = createRandomFile(uploadUuid);%0A    files.add(uploadFile);%0A%0A    FileLink fileLink = client.upload(uploadFile.getPath(), %22text/plain%22);%0A%0A    fileLink.delete();%0A%0A    thrown.expect(ResourceNotFoundException.class);%0A    fileLink.getContent();%0A  %7D%0A%0A  /** Deletes any files uploaded during tests
 . */

Add integration test for delete