@Test
  public void testCanDeclineInvitation() throws Exception {

    Mockito.when(idGenerator.generate()).thenReturn("id1").thenReturn("id2");

    String input = "";

    Socket client1 = new Socket("localhost", 9898);
    Socket client2 = new Socket("localhost", 9898);

    ObjectOutputStream o1 = new ObjectOutputStream(client1.getOutputStream());
    ObjectInputStream i1 = new ObjectInputStream(client1.getInputStream());

    // Client 1 connection
    o1.writeObject("000|Hans");
    input = (String) i1.readObject();
    String id1 = input.split("\\|")[1].split(",")[0];

    ObjectOutputStream o2 = new ObjectOutputStream(client2.getOutputStream());
    ObjectInputStream i2 = new ObjectInputStream(client2.getInputStream());

    o2.writeObject("000|John");

    // Connection established
    input = (String) i2.readObject();

    // Read player list - One for own connection and one for other connection
    i1.readObject();
    i1.readObject();

    // Find player 2 id
    String id2 = input.split("\\|")[1].split(",")[0];

    // Player list
    input = (String) i2.readObject();

    // Send invite
    o1.writeObject("005|" + id2 + "," + "tictactoe" + "," + id1);

    input = (String) i2.readObject();
    String[] splitted = input.split("\\|");
    String invitationReceiverId = splitted[1].split(",")[0];
    String invitationSenderId = splitted[1].split(",")[2];
    String gameType = splitted[1].split(",")[1];

    o2.writeObject("010|" + invitationSenderId);

    input = (String) i1.readObject();

    assertEquals("010|id1", input);

    server.stop();
  }
  @Test
  public void testCanConnectOneClient() throws Exception {

    Mockito.when(idGenerator.generate()).thenReturn("a0b257f5-5b0b-40bc-9793-aa324b7e3ab2");

    Socket socket = new Socket("localhost", 9898);

    ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
    ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());

    outputStream.writeObject("000|Hans");
    String input = (String) inputStream.readObject();

    outputStream.close();
    inputStream.close();
    server.stop();

    Assert.assertEquals(input, "008|a0b257f5-5b0b-40bc-9793-aa324b7e3ab2,Hans");
  }
  @Test
  public void testCanInvitePersonToGame() throws Exception {

    Mockito.when(idGenerator.generate()).thenReturn("id1").thenReturn("id2");

    String input = "";

    Socket client1 = new Socket("localhost", 9898);
    Socket client2 = new Socket("localhost", 9898);

    ObjectOutputStream oSClient1 = new ObjectOutputStream(client1.getOutputStream());
    ObjectInputStream iSClient1 = new ObjectInputStream(client1.getInputStream());

    ObjectOutputStream oSClient2 = new ObjectOutputStream(client2.getOutputStream());

    oSClient1.writeObject("000|Hans");
    // Connection established
    input = (String) iSClient1.readObject();
    String id1 = input.split("\\|")[1].split(",")[0];

    oSClient2.writeObject("000|John");

    ObjectInputStream iSClient2 = new ObjectInputStream(client2.getInputStream());
    // Connection established
    input = (String) iSClient2.readObject();

    // Find player 2 id
    String id2 = input.split("\\|")[1].split(",")[0];

    // Player list
    input = (String) iSClient2.readObject();

    // Send invite
    oSClient1.writeObject("005|" + id2 + "," + "tictactoe" + "," + id1);

    input = (String) iSClient2.readObject();

    assertEquals("005|id2,tictactoe,id1", input);

    server.stop();
  }
  @Test
  public void testCanConnectMultipleClients() throws Exception {

    when(idGenerator.generate()).thenReturn("a0b257f5-5b0b-40bc-9793-aa324b7e3ab2");

    TestRunnable client1 = makeClient("Hans");
    TestRunnable client2 = makeClient("John");
    TestRunnable client3 = makeClient("Joe");

    new Thread(client1).start();
    new Thread(client2).start();
    new Thread(client3).start();

    Thread.sleep(200);

    Assert.assertEquals("008|a0b257f5-5b0b-40bc-9793-aa324b7e3ab2,Hans", client1.getOutput());
    Assert.assertEquals("008|a0b257f5-5b0b-40bc-9793-aa324b7e3ab2,John", client2.getOutput());
    Assert.assertEquals("008|a0b257f5-5b0b-40bc-9793-aa324b7e3ab2,Joe", client3.getOutput());

    server.stop();
  }
 @After
 public void tearDown() throws Exception {
   server.stop();
 }
  /**
   * The main method.
   *
   * @param args the arguments
   */
  public static void main(String[] args) {

    final StringParameter PRM_BINDINGNAME =
        new StringParameter(
            "bindingName",
            "the name this server shall use to bind its remote reference in the RMI registry.");
    ;
    final BooleanParameter PRM_INITREGISTRY =
        new BooleanParameter(
            "initRegistry",
            "a boolean value, i.e. either true or false, indicating whether this server is responsible for creating the RMI registry or not.");
    final StringParameter PRM_SERVERNAMES =
        new StringParameter(
            "serverNames",
            "a list of names, separated by space characters, indicating the name of the other servers' remote references.");
    final CommandLineParser clp =
        new CommandLineParser("java server.Server", "Server for the lab2 event scheduling system.");
    clp.addParameters(PRM_BINDINGNAME, PRM_INITREGISTRY, PRM_SERVERNAMES);

    try {
      // Parse command line arguments
      clp.parse(args);

    } catch (ParseException pex) {
      logger.severe("Command parse error: " + pex.getMessage());
      System.out.println(clp.getUsageString());
      return;
    } catch (ValidationException vex) {
      logger.severe("Parameter validation error: " + vex.getMessage());
      System.out.println(clp.getUsageString());
      return;
    }

    RegistryInfo regInfo = null;
    try {
      regInfo = RegistryInfo.readRegistryInfo(PROPERTIES_FILE);
    } catch (ParseException pex) {
      logger.severe("Couldn't read properties file: " + pex.getMessage());
      return;
    } catch (FileNotFoundException fnfex) {
      logger.severe("The file \"" + PROPERTIES_FILE + "\" could not be found");
      return;
    } catch (IOException ioex) {
      logger.severe("Couldn't read properties file: " + ioex.getMessage());
      return;
    }

    Server srv =
        new Server(
            PRM_BINDINGNAME.getValue(),
            PRM_INITREGISTRY.getValue(),
            regInfo,
            PRM_SERVERNAMES.getValue().split("\\s"));

    if (!srv.start()) System.exit(1);

    try {
      new BufferedReader(new InputStreamReader(System.in)).readLine();
    } catch (IOException e) {
      logger.warning("Couldn't read from stdin");
    }

    try {
      srv.stop();
    } catch (Exception ex) {
      System.err.println("Error: " + ex.getMessage());
    }

    System.out.println("Shutting down..");
  }