package com.turn.ttorrent.common;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.FileUtils;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.testng.Assert.*;
import static org.testng.Assert.assertEqualsNoOrder;

@Test
public class TorrentTest {

  public void test_create_torrent() throws URISyntaxException, IOException, NoSuchAlgorithmException, InterruptedException {
    URI announceURI = new URI("http://localhost:6969/announce");
    String createdBy = "Test";
    Torrent t = Torrent.create(new File("src/test/resources/parentFiles/file1.jar"), announceURI, createdBy);
    assertEquals(createdBy, t.getCreatedBy());
    assertEquals(announceURI, t.getAnnounceList().get(0).get(0));
  }

  public void load_torrent_created_by_utorrent() throws IOException, NoSuchAlgorithmException, URISyntaxException {
    Torrent t = Torrent.load(new File("src/test/resources/torrents/file1.jar.torrent"));
    assertEquals(new URI("http://localhost:6969/announce"), t.getAnnounceList().get(0).get(0));
    assertEquals("B92D38046C76D73948E14C42DF992CAF25489D08", t.getHexInfoHash());
    assertEquals("uTorrent/3130", t.getCreatedBy());
  }

  public void torrent_from_multiple_files() throws URISyntaxException, InterruptedException, NoSuchAlgorithmException, IOException {
    URI announceURI = new URI("http://localhost:6969/announce");
    String createdBy = "Test2";
    final File parentDir = new File("src/test/resources/parentFiles/parentDir");
    final long creationTimeSecs = 1376051000;
    Torrent t = Torrent.create(parentDir,addFilesRecursively(parentDir), announceURI, null, createdBy, creationTimeSecs, Torrent.DEFAULT_PIECE_LENGTH);
    File torrentFileWin = new File("src/test/resources/torrents/parentDir.win.torrent");
    File torrentFileLinux = new File("src/test/resources/torrents/parentDir.linux.torrent");
    final byte[] expectedBytesWin = FileUtils.readFileToByteArray(torrentFileWin);
    final byte[] expectedBytesLinux = FileUtils.readFileToByteArray(torrentFileLinux);
    final byte[] actualBytes = t.getEncoded();

    assertTrue(Hex.encodeHexString(expectedBytesWin).equals(Hex.encodeHexString(actualBytes)) || Hex.encodeHexString(expectedBytesLinux).equals(Hex.encodeHexString(actualBytes)));
  }

  public void testFilenames() throws IOException, NoSuchAlgorithmException {
    File torrentFile = new File("src/test/resources/torrents/parentDir.win.torrent");
    Torrent t2 = Torrent.load(torrentFile);
    final List<String> tmpFileNames = t2.getFilenames();
    final List<String> normalizedFilenames = new ArrayList<String>(tmpFileNames.size());
    for (String filename : tmpFileNames) {
      normalizedFilenames.add(filename.replaceAll("\\\\", "/"));
    }
    String[] expectedFilenames = new String[]
    {"parentDir/AccuRevCommon.jar",
     "parentDir/commons-io-cio2.5_3.jar",
     "parentDir/commons-io-cio2.5_3.jar.link",
     "parentDir/inDir/application.wadl",
     "parentDir/storage.version"};
    assertEqualsNoOrder(normalizedFilenames.toArray(new String[normalizedFilenames.size()]), expectedFilenames);
    System.out.println();
  }

  private static List<File> addFilesRecursively(File baseDir){
    final File[] files = baseDir.listFiles();
    if (files == null) {
      return Collections.emptyList();
    }
    List<File> list = new ArrayList<File>();
    for (File file : files) {
      if (file.isDirectory()){
        list.addAll(addFilesRecursively(file));
      } else if (file.isFile()){
        list.add(file);
      }
    }
    return list;
  }
}

@@ -323,95 +323,9 @@
 til.
-ArrayList;%0Aimport java.util.Arrays;%0Aimport java.util.Collections;%0Aimport java.util.List
+*
 ;%0A%0Ai
@@ -1701,64 +1701,456 @@
     
-Torrent t = Torrent.create(parentDir,addFilesRecursively
+final String%5B%5D fileNames = new String%5B%5D%0A            %7B%22AccuRevCommon.jar%22,%0A                    %22commons-io-cio2.5_3.jar%22,%0A                    %22commons-io-cio2.5_3.jar.link%22,%0A                    %22inDir/application.wadl%22,%0A                    %22storage.version%22%7D;%0A    final List%3CFile%3E files = new ArrayList%3CFile%3E();%0A    for (String fileName : fileNames) %7B%0A      files.add(new File(parentDir, fileName));%0A    %7D%0A    Torrent createdTorrent = Torrent.create
 (par
@@ -2155,17 +2155,22 @@
 arentDir
-)
+,files
 , announ
@@ -2625,16 +2625,29 @@
 Bytes = 
+createdTorren
 t.getEnc
@@ -3702,430 +3702,6 @@
  %7D%0A%0A
-  private static List%3CFile%3E addFilesRecursively(File baseDir)%7B%0A    final File%5B%5D files = baseDir.listFiles();%0A    if (files == null) %7B%0A      return Collections.emptyList();%0A    %7D%0A    List%3CFile%3E list = new ArrayList%3CFile%3E();%0A    for (File file : files) %7B%0A      if (file.isDirectory())%7B%0A        list.addAll(addFilesRecursively(file));%0A      %7D else if (file.isFile())%7B%0A        list.add(file);%0A      %7D%0A    %7D%0A    return list;%0A  %7D%0A
 %7D%0A

test always failing test