package Networking;

import Networking.Client.ConnectionHandler;
import Networking.Server.ClientServerDispatcher;
import Networking.Server.Match;
import Networking.Server.ServerDispatcher;
import java.io.IOException;
import java.util.List;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class NetworkTest {

    public NetworkTest() throws InterruptedException {
        System.setProperty("TEST", "true");
        ServerDispatcher.getInstance().start(Constants.MasterServerConstants.PORT);
        Thread.sleep(1500);
    }

    @Test
    public void masterServerListingTest() throws InterruptedException, IOException {
        Match match = new Match("Test", "127.0.0.1",
                Constants.MasterServerConstants.PORT + 1, "test", 2);
        ConnectionHandler.getInstance().hostMatch(match);
        Thread.sleep(1500);
        List<Match> activeMatches
                = ServerDispatcher.getInstance().getActiveMatches();
        assertEquals(activeMatches.size(), 1);
        assertEquals(match, activeMatches.get(0));
    }

    @AfterClass
    public static void stopServers() {
        ConnectionHandler.getInstance().disconnectFromMatch();
        ClientServerDispatcher.getInstance().stop();
        ServerDispatcher.getInstance().stop();
    }
}

@@ -54,24 +54,105 @@
 ionHandler;%0A
+import Networking.Requests.ChatMessage;%0Aimport Networking.Requests.GetMatchList;%0A
 import Netwo
@@ -336,21 +336,16 @@
 it.After
-Class
 ;%0Aimport
@@ -383,16 +383,41 @@
 Equals;%0A
+import org.junit.Before;%0A
 import o
@@ -463,217 +463,914 @@
 t %7B%0A
-%0A    public NetworkTest() throws InterruptedException %7B%0A        System.setProperty(%22TEST%22, %22true%22);%0A        ServerDispatcher.getInstance().start(Constants.MasterServerConstants.PORT);%0A        Thread.sleep(1500
+    %0A    private int sleepAmount = 1500;%0A    private final Match activeMatch;%0A    %0A    public NetworkTest() %7B%0A        System.setProperty(%22TEST%22, %22true%22);%0A        sleepAmount = 1500;%0A        activeMatch =  new Match(%22Test%22, %22127.0.0.1%22,%0A                Constants.MasterServerConstants.PORT + 1, %22test%22, 2);%0A        ServerDispatcher.getInstance().start(Constants.MasterServerConstants.PORT);%0A    %7D%0A    %0A    @Before%0A    public void startServers() throws InterruptedException %7B%0A        ServerDispatcher.getInstance().start(Constants.MasterServerConstants.PORT);%0A        Thread.sleep(sleepAmount);%0A        ConnectionHandler.getInstance().hostMatch(activeMatch);%0A        Thread.sleep(sleepAmount);%0A    %7D%0A    %0A    @After%0A    public void stopServers() %7B%0A        ConnectionHandler.getInstance().disconnectFromMatch();%0A        ClientServerDispatcher.getInstance().stop();%0A        ServerDispatcher.getInstance().stop(
 );%0A 
@@ -1370,24 +1370,28 @@
 op();%0A    %7D%0A
+    
 %0A    @Test%0A 
@@ -1475,320 +1475,171 @@
 tion
- %7B%0A        Match match = new Match(%22Test%22, %22127.0.0.1%22,%0A                Constants.MasterServerConstants.PORT + 1, %22test%22, 2);%0A        ConnectionHandler.getInstance().hostMatch(match);%0A        Thread.sleep(1500);%0A        List%3CMatch%3E activeMatches%0A                = ServerDispatcher.getInstance().getActiveMatches(
+, ClassNotFoundException %7B%0A        List%3CMatch%3E activeMatches%0A                = (List%3CMatch%3E)ConnectionHandler.getInstance().readFromMasterServer(new GetMatchList()
 );%0A 
@@ -1705,17 +1705,23 @@
 tEquals(
-m
+activeM
 atch, ac
@@ -1751,24 +1751,50 @@
   %7D%0A
+    
 %0A    @
-AfterC
+Test(expected=IOException.c
 lass
+)
 %0A   
@@ -1805,174 +1805,497 @@
 lic 
-static void stopServers() %7B%0A        ConnectionHandler.getInstance().disconnectFromMatch();%0A        ClientServerDispatcher.getInstance().stop();%0A        ServerDispatch
+void masterServerDownTest() throws IOException, ClassNotFoundException %7B%0A        ServerDispatcher.getInstance().stop();%0A        ConnectionHandler.getInstance().readFromMasterServer(new GetMatchList());%0A    %7D%0A    %0A    @Test(expected=IOException.class)%0A    public void matchServerDownTest() throws IOException, ClassNotFoundException %7B%0A        ClientServerDispatcher.getInstance().stop();%0A        ConnectionHandler.getInstance().sendToMatch(new ChatMessage(%22Hello%22));%0A        ConnectionHandl
 er.g
@@ -2299,32 +2299,41 @@
 r.getInstance().
-stop
+readFromMatch
 ();%0A    %7D%0A%7D%0A

Add tests for server down case