public void ConnectSendsBackError()
        {
            MockRepository mockRepository = new MockRepository();

            var clientRepository = mockRepository.Stub<IClientRepository>();
            var aClient = mockRepository.StrictMock<IClient>();

            clientRepository.Expect(repository => repository.GetByID("abc"))
                            .Return(aClient);
            aClient.Expect(client => client.Enqueue(null))
                   .IgnoreArguments()
                   .WhenCalled(mi =>
                   {
                       Assert.AreEqual(1, mi.Arguments.Length);
                       var messages = mi.Arguments[0] as Message[];
                       Message reply = messages.Single() as Message;
                       Assert.AreEqual("errorOccurred", reply.GetData<string>("type"));
                       Assert.AreEqual("it's wrong", reply.GetData<string>("errorMessage"));
                       Assert.AreEqual("def", reply.channel);
                   });
            aClient.Expect(c => c.FlushQueue());

            var connectMessageHandler = new TelnetProtocolTranslator(null, clientRepository, null)
            {
                ClientId = "abc",
                Channel = "def"
            };

            mockRepository.ReplayAll();
            connectMessageHandler.OnErrorOccurred("it's wrong");
            mockRepository.VerifyAll();
        }
        public void ConnectStartsConnectionAndCaches()
        {
            MockRepository mockRepository = new MockRepository();
            var socketConnection = mockRepository.DynamicMock<IHTMLConnection>();
            TelnetProtocolTranslator connectMessageHandler =
                new TelnetProtocolTranslator(socketConnection, null, null);

            socketConnection.Expect(connection => connection.StartConnect("test.com", 25));

            socketConnection.Expect(connection => connection.ServerDisconnected += connectMessageHandler.OnDisconnected);
            socketConnection.Expect(connection => connection.ConnectionSucceeded += connectMessageHandler.OnConnectSucceeded);
            socketConnection.Expect(connection => connection.ErrorOccurred += connectMessageHandler.OnErrorOccurred);

            var message = new Message();
            message.SetData("type", "connect");
            message.SetData("server", "test.com");
            message.SetData("port", 25);
            message.clientId = "abc";
            message.channel = "def";

            mockRepository.ReplayAll();
            connectMessageHandler.HandleMessage(message);
            Assert.AreEqual("abc", connectMessageHandler.ClientId);
            Assert.AreEqual("def", connectMessageHandler.Channel);
            mockRepository.VerifyAll();
        }
        public void TextTypedForwardsLine()
        {
            MockRepository mockRepository = new MockRepository();
            var socketConnection = mockRepository.DynamicMock<IHTMLConnection>();
            var connectMessageHandler = new TelnetProtocolTranslator(socketConnection, null, null);

            socketConnection.Expect(connection => connection.Send("line typed"));

            var message = new Message();
            message.SetData("type", "textEntered");
            message.SetData("text", "line typed");

            mockRepository.ReplayAll();
            connectMessageHandler.HandleMessage(message);
            mockRepository.VerifyAll();
        }
        public void ReceiveForwardsToBrowser()
        {
            MockRepository mockRepository = new MockRepository();

            var clientRepository = mockRepository.Stub<IClientRepository>();
            var aClient = mockRepository.StrictMock<IClient>();

            var messageHandler = new TelnetProtocolTranslator(
                    null,
                    clientRepository,
                    null
            );
            messageHandler.ClientId = "abc";
            messageHandler.Channel = "def";
            clientRepository.Expect(repository => repository.GetByID("abc"))
                            .Return(aClient);
            aClient.Expect(client => client.Enqueue(null))
                   .IgnoreArguments()
                   .WhenCalled(mi =>
                   {
                       Assert.AreEqual(1, mi.Arguments.Length);
                       var messages = mi.Arguments[0] as Message[];
                       Message reply = messages.Single() as Message;
                       Assert.AreEqual(
                           "textReceived",
                           reply.GetData<string>("type")
                       );
                       Assert.AreEqual("ABCD", reply.GetData<string>("text"));
                       Assert.AreEqual("def", reply.channel);
                   });
            aClient.Expect(c => c.FlushQueue());

            mockRepository.ReplayAll();
            messageHandler.OnDataReceived("ABCD");
            mockRepository.VerifyAll();
        }
        public void HandleDisconnectDisconnectsSocketAndRemovesFromCache()
        {
            MockRepository mockRepository = new MockRepository();

            var clientRepository = mockRepository.Stub<IClientRepository>();
            var aClient = mockRepository.StrictMock<IClient>();

            var messageHandlerCache = mockRepository.StrictMock<IMessageHandlerCache>();
            messageHandlerCache.Expect(mh => mh.Remove(aClient));

            var connection = mockRepository.StrictMock<IHTMLConnection>();
            connection.Expect(conn => conn.StartDisconnect());

            TelnetProtocolTranslator messageHandler = new TelnetProtocolTranslator(
                connection,
                clientRepository,
                messageHandlerCache
            );

            mockRepository.ReplayAll();
            messageHandler.HandleDisconnect(aClient);
            mockRepository.VerifyAll();
        }
        public void FindTranslatorObject_ReturnsExistingObject()
        {
            MockRepository mockRepository = new MockRepository();

            var clientRepository = mockRepository.Stub<IClientRepository>();
            var aClient = mockRepository.StrictMock<IClient>();
            clientRepository.Expect(repository => repository.GetByID("abc"))
                            .Return(aClient);

            Message message = new Message();
            message.clientId = "abc";

            var cache = new MessageHandlerCache();
            var translator = new TelnetProtocolTranslator(null, null, null);
            cache["abc"] = translator;

            Assert.AreEqual(translator, TelnetProtocolTranslator.FindTranslatorObject(cache, message, null));
        }
        public void FindTranslatorObject_MakesNewObjectIfNeeded()
        {
            MockRepository mockRepository = new MockRepository();

            var clientRepository = mockRepository.Stub<IClientRepository>();
            var aClient = mockRepository.StrictMock<IClient>();
            clientRepository.Expect(repository => repository.GetByID("abc"))
                            .Return(aClient);

            var message = new Message();
            message.clientId = "abc";

            var cache = new MessageHandlerCache();
            TelnetProtocolTranslator translator = new TelnetProtocolTranslator(null, null, null);

            Assert.AreEqual(translator, TelnetProtocolTranslator.FindTranslatorObject(cache, message, ()=>translator));
        }