<commit_before>package org.amc.game.chessserver;

import static org.amc.game.chessserver.StompController.MESSAGE_HEADER_TYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.SESSION_ATTRIBUTES;

import org.amc.DAOException;
import org.amc.dao.DAO;
import org.amc.dao.DAOInterface;
import org.amc.dao.EntityManagerCache;
import org.amc.dao.SCGDAOInterface;
import org.amc.game.chess.*;
import org.amc.game.chessserver.ServerChessGameFactory.GameType;
import org.amc.game.chessserver.observers.JsonChessGameView;
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.messaging.Message;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.AbstractSubscribableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
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.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"/EntityManagerFactory.groovy", "/SpringTestConfig.xml", "/GameServerSecurity.xml",
        "/GameServerWebSockets.xml", "/EmailServiceContext.xml" })
public class StompControllerIT {

    private static final String SESSION_ID = "20";

    private static final String SUBSCRIPTION_ID = "20";

    private static final String SESSION_ATTRIBUTE = "PLAYER";

    private static final String MESSAGE_DESTINATION = "/app/move/";
    private static final String SAVE_MESSAGE_DESTINATION = "/app/save/";

    private static final String ONEVIEW_MESSAGE_DESTINATION = "/app/oneViewMove/";

    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private AbstractSubscribableChannel clientInboundChannel;

    @Autowired
    private AbstractSubscribableChannel clientOutboundChannel;

    @Autowired
    private AbstractSubscribableChannel brokerChannel;

    private TestChannelInterceptor clientOutboundChannelInterceptor;

    private TestChannelInterceptor brokerChannelInterceptor;

    private final long gameUUID = 1234L;

    private final long oneViewChessGameUUID = 4321L;

    private String[] moves = { "A2-A3", "A7-A6", "B2-B3", "B7-B6", "C2-C3", "C7-C6", "D2-D3",
            "D7-D6", "E2-E3", "E7-E6", "F2-F3", "F7-F6", "G2-G3", "G7-G6", "H2-H3", "H7-H6" };

    private DatabaseFixture fixture = new DatabaseFixture();

    private DAOInterface<Player> playerDAO;

    private Player stephen;

    private Player nobby;

    private JsonChessGameView view;

    private SimpMessagingTemplate template;

    private SCGDAOInterface serverChessGameDAO;

    @Before
    public void setup() throws Exception {
        fixture.setUp();

        playerDAO = new DAO<Player>(HumanPlayer.class);
        playerDAO.setEntityManager(fixture.getEntityManager());
        stephen = playerDAO.findEntities("userName", "stephen").get(0);
        nobby = playerDAO.findEntities("userName", "nobby").get(0);

        this.brokerChannelInterceptor = new TestChannelInterceptor();
        this.brokerChannelInterceptor.setIncludedDestinations("/user/**");
        this.brokerChannel.addInterceptor(this.brokerChannelInterceptor);

        this.clientOutboundChannelInterceptor = new TestChannelInterceptor();
        this.clientOutboundChannelInterceptor.setIncludedDestinations(MESSAGE_DESTINATION);
        this.clientOutboundChannel.addInterceptor(this.clientOutboundChannelInterceptor);

        serverChessGameDAO = (SCGDAOInterface) wac.getBean("myServerChessGameDAO");
        clearDAO();

        ServerChessGameFactory scgfactory = (ServerChessGameFactory) wac
                        .getBean("serverChessGameFactory");

        AbstractServerChessGame scg = scgfactory.getServerChessGame(GameType.NETWORK_GAME,
                        gameUUID, stephen);
        scg.addOpponent(nobby);

        serverChessGameDAO.saveServerChessGame(scg);

        AbstractServerChessGame oneViewChessGame = scgfactory.getServerChessGame(
                        GameType.LOCAL_GAME, oneViewChessGameUUID, stephen);
        oneViewChessGame.addOpponent(nobby);

        serverChessGameDAO.saveServerChessGame(oneViewChessGame);

        template = mock(SimpMessagingTemplate.class);
        view = new JsonChessGameView(template);
        view.setGameToObserver(oneViewChessGame);
    }

    private void clearDAO() {
        EntityManagerCache entityCache = (EntityManagerCache) wac.getBean("myEntityManagerCache");
        entityCache.getEntityManager(gameUUID).close();
        entityCache.getEntityManager(oneViewChessGameUUID).close();
    }

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

    @Test
    public void TwoViewChessGameMove() throws Exception {
        subscribe();

        for (int i = 0; i < moves.length; i++) {
            AbstractServerChessGame scg = serverChessGameDAO.getServerChessGame(gameUUID);
            twoViewMove(scg.getChessGame().getCurrentPlayer(), gameUUID, moves[i]);
            testInfoMessageSent();
            verifyMove(scg, moves[i]);
        }
    }

    private void verifyMove(AbstractServerChessGame scg, String moveString) throws DAOException {
        scg = serverChessGameDAO.getServerChessGame(scg.getUid());
        Move actualMove = scg.getChessGame().getTheLastMove();
        Move expectedMove = new Move(moveString);
        assertEquals(expectedMove.getStart(), actualMove.getStart());
        assertEquals(expectedMove.getEnd(), actualMove.getEnd());
    }

    private void testInfoMessageSent() throws Exception {
        testMessageSent(MessageType.INFO);
    }

    private void testErrorMessageSent() throws Exception {
        testMessageSent(MessageType.ERROR);
    }<commit_msg>fix reassign parameter in method verifyMove<commit_after>package org.amc.game.chessserver;

import static org.amc.game.chessserver.StompController.MESSAGE_HEADER_TYPE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.springframework.messaging.simp.SimpMessageHeaderAccessor.SESSION_ATTRIBUTES;

import org.amc.DAOException;
import org.amc.dao.DAO;
import org.amc.dao.DAOInterface;
import org.amc.dao.EntityManagerCache;
import org.amc.dao.SCGDAOInterface;
import org.amc.game.chess.*;
import org.amc.game.chessserver.ServerChessGameFactory.GameType;
import org.amc.game.chessserver.observers.JsonChessGameView;
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.messaging.Message;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.AbstractSubscribableChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Controller;
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.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"/EntityManagerFactory.groovy", "/SpringTestConfig.xml", "/GameServerSecurity.xml",
        "/GameServerWebSockets.xml", "/EmailServiceContext.xml" })
public class StompControllerIT {

    private static final String SESSION_ID = "20";

    private static final String SUBSCRIPTION_ID = "20";

    private static final String SESSION_ATTRIBUTE = "PLAYER";

    private static final String MESSAGE_DESTINATION = "/app/move/";
    private static final String SAVE_MESSAGE_DESTINATION = "/app/save/";

    private static final String ONEVIEW_MESSAGE_DESTINATION = "/app/oneViewMove/";

    @Autowired
    private WebApplicationContext wac;

    @Autowired
    private AbstractSubscribableChannel clientInboundChannel;

    @Autowired
    private AbstractSubscribableChannel clientOutboundChannel;

    @Autowired
    private AbstractSubscribableChannel brokerChannel;

    private TestChannelInterceptor clientOutboundChannelInterceptor;

    private TestChannelInterceptor brokerChannelInterceptor;

    private final long gameUUID = 1234L;

    private final long oneViewChessGameUUID = 4321L;

    private String[] moves = { "A2-A3", "A7-A6", "B2-B3", "B7-B6", "C2-C3", "C7-C6", "D2-D3",
            "D7-D6", "E2-E3", "E7-E6", "F2-F3", "F7-F6", "G2-G3", "G7-G6", "H2-H3", "H7-H6" };

    private DatabaseFixture fixture = new DatabaseFixture();

    private DAOInterface<Player> playerDAO;

    private Player stephen;

    private Player nobby;

    private JsonChessGameView view;

    private SimpMessagingTemplate template;

    private SCGDAOInterface serverChessGameDAO;

    @Before
    public void setup() throws Exception {
        fixture.setUp();

        playerDAO = new DAO<Player>(HumanPlayer.class);
        playerDAO.setEntityManager(fixture.getEntityManager());
        stephen = playerDAO.findEntities("userName", "stephen").get(0);
        nobby = playerDAO.findEntities("userName", "nobby").get(0);

        this.brokerChannelInterceptor = new TestChannelInterceptor();
        this.brokerChannelInterceptor.setIncludedDestinations("/user/**");
        this.brokerChannel.addInterceptor(this.brokerChannelInterceptor);

        this.clientOutboundChannelInterceptor = new TestChannelInterceptor();
        this.clientOutboundChannelInterceptor.setIncludedDestinations(MESSAGE_DESTINATION);
        this.clientOutboundChannel.addInterceptor(this.clientOutboundChannelInterceptor);

        serverChessGameDAO = (SCGDAOInterface) wac.getBean("myServerChessGameDAO");
        clearDAO();

        ServerChessGameFactory scgfactory = (ServerChessGameFactory) wac
                        .getBean("serverChessGameFactory");

        AbstractServerChessGame scg = scgfactory.getServerChessGame(GameType.NETWORK_GAME,
                        gameUUID, stephen);
        scg.addOpponent(nobby);

        serverChessGameDAO.saveServerChessGame(scg);

        AbstractServerChessGame oneViewChessGame = scgfactory.getServerChessGame(
                        GameType.LOCAL_GAME, oneViewChessGameUUID, stephen);
        oneViewChessGame.addOpponent(nobby);

        serverChessGameDAO.saveServerChessGame(oneViewChessGame);

        template = mock(SimpMessagingTemplate.class);
        view = new JsonChessGameView(template);
        view.setGameToObserver(oneViewChessGame);
    }

    private void clearDAO() {
        EntityManagerCache entityCache = (EntityManagerCache) wac.getBean("myEntityManagerCache");
        entityCache.getEntityManager(gameUUID).close();
        entityCache.getEntityManager(oneViewChessGameUUID).close();
    }

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

    @Test
    public void TwoViewChessGameMove() throws Exception {
        subscribe();

        for (int i = 0; i < moves.length; i++) {
            AbstractServerChessGame scg = serverChessGameDAO.getServerChessGame(gameUUID);
            twoViewMove(scg.getChessGame().getCurrentPlayer(), gameUUID, moves[i]);
            testInfoMessageSent();
            verifyMove(scg, moves[i]);
        }
    }

    private void verifyMove(AbstractServerChessGame abstractServerChessGame, String moveString) throws DAOException {
    	AbstractServerChessGame scg = serverChessGameDAO.
    			getServerChessGame(abstractServerChessGame.getUid());
        Move actualMove = scg.getChessGame().getTheLastMove();
        Move expectedMove = new Move(moveString);
        assertEquals(expectedMove.getStart(), actualMove.getStart());
        assertEquals(expectedMove.getEnd(), actualMove.getEnd());
    }

    private void testInfoMessageSent() throws Exception {
        testMessageSent(MessageType.INFO);
    }

    private void testErrorMessageSent() throws Exception {
        testMessageSent(MessageType.ERROR);
    }
{'fraction_non_alphanumeric': 0.09484205150652951, 'fraction_numerical': 0.007295542423579193, 'mean_word_length': 4.371473354231975, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7248417', 'n_tokens_mistral': 4035, 'n_tokens_neox': 3942, 'n_words': 677}