package com.tguzik.cwierkacz.twitter;

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

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.tguzik.cwierkacz.cache.dataobject.FunctionalAccount;
import com.tguzik.cwierkacz.cache.dataobject.Tweet;

public class TwitterAccountListenerTest
{

    private final String username2 = "cwierkacz13";
    private final String accessToken2 = "1291708484-x2GIuJprsYm4Wf2MWSFvsRYkdVPxUZncUxzKaJw";
    private final String accessTokenSecret2 = "jxqddQ8NdcFa0uqJJGuQcMgBifjj0us8BX4GNto4YQ";
    private final long userId2 = 1291708484;

    private final String username = "cwierkacz1";
    private final String accessToken = "1291752048-EINIa817O7rBs5FTcyQu6dozbgV6KQJx339Cn3I";
    private final String accessTokenSecret = "sIxeZvvJ0eSxgy3Zj27829De0KI0HytKtPcWrLK9Y";
    private final long userId = 1291752048;

    private TwitterAccountListener listener;
    private MemoryTwitterStorage storage;

    FunctionalAccount user = FunctionalAccount.create(userId, -1, username, accessToken, accessTokenSecret);

    FunctionalAccount user2 = FunctionalAccount.create(userId2,
                                                       -1,
                                                       username2,
                                                       accessToken2,
                                                       accessTokenSecret2);

    TwitterAccount account1, account2;

    class MemoryTwitterStorage implements TwitterStorage
    {
        private final Map<Long, Tweet> tweets = new HashMap<Long, Tweet>();

        @Override
        public void addUnclassifiedTweet( Tweet tweet ) {
            tweets.put(tweet.getTweetId().toValue(), tweet);

        }

        @Override
        public void removeTweet( long tweetId ) {
            tweets.remove(tweetId);
        }

        public int size( ) {
            return tweets.size();
        }

        public boolean contain( long tweetId ) {
            return tweets.containsKey(tweetId);
        }

        public boolean contain( Tweet tweet ) {
            return tweets.containsValue(tweet);
        }

        @Override
        public String toString( ) {
            Iterator<Entry<Long, Tweet>> it = tweets.entrySet().iterator();
            String msg = "";
            while ( it.hasNext() ) {
                Entry<Long, Tweet> pairs = it.next();
                msg += ( pairs.getKey() + " = " + pairs.getValue() + "\n" );
                it.remove();
            }
            return msg;
        }

    }

    @Before
    public void setUp( ) throws Exception {

        account1 = new TwitterAccount(user);
        account2 = new TwitterAccount(user2);

        listener = new TwitterAccountListener(user);

        storage = new MemoryTwitterStorage();

        listener.initialize(storage);
        listener.listen();
        Thread.sleep(1000); //waiting for fully start
    }

    @After
    public void closeAll( ) {
        listener.stopListen();
    }

    @Test
    public void testAddTweetByOwner( ) throws TwitterActionException,
                                      InterruptedException,
                                      TwitterAuthenticationException {

        Tweet tweet = account1.composeNewTweet("2MSGAAAAAaa " + new Date().getTime());
        Thread.sleep(1000);
        assertEquals(1, storage.size());
        assertTrue(storage.contain(tweet.getTweetId().toValue()));
    }

    @Test
    public void testAddReplyTweetFromOther( ) throws TwitterActionException,
                                             InterruptedException,
                                             TwitterAuthenticationException {

        Tweet tweet = account1.composeNewTweet("2MSGAAAAAaa " + new Date().getTime());
        Tweet replyTweet = account2.composeNewReplyTweet("reply " + new Date().getTime(), tweet);
        Thread.sleep(1000);
        assertEquals(2, storage.size());
        assertTrue(storage.contain(tweet.getTweetId().toValue()));
        assertTrue(storage.contain(replyTweet.getTweetId().toValue()));
    }

    @Test
    public void testAddReplyTweetForOther( ) throws TwitterActionException,
                                            InterruptedException,
                                            TwitterAuthenticationException {

        Tweet tweet = account2.composeNewTweet("2MSGAAAAAaa " + new Date().getTime());
        Tweet replyTweet = account1.composeNewReplyTweet("reply " + new Date().getTime(), tweet);
        Thread.sleep(1000);
        assertEquals(1, storage.size());
        assertTrue(storage.contain(replyTweet.getTweetId().toValue()));
    }

    @Test
    public void testAddRetweetOther( ) throws TwitterActionException,
                                      InterruptedException,
                                      TwitterAuthenticationException {

        Tweet tweet = account2.composeNewTweet("2MSGAAAAAaa " + new Date().getTime());
        Tweet retweetTweet = account1.composeNewReTweet(tweet);
        Thread.sleep(1000);
        assertEquals(1, storage.size());
        assertTrue(storage.contain(retweetTweet.getTweetId().toValue()));
    }

    @Test
    public void testAddRetweetOwner( ) throws TwitterActionException,
                                      InterruptedException,
                                      TwitterAuthenticationException {

        Tweet tweet = account1.composeNewTweet("2MSGAAAAAaa " + new Date().getTime());
        Tweet retweetTweet = account2.composeNewReTweet(tweet);
        Thread.sleep(1000);
        assertEquals(2, storage.size());
        assertTrue(storage.contain(tweet.getTweetId().toValue()));
        assertTrue(storage.contain(retweetTweet.getTweetId().toValue()));
    }

}

@@ -5907,10 +5907,945 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testDeleteTweetByOwner( ) throws TwitterActionException, InterruptedException %7B%0A        Tweet tweet = account1.composeNewTweet(%222MSGAAAAAaa %22 + new Date().getTime());%0A        Thread.sleep(1000);%0A        assertEquals(1, storage.size());%0A%0A        account1.deleteTweet(tweet);%0A%0A        Thread.sleep(1000);%0A        assertEquals(0, storage.size());%0A    %7D%0A%0A    @Test%0A    public void testDeleteReplyTweetByOther( ) throws TwitterActionException, InterruptedException %7B%0A        Tweet tweet = account1.composeNewTweet(%222MSGAAAAAaa %22 + new Date().getTime());%0A        Tweet replyTweet = account2.composeNewReplyTweet(%22reply %22 + new Date().getTime(), tweet);%0A        Thread.sleep(1000);%0A        assertEquals(2, storage.size());%0A%0A        account2.deleteTweet(replyTweet);%0A%0A        Thread.sleep(1000);%0A        assertEquals(1, storage.size());%0A        assertTrue(!storage.contain(replyTweet.getTweetId().toValue()));%0A    %7D%0A%0A
 %7D%0A

add test to deleting listener