fix: improve server test coverage and functionality
This commit is contained in:
parent
bf3bd7d0a4
commit
1a8566895b
@ -4,13 +4,16 @@ import org.java_websocket.WebSocket;
|
|||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
import org.mockito.MockedStatic;
|
import org.mockito.MockedStatic;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
import org.mockito.invocation.Invocation;
|
||||||
import org.tinylog.configuration.Configuration;
|
import org.tinylog.configuration.Configuration;
|
||||||
import uulm.teamname.marvelous.gamelibrary.json.JSON;
|
import uulm.teamname.marvelous.gamelibrary.json.JSON;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.BasicMessage;
|
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.RoleEnum;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage;
|
import uulm.teamname.marvelous.gamelibrary.messages.client.CharacterSelectionMessage;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.client.HelloServerMessage;
|
import uulm.teamname.marvelous.gamelibrary.messages.client.HelloServerMessage;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage;
|
import uulm.teamname.marvelous.gamelibrary.messages.client.PlayerReadyMessage;
|
||||||
|
import uulm.teamname.marvelous.gamelibrary.messages.client.ReconnectMessage;
|
||||||
import uulm.teamname.marvelous.gamelibrary.messages.server.*;
|
import uulm.teamname.marvelous.gamelibrary.messages.server.*;
|
||||||
import uulm.teamname.marvelous.server.lobbymanager.LobbyConnection;
|
import uulm.teamname.marvelous.server.lobbymanager.LobbyConnection;
|
||||||
import uulm.teamname.marvelous.server.lobbymanager.LobbyManager;
|
import uulm.teamname.marvelous.server.lobbymanager.LobbyManager;
|
||||||
@ -66,7 +69,6 @@ class MarvelousServerTest extends BaseGameLogicTest {
|
|||||||
// you should also understand the server, which serves as a good intro to it. You can - of course - also
|
// you should also understand the server, which serves as a good intro to it. You can - of course - also
|
||||||
// just ignore it.
|
// just ignore it.
|
||||||
@Test
|
@Test
|
||||||
@Disabled("Work in progress complete server test")
|
|
||||||
void main() {
|
void main() {
|
||||||
UserManager m = UserManager.getInstance();
|
UserManager m = UserManager.getInstance();
|
||||||
|
|
||||||
@ -96,15 +98,44 @@ class MarvelousServerTest extends BaseGameLogicTest {
|
|||||||
ensurePlayerReady(m, p2, true, RoleEnum.PLAYER);
|
ensurePlayerReady(m, p2, true, RoleEnum.PLAYER);
|
||||||
ensureSpectatorReady(m, s1, true, RoleEnum.SPECTATOR);
|
ensureSpectatorReady(m, s1, true, RoleEnum.SPECTATOR);
|
||||||
|
|
||||||
//these are broken right now because Server doesn't get mocked in lobby threads
|
ensureCharacterSelection(m, p1, true);
|
||||||
ensureCharacterSelection(m, p1, false);
|
ensureCharacterSelection(m, p2, false);
|
||||||
ensureCharacterSelection(m, p2, true);
|
|
||||||
|
|
||||||
|
GameStructureMessage game = new GameStructureMessage();
|
||||||
|
game.playerOneName = "Player 1";
|
||||||
|
game.playerTwoName = "Player 2";
|
||||||
|
game.matchconfig = partyConfig;
|
||||||
|
game.scenarioconfig = scenarioConfig;
|
||||||
|
|
||||||
|
game.assignment = ParticipantType.PlayerOne;
|
||||||
|
ensureReceived(p1, game);
|
||||||
|
|
||||||
|
game.assignment = ParticipantType.PlayerTwo;
|
||||||
|
ensureReceived(p2, game);
|
||||||
|
|
||||||
|
game.assignment = ParticipantType.Spectator;
|
||||||
|
ensureReceived(s1, game);
|
||||||
|
|
||||||
|
EventMessage events = new EventMessage();
|
||||||
|
ensureReceived(p1, events);
|
||||||
|
ensureReceived(p2, events);
|
||||||
|
ensureReceived(s1, events);
|
||||||
|
|
||||||
|
clearInvocations(p1, p2, s1);
|
||||||
|
|
||||||
|
m.disconnectUser(p1, true);
|
||||||
|
|
||||||
|
m.connectUser(p1);
|
||||||
|
ensureHandshake(m, p1, "Player 1", "1234", true);
|
||||||
|
|
||||||
|
ReconnectMessage message = new ReconnectMessage();
|
||||||
|
message.reconnect = true;
|
||||||
|
sendMessage(m, p1, message);
|
||||||
|
|
||||||
|
ensureReceived(p1, new GeneralAssignmentMessage());
|
||||||
ensureReceived(p1, new GameStructureMessage());
|
ensureReceived(p1, new GameStructureMessage());
|
||||||
ensureReceived(p2, new GameStructureMessage());
|
|
||||||
ensureReceived(s1, new GameStructureMessage());
|
|
||||||
|
|
||||||
System.out.println("hi");
|
System.out.println("Test Completed");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureHandshake(UserManager m, WebSocket c, String name, String deviceID, boolean runningGame) {
|
private void ensureHandshake(UserManager m, WebSocket c, String name, String deviceID, boolean runningGame) {
|
||||||
@ -140,7 +171,7 @@ class MarvelousServerTest extends BaseGameLogicTest {
|
|||||||
ensureResponse(m, c, message, response);
|
ensureResponse(m, c, message, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureCharacterSelection(UserManager m, WebSocket c, boolean selectionComplete) {
|
private void ensureCharacterSelection(UserManager m, WebSocket c, boolean confirmSelection) {
|
||||||
CharacterSelectionMessage message = new CharacterSelectionMessage();
|
CharacterSelectionMessage message = new CharacterSelectionMessage();
|
||||||
message.characters = new Boolean[12];
|
message.characters = new Boolean[12];
|
||||||
for(int i = 0; i < 6; i++) {
|
for(int i = 0; i < 6; i++) {
|
||||||
@ -150,13 +181,27 @@ class MarvelousServerTest extends BaseGameLogicTest {
|
|||||||
message.characters[i] = false;
|
message.characters[i] = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(confirmSelection) {
|
||||||
ConfirmSelectionMessage response = new ConfirmSelectionMessage();
|
ConfirmSelectionMessage response = new ConfirmSelectionMessage();
|
||||||
response.selectionComplete = selectionComplete;
|
response.selectionComplete = false;
|
||||||
|
|
||||||
ensureResponse(m, c, message, response);
|
ensureResponse(m, c, message, response);
|
||||||
|
} else {
|
||||||
|
sendMessage(m, c, message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/** Sends a message from the socket. */
|
||||||
|
private void sendMessage(UserManager m, WebSocket c, BasicMessage message) {
|
||||||
|
Optional<String> in = json.stringify(message);
|
||||||
|
|
||||||
|
if(in.isPresent()) {
|
||||||
|
m.messageReceived(c, in.get());
|
||||||
|
}else {
|
||||||
|
throw new IllegalArgumentException("[TEST] Message in test call could not be serialized!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Ensures that the given socket received the given response in response to the given message. */
|
/** Ensures that the given socket received the given response in response to the given message. */
|
||||||
private void ensureResponse(UserManager m, WebSocket c, BasicMessage message, BasicMessage response) {
|
private void ensureResponse(UserManager m, WebSocket c, BasicMessage message, BasicMessage response) {
|
||||||
Optional<String> in = json.stringify(message);
|
Optional<String> in = json.stringify(message);
|
||||||
@ -173,10 +218,46 @@ class MarvelousServerTest extends BaseGameLogicTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Ensures that the given socket received the given response. */
|
/** Ensures that the given socket received the given response. */
|
||||||
private void ensureReceived(WebSocket c, BasicMessage response) {
|
private <T extends BasicMessage> void ensureReceived(WebSocket c, T response) {
|
||||||
verify(c).send((String)argThat(
|
boolean found = false;
|
||||||
(out)->verifyResponse((String)out, response)
|
|
||||||
));
|
for(Invocation i: mockingDetails(c).getInvocations()) {
|
||||||
|
Optional<BasicMessage> received = json.parse(i.getArgument(0, String.class));
|
||||||
|
|
||||||
|
if(received.isPresent()) {
|
||||||
|
BasicMessage parsed = received.get();
|
||||||
|
|
||||||
|
if(parsed.getClass() != response.getClass()) {
|
||||||
|
continue; //message is not of the right type
|
||||||
|
}
|
||||||
|
|
||||||
|
T message = (T) parsed;
|
||||||
|
|
||||||
|
for(Field field: response.getClass().getDeclaredFields()) {
|
||||||
|
try {
|
||||||
|
Object value = field.get(response);
|
||||||
|
Object actualValue;
|
||||||
|
try {
|
||||||
|
actualValue = field.get(message);
|
||||||
|
}catch(IllegalArgumentException ignored) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(value != null && !value.equals(actualValue)) {
|
||||||
|
throw new IllegalArgumentException("[TEST] Field " + field.getName() + " expected to be '" + value + "' but was '" + field.get(message) + "'");
|
||||||
|
}
|
||||||
|
}catch(IllegalAccessException ignored) { }
|
||||||
|
}
|
||||||
|
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}else {
|
||||||
|
throw new IllegalArgumentException("[TEST] Response in test call could not be deserialized!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!found) {
|
||||||
|
throw new IllegalArgumentException("[TEST] Expected response " + response + " was not received!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user