DS-Lab / src / test / java / dslab / transfer / TransferServerProtocolTest.java
TransferServerProtocolTest.java
Raw
package dslab.transfer;

import dslab.*;
import dslab.routing.DeliveryService;
import dslab.routing.DomainNameResolver;
import dslab.util.ComponentId;
import dslab.util.Config;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.mockito.Mockito.mock;

/**
 * TransferServerProtocolTest.
 */
public class TransferServerProtocolTest extends TestBase {

    private static final Log LOG = LogFactory.getLog(TransferServerProtocolTest.class);

    private final String componentId = "transfer-1";

    private ITransferServer component;
    private int serverPort;

    @Before
    public void setUp() throws Exception {
        component = new TransferServer(in, out, new MockComponentFactory(
                new ComponentId(componentId), new Config(componentId)));

        serverPort = new Config(componentId).getInt("tcp.port");

        new Thread(component).start();

        LOG.info("Waiting for server socket to appear");
        Sockets.waitForSocket("localhost", serverPort, Constants.COMPONENT_STARTUP_WAIT);
    }
    @Test(timeout = 15000)
    public void defaultDmtpInteractionTest() throws Exception {
        try (JunitSocketClient client = new JunitSocketClient(serverPort, err)) {
            client.verify("ok DMTP2.0");
            client.sendAndVerify("begin", "ok");
            client.sendAndVerify("from trillian@earth.planet", "ok");
            client.sendAndVerify("to arthur@earth.planet", "ok 1");
            client.sendAndVerify("subject hello", "ok");
            client.sendAndVerify("data hello from junit", "ok");
            client.sendAndVerify("send", "ok");
            client.sendAndVerify("quit", "ok bye");
        }
    }

    @After
    public void tearDown() throws Exception {
        in.addLine("shutdown"); // send "shutdown" command to command line
        Thread.sleep(Constants.COMPONENT_TEARDOWN_WAIT);
    }
    @Test(timeout = 15000)
    public void sendWithoutRecipient_returnsErrorOnSend() throws Exception {
        try (JunitSocketClient client = new JunitSocketClient(serverPort, err)) {
            client.verify("ok DMTP2.0");
            client.sendAndVerify("begin", "ok");
            client.sendAndVerify("from trillian@earth.planet", "ok");
            client.sendAndVerify("subject hello", "ok");
            client.sendAndVerify("data hello from junit", "ok");
            client.sendAndVerify("send", "error");
            client.sendAndVerify("quit", "ok bye");
        }
    }

    private static class MockComponentFactory extends ComponentFactory {
        public MockComponentFactory(ComponentId componentId, Config config) {
            super(componentId, config);
        }

        @Override public synchronized DomainNameResolver getDomainNameResolver() {
            return mock(DomainNameResolver.class);
        }
    }

}