LeagueService JUnit test to verify that number of games in series can't be broken.

This commit is contained in:
marcins78@gmail.com
2012-05-02 17:44:56 +00:00
parent f2a6d93d50
commit 7d55ebce92
5 changed files with 137 additions and 18 deletions

View File

@@ -94,12 +94,14 @@ public class CachedLeagueMatchDAO implements LeagueMatchDAO {
}
@Override
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match) {
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, String winner, String loser) {
_readWriteLock.writeLock().lock();
try {
LeagueMatch match = new LeagueMatch(winner, loser);
getLeagueMatchesInWriteLock(league).add(match);
getLeagueSerieMatchesInWriteLock(league, leagueSeason).add(match);
_leagueMatchDAO.addPlayedMatch(league, leagueSeason, match);
_leagueMatchDAO.addPlayedMatch(league, leagueSeason, winner, loser);
} finally {
_readWriteLock.writeLock().unlock();
}

View File

@@ -85,7 +85,7 @@ public class DbLeagueMatchDAO implements LeagueMatchDAO {
}
@Override
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match) {
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, String winner, String loser) {
try {
Connection conn = _dbAccess.getDataSource().getConnection();
try {
@@ -93,8 +93,8 @@ public class DbLeagueMatchDAO implements LeagueMatchDAO {
try {
statement.setString(1, league.getType());
statement.setString(2, leagueSeason.getName());
statement.setString(3, match.getWinner());
statement.setString(4, match.getLoser());
statement.setString(3, winner);
statement.setString(4, loser);
statement.execute();
} finally {
statement.close();

View File

@@ -11,5 +11,5 @@ public interface LeagueMatchDAO {
public Collection<LeagueMatch> getLeagueSerieMatches(League league, LeagueSerieData leagueSerie);
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, LeagueMatch match);
public void addPlayedMatch(League league, LeagueSerieData leagueSeason, String winner, String loser);
}

View File

@@ -143,10 +143,10 @@ public class LeagueService {
}
public void reportLeagueGameResult(League league, LeagueSerieData serie, String winner, String loser) {
addMatch(league, serie, winner, loser);
_leagueMatchDao.addPlayedMatch(league, serie, winner, loser);
addPoints(league, serie, winner, 2);
addPoints(league, serie, loser, 1);
_leaguePointsDao.addPoints(league, serie, winner, 2);
_leaguePointsDao.addPoints(league, serie, loser, 1);
_leagueStandings.remove(getLeagueMapKey(league));
_leagueSerieStandings.remove(getLeagueSerieMapKey(league, serie));
@@ -155,15 +155,6 @@ public class LeagueService {
awardPrizesToPlayer(league, serie, loser, false);
}
private void addPoints(League league, LeagueSerieData serie, String player, int points) {
_leaguePointsDao.addPoints(league, serie, player, points);
}
private void addMatch(League league, LeagueSerieData serie, String winner, String loser) {
LeagueMatch match = new LeagueMatch(winner, loser);
_leagueMatchDao.addPlayedMatch(league, serie, match);
}
private void awardPrizesToPlayer(League league, LeagueSerieData serie, String player, boolean winner) {
int count = 0;
Collection<LeagueMatch> playerMatchesPlayedOn = getPlayerMatchesInSerie(league, serie, player);

View File

@@ -0,0 +1,126 @@
package com.gempukku.lotro.league;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.db.LeagueDAO;
import com.gempukku.lotro.db.LeagueMatchDAO;
import com.gempukku.lotro.db.LeagueParticipationDAO;
import com.gempukku.lotro.db.LeaguePointsDAO;
import com.gempukku.lotro.db.vo.League;
import com.gempukku.lotro.db.vo.LeagueMatch;
import org.junit.Test;
import org.mockito.Mockito;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class LeagueServiceTest {
@Test
public void testJoiningLeagueAfterMaxGamesPlayed() throws Exception {
LeagueDAO leagueDao = Mockito.mock(LeagueDAO.class);
StringBuilder sb = new StringBuilder();
sb.append("20120502" + "," + "default" + "," + "1" + "," + "1");
for (int i = 0; i < 1; i++)
sb.append("," + "lotr_block" + "," + "7" + "," + "2");
List<League> leagues = new ArrayList<League>();
League league = new League(5000, "League name", "leagueType", NewConstructedLeagueData.class.getName(), sb.toString(), 0);
leagues.add(league);
LeagueSerieData leagueSerie = league.getLeagueData().getSeries().get(0);
Mockito.when(leagueDao.loadActiveLeagues(Mockito.anyInt())).thenReturn(leagues);
LeaguePointsDAO leaguePointsDAO = Mockito.mock(LeaguePointsDAO.class);
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatch>(matches));
Mockito.when(leagueMatchDAO.getLeagueSerieMatches(league, leagueSerie)).thenReturn(new HashSet<LeagueMatch>(matches));
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
LeagueService leagueService = new LeagueService(leagueDao, leaguePointsDAO, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
assertTrue(leagueService.canPlayRankedGame(league, leagueSerie, "player1"));
assertTrue(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player2"));
leagueService.reportLeagueGameResult(league, leagueSerie, "player1", "player2");
assertTrue(leagueService.canPlayRankedGame(league, leagueSerie, "player1"));
assertFalse(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player2"));
assertTrue(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player3"));
Mockito.verify(leagueMatchDAO).getLeagueMatches(league);
Mockito.verify(leagueMatchDAO).getLeagueSerieMatches(league, leagueSerie);
Mockito.verify(leagueMatchDAO).addPlayedMatch(league, leagueSerie, "player1", "player2");
Mockito.verifyNoMoreInteractions(leagueMatchDAO);
leagueService.reportLeagueGameResult(league, leagueSerie, "player1", "player3");
assertFalse(leagueService.canPlayRankedGame(league, leagueSerie, "player1"));
assertFalse(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player2"));
assertFalse(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player3"));
Mockito.verify(leagueMatchDAO).addPlayedMatch(league, leagueSerie, "player1", "player3");
Mockito.verifyNoMoreInteractions(leagueMatchDAO);
}
@Test
public void testJoiningLeagueAfterMaxGamesPlayedWithPreloadedDb() throws Exception {
LeagueDAO leagueDao = Mockito.mock(LeagueDAO.class);
StringBuilder sb = new StringBuilder();
sb.append("20120502" + "," + "default" + "," + "1" + "," + "1");
for (int i = 0; i < 1; i++)
sb.append("," + "lotr_block" + "," + "7" + "," + "2");
List<League> leagues = new ArrayList<League>();
League league = new League(5000, "League name", "leagueType", NewConstructedLeagueData.class.getName(), sb.toString(), 0);
leagues.add(league);
LeagueSerieData leagueSerie = league.getLeagueData().getSeries().get(0);
Mockito.when(leagueDao.loadActiveLeagues(Mockito.anyInt())).thenReturn(leagues);
LeaguePointsDAO leaguePointsDAO = Mockito.mock(LeaguePointsDAO.class);
LeagueMatchDAO leagueMatchDAO = Mockito.mock(LeagueMatchDAO.class);
Set<LeagueMatch> matches = new HashSet<LeagueMatch>();
matches.add(new LeagueMatch("player1", "player2"));
Mockito.when(leagueMatchDAO.getLeagueMatches(league)).thenReturn(new HashSet<LeagueMatch>(matches));
Mockito.when(leagueMatchDAO.getLeagueSerieMatches(league, leagueSerie)).thenReturn(new HashSet<LeagueMatch>(matches));
LeagueParticipationDAO leagueParticipationDAO = Mockito.mock(LeagueParticipationDAO.class);
CollectionsManager collectionsManager = Mockito.mock(CollectionsManager.class);
LeagueService leagueService = new LeagueService(leagueDao, leaguePointsDAO, leagueMatchDAO, leagueParticipationDAO, collectionsManager);
assertTrue(leagueService.canPlayRankedGame(league, leagueSerie, "player1"));
assertFalse(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player2"));
assertTrue(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player3"));
leagueService.reportLeagueGameResult(league, leagueSerie, "player1", "player3");
Mockito.verify(leagueMatchDAO).getLeagueMatches(league);
Mockito.verify(leagueMatchDAO).getLeagueSerieMatches(league, leagueSerie);
Mockito.verify(leagueMatchDAO).addPlayedMatch(league, leagueSerie, "player1", "player3");
Mockito.verifyNoMoreInteractions(leagueMatchDAO);
assertFalse(leagueService.canPlayRankedGame(league, leagueSerie, "player1"));
assertFalse(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player2"));
assertFalse(leagueService.canPlayRankedGameAgainst(league, leagueSerie, "player1", "player3"));
}
}