package com.zipcoder.server;

import org.easymock.EasyMock;
import org.junit.Test;

import java.io.IOException;
import java.net.ServerSocket;

public class ServerTest {

    @Test
    public void testStart_whenSocketIsClosed() throws IOException {
        ServerSocket serverSocket = getMockServerSocket(true);
        EasyMock.replay(serverSocket);

        Server server = new Server(serverSocket);
        server.start();

        EasyMock.verify(serverSocket);
    }

    @Test
    public void testStart_whenSocketIsOpen() throws IOException {
        ServerSocket serverSocket = getMockServerSocket(false, true);
        EasyMock.expect(serverSocket.accept()).andReturn(null);
        EasyMock.replay(serverSocket);

        Server server = new Server(serverSocket);
        server.start();

        EasyMock.verify(serverSocket);
    }

    private ServerSocket getMockServerSocket(boolean... returnValues) {
        ServerSocket serverSocket = EasyMock.createMock(ServerSocket.class);
        for (boolean value : returnValues){
            EasyMock.expect(serverSocket.isClosed()).andReturn(value);
        }
        return serverSocket;
    }
}