added NetworkDialogClientTests
testEnterIP -> passes testEnterPort -> can't be tested in model testConnectToServer -> passes testCantConnectToServer -> passes testCancelJoining -> passes
This commit is contained in:
		@@ -11,6 +11,8 @@
 | 
			
		||||
import pp.mdga.notification.Notification;
 | 
			
		||||
import pp.mdga.notification.StartDialogNotification;
 | 
			
		||||
 | 
			
		||||
import java.lang.System.Logger;
 | 
			
		||||
import java.lang.System.Logger.Level;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
@@ -19,7 +21,7 @@
 | 
			
		||||
 * It is responsible for handling the game logic on the client side.
 | 
			
		||||
 */
 | 
			
		||||
public class ClientGameLogic implements ServerInterpreter {
 | 
			
		||||
    static final System.Logger LOGGER = System.getLogger(ClientGameLogic.class.getName());
 | 
			
		||||
    static final Logger LOGGER = System.getLogger(ClientGameLogic.class.getName());
 | 
			
		||||
 | 
			
		||||
    private Game game;
 | 
			
		||||
    private final ClientSender clientSender;
 | 
			
		||||
@@ -53,7 +55,7 @@ public ClientGameLogic(ClientSender clientSender) {
 | 
			
		||||
     * @param msg the message to be sent
 | 
			
		||||
     */
 | 
			
		||||
    public void send(ClientMessage msg) {
 | 
			
		||||
        LOGGER.log(System.Logger.Level.INFO, "send {0}", msg);
 | 
			
		||||
        LOGGER.log(Level.INFO, "send {0}", msg);
 | 
			
		||||
        clientSender.send(msg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -581,7 +583,7 @@ public void selectLeave() {
 | 
			
		||||
    /**
 | 
			
		||||
     * This method calls the selectJoin method of the state
 | 
			
		||||
     *
 | 
			
		||||
     * @param ip the ip to cennect to
 | 
			
		||||
     * @param ip the ip to connect to
 | 
			
		||||
     */
 | 
			
		||||
    public void selectJoin(String ip) {
 | 
			
		||||
        state.selectJoin(ip);
 | 
			
		||||
 
 | 
			
		||||
@@ -2,52 +2,157 @@
 | 
			
		||||
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import pp.mdga.client.ClientGameLogic;
 | 
			
		||||
import pp.mdga.client.ClientSender;
 | 
			
		||||
import pp.mdga.client.DialogsState;
 | 
			
		||||
import pp.mdga.client.dialogstate.LobbyState;
 | 
			
		||||
import pp.mdga.game.Color;
 | 
			
		||||
import pp.mdga.game.Game;
 | 
			
		||||
import pp.mdga.game.Piece;
 | 
			
		||||
import pp.mdga.game.Player;
 | 
			
		||||
import pp.mdga.message.client.DeselectTSKMessage;
 | 
			
		||||
import pp.mdga.message.client.LeaveGameMessage;
 | 
			
		||||
import pp.mdga.message.client.LobbyNotReadyMessage;
 | 
			
		||||
import pp.mdga.message.client.LobbyReadyMessage;
 | 
			
		||||
import pp.mdga.message.client.SelectTSKMessage;
 | 
			
		||||
import pp.mdga.message.server.UpdateTSKMessage;
 | 
			
		||||
import pp.mdga.notification.InfoNotification;
 | 
			
		||||
import pp.mdga.notification.TskSelectNotification;
 | 
			
		||||
import pp.mdga.server.ServerGameLogic;
 | 
			
		||||
import pp.mdga.server.ServerSender;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
import static org.mockito.Mockito.*;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * this test-class tests the testcases T084-T095
 | 
			
		||||
 */
 | 
			
		||||
public class LobbyStateTest {
 | 
			
		||||
 | 
			
		||||
    private Game game;
 | 
			
		||||
 | 
			
		||||
    private ClientSender clientSender;
 | 
			
		||||
    private ClientGameLogic clientGameLogic;
 | 
			
		||||
 | 
			
		||||
    //declare server here
 | 
			
		||||
    private ServerGameLogic serverGameLogic;
 | 
			
		||||
    private ServerSender serverSender;
 | 
			
		||||
 | 
			
		||||
    //declare player-Client here
 | 
			
		||||
    private Player playerClient;
 | 
			
		||||
    private String nameClient = "Client";
 | 
			
		||||
    private Color clientColor;
 | 
			
		||||
    private int IDClient;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //declare player-host here
 | 
			
		||||
    private Player playerHost;
 | 
			
		||||
    private String nameHost = "Host";
 | 
			
		||||
    private Color hostColor;
 | 
			
		||||
    private int IDHost;
 | 
			
		||||
 | 
			
		||||
    private DialogsState dialogs;
 | 
			
		||||
    private LobbyState lobby;
 | 
			
		||||
    private pp.mdga.server.automaton.LobbyState lobbyState;
 | 
			
		||||
 | 
			
		||||
    private SelectTSKMessage selectTSKMessage;
 | 
			
		||||
    private DeselectTSKMessage deselectTSKMessage;
 | 
			
		||||
    private UpdateTSKMessage updateTSKMessage;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void setUp() {
 | 
			
		||||
        // This method will be executed before each test.
 | 
			
		||||
        // Initialize common objects or setup required state for Lobby actions.
 | 
			
		||||
        Game game = new Game();
 | 
			
		||||
 | 
			
		||||
        clientSender = mock(ClientSender.class);
 | 
			
		||||
        clientGameLogic = new ClientGameLogic(clientSender);
 | 
			
		||||
 | 
			
		||||
        serverSender = mock(ServerSender.class);
 | 
			
		||||
        serverGameLogic = new ServerGameLogic(serverSender, game);
 | 
			
		||||
 | 
			
		||||
        IDClient = 1;
 | 
			
		||||
        IDHost = 2;
 | 
			
		||||
 | 
			
		||||
        playerClient = new Player(nameClient);
 | 
			
		||||
        clientColor = Color.NONE;
 | 
			
		||||
        playerClient.setColor(clientColor);
 | 
			
		||||
        playerClient.initialize();
 | 
			
		||||
        game.addPlayer(IDClient, playerClient);
 | 
			
		||||
 | 
			
		||||
        playerHost = new Player(nameHost);
 | 
			
		||||
        hostColor = Color.ARMY;
 | 
			
		||||
        playerHost.setColor(hostColor);
 | 
			
		||||
        playerHost.initialize();
 | 
			
		||||
        game.addPlayer(IDHost, playerHost);
 | 
			
		||||
 | 
			
		||||
        //initialize the playerData
 | 
			
		||||
        for(Map.Entry<Integer, Player> entry : game.getPlayers().entrySet()){
 | 
			
		||||
            game.addPlayer(entry.getKey(), entry.getValue());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        dialogs = clientGameLogic.getDialogs();
 | 
			
		||||
        lobby = dialogs.getLobby();
 | 
			
		||||
        lobbyState = serverGameLogic.getLobbyState();
 | 
			
		||||
 | 
			
		||||
        selectTSKMessage = new SelectTSKMessage(Color.CYBER);
 | 
			
		||||
        deselectTSKMessage = new DeselectTSKMessage(Color.CYBER);
 | 
			
		||||
        updateTSKMessage = new UpdateTSKMessage(IDHost, Color.CYBER, true);
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-Lobby-01
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSelectTSK() {
 | 
			
		||||
        // TODO: Implement test logic for selecting a task (TSK)
 | 
			
		||||
        // test if client is in the Lobby
 | 
			
		||||
        clientGameLogic.setState(dialogs);
 | 
			
		||||
        dialogs.setState(lobby);
 | 
			
		||||
        assertEquals(clientGameLogic.getState(), dialogs);
 | 
			
		||||
        assertEquals(dialogs.getState(), lobby);
 | 
			
		||||
 | 
			
		||||
        //test if server is in the lobby state
 | 
			
		||||
        serverGameLogic.setCurrentState(lobbyState);
 | 
			
		||||
        assertEquals(lobbyState, serverGameLogic.getCurrentState());
 | 
			
		||||
 | 
			
		||||
        serverGameLogic.received(selectTSKMessage, IDClient);
 | 
			
		||||
        assertEquals(Color.CYBER, serverGameLogic.getGame().getPlayerById(IDClient).getColor());
 | 
			
		||||
 | 
			
		||||
        //clientGameLogic.received(updateTSKMessage);
 | 
			
		||||
        //assertEquals(new TskSelectNotification(Color.CYBER, nameClient,false), clientGameLogic.getNotification());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-Lobby-02
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testDeselectTSK() {
 | 
			
		||||
        // TODO: Implement test logic for deselecting a previously selected task (TSK)
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-Lobby-03
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testChangeTSK() {
 | 
			
		||||
        // TODO: Implement test logic for changing a selected task (TSK)
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-Lobby-04
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testReady() {
 | 
			
		||||
        // TODO: Implement test logic for setting the player status to "ready"
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-Lobby-05
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testNotReady() {
 | 
			
		||||
        // TODO: Implement test logic for setting the player status to "not ready"
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-Lobby-06
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testLeaveLobby() {
 | 
			
		||||
        // TODO: Implement test logic for a player leaving the lobby
 | 
			
		||||
//        final LeaveGameMessage message = mock(LeaveGameMessage.class);
 | 
			
		||||
//        serverLogic.received(message, logic.getOwnPlayerId());
 | 
			
		||||
//        verify(serverLogic).received(message, logic.getOwnPlayerId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-Lobby-07
 | 
			
		||||
 
 | 
			
		||||
@@ -2,44 +2,174 @@
 | 
			
		||||
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import pp.mdga.Resources;
 | 
			
		||||
import pp.mdga.client.ClientGameLogic;
 | 
			
		||||
import pp.mdga.client.ClientSender;
 | 
			
		||||
import pp.mdga.client.DialogsState;
 | 
			
		||||
import pp.mdga.client.dialogstate.LobbyState;
 | 
			
		||||
import pp.mdga.client.dialogstate.NetworkDialogState;
 | 
			
		||||
import pp.mdga.client.dialogstate.StartDialogState;
 | 
			
		||||
import pp.mdga.game.Color;
 | 
			
		||||
import pp.mdga.game.Game;
 | 
			
		||||
import pp.mdga.game.Player;
 | 
			
		||||
import pp.mdga.message.client.JoinedLobbyMessage;
 | 
			
		||||
import pp.mdga.message.server.LobbyAcceptMessage;
 | 
			
		||||
import pp.mdga.message.server.LobbyDenyMessage;
 | 
			
		||||
import pp.mdga.notification.InfoNotification;
 | 
			
		||||
import pp.mdga.server.ServerGameLogic;
 | 
			
		||||
import pp.mdga.server.ServerSender;
 | 
			
		||||
 | 
			
		||||
import java.util.prefs.Preferences;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.*;
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * this test-class tests the testcases T079-T083
 | 
			
		||||
 */
 | 
			
		||||
public class NetworkDialogClientTest {
 | 
			
		||||
 | 
			
		||||
    private final String IP = "127.0.0.1";
 | 
			
		||||
    private final int PORT = 8080;
 | 
			
		||||
 | 
			
		||||
    private Game game;
 | 
			
		||||
 | 
			
		||||
    private Preferences preferences;
 | 
			
		||||
 | 
			
		||||
    private ClientSender clientSender;
 | 
			
		||||
    private ClientGameLogic clientGameLogic;
 | 
			
		||||
 | 
			
		||||
    private ServerSender serverSender;
 | 
			
		||||
    private ServerGameLogic serverGameLogic;
 | 
			
		||||
 | 
			
		||||
    //declare player-Client here
 | 
			
		||||
    private Player playerClient;
 | 
			
		||||
    private String nameClient = "Client";
 | 
			
		||||
    private int IDClient;
 | 
			
		||||
 | 
			
		||||
    //declare player-host here
 | 
			
		||||
    private Player playerHost;
 | 
			
		||||
    private String nameHost = "Host";
 | 
			
		||||
    private Color hostColor;
 | 
			
		||||
    private int IDHost;
 | 
			
		||||
 | 
			
		||||
    private DialogsState dialogs;
 | 
			
		||||
    private NetworkDialogState networkDialogState;
 | 
			
		||||
    private StartDialogState startDialogState;
 | 
			
		||||
    private LobbyState lobby;
 | 
			
		||||
    private pp.mdga.server.automaton.LobbyState lobbyState;
 | 
			
		||||
 | 
			
		||||
    private JoinedLobbyMessage joinedLobbyMessage;
 | 
			
		||||
    private LobbyDenyMessage lobbyDenyMessage;
 | 
			
		||||
    private LobbyAcceptMessage lobbyAcceptMessage;
 | 
			
		||||
 | 
			
		||||
    private InfoNotification infoNotification;
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void setUp() {
 | 
			
		||||
        // This method will be executed before each test.
 | 
			
		||||
        // Initialize common objects or setup required state for Network Dialog Client actions.
 | 
			
		||||
        game = new Game();
 | 
			
		||||
 | 
			
		||||
        clientSender = mock(ClientSender.class);
 | 
			
		||||
        serverSender = mock(ServerSender.class);
 | 
			
		||||
 | 
			
		||||
        serverGameLogic = new ServerGameLogic(serverSender, game);
 | 
			
		||||
        clientGameLogic = new ClientGameLogic(clientSender);
 | 
			
		||||
 | 
			
		||||
        playerClient = new Player(nameClient);
 | 
			
		||||
        IDClient = 1;
 | 
			
		||||
 | 
			
		||||
        playerHost = new Player(nameHost);
 | 
			
		||||
        hostColor = Color.ARMY;
 | 
			
		||||
        IDHost = 2;
 | 
			
		||||
        game.addPlayer(IDHost, playerHost);
 | 
			
		||||
 | 
			
		||||
        dialogs = clientGameLogic.getDialogs();
 | 
			
		||||
        networkDialogState = dialogs.getNetworkDialog();
 | 
			
		||||
        startDialogState = dialogs.getStartDialog();
 | 
			
		||||
        lobby = dialogs.getLobby();
 | 
			
		||||
        lobbyState = serverGameLogic.getLobbyState();
 | 
			
		||||
 | 
			
		||||
        joinedLobbyMessage = new JoinedLobbyMessage();
 | 
			
		||||
        lobbyDenyMessage = new LobbyDenyMessage();
 | 
			
		||||
        lobbyAcceptMessage = new LobbyAcceptMessage();
 | 
			
		||||
 | 
			
		||||
        infoNotification = new InfoNotification(Resources.stringLookup("lobby.deny.join"));
 | 
			
		||||
 | 
			
		||||
        preferences = Preferences.userNodeForPackage(NetworkDialogClientTest.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-NetworkDialogClient-01
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testEnterIP() {
 | 
			
		||||
        // TODO: Implement test logic for entering an IP address
 | 
			
		||||
        // test if client is in the Lobby
 | 
			
		||||
        clientGameLogic.setState(dialogs);
 | 
			
		||||
        dialogs.setState(networkDialogState);
 | 
			
		||||
        assertEquals(dialogs, clientGameLogic.getState());
 | 
			
		||||
        assertEquals(networkDialogState, dialogs.getState());
 | 
			
		||||
 | 
			
		||||
        clientGameLogic.selectJoin(IP);
 | 
			
		||||
        assertEquals(IP, preferences.get("IP", IP));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-NetworkDialogClient-02
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testEnterPort() {
 | 
			
		||||
        // TODO: Implement test logic for entering a port number
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // UC-NetworkDialogClient-03
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testConnectToServer() {
 | 
			
		||||
        // TODO: Implement test logic for attempting to connect to a server
 | 
			
		||||
        // test if client is in the Lobby
 | 
			
		||||
        clientGameLogic.setState(dialogs);
 | 
			
		||||
        dialogs.setState(networkDialogState);
 | 
			
		||||
        assertEquals(dialogs, clientGameLogic.getState());
 | 
			
		||||
        assertEquals(networkDialogState, dialogs.getState());
 | 
			
		||||
 | 
			
		||||
        //test if server is in the lobby state
 | 
			
		||||
        serverGameLogic.setCurrentState(lobbyState);
 | 
			
		||||
        assertEquals(lobbyState, serverGameLogic.getCurrentState());
 | 
			
		||||
 | 
			
		||||
        //Server accepts connection
 | 
			
		||||
        clientGameLogic.getDialogs().getNetworkDialog().received(lobbyAcceptMessage);
 | 
			
		||||
        assertEquals(dialogs, clientGameLogic.getState());
 | 
			
		||||
        assertEquals(lobby, dialogs.getState());
 | 
			
		||||
 | 
			
		||||
        serverGameLogic.received(joinedLobbyMessage, IDClient);
 | 
			
		||||
        assertTrue(serverGameLogic.getGame().getPlayers().containsKey(IDClient));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCantConnectToServer() {
 | 
			
		||||
        // TODO: Implement test logic for handling failed server connection attempts
 | 
			
		||||
    }
 | 
			
		||||
        // test if client is in the Lobby
 | 
			
		||||
        clientGameLogic.setState(dialogs);
 | 
			
		||||
        dialogs.setState(networkDialogState);
 | 
			
		||||
        assertEquals(dialogs, clientGameLogic.getState());
 | 
			
		||||
        assertEquals(networkDialogState, dialogs.getState());
 | 
			
		||||
 | 
			
		||||
        //test if server is in the lobby state
 | 
			
		||||
        serverGameLogic.setCurrentState(lobbyState);
 | 
			
		||||
        assertEquals(lobbyState, serverGameLogic.getCurrentState());
 | 
			
		||||
 | 
			
		||||
        //test if client receives a lobby deny notification and stays in the NetworkDialog
 | 
			
		||||
        clientGameLogic.getDialogs().getNetworkDialog().received(lobbyDenyMessage);
 | 
			
		||||
        infoNotification =(InfoNotification) clientGameLogic.getNotification();
 | 
			
		||||
        assertEquals(Resources.stringLookup("lobby.deny.join"),infoNotification.getMessage());
 | 
			
		||||
        assertEquals(dialogs, clientGameLogic.getState());
 | 
			
		||||
        assertEquals(startDialogState, dialogs.getState());    }
 | 
			
		||||
 | 
			
		||||
    // UC-NetworkDialogClient-04
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCancelJoining() {
 | 
			
		||||
        // TODO: Implement test logic for canceling the joining process
 | 
			
		||||
        // test if client is in the Lobby
 | 
			
		||||
        clientGameLogic.setState(dialogs);
 | 
			
		||||
        dialogs.setState(networkDialogState);
 | 
			
		||||
        assertEquals(dialogs, clientGameLogic.getState());
 | 
			
		||||
        assertEquals(networkDialogState, dialogs.getState());
 | 
			
		||||
 | 
			
		||||
        //tests if client can return to the Start Menu
 | 
			
		||||
        clientGameLogic.selectLeave();
 | 
			
		||||
        assertEquals(dialogs, clientGameLogic.getState());
 | 
			
		||||
        assertEquals(startDialogState, dialogs.getState());
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user