package org.amc.game.chessserver;

import static org.junit.Assert.*;

import org.amc.game.chess.ChessBoard;
import org.amc.game.chess.ChessGame;
import org.amc.game.chess.Colour;
import org.amc.game.chess.HumanPlayer;
import org.amc.game.chess.Player;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockServletContext;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class StartPageControllerJoinGameTest {
    private MockServletContext servletContext;
    private Model model;
    private ConcurrentMap<Long, ChessGame> gameMap;
    private StartPageController controller;
    private Player whitePlayer;
    private Player blackPlayer;
    private long gameUUID=1234L;
    
    @Before
    public void setUp() throws Exception {
        servletContext=new MockServletContext();
        model=new ExtendedModelMap();
        gameMap =new ConcurrentHashMap<>();
        servletContext.setAttribute(ServerConstants.GAMEMAP.toString(), gameMap);
        controller=new StartPageController();
        controller.setServletContext(servletContext);
        whitePlayer=new HumanPlayer("Ted", Colour.WHITE);
        blackPlayer=new HumanPlayer("Chris", Colour.WHITE);
        ChessGame chessGame=new ChessGame(new ChessBoard(),whitePlayer,blackPlayer);
        gameMap.put(gameUUID, chessGame);
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() {
        model.addAttribute(ServerConstants.PLAYER.toString(), whitePlayer);
        controller.joinGame(blackPlayer, gameUUID);
        
    }

}

@@ -67,83 +67,8 @@
 *;%0A%0A
-import org.amc.game.chess.ChessBoard;%0Aimport org.amc.game.chess.ChessGame;%0A
 impo
@@ -97,16 +97,16 @@
 Colour;%0A
+
 import o
@@ -329,27 +329,35 @@
 ork.
-ui.ExtendedModelMap
+test.web.ModelAndViewAssert
 ;%0Aim
@@ -385,16 +385,32 @@
 ork.
-ui.Model
+web.servlet.ModelAndView
 ;%0A%0Ai
@@ -597,33 +597,8 @@
 xt;%0A
-    private Model model;%0A
     
@@ -625,16 +625,22 @@
 p%3CLong, 
+Server
 ChessGam
@@ -664,23 +664,33 @@
 rivate S
-tartPag
+erverJoinChessGam
 eControl
@@ -915,46 +915,8 @@
 ();%0A
-        model=new ExtendedModelMap();%0A
     
@@ -1061,23 +1061,33 @@
 er=new S
-tartPag
+erverJoinChessGam
 eControl
@@ -1273,16 +1273,22 @@
         
+Server
 ChessGam
@@ -1299,24 +1299,30 @@
 essGame=new 
+Server
 ChessGame(ne
@@ -1323,42 +1323,13 @@
 ame(
-new ChessBoard(),whitePlayer,black
+white
 Play
@@ -1493,84 +1493,1172 @@
     
-model.addAttribute(ServerConstants.PLAYER.toString(), whitePlayer);%0A        
+ModelAndView mav=controller.joinGame(gameMap,blackPlayer, gameUUID);%0A        ModelAndViewAssert.assertViewName(mav, %22chessGamePortal%22);%0A        ModelAndViewAssert.assertModelAttributeAvailable(mav, %22GAME_UUID%22);%0A        ServerChessGame chessGame=gameMap.get(gameUUID);%0A        assertEquals(ServerChessGame.status.IN_PROGRESS,chessGame.getCurrentStatus());%0A        assertEquals(chessGame.getOpponent(),blackPlayer);%0A    %7D%0A    %0A    @Test%0A    public void testPlayerJoinsOwnGame()%7B%0A        ModelAndView mav=controller.joinGame(gameMap,whitePlayer, gameUUID);%0A        ModelAndViewAssert.assertViewName(mav, %22forward:/app/chessgame/chessapplication%22);%0A        ModelAndViewAssert.assertModelAttributeAvailable(mav, %22ERRORS%22);%0A        ServerChessGame chessGame=gameMap.get(gameUUID);%0A        assertEquals(ServerChessGame.status.AWAITING_PLAYER,chessGame.getCurrentStatus());%0A    %7D%0A    %0A    @Test%0A    public void testPlayerJoinsGameAlreadyInProgress()%7B%0A        ServerChessGame chessGame=gameMap.get(gameUUID);%0A        chessGame.addOpponent(blackPlayer);%0A        assertNotEquals(ServerChessGame.status.AWAITING_PLAYER,chessGame.getCurrentStatus());%0A        ModelAndView mav=
 cont
@@ -2673,16 +2673,24 @@
 oinGame(
+gameMap,
 blackPla
@@ -2697,32 +2697,196 @@
 yer, gameUUID);%0A
+        ModelAndViewAssert.assertViewName(mav, %22forward:/app/chessgame/chessapplication%22);%0A        ModelAndViewAssert.assertModelAttributeAvailable(mav, %22ERRORS%22);%0A
         %0A    %7D%0A%0A

implement test cases for normal join, player joining own game and player joining game already in progress