package org.pegadi.server.score; import no.dusken.common.model.Person; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.pegadi.games.Score; import org.pegadi.games.tetris.TetrisScore; import org.pegadi.server.AbstractDatabaseTest; import org.pegadi.server.ScoreServer; import org.pegadi.server.user.UserServer; import java.util.Calendar; import java.util.List; import java.util.Map; import static junit.framework.Assert.assertNotNull; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ public class ScoreServerImplTest extends AbstractDatabaseTest { private ScoreServer scoreServer; private UserServer userServer; @Before public void setUp() throws Exception { scoreServer = new ScoreServerImpl(); userServer = mock(UserServer.class); ((ScoreServerImpl) scoreServer).setDataSource(getDataSource()); ((ScoreServerImpl) scoreServer).setUserServer(userServer); Person user = new Person(1L, "first", "last", "username", "fl@tetris.org"); Person user2 = new Person(2L, "first1", "last", "username2", "fl@tetris.org"); Person user3 = new Person(3L, "first2", "last", "username3", "fl@tetris.org"); when(userServer.getUserByUsername("username")).thenReturn(user); when(userServer.getUserByUsername("username2")).thenReturn(user2); when(userServer.getUserByUsername("username3")).thenReturn(user3); } @Test public void testStartGame() throws Exception { TetrisScore score = (TetrisScore) scoreServer.startGame(userServer.getUserByUsername("username"), "tetris"); assertEquals("wrong domain", "tetris", score.getDomain()); assertEquals("Wrong number of lines", 0, score.getLines()); assertEquals("Wrong score", 0, score.getScore()); assertNotNull("Wrong date", score.getDate()); assertEquals("Wrong userid", "username", score.getUserID()); assertEquals("Wrong username", "first last", score.getUserName()); } @Test public void testUpdateScore() throws Exception { TetrisScore score = (TetrisScore) scoreServer.startGame(userServer.getUserByUsername("username"), "tetris"); score.setLevel(10); score.setLines(666); score.setScore(1234567); scoreServer.updateScore(score); Map result = simpleJdbcTemplate.queryForMap("Select * from score_tetris where id=?", score.getID()); assertEquals("Wrong score", 1234567l, result.get("score")); assertEquals("Wrong number of lines", 666, result.get("linecount")); assertEquals("Wrong level", 10, result.get("level")); assertEquals("Score not active", true, result.get("active")); } @Test public void testEndGame() throws Exception { TetrisScore score = (TetrisScore) scoreServer.startGame(userServer.getUserByUsername("username"), "tetris"); score.setLevel(10); score.setLines(666); score.setScore(1234567); scoreServer.updateScore(score); TetrisScore endscore = (TetrisScore) scoreServer.endGame(score); assertEquals("Wrong endscore", 1234567l, endscore.getScore()); assertEquals("Wrong userid", "username", endscore.getUserID()); assertEquals("Wrong username", "first last", endscore.getUserName()); assertEquals("Wrong domain", "tetris", endscore.getDomain()); assertEquals("Wrong level", 10, endscore.getLevel()); assertEquals("Wrong number of lines", 666, endscore.getLines()); Map result = simpleJdbcTemplate.queryForMap("Select * from score_tetris where id=?", score.getID()); assertEquals("Wrong score", 1234567l, result.get("score")); assertEquals("Wrong number of lines", 666, result.get("linecount")); assertEquals("Wrong level", 10, result.get("level")); assertEquals("Score not active", false, result.get("active")); } @Test public void testCancelGame() throws Exception { TetrisScore score = (TetrisScore) scoreServer.startGame(userServer.getUserByUsername("username"), "tetris"); scoreServer.cancelGame("username", score); } @Test @Ignore //Derby does not support the limit function public void testGetHighScore() throws Exception { List<? extends Score> scores = scoreServer.getHighScore("tetris", 2, false); assertEquals("Wrong number of scores", 2, scores.size()); assertEquals("Wrong top score", 23214, scores.get(0).getScore()); assertEquals("Wrong second score", 2324, scores.get(1).getScore()); } @Test @Ignore //Derby does not support the limit function public void testGetUserScore() throws Exception { List<? extends Score> scores = scoreServer.getUserScore("tetris", "username", 2); assertEquals("Wrong number of scores", 2, scores.size()); assertEquals("Wrong top score", 440, scores.get(0).getScore()); assertEquals("Wrong second score", 240, scores.get(1).getScore()); } @Test @Ignore //Derby does not support the limit function public void testGetDayScore() throws Exception { Calendar c = Calendar.getInstance(); c.set(2001, 3, 29); List<? extends Score> scores = scoreServer.getDayScore("tetris", c.getTime(), 4); assertEquals("Wrong number of scores", 4, scores.size()); assertEquals("Wrong top score", 23214, scores.get(0).getScore()); assertEquals("Wrong second score", 2324, scores.get(1).getScore()); assertEquals("Wrong third score", 440, scores.get(2).getScore()); assertEquals("Wrong fourth score", 240, scores.get(3).getScore()); } }