/**
 * This code is free software; you can redistribute it and/or modify it under
 * the terms of the new BSD License.
 *
 * Copyright (c) 2012, Sebastian Staudt
 */

package com.github.koraktor.steamcondenser.steam.servers;

import java.net.InetAddress;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.github.koraktor.steamcondenser.steam.sockets.GoldSrcSocket;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.whenNew;

/**
 *
 *
 * @author Sebastian Staudt
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(GoldSrcServer.class)
public class GoldSrcServerTest {

    private static InetAddress LOCALHOST;

    private GoldSrcServer server;

    private GoldSrcSocket socket;

    @Before
    public void setup() throws Exception {
        LOCALHOST = InetAddress.getByAddress(new byte[] { 0x7f, 0x0, 0x0, 0x1 });
        this.server = spy(new GoldSrcServer(LOCALHOST, 27015));
        this.socket = mock(GoldSrcSocket.class);
        this.server.socket = this.socket;
    }

    @Test
    public void testGetMaster() throws Exception {
        MasterServer master = mock(MasterServer.class);
        whenNew(MasterServer.class).withArguments(MasterServer.GOLDSRC_MASTER_SERVER).thenReturn(master);

        assertThat(GoldSrcServer.getMaster(), is(equalTo(master)));
    }

    @Test
    public void testInitSocket() throws Exception {
        GoldSrcSocket socket = mock(GoldSrcSocket.class);
        whenNew(GoldSrcSocket.class).withArguments(LOCALHOST, 27015, false).thenReturn(socket);

        this.server.initSocket();

        assertThat((GoldSrcSocket) this.server.socket, is(equalTo(socket)));
    }

    @Test
    public void testRconAuth() throws Exception {
        assertTrue(this.server.rconAuth("password"));
        assertThat(this.server.rconPassword, is(equalTo("password")));
    }

    @Test
    public void testRconExec() throws Exception {
        when(this.socket.rconExec("password", "command")).thenReturn("test");

        this.server.rconPassword = "password";

        assertThat(this.server.rconExec("command"), is(equalTo("test")));
    }

}

@@ -451,16 +451,90 @@
 unner;%0A%0A
+import com.github.koraktor.steamcondenser.exceptions.RCONNoAuthException;%0A
 import c
@@ -685,16 +685,66 @@
 qualTo;%0A
+import static org.hamcrest.core.IsNull.nullValue;%0A
 import s
@@ -2252,16 +2252,22 @@
 RconAuth
+Failed
 () throw
@@ -2272,32 +2272,412 @@
 ows Exception %7B%0A
+        when(this.socket.rconExec(%22password%22, %22%22)).%0A                thenThrow(new RCONNoAuthException());%0A%0A        assertThat(this.server.rconAuth(%22password%22), is(false));%0A        assertThat(this.server.rconPassword, is(nullValue()));%0A    %7D%0A%0A    @Test%0A    public void testRconAuthSuccessful() throws Exception %7B%0A        when(this.socket.rconExec(%22password%22, %22%22)).thenReturn(%22%22);%0A%0A
         assertTr
@@ -2794,32 +2794,32 @@
    %7D%0A%0A    @Test%0A
-
     public void 
@@ -2844,32 +2844,78 @@
 ows Exception %7B%0A
+        this.server.rconAuthenticated = true;%0A
         when(thi

Fix tests for improved GoldSrc authentication