Skip to content

Commit

Permalink
Added J unit tests for MainLobby and SubLobby
Browse files Browse the repository at this point in the history
  • Loading branch information
Vmai711 authored Apr 30, 2024
1 parent dbfc9f7 commit 90e35de
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/test/java/uta/cse3310/MainLobbyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package uta.cse3310;

import junit.framework.TestCase;

import org.java_websocket.WebSocket;
import org.mockito.Mockito;

public class MainLobbyTest extends TestCase {
// Valid username
String usernamegood = "Bobby2";
// Empty username
String usernameMT = "";
// Non-existing username
String usernameNE = "John";

// Testing LogIn()
public void testLogIn() {
MainLobby mainLobby = new MainLobby();
WebSocket mockWebSocket = Mockito.mock(WebSocket.class);

// Logging in with a valid username
boolean namecheck = mainLobby.logIn(mockWebSocket, usernamegood);
assertTrue(namecheck);

// Logging in with an empty username
boolean MTcheck = mainLobby.logIn(mockWebSocket, usernameMT);
assertFalse(MTcheck);

// Logging in with a duplicate username
boolean namecheckcpy = mainLobby.logIn(mockWebSocket, usernamegood);
assertFalse(namecheckcpy);
}

// Testing LogOff()
public void testLogOff() {
MainLobby mainLobby = new MainLobby();
WebSocket mockWebSocket = Mockito.mock(WebSocket.class);

// Logging off with an existing WebSocket
mainLobby.logIn(mockWebSocket, usernamegood);
assertTrue(mainLobby.logOff(mockWebSocket));

// Logging off with a non-existing WebSocket
assertFalse(mainLobby.logOff(mockWebSocket));
}

// Testing ResetUser()
public void testResetUser() {
MainLobby mainLobby = new MainLobby();
WebSocket mockWebSocket = Mockito.mock(WebSocket.class);

// Resetting an existing user
mainLobby.logIn(mockWebSocket, usernamegood);
assertTrue(mainLobby.resetUser(usernamegood));

// Resetting a non-existing user
assertFalse(mainLobby.resetUser(usernameNE));
}

// Testing findPlayerInMainLobby()
public void testFindPlayerInMainLobby() {
MainLobby mainLobby = new MainLobby();
WebSocket mockWebSocket = Mockito.mock(WebSocket.class);

// Finding a player with an existing WebSocket
mainLobby.logIn(mockWebSocket, usernamegood);
assertNotNull(mainLobby.findPlayerInMainLobby(mockWebSocket));

// Finding a player with a non-existing WebSocket
assertNull(mainLobby.findPlayerInMainLobby(Mockito.mock(WebSocket.class)));
}

// Testing FindPlayerWebSocket()
public void testFindPlayerWebSocket() {
MainLobby mainLobby = new MainLobby();
WebSocket mockWebSocket = Mockito.mock(WebSocket.class);

// Finding a WebSocket with an existing username
mainLobby.logIn(mockWebSocket, usernamegood);
assertNotNull(mainLobby.findPlayerWebSocket(usernamegood));

// Finding a WebSocket with a non-existing username
assertNull(mainLobby.findPlayerWebSocket(usernameNE));
}
}
84 changes: 84 additions & 0 deletions src/test/java/uta/cse3310/SubLobbyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package uta.cse3310;

import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;


public class SubLobbyTest extends TestCase {

private SubLobby subLobby;
private Player player1;
private Player player2;

// Setting up Test variables
public void setUp() {
player1 = new Player("Alice", null);
player2 = new Player("Bob", null);
subLobby = new SubLobby(2, player1);
}

// Testing if a player is added
public void testAddPlayer() {
subLobby.addPlayer(player2);
List<Player> players = subLobby.getPlayers();
assertTrue(players.contains(player2));
}

// Testing if a player is removed
public void testRemovePlayer() {
subLobby.addPlayer(player2);
subLobby.removePlayer(player2);
List<Player> players = subLobby.getPlayers();
assertFalse(players.contains(player2));
}

// Test checking if SubLobby is full
public void testIsSubLobbyFull() {
assertFalse(subLobby.isSubLobbyFull());
subLobby.addPlayer(player2);
assertTrue(subLobby.isSubLobbyFull());
}

public void testCreateOrJoinSubLobby() {
Vector<SubLobby> activeGames = new Vector<>();

// Test if player is added to new sublobby
SubLobby newSubLobby = SubLobby.createOrJoinSubLobby(2, activeGames, player2);
assertNotNull(newSubLobby);
assertEquals(1, activeGames.size());
assertEquals(1, activeGames.get(0).getPlayers().size());

// Test if player is added to an existing sublobby
SubLobby existingSubLobby = SubLobby.createOrJoinSubLobby(2, activeGames, player1);
assertNotNull(existingSubLobby);
assertEquals(1, activeGames.size());
assertEquals(2, activeGames.get(0).getPlayers().size());

// Test if max concurrent games limit is reached
for (int i = 1; i < SubLobby.MAX_ACTIVE_GAMES; i++) {
activeGames.add(new SubLobby(2, new Player("Player" + i, null)));
}
SubLobby fullSubLobby = SubLobby.createOrJoinSubLobby(2, activeGames, new Player("Player", null));
assertNotNull(fullSubLobby);
assertEquals(SubLobby.MAX_ACTIVE_GAMES, activeGames.size());
}

// Testing if names can be read
public void testGetPlayerNames() {
subLobby.addPlayer(player2);
List<String> expectedNames = new ArrayList<>();
expectedNames.add("Alice");
expectedNames.add("Bob");
assertEquals(expectedNames, subLobby.getPlayerNames());
}

// Testing if all players can ready up
public void testAllPlayersReady() {
assertFalse(subLobby.allPlayersReady());
player1.setReady(true);
assertTrue(subLobby.allPlayersReady());
}
}

0 comments on commit 90e35de

Please sign in to comment.