package cc.twittertools.spider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.BufferedReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Charsets;
import com.google.common.collect.Sets;
import com.google.common.io.Resources;

import cc.twittertools.post.Tweet;


public class TweetsHtmlParserTest
{
  private final static String USER = "charlie_whiting";
  private final TweetsHtmlParser parser = new TweetsHtmlParser();
  private final String samplePageHtml;
  private final String sampleUser;
  
  public TweetsHtmlParserTest() throws IOException, URISyntaxException
  { sampleUser = "rtraister";
    String variant = "-3";
    try (
      BufferedReader rdr = Files.newBufferedReader(Paths.get(Resources.getResource(sampleUser + "2016" + variant + ".html").toURI()), Charsets.UTF_8)
    )
    {
      StringBuilder input = new StringBuilder();
      String line = null;
      while ((line = rdr.readLine()) != null)
        input.append (line);
      
      samplePageHtml = input.toString();
    }
  }
  
  @Before
  public void setUp() throws Exception
  {
    
  }
  
  @After
  public void tearDown() throws Exception
  {
    
  }
  
  @Test
  public void testParser() throws Exception
  {
    List<Tweet> tweets = parser.parse("rtraister", "  \t \n");
    //assertThat(tweets, allOf(notNullValue(), empty()));
    
    tweets = parser.parse(sampleUser, samplePageHtml);
    //assertThat(tweets, allOf(notNullValue(), hasSize(20)));
    assertEquals(tweets.get(3).getAuthor(), "charlie_whiting");
    assertEquals(tweets.get(3).getMsg(), "Cool as well as lump-in-throat inducing. @RoyalAirForceUK is tweeting the signals received from the #Dambusters70 on their 70th anniv.");
    assertEquals(tweets.get(3).getMsgLessSigils(), "Cool as well as lump-in-throat inducing.  is tweeting the signals received from the  on their 70th anniv.");
    assertEquals(tweets.get(3).getAddressees(), Sets.newHashSet("RoyalAirForceUK"));
    assertEquals(tweets.get(3).getHashTags(), Sets.newHashSet("Dambusters70"));
    assertFalse(tweets.get(3).containsRetweet());
    assertFalse(tweets.get(3).isManualRetweet());
    assertEquals(tweets.get(3).getId(), 335212126207619072L);
    
    System.out.println (StringUtils.join(tweets, "\n\n"));
  }
}

@@ -143,24 +143,76 @@
 eredReader;%0A
+import java.io.BufferedWriter;%0Aimport java.io.File;%0A
 import java.
@@ -291,16 +291,43 @@
 .Files;%0A
+import java.nio.file.Path;%0A
 import j
@@ -346,16 +346,44 @@
 .Paths;%0A
+import java.util.ArrayList;%0A
 import j
@@ -1844,24 +1844,26 @@
 Size(20)));%0A
+//
     assertEq
@@ -1910,24 +1910,26 @@
 _whiting%22);%0A
+//
     assertEq
@@ -2088,32 +2088,34 @@
  70th anniv.%22);%0A
+//
     assertEquals
@@ -2255,24 +2255,26 @@
 h anniv.%22);%0A
+//
     assertEq
@@ -2342,24 +2342,26 @@
 ForceUK%22));%0A
+//
     assertEq
@@ -2424,24 +2424,26 @@
 sters70%22));%0A
+//
     assertFa
@@ -2472,32 +2472,34 @@
 ainsRetweet());%0A
+//
     assertFalse(
@@ -2528,24 +2528,26 @@
 Retweet());%0A
+//
     assertEq
@@ -2600,55 +2600,672 @@
 L);%0A
-    %0A    System.out.println (StringUtils.join(t
+%0A    File tmpFile = File.createTempFile(USER + %22-sample-tweets-%22, %22.txt%22);%0A    tmpFile.deleteOnExit();%0A    Path tmpPath = Paths.get(tmpFile.getAbsolutePath());%0A%0A    try (BufferedWriter wtr = Files.newBufferedWriter(tmpPath, Charsets.UTF_8)) %7B%0A      Tweet.WRITER.writeAllAsTabDelim(wtr, tweets.iterator());%0A    %7D%0A    final List%3CTweet%3E readTweets;%0A    try (BufferedReader rdr = Files.newBufferedReader(tmpPath, Charsets.UTF_8)) %7B%0A      readTweets = Tweet.WRITER.readAllFromTabDelim(rdr);%0A    %7D%0A    %0A    System.out.println (StringUtils.join(tweets, %22%5Cn%5Cn%22));%0A    System.out.println (%22%5Cn-------------------------%5Cn%5Cn%5Cn%22);%0A    System.out.println (StringUtils.join(readT
 weet

Test the new tweet serialisation to tab-delimited text files.