package org.lantern.endpoints;

import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;
import org.lantern.TestingUtils;
import org.lantern.endpoints.FriendApi;
import org.lantern.oauth.OauthUtils;
import org.lantern.oauth.RefreshToken;
import org.lantern.state.ClientFriend;
import org.lantern.state.Friend;
import org.lantern.state.Model;
import org.lantern.util.HttpClientFactory;
import org.lantern.util.Stopwatch;
import org.lantern.util.StopwatchManager;

/**
 * Tests for the friends syncing REST API.
 */
public class FriendEndpointTest {

    @Test
    public void testFriendEndpiont() throws Exception {
        final HttpClientFactory httpClientFactory = 
                TestingUtils.newHttClientFactory();
        final Model model = TestingUtils.newModel();
        final OauthUtils utils = new OauthUtils(httpClientFactory, model, new RefreshToken(model));
        final FriendApi api = new FriendApi(utils);
        
        final ClientFriend friend = new ClientFriend();
        friend.setEmail("test@test.com");
        friend.setName("Tester");
        api.insertFriend(friend);
        
        final Stopwatch friendsWatch = 
            StopwatchManager.getStopwatch("friends-api", 
                "org.lantern", "listFriends");
        
        List<ClientFriend> friends = null;
        for (int i = 0; i < 10; i++) {
            friendsWatch.start();
            friends = api.listFriends();
            friendsWatch.stop();
            friendsWatch.logSummary();
        }
        friendsWatch.logSummary();
        StopwatchManager.logSummaries("org.lantern");
        
        for (final ClientFriend f : friends) {
            final Long id = f.getId();
            api.removeFriend(id);
            // Give the db a chance to sync.
            Thread.sleep(100);
        }
        
        final List<ClientFriend> postDelete = api.listFriends();
        
        assertEquals(0, postDelete.size());
        
        final Friend inserted = api.insertFriend(friend);
        
        final String updatedName = "brandnew@email.com";
        inserted.setEmail(updatedName);
        final Friend updated = api.updateFriend(inserted);
        
        assertEquals(updatedName, updated.getEmail());
        
        final List<ClientFriend> newList = api.listFriends();
        for (final ClientFriend f : newList) {
            assertEquals(updatedName, f.getEmail());
            
            final Long id = f.getId();
            final Friend get = api.getFriend(id);
            assertEquals(id, get.getId());
            
            api.removeFriend(id);
            // Give the db a chance to sync.
            Thread.sleep(100);
        }
        
        final List<ClientFriend> empty = api.listFriends();
        assertEquals(0, empty.size());
    }
}
@@ -479,16 +479,73 @@
 Manager;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0A/**%0A *
@@ -622,16 +622,89 @@
 ntTest %7B
+%0A    %0A    private final Logger log = LoggerFactory.getLogger(getClass());
 %0A%0A    @T
@@ -1482,18 +1482,17 @@
  0; i %3C 
-10
+2
 ; i++) %7B
@@ -1739,32 +1739,93 @@
 ern%22);%0A        %0A
+        log.debug(%22Deleting all friends from: %7B%7D%22, friends);%0A
         for (fin
@@ -1965,32 +1965,81 @@
 chance to sync.%0A
+            log.debug(%22Removing friend: %7B%7D%22, f);%0A
             Thre
@@ -2039,33 +2039,34 @@
    Thread.sleep(
-1
+20
 00);%0A        %7D%0A 

test tweaks -- failing apparently due to eventual consistency issues in the db