package org.amc.dao;

import static org.junit.Assert.*;

import org.amc.DAOException;
import org.amc.game.chess.ChessGameFactory;
import org.amc.game.chessserver.DatabaseSignUpFixture;
import org.amc.game.chessserver.AbstractServerChessGame;
import org.amc.game.chessserver.observers.ObserverFactoryChain;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.WebApplicationContext;

import java.util.Set;


@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"/SpringTestConfig.xml", "/GameServerSecurity.xml", "/GameServerWebSockets.xml", "/GameObservers.xml"})
public class ServerChessGameDAOTest {

    @Autowired
    private WebApplicationContext wac;
    
    private DatabaseSignUpFixture signUpfixture = new DatabaseSignUpFixture();
    private ServerChessGameTestDatabaseEntity serverChessGamesfixture;
    private ServerChessGameDAO dao;
    private EntityManagerCache emCache;
    
    @Before
    public void setUp() throws Exception {
        this.signUpfixture.setUp();
        dao = (ServerChessGameDAO) wac.getBean("myServerChessGameDAO");
        ObserverFactoryChain chain = (ObserverFactoryChain) wac.getBean("defaultObserverFactoryChain");
        emCache = (EntityManagerCache) wac.getBean("myEntityManagerCache");
        dao.setObserverFactoryChain(chain);
        serverChessGamesfixture = new ServerChessGameTestDatabaseEntity();      
    }

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

    @Test
    public void getGameUidsTest() {
        Set<Long> uids = dao.getGameUids();
        assertEquals(1, uids.size());
        assertTrue(uids.contains(serverChessGamesfixture.getUID()));
    }

    @Test
    public void getServerChessGameTest() throws DAOException {
        AbstractServerChessGame scgGame = dao.getServerChessGame(serverChessGamesfixture.getUID());
        assertEquals(2, scgGame.getNoOfObservers());
        scgGame = dao.getServerChessGame(serverChessGamesfixture.getUID());
        assertEquals(2, scgGame.getNoOfObservers());
    }
    
    @Test
    public void getServerChessGameEntityManagerClosed() throws DAOException {
        AbstractServerChessGame scgGame = dao.getServerChessGame(serverChessGamesfixture.getUID());
        assertEquals(2, scgGame.getNoOfObservers());
        
        emCache.getEntityManager(scgGame.getUid()).close();
        scgGame = dao.getServerChessGame(serverChessGamesfixture.getUID());
        assertEquals(2, scgGame.getNoOfObservers());
    }
}

@@ -83,52 +83,8 @@
 on;%0A
-import org.amc.game.chess.ChessGameFactory;%0A
 impo
@@ -689,31 +689,8 @@
 t;%0A%0A
-import java.util.Set;%0A%0A
 %0A@Ru
@@ -1785,212 +1785,8 @@
  %7D%0A%0A
-    @Test%0A    public void getGameUidsTest() %7B%0A        Set%3CLong%3E uids = dao.getGameUids();%0A        assertEquals(1, uids.size());%0A        assertTrue(uids.contains(serverChessGamesfixture.getUID()));%0A    %7D%0A%0A
     

update due to getGameUids removal from DAO