Compare commits

...

6 Commits

18 changed files with 92 additions and 1044 deletions

@ -1 +1 @@
Subproject commit adb0a497112f842434b2fe1adf62a5ae5874a929
Subproject commit ddd0f2f953aa29932706a429faa82da58fd62815

View File

@ -2,6 +2,7 @@ plugins {
id 'java'
id 'idea'
id "org.sonarqube" version "3.2.0"
id 'jacoco'
}
repositories {
@ -15,6 +16,70 @@ test {
maxParallelForks = 1
}
// Fancy live test output (from https://stackoverflow.com/questions/3963708/gradle-how-to-display-test-results-in-the-console-in-real-time)
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
tasks.withType(Test) {
testLogging {
// set options for log level LIFECYCLE
events TestLogEvent.FAILED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED
// TestLogEvent.STANDARD_OUT
exceptionFormat TestExceptionFormat.FULL
showExceptions true
showCauses true
showStackTraces true
// set options for log level DEBUG and INFO
debug {
events TestLogEvent.STARTED,
TestLogEvent.FAILED,
TestLogEvent.PASSED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR
TestLogEvent.STANDARD_OUT
exceptionFormat TestExceptionFormat.FULL
}
// info.events = debug.events
// info.exceptionFormat = debug.exceptionFormat
afterSuite { desc, result ->
if (!desc.parent) { // will match the outermost suite
def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
def startItem = '| ', endItem = ' |'
def repeatLength = startItem.length() + output.length() + endItem.length()
println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
}
}
}
}
jacocoTestReport {
reports {
html.enabled true
xml.enabled true
xml.destination file("${buildDir}/reports/jacoco.xml")
}
}
plugins.withType(JacocoPlugin) {
tasks["test"].finalizedBy 'jacocoTestReport'
}
sonarqube {
properties {
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.host.url", "https://sonarqube.yandrik.dev"
property "sonar.coverage.jacoco.xmlReportPath", "${buildDir}/reports/jacoco.xml"
}
}
var mainClassName = "uulm.teamname.marvelous.server.Server"
jar {

View File

@ -1,7 +1,6 @@
package uulm.teamname.marvelous.server.lobby;
import org.tinylog.Logger;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.concurrent.*;
@ -12,13 +11,16 @@ public class TimeoutTimer {
private final ScheduledExecutorService timer;
private ScheduledFuture<Participant> player1AlmostTimeout, player1Timeout;
private ScheduledFuture<Participant> player2AlmostTimeout, player2Timeout;
private ScheduledFuture<Participant> player1AlmostTimeout;
private ScheduledFuture<Participant> player1Timeout;
private ScheduledFuture<Participant> player2AlmostTimeout;
private ScheduledFuture<Participant> player2Timeout;
private final BiConsumer<Participant, Integer> almostTimeoutCallback;
private final Consumer<Participant> timeoutCallback;
private final int almostTimeoutTime, timeoutTime;
private final int almostTimeoutTime;
private final int timeoutTime;
/**
* Class that manages timeouts of players after not sending a message for a long time.

View File

@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobby;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import java.util.concurrent.*;
@ -20,12 +19,7 @@ public class TurnTimeoutTimer {
public TurnTimeoutTimer(int maxRoundTime, Consumer<Participant> callback) {
String lobbyThreadName = Thread.currentThread().getName();
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, lobbyThreadName + "-TurnTimerThread");
}
};
ThreadFactory threadFactory = r -> new Thread(r, lobbyThreadName + "-TurnTimerThread");
this.timer = Executors.newSingleThreadScheduledExecutor(threadFactory);
this.maxRoundTime = maxRoundTime;
this.callback = callback;

View File

@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance;
import uulm.teamname.marvelous.gamelibrary.requests.Request;

View File

@ -5,7 +5,6 @@ import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;

View File

@ -51,7 +51,7 @@ public class Pipeline {
for (Segment segment : segments) {
// Give the segment the packet, carrier and abort, and let it process requests
segment.processRequests(packet, carrier, abort);
if (packet.size() == 0 || abort.get()) { // if packet is empty (all requests processed) or abort initiated
if (packet.isEmpty() || abort.get()) { // if packet is empty (all requests processed) or abort initiated
break; // (abort boolean true), break out of the loop
}
}

View File

@ -3,7 +3,6 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.requests.CharacterRequest;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.server.lobbymanager.Participant;

View File

@ -2,12 +2,9 @@ package uulm.teamname.marvelous.server.lobby.pipelining;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.gamelogic.GameInstance;
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobby.Lobby;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

View File

@ -26,7 +26,9 @@ public class LobbyConnection implements Runnable {
public final String gameID;
public LobbyConnectionState state = LobbyConnectionState.Waiting;
private Participant player1, player2;
private Participant player1;
private Participant player2;
private final HashSet<Participant> spectators = new HashSet<>(10);
private final HashMap<SUID, List<Integer>> selection = new HashMap<>(2);
public final HashMap<ParticipantType, CharacterProperties[]> options = new HashMap<>(2);
@ -59,7 +61,6 @@ public class LobbyConnection implements Runnable {
if (participant.type == ParticipantType.Spectator) {
Logger.trace("Adding spectator");
spectators.add(participant);
return;
} else if (participant.type == ParticipantType.PlayerOne) {
player1 = participant;
} else {

View File

@ -120,7 +120,7 @@ public class LobbyManager {
int n = 0;
for (int i = 0; i < 12; i++) {
if (message.characters[i]) {
if (Boolean.TRUE.equals(message.characters[i])) {
selected[n++] = options[i].characterID;
}
}

View File

@ -2,7 +2,6 @@ package uulm.teamname.marvelous.server.lobbymanager;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.server.lobby.Lobby;
import uulm.teamname.marvelous.server.netconnector.Client;
import uulm.teamname.marvelous.server.netconnector.SUID;
@ -52,4 +51,12 @@ public class Participant {
Participant other = (Participant) o;
return other.id.equals(id);
}
@Override
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + ((client == null) ? 0 : client.hashCode()) + ((id == null) ? 0 : id.hashCode());
return result;
}
}

View File

@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/
public class UserManager {
private static UserManager instance;
private static final String errorInvalidMessage = "Invalid message.";
/**
* @return the current instance of the UserManager
@ -175,7 +176,7 @@ public class UserManager {
return;
}
if(message.reconnect) {
if(Boolean.TRUE.equals(message.reconnect)) {
Logger.trace("Reconnecting to lobby. Forwarding reconnect instruction to the LobbyManager");
if(LobbyManager.getInstance().handleReconnect(client)) {
Logger.trace("Successfully reconnected client, changing state to Playing...");
@ -200,12 +201,12 @@ public class UserManager {
}
Logger.trace("Relaying message to LobbyManager");
if(message.startGame) {
if(Boolean.TRUE.equals(message.startGame)) {
if(LobbyManager.getInstance().handleReady(client, message)) {
client.setState(ClientState.Assigned);
} else {
Logger.trace("Sending error to client as message couldn't be processed properly");
client.sendError("Invalid message.");
client.sendError(errorInvalidMessage);
}
} else {
Logger.debug("Disconnecting client as game couldn't be started");
@ -227,7 +228,7 @@ public class UserManager {
if(LobbyManager.getInstance().handleSelection(client, message)) {
Logger.trace("Handled successfully");
} else {
client.sendError("Invalid message.");
client.sendError(errorInvalidMessage);
}
}
@ -247,7 +248,7 @@ public class UserManager {
//"i approve" - the server
} else {
Logger.debug("Message couldn't be handled, sending error to client");
client.sendError("Invalid message.");
client.sendError(errorInvalidMessage);
}
}

View File

@ -1,110 +0,0 @@
package uulm.teamname.marvelous.server.lobby;
import org.java_websocket.WebSocket;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import uulm.teamname.marvelous.gamelibrary.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.config.FieldType;
import uulm.teamname.marvelous.gamelibrary.config.PartyConfig;
import uulm.teamname.marvelous.gamelibrary.config.ScenarioConfig;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.requests.Request;
import uulm.teamname.marvelous.gamelibrary.requests.RequestBuilder;
import uulm.teamname.marvelous.gamelibrary.requests.RequestType;
import uulm.teamname.marvelous.server.lobbymanager.LobbyConnection;
import uulm.teamname.marvelous.server.lobbymanager.Participant;
import uulm.teamname.marvelous.server.netconnector.Client;
import java.util.List;
import static org.mockito.Mockito.*;
class LobbyTest {
@Mock
Lobby lobby;
LobbyConnection connection;
@BeforeEach
void beforeEach(){
var gameID = "GameID";
connection = mock(LobbyConnection.class);
var partyConfig = mock(PartyConfig.class);
var scenarioConfig = new ScenarioConfig();
scenarioConfig.scenario = new FieldType[][] {
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS},
{FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS, FieldType.GRASS}
};
var characterConfig = mock(CharacterConfig.class);
lobby = spy(new Lobby(
gameID,
connection,
partyConfig,
characterConfig,
scenarioConfig,
List.of(1, 2, 3, 4, 5, 6),
List.of(7, 8, 9, 10, 11, 12)
));
}
@Test
@Disabled
void receiveRequestsTest(){
var requests = new Request[] {
new RequestBuilder(RequestType.Req).buildGameRequest(),
new RequestBuilder(RequestType.MoveRequest).buildGameRequest()
};
var playerConnection = mock(WebSocket.class);
doNothing().when(lobby).updateTurnTimer();
Participant playerOne = new Participant(
new Client(playerConnection), "LobbyOne", ParticipantType.PlayerOne, false);
lobby.receiveRequests(requests, playerOne);
}
@Test
@Disabled
void soonTimeoutTest(){
var participant = mock(Participant.class);
lobby.soonTimeout(participant, 15);
verify(connection).sendEvents(participant, new EventBuilder(EventType.TimeoutWarningEvent).buildGameStateEvent());
}
@Test
@Disabled
void generateWinPlayer1Test(){
var webSoc = mock(WebSocket.class);
Participant winner = new Participant(new Client(webSoc), "someLobby", ParticipantType.PlayerOne, false);
lobby.triggerWin(winner);
verify(connection).broadcastEvents(
new EventBuilder(EventType.WinEvent)
.withPlayerWon(1)
.buildGameStateEvent(),
new EventBuilder(EventType.DisconnectEvent)
.buildGameStateEvent());
verify(connection).terminate();
}
@Test
@Disabled
void generateWinPlayer2Test(){
var webSoc = mock(WebSocket.class);
Participant winner = new Participant(new Client(webSoc), "someLobby", ParticipantType.PlayerOne, false);
lobby.triggerWin(winner);
verify(connection).broadcastEvents(
new EventBuilder(EventType.WinEvent)
.withPlayerWon(2)
.buildGameStateEvent(),
new EventBuilder(EventType.DisconnectEvent)
.buildGameStateEvent());
verify(connection).terminate();
}
}

View File

@ -1,294 +0,0 @@
package uulm.teamname.marvelous.server.lobbymanager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.MockedStatic;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventBuilder;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import org.java_websocket.WebSocket;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.server.EventMessage;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.function.BiConsumer;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
class LobbyConnectionTest {
@BeforeEach
void setUp() {
}
BiConsumer<WebSocket, BasicMessage> sendMessageCallback;
BiConsumer<WebSocket, String> sendErrorCallback;
WebSocket player1, player2, spectator;
Participant player1Participant, player2Participant, spectatorParticipant;
LobbyConnection connection;
@BeforeEach
void beforeEach() {
sendMessageCallback = mock(BiConsumer.class);
sendErrorCallback = mock(BiConsumer.class);
// player1 = mock(WebSocket.class);
// player2 = mock(WebSocket.class);
// spectator = mock(WebSocket.class);
//
// player1Participant = new Participant(player1, ParticipantType.PlayerOne, "Player1");
// player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "Player2");
// spectatorParticipant = new Participant(spectator, ParticipantType.Spectator, "Spectator");
//
// connection = new LobbyConnection("/AwesomeGame", sendMessageCallback, sendErrorCallback);
}
/*
@Test
void lobbyIsFullTest() {
assertThat(connection.isFull()).isFalse();
connection.addPlayer1(player1Participant);
assertThat(connection.isFull()).isFalse();
connection.addPlayer2(player2Participant);
assertThat(connection.isFull()).isTrue();
}
@Test
void lobbyIsFullTestTwo() {
assertThat(connection.isFull()).isFalse();
connection.addPlayer2(player2Participant);
assertThat(connection.isFull()).isFalse();
connection.addSpectator(spectatorParticipant);
assertThat(connection.isFull()).isFalse();
connection.addPlayer1(player1Participant);
assertThat(connection.isFull()).isTrue();
}
@Test
void addPlayerOneTest(){
var websocket = mock(WebSocket.class);
Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne");
assertThat(connection.addPlayer1(playerOne)).isTrue();
assertThat(connection.hasPlayer1()).isTrue();
assertThat(connection.addPlayer1(playerOne)).isFalse();
}
@Test
void addPlayerTwoTest(){
var websocket = mock(WebSocket.class);
Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo");
assertThat(connection.addPlayer2(playerTwo)).isTrue();
assertThat(connection.hasPlayer2()).isTrue();
assertThat(connection.addPlayer2(playerTwo)).isFalse();
}
@Test
void addPlayerTest(){
var websocket = mock(WebSocket.class);
Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne");
Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo");
Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator");
assertThat(connection.addPlayer(playerOne)).isTrue();
assertThat(connection.addPlayer(playerTwo)).isTrue();
assertThat(connection.addPlayer(spectator)).isFalse();
assertThat(connection.contains(playerOne)).isTrue();
assertThat(connection.contains(playerTwo)).isTrue();
assertThat(connection.contains(spectator)).isFalse();
}
@Test
void addSpectatorTest(){
var websocket = mock(WebSocket.class);
Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne");
Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo");
Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator");
assertThat(connection.addSpectator(playerOne)).isFalse();
assertThat(connection.addSpectator(playerTwo)).isFalse();
assertThat(connection.addSpectator(spectator)).isTrue();
assertThat(connection.contains(playerOne)).isFalse();
assertThat(connection.contains(playerTwo)).isFalse();
assertThat(connection.contains(spectator)).isTrue();
}
@Test
void addOneSpectatorAndOnePlayer(){
var websocket = mock(WebSocket.class);
Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne");
Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator");
assertThat(connection.addPlayer(playerOne)).isTrue();
assertThat(connection.addSpectator(spectator)).isTrue();
assertThat(connection.contains(playerOne)).isTrue();
assertThat(connection.contains(spectator)).isTrue();
}
@Test
void removeParticipantTest(){
var websocket = mock(WebSocket.class);
Participant playerOne = new Participant(websocket, ParticipantType.PlayerOne, "PlayerOne");
Participant playerTwo = new Participant(websocket, ParticipantType.PlayerTwo, "PlayerTwo");
Participant spectator = new Participant(websocket, ParticipantType.Spectator,"Spectator");
connection.addPlayer(playerOne);
connection.addPlayer(playerTwo);
connection.addSpectator(spectator);
assertThat(connection.removeParticipant(spectator)).isTrue();
assertThat(connection.removeParticipant(playerOne)).isTrue();
assertThat(connection.removeParticipant(playerOne)).isFalse();
assertThat(connection.removeParticipant(playerTwo)).isTrue();
}
@Test
void sendMessageTest(){
var webSocket = mock(WebSocket.class);
var participant = new Participant(webSocket, ParticipantType.PlayerOne, "playerOne");
var event = new Event[]{
new EventBuilder(EventType.PauseStartEvent).buildGameEvent(),
new EventBuilder(EventType.MoveEvent).buildGameEvent()
};
var message = new EventMessage();
message.messages = event;
connection.sendEvents(participant, event);
verify(sendMessageCallback).accept(participant.getConnection(), message);
}
@Test
void broadcastEventTest(){
var player1 = mock(WebSocket.class);
var player2 = mock(WebSocket.class);
var spectator1 = mock(WebSocket.class);
var spectator2 = mock(WebSocket.class);
Participant playerOne = new Participant(player1, ParticipantType.PlayerOne, "playerOne");
Participant playerTwo = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo");
Participant spectatorOne = new Participant(spectator1, ParticipantType.Spectator, "spectatorOne");
Participant spectatorTwo = new Participant(spectator2, ParticipantType.Spectator, "spectatorTwo");
connection.addPlayer(playerOne);
connection.addPlayer(playerTwo);
connection.addSpectator(spectatorOne);
connection.addSpectator(spectatorTwo);
var events = new Event[]{
new EventBuilder(EventType.PauseStartEvent).buildGameEvent(),
new EventBuilder(EventType.MoveEvent).buildGameEvent()
};
var message = new EventMessage();
message.messages = events;
connection.broadcastEvents(events);
verify(sendMessageCallback).accept(playerOne.getConnection(), message);
verify(sendMessageCallback).accept(playerTwo.getConnection(), message);
verify(sendMessageCallback).accept(spectatorOne.getConnection(), message);
verify(sendMessageCallback).accept(spectatorTwo.getConnection(), message);
}
@Test
void broadcastEventsListToArrayTest(){
var player1 = mock(WebSocket.class);
var player2 = mock(WebSocket.class);
var spectatorSocket = mock(WebSocket.class);
Participant playerOne = new Participant(player1, ParticipantType.PlayerOne, "playerOne");
Participant playerTwo = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo");
Participant spectator = new Participant(spectatorSocket, ParticipantType.Spectator, "spectator");
connection.addPlayer(playerOne);
connection.addPlayer(playerTwo);
connection.addSpectator(spectator);
var events = new ArrayList<Event>();
events.add(new EventBuilder(EventType.PauseStartEvent).buildGameEvent());
events.add(new EventBuilder(EventType.MoveEvent).buildGameEvent());
connection.broadcastEvents(events);
var message = new EventMessage();
message.messages = events.toArray(new Event[0]);
verify(sendMessageCallback).accept(playerOne.getConnection(), message);
verify(sendMessageCallback).accept(playerTwo.getConnection(), message);
verify(sendMessageCallback).accept(spectator.getConnection(), message);
}
@Test
void hashCodeTest() {
var hashSet = new HashSet<LobbyConnection>();
LobbyConnection connection = new LobbyConnection("yay", null, null);
hashSet.add(connection);
assertThat(hashSet).containsOnly(connection);
connection.addPlayer1(mock(Participant.class));
assertThat(hashSet).containsOnly(connection);
var spectator = new Participant(null, ParticipantType.Spectator, "SomeSpectator");
connection.addSpectator(spectator);
assertThat(hashSet).containsOnly(connection);
}
@Test
void broadcastToAllExceptTest(){
var player1 = mock(WebSocket.class);
var player2 = mock(WebSocket.class);
var spectator1 = mock(WebSocket.class);
var spectator2 = mock(WebSocket.class);
Participant playerOne = new Participant(player1, ParticipantType.PlayerOne, "playerOne");
Participant playerTwo = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo");
Participant spectatorOne = new Participant(spectator1, ParticipantType.Spectator, "spectatorOne");
Participant spectatorTwo = new Participant(spectator2, ParticipantType.Spectator, "spectatorTwo");
connection.addPlayer(playerOne);
connection.addPlayer(playerTwo);
connection.addSpectator(spectatorOne);
connection.addSpectator(spectatorTwo);
var events = new Event[]{
new EventBuilder(EventType.PauseStartEvent).buildGameEvent(),
new EventBuilder(EventType.MoveEvent).buildGameEvent()
};
var message = new EventMessage();
message.messages = events;
connection.broadcastToAllExcept(playerTwo, events);
verify(sendMessageCallback).accept(playerOne.getConnection(), message);
//playerTwo is the only one who doesn't receive the message
verify(sendMessageCallback, never()).accept(playerTwo.getConnection(), message);
verify(sendMessageCallback).accept(spectatorOne.getConnection(), message);
verify(sendMessageCallback).accept(spectatorTwo.getConnection(), message);
}
@Test
void terminateTest(){
assertThat(connection.isActive()).isFalse();
connection.terminateConnection();
assertThat(connection.isActive()).isTrue();
} */
}

View File

@ -1,317 +0,0 @@
package uulm.teamname.marvelous.server.lobbymanager;
import org.java_websocket.WebSocket;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.gamelibrary.messages.RoleEnum;
import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage;
import uulm.teamname.marvelous.server.Server;
import uulm.teamname.marvelous.server.lobby.Lobby;
import uulm.teamname.marvelous.server.netconnector.UserManager;
import java.lang.reflect.InvocationTargetException;
import java.util.function.BiConsumer;
import static org.mockito.Mockito.*;
import static org.assertj.core.api.Assertions.*;
class LobbyManagerTest {
/*
BiConsumer<WebSocket, BasicMessage> onMessageCallback;
BiConsumer<WebSocket, String> onErrorCallback;
LobbyManager manager;
WebSocket player1;
WebSocket player2;
WebSocket player3;
WebSocket spectator;
PlayerReadyMessage playerReady;
PlayerReadyMessage spectatorReady;
PlayerReadyMessage aiReady;
Participant player1Participant;
Participant player2Participant;
Participant player3Participant;
LobbyRunner lobbyRunner;
MockedStatic<LobbyRunner> lobbyRunnerMockedStatic;
MockedStatic<Server> serverMockedStatic;
@BeforeEach
void beforeEach() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
onMessageCallback = mock(BiConsumer.class);
onErrorCallback = mock(BiConsumer.class);
manager = new LobbyManager(onMessageCallback, onErrorCallback);
player1 = mock(WebSocket.class);
player2 = mock(WebSocket.class);
player3 = mock(WebSocket.class);
spectator = mock(WebSocket.class);
playerReady = new PlayerReadyMessage();
playerReady.role = RoleEnum.PLAYER;
spectatorReady = new PlayerReadyMessage();
spectatorReady.role = RoleEnum.SPECTATOR;
aiReady = new PlayerReadyMessage();
aiReady.role = RoleEnum.KI;
player1Participant = new Participant(
player1,
ParticipantType.PlayerOne,
"AwesomePlayer");
player2Participant = new Participant(
player2,
ParticipantType.PlayerTwo,
"MoreAwesomePlayer");
player3Participant = new Participant(
player3,
ParticipantType.PlayerOne,
"AwesomestAwesomePlayer");
var constructor = LobbyRunner.class.getDeclaredConstructor();
constructor.setAccessible(true);
lobbyRunner = spy(constructor.newInstance());
doNothing().when(lobbyRunner).startLobby(any(LobbyConnection.class));
serverMockedStatic = Mockito.mockStatic(Server.class);
serverMockedStatic.when(Server::getMaxLobbies).thenReturn(2);
lobbyRunnerMockedStatic = Mockito.mockStatic(LobbyRunner.class);
lobbyRunnerMockedStatic.when(LobbyRunner::getInstance).thenReturn(lobbyRunner);
}
@AfterEach
void afterEach() {
lobbyRunnerMockedStatic.close();
serverMockedStatic.close();
}
@Test
void lobbyManagerGetsCreatedEmpty() {
assertThat(manager.getLobbies()).isEmpty();
}
@Test
@DisplayName("When a participant is added, a new Lobby is created")
void lobbyCreationTest() {
var message = new PlayerReadyMessage();
message.role = RoleEnum.PLAYER;
assertThat(manager.getLobbies()).isEmpty();
when(player1.getResourceDescriptor()).thenReturn("/ResourcesFTW");
manager.assignLobbyToConnection(player1, "AwesomePlayer", message);
Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "AwesomePlayer");
assertThat(manager.getLobbies()).containsOnlyKeys(player1Participant);
assertThat(manager.getLobbies().get(player1Participant)).isNotNull();
assertThat(manager.getLobbies().get(player1Participant).getPlayer1()).isEqualTo(player1Participant);
assertThat(manager.getResourceDescriptorToLobby()).containsOnlyKeys("/ResourcesFTW");
assertThat(manager.getResourceDescriptorToLobby().get("/ResourcesFTW"))
.isEqualTo(manager.getLobbies().get(player1Participant))
.isNotNull();
}
@Test
@DisplayName("when two participants with same ResourceDescriptor connect, they get assigned to the same lobby")
void twoParticipantsSameLobbyTest() {
when(player1.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
when(player2.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
Participant player1Participant = new Participant(
player1, ParticipantType.PlayerOne, "AwesomePlayer");
Participant player2Participant = new Participant(
player2, ParticipantType.PlayerTwo, "MoreAwesomePlayer");
assertThat(manager.getLobbies()).containsOnlyKeys(player1Participant, player2Participant);
assertThat(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotNull();
assertThat(manager.getResourceDescriptorToLobby()).containsOnlyKeys("/fancyResourceDescriptor");
assertThat(manager.getResourceDescriptorToLobby().get("/fancyResourceDescriptor"))
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotNull();
}
@Test
@DisplayName("when three participants with same ResourceDescriptor connect, the third gets a new, random lobby")
void threeParticipantsRandomLobbyTest() {
when(player1.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
when(player2.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
when(player3.getResourceDescriptor()).thenReturn("/fancyResourceDescriptor");
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
manager.assignLobbyToConnection(player3, "AwesomestAwesomePlayer", playerReady);
assertThat(manager.getLobbies()).containsOnlyKeys(
player1Participant,
player2Participant,
player3Participant);
assertThat(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotEqualTo(manager.getLobbies().get(player3Participant))
.isNotNull();
assertThat(manager.getResourceDescriptorToLobby()).containsKey("/fancyResourceDescriptor");
assertThat(manager.getResourceDescriptorToLobby().get("/fancyResourceDescriptor"))
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotNull();
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotEqualTo(manager.getLobbies().get(player3Participant));
System.out.println(manager.getLobbies().get(player3Participant));
System.out.println(manager.getResourceDescriptorToLobby());
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player3Participant).gameID))
.isNotNull()
.isNotEqualTo(manager.getLobbies().get(player1Participant))
.isNotEqualTo(manager.getLobbies().get(player2Participant))
.isEqualTo(manager.getLobbies().get(player3Participant));
}
@Test
@DisplayName("On Participant without ResourceDescriptor random lobby gets generated")
void randomLobbyTest() {
when(player1.getResourceDescriptor()).thenReturn(null);
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
assertThat(manager.getLobbies()).hasSize(1);
assertThat(manager.getLobbies().get(player1Participant)).isNotNull();
assertThat(manager.getLobbies().get(player1Participant).gameID).isNotNull();
assertThat(manager.getResourceDescriptorToLobby()).hasSize(1);
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player1Participant));
}
@Test
@DisplayName("When two people without ResourceDescriptor join, they get allocated to the same lobby")
void randomLobbyTwoPlayerTest() {
when(player1.getResourceDescriptor()).thenReturn(null);
when(player2.getResourceDescriptor()).thenReturn(null);
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
assertThat(manager.getLobbies()).hasSize(2);
assertThat(manager.getLobbies().get(player1Participant))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player2Participant));
assertThat(manager.getResourceDescriptorToLobby()).hasSize(1);
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant));
}
@Test
@DisplayName("When three players connect, a new lobby gets created")
void randomLobbyThreePlayerTest() {
when(player1.getResourceDescriptor()).thenReturn(null);
when(player2.getResourceDescriptor()).thenReturn(null);
when(player3.getResourceDescriptor()).thenReturn(null);
manager.assignLobbyToConnection(player1, "AwesomePlayer", playerReady);
manager.assignLobbyToConnection(player2, "MoreAwesomePlayer", playerReady);
manager.assignLobbyToConnection(player3, "AwesomestAwesomePlayer", playerReady);
assertThat(manager.getLobbies()).hasSize(3);
assertThat(manager.getLobbies().get(player1Participant))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotEqualTo(manager.getLobbies().get(player3Participant));
assertThat(manager.getLobbies().get(player3Participant)).isNotNull();
assertThat(manager.getResourceDescriptorToLobby()).hasSize(2);
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isNotEqualTo(manager.getLobbies().get(player3Participant));
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player3Participant).gameID))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player3Participant));
}
@Test
@DisplayName("Spectator joins full lobby")
void spactatorsJoinLobby() {
when(player1.getResourceDescriptor()).thenReturn("/awesomeLobby");
when(player2.getResourceDescriptor()).thenReturn("/awesomeLobby");
when(spectator.getResourceDescriptor()).thenReturn("/awesomeLobby");
Participant player1Participant = new Participant(player1, ParticipantType.PlayerOne, "playerOne");
Participant player2Participant = new Participant(player2, ParticipantType.PlayerTwo, "playerTwo");
Participant spectator1 = new Participant(spectator, ParticipantType.Spectator, "spectator1");
manager.assignLobbyToConnection(player1, "playerOne", playerReady);
verify(lobbyRunner, never()).startLobby(any(LobbyConnection.class));
manager.assignLobbyToConnection(player2, "playerTwo", playerReady);
verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
when(lobbyRunner.isStarted(any(LobbyConnection.class))).thenReturn(true);
manager.assignLobbyToConnection(spectator, "spectator1", spectatorReady);
verify(lobbyRunner, times(1)).startLobby(any(LobbyConnection.class));
// manager.assignLobbyToConnection(spectator, "spectator2", spectatorReady);
assertThat(manager.getResourceDescriptorToLobby()).hasSize(1);
assertThat(manager.getLobbies()).containsOnlyKeys(
player1Participant,
player2Participant,
spectator1);
assertThat(manager.getLobbies().get(player1Participant))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player2Participant))
.isEqualTo(manager.getLobbies().get(spectator1));
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isNotNull()
.isEqualTo(manager.getLobbies().get(player1Participant))
.isEqualTo(manager.getLobbies().get(player2Participant))
.isEqualTo(manager.getLobbies().get(spectator1));
assertThat(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player1Participant).gameID))
.isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(player2Participant).gameID))
.isEqualTo(manager.getResourceDescriptorToLobby().get(manager.getLobbies().get(spectator1).gameID));
} */
}

View File

@ -1,88 +0,0 @@
package uulm.teamname.marvelous.server.lobbymanager;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import uulm.teamname.marvelous.gamelibrary.messages.ParticipantType;
import uulm.teamname.marvelous.server.Server;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.*;
class LobbyRunnerTest {
/*
LobbyConnection connection;
LobbyRunner lobbyRunner;
MockedStatic<Server> serverMockedStatic;
@BeforeEach
void setUp()
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
connection = mock(LobbyConnection.class);
// when(connection.gameID).thenReturn("/SomeGameID");
var constructor = LobbyRunner.class.getDeclaredConstructor();
constructor.setAccessible(true);
lobbyRunner = constructor.newInstance();
serverMockedStatic = mockStatic(Server.class);
serverMockedStatic.when(Server::getMaxLobbies).thenReturn(1);
}
@AfterEach
void tearDown() {
serverMockedStatic.close();
}
// It seems very impractical indeed to properly to check if another thread executed this, so I won't
@Test
void lobbyGetsStartedTest() throws InterruptedException {
lobbyRunner.startLobby(connection);
Thread.sleep(10);
verify(connection).run();
}
@Test
void canAddLobbyTest() throws InterruptedException {
assertThat(lobbyRunner.canAddLobby()).isTrue();
lobbyRunner.startLobby(connection);
assertThat(lobbyRunner.canAddLobby()).isFalse();
}
@Test
void removeLobbyRemovesLobbyAndSetsTerminationFlag() {
lobbyRunner.startLobby(connection);
}
@Test
void lobbyIsStartedTest() {
connection = spy(new LobbyConnection("SomeGameID", null, null));
doNothing().when(connection).run();
assertThat(lobbyRunner.isStarted(connection)).isFalse();
lobbyRunner.startLobby(connection);
assertThat(lobbyRunner.isStarted(connection)).isTrue();
connection.addSpectator(new Participant(null, ParticipantType.Spectator, "Hi"));
connection.addSpectator(new Participant(null, ParticipantType.Spectator, "Hi2"));
connection.removeParticipant(new Participant(null, ParticipantType.Spectator, "Hi"));
assertThat(lobbyRunner.isStarted(connection)).isTrue();
lobbyRunner.removeLobby(connection);
assertThat(lobbyRunner.isStarted(connection)).isFalse();
}
*/
}

View File

@ -60,211 +60,4 @@ class UserManagerTest {
// TODO: test this
}
// @Test
// void handshakeGetsCompletedProperlyTest() {
// // manager.connectUser(connection, handshake);
//
// manager.getNewUsers().add(connection);
// // assertThat(manager.getUserCount()).isEqualTo(1);
// assertThat(manager.getNewUsers()).containsOnly(connection);
// assertThat(manager.getReadyToConnect()).isEmpty();
//
// var message = new HelloServerMessage();
// message.name = "Some Awesome Name";
// message.deviceID = "Some Interesting ID";
//
// manager.handshake(connection, message);
//
// assertThat(manager.getUserCount()).isEqualTo(1);
// assertThat(manager.getNewUsers()).isEmpty();
// assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection);
// }
//
// @Test
// void reconnectMessagesGetAssignedProperly() {
// manager.messageReceived(
// connection,
// "{\"messageType\":\"RECONNECT\",\"reconnect\":\"TRUE\"}");
//
// verify(manager).reconnectClient(
// eq(connection),
// any(ReconnectMessage.class));
// }
//
// @Test
// void reconnectWorksProperly() {
// manager.getActiveParticipants().put(clientID, participant);
// manager.getReadyToReconnect().put(connection, clientID);
//
// assertThat(manager.getInGame()).isEmpty();
//
// var message = new ReconnectMessage();
// message.reconnect = true;
//
// manager.reconnectClient(connection, message);
//
// assertThat(manager.getReadyToReconnect()).isEmpty();
//
// assertThat(manager.getInGame()).containsOnlyKeys(connection);
// assertThat(manager.getInGame().get(connection)).isEqualTo(participant);
//
// // verify(participant).setConnection(connection);
// }
/*
UserManager manager;
WebSocket connection;
SUID clientID;
Participant participant;
ClientHandshake handshake;
@BeforeEach
void beforeEach()
throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
connection = mock(WebSocket.class);
var constructor = UserManager.class.getDeclaredConstructor();
constructor.setAccessible(true);
manager = spy(constructor.newInstance());
handshake = mock(ClientHandshake.class);
when(handshake.getResourceDescriptor()).thenReturn("/someDescriptor");
clientID = new SUID("Awesome Name", "Awesome deviceID");
participant = spy(new Participant(connection, ParticipantType.PlayerOne, "Awesome Name"));
}
@Test
void userIsConnectedTest() {
assertThat(manager.getUserCount()).isEqualTo(0);
assertThat(manager.getNewUsers()).doesNotContain(connection);
manager.connectUser(connection);
assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.isUserConnected(connection)).isTrue();
assertThat(manager.getNewUsers()).containsOnly(connection);
}
@Test
void helloServerMessagesGetAssignedProperly() {
manager.messageReceived(
connection,
"{\"messageType\":\"HELLO_SERVER\",\"name\":\"SomeAwesomeName\",\"deviceID\":\"YAY\"}");
verify(manager).handshake(
eq(connection),
any(HelloServerMessage.class));
}
@Test
void handshakeGetsCompletedProperlyTest() {
// manager.connectUser(connection, handshake);
manager.getNewUsers().add(connection);
// assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.getNewUsers()).containsOnly(connection);
assertThat(manager.getReadyToConnect()).isEmpty();
var message = new HelloServerMessage();
message.name = "Some Awesome Name";
message.deviceID = "Some Interesting ID";
manager.handshake(connection, message);
assertThat(manager.getUserCount()).isEqualTo(1);
assertThat(manager.getNewUsers()).isEmpty();
assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection);
}
@Test
void reconnectMessagesGetAssignedProperly() {
manager.messageReceived(
connection,
"{\"messageType\":\"RECONNECT\",\"reconnect\":\"TRUE\"}");
verify(manager).reconnectClient(
eq(connection),
any(ReconnectMessage.class));
}
@Test
void reconnectWorksProperly() {
manager.getActiveParticipants().put(clientID, participant);
manager.getReadyToReconnect().put(connection, clientID);
assertThat(manager.getInGame()).isEmpty();
var message = new ReconnectMessage();
message.reconnect = true;
manager.reconnectClient(connection, message);
assertThat(manager.getReadyToReconnect()).isEmpty();
assertThat(manager.getInGame()).containsOnlyKeys(connection);
assertThat(manager.getInGame().get(connection)).isEqualTo(participant);
// verify(participant).setConnection(connection); // TODO: fix this
}
@Test
void notReconnectTransfersPlayerToReadyToConnect() {
manager.getReadyToReconnect().put(connection, clientID);
assertThat(manager.getReadyToReconnect()).containsOnlyKeys(connection);
assertThat(manager.getReadyToConnect()).isEmpty();
assertThat(manager.getInGame()).isEmpty();
var message = new ReconnectMessage();
message.reconnect = false;
manager.reconnectClient(connection, message);
assertThat(manager.getReadyToReconnect()).isEmpty();
assertThat(manager.getReadyToConnect()).containsOnlyKeys(connection);
assertThat(manager.getReadyToConnect().get(connection)).isEqualTo(clientID);
assertThat(manager.getInGame()).isEmpty();
}
@Test
void playerReadyMessagesGetAssignedProperly() {
manager.messageReceived(
connection,
"{\"messageType\":\"PLAYER_READY\",\"startGame\":\"false\",\"role\":\"PLAYER\"}");
verify(manager).playerReady(
eq(connection),
any(PlayerReadyMessage.class));
}
@Test
void characterSelectionMessageGetsAssignedProperly() {
manager.messageReceived(
connection,
"{\"messageType\":\"CHARACTER_SELECTION\",\"characters\":" +
"[true, true, false, true, false, false, true, true, false, false, false, true]}");
verify(manager).charactersSelected(
eq(connection),
any(CharacterSelectionMessage.class));
}
@Test
void requestMessagesGetRelayedProperly() {
manager.messageReceived(
connection,
"{\"messageType\":\"REQUESTS\",\"messages\":[{\"requestType\":\"EndRoundRequest\"}]}");
verify(manager).relayRequestMessage(
eq(connection),
any(RequestMessage.class));
} */
}