package org.amc.dao;

import static org.junit.Assert.*;

import org.amc.game.chess.ChessBoard;
import org.amc.game.chess.ChessBoardUtilities;
import org.amc.game.chess.ChessGame;

import org.amc.game.chess.ComparePlayers;
import org.amc.game.chess.Move;
import org.amc.game.chess.Player;
import org.amc.game.chess.SimpleInputParser;
import org.amc.game.chessserver.DatabaseSignUpFixture;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ChessGameDAOTest {

    private DatabaseSignUpFixture signUpfixture = new DatabaseSignUpFixture();
    private DAO<ChessGame> chessGameDAO;

    private Player laura;
    private Player nobby;
    private ChessGame game;
    private SimpleInputParser mlParser;
    private int id;
    private static final String[] MOVES = {"A2A3", "A7A6"};
 
    @Before
    public void setUp() throws Exception {
        this.signUpfixture.setUp();
        this.mlParser = new SimpleInputParser();
        ServerChessGameTestDatabaseEntity serverChessGamesfixture = new ServerChessGameTestDatabaseEntity();
        laura = serverChessGamesfixture.getBlackPlayer();
        nobby = serverChessGamesfixture.getWhitePlayer();
        game = serverChessGamesfixture.getChessGame();
        chessGameDAO = new DAO<>(ChessGame.class);  
        
        id = game.getId();
    }

    @After
    public void tearDown() throws Exception {
        this.signUpfixture.tearDown();
    }

    @Test
    public void test() throws Exception {
        ChessGame game = chessGameDAO.getEntity(id);
        game.changePlayer();
        assertTrue(ComparePlayers.comparePlayers(game.getCurrentPlayer(), laura));
        assertTrue(ComparePlayers.comparePlayers(game.getBlackPlayer(), laura));
        assertTrue(ComparePlayers.comparePlayers(game.getWhitePlayer(), nobby));
        
    }
    
    @Test
    public void testChessBoardSaved() throws Exception {
        ChessGame game = chessGameDAO.getEntity(id);
        ChessBoard newBoard = new ChessBoard();
        newBoard.initialise();
        
        ChessBoardUtilities.compareBoards(newBoard, game.getChessBoard());
        
        for(String move : MOVES) {
            Move gameMove = mlParser.parseMoveString(move);
            game.move(game.getCurrentPlayer(), gameMove );
            game.changePlayer();
            newBoard.move(gameMove);
        }
        
        ChessBoardUtilities.compareBoards(newBoard, game.getChessBoard());
    }
}

@@ -50,16 +50,57 @@
 ert.*;%0A%0A
+import org.amc.EntityManagerThreadLocal;%0A
 import o
@@ -129,16 +129,16 @@
 sBoard;%0A
-
 import o
@@ -213,17 +213,16 @@
 ssGame;%0A
-%0A
 import o
@@ -362,24 +362,71 @@
 nputParser;%0A
+import org.amc.game.chess.view.ChessBoardView;%0A
 import org.a
@@ -464,24 +464,25 @@
 nUpFixture;%0A
+%0A
 import org.j
@@ -542,16 +542,17 @@
 .Test;%0A%0A
+%0A
 public c
@@ -894,16 +894,24 @@
 , %22A7A6%22
+, %22B1C3%22
 %7D;%0A %0A   
@@ -1358,14 +1358,8 @@
 new 
-DAO%3C%3E(
 Ches
@@ -1367,18 +1367,14 @@
 Game
-.class);  
+DAO();
 %0A   
@@ -1642,32 +1642,105 @@
 changePlayer();%0A
+        %0A        EntityManagerThreadLocal.closeEntityManager();%0A        %0A
         assertTr
@@ -2188,32 +2188,82 @@
 ise();%0A        %0A
+        new ChessBoardView(game.getChessBoard());%0A
         ChessBoa
@@ -2560,24 +2560,24 @@
 ;%0A        %7D%0A
-
         %0A   
@@ -2565,32 +2565,139 @@
      %7D%0A        %0A
+        EntityManagerThreadLocal.closeEntityManager();%0A        %0A        game = chessGameDAO.getEntity(id);%0A
         ChessBoa

change test to handle entitymanager close and flush