package org.amc.game.chessserver.observers;

import static org.mockito.Mockito.*;

import org.amc.dao.ServerChessGameDAO;
import org.amc.game.GameSubject;
import org.amc.game.chess.HumanPlayer;
import org.amc.game.chess.Player;
import org.amc.game.chessserver.AbstractServerChessGame;
import org.amc.game.chessserver.AbstractServerChessGame.ServerGameStatus;
import org.amc.game.chessserver.TwoViewServerChessGame;
import org.amc.game.chessserver.observers.GameFinishedListener;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.concurrent.TimeUnit;

public class FinishedChessGameRemovalUnitTest {

    
    AbstractServerChessGame serverChessGame;
    long uid = 1234L;
    Player player = new HumanPlayer("Adrian McLaughlin");
    GameFinishedListener listener;
    ThreadPoolTaskScheduler scheduler;
    
    @Mock
    private ServerChessGameDAO serverChessGameDAO;
    
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        scheduler=new ThreadPoolTaskScheduler();
        scheduler.afterPropertiesSet();
        
        player.setUserName("adrian");
        serverChessGame = new TwoViewServerChessGame(uid, player);
        listener = new GameFinishedListener();
        listener.setServerChessGameDAO(serverChessGameDAO);
        listener.setTaskScheduler(scheduler);
        listener.setDelayTime(1);
        
    }
    
    @Test
    public void test() throws Exception {
        listener.setGameToObserver(serverChessGame);
        serverChessGame.setCurrentStatus(ServerGameStatus.AWAITING_PLAYER);
        serverChessGame.setCurrentStatus(ServerGameStatus.FINISHED);
        
        checkServerChessGameDeleted();
    }
    
    
    private void checkServerChessGameDeleted() throws Exception {
        waitForThreadPoolShutdown();
        verify(serverChessGameDAO, times(1)).deleteEntity(eq(serverChessGame));
    }
    
    private void waitForThreadPoolShutdown() throws Exception {
        scheduler.getScheduledThreadPoolExecutor().shutdown();
        scheduler.getScheduledThreadPoolExecutor().awaitTermination(60, TimeUnit.SECONDS);
    }
    
    @Test
    public void setGameToObserverTest() throws Exception {
        serverChessGame.setCurrentStatus(ServerGameStatus.FINISHED);
        listener.setGameToObserver(serverChessGame);
        checkServerChessGameDeleted();
    }
    
    @Test
    public void setGameToObserverObjectTest() throws Exception {
        serverChessGame.setCurrentStatus(ServerGameStatus.FINISHED);
        GameSubject serverChessGameObject = serverChessGame;
        listener.setGameToObserver(serverChessGameObject);
        checkServerChessGameDeleted();
    }
    
    @Test
    public void setGameToObserverObjectNotServerChessGameTest() throws Exception {
        serverChessGame.setCurrentStatus(ServerGameStatus.FINISHED);
        GameSubject subject = new GameSubject();
        listener.setGameToObserver(subject);
        checkServerChessGameNotDeleted();
    }
    
    private void checkServerChessGameNotDeleted() throws Exception {
        waitForThreadPoolShutdown();
        verify(serverChessGameDAO, times(0)).deleteEntity(eq(serverChessGame));
    }
}

@@ -76,16 +76,45 @@
 ito.*;%0A%0A
+import org.amc.DAOException;%0A
 import o
@@ -144,16 +144,16 @@
 ameDAO;%0A
-
 import o
@@ -497,24 +497,72 @@
 edListener;%0A
+import org.apache.openjpa.jdbc.kernel.exps.Abs;%0A
 import org.j
@@ -824,21 +824,17 @@
 Test %7B%0A%0A
-    
 %0A
+
     Abst
@@ -1024,21 +1024,17 @@
 eduler;%0A
-    
 %0A
+
     @Moc
@@ -1086,21 +1086,17 @@
 ameDAO;%0A
-    
 %0A
+
     @Bef
@@ -1203,17 +1203,19 @@
 cheduler
-=
+ = 
 new Thre
@@ -1277,25 +1277,17 @@
 sSet();%0A
-        
 %0A
+
         
@@ -1574,28 +1574,16 @@
 1);%0A
-        
 %0A
+
     %7D%0A
-    
 %0A
+
     
@@ -1832,17 +1832,9 @@
 D);%0A
-        
 %0A
+
     
@@ -1874,25 +1874,17 @@
 ;%0A    %7D%0A
-    %0A    
+%0A
 %0A    pri
@@ -2057,37 +2057,33 @@
 ssGame));%0A    %7D%0A
-    
 %0A
+
     private void
@@ -2282,37 +2282,33 @@
 SECONDS);%0A    %7D%0A
-    
 %0A
+
     @Test%0A    pu
@@ -2519,37 +2519,33 @@
 eleted();%0A    %7D%0A
-    
 %0A
+
     @Test%0A    pu
@@ -2833,29 +2833,25 @@
 ed();%0A    %7D%0A
-    
 %0A
+
     @Test%0A  
@@ -3142,20 +3142,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri

clean up format