Esempio n. 1
0
        public void CheckMessageWhenNoNewUserShouldAddMessageToChatAndSendNewMessages()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            mockSocket.SentMessages.Add("Initial message");

            string trimmedReceivedData = "userName<sep>sentMessage<sep>server: userName joined the chat.<eof>";

            mockSocket.TextToReceive = trimmedReceivedData;

            server.CheckMessage(mockSocket);

            Assert.False(server.IsNewUser("userName"));
            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.True(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Equal("sentMessage", server.CheckMessage(mockSocket));
            Assert.False(server.IsNewUser("userName"));

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("Initial message", item),
                              item => Assert.Equal("server: userName joined the chat.<eof>", item),
                              item => Assert.Equal("userName: sentMessage<eof>", item));
        }
Esempio n. 2
0
        public void CheckMessageWhenNewUserShouldSendOnlyGreetingMessage()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string initialTestMessage = "someUser<sep>Initial message<sep>testLastMessageReceived<eof>";

            mockSocket.TextToReceive = initialTestMessage;

            server.CheckMessage(mockSocket);

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("server: someUser joined the chat.<eof>", item));

            string testMessage = "someUser<sep>Initial message<sep>server: someUser joined the chat.<eof>";

            mockSocket.TextToReceive = testMessage;

            Assert.False(server.IsNewUser("someUser"));
            Assert.Equal(3, testMessage.Split("<sep>").Length);
            Assert.True(testMessage.IndexOf("<eof>") > -1);
            Assert.Equal("Initial message", server.CheckMessage(mockSocket));
            Assert.False(server.IsNewUser("someUser"));

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("server: someUser joined the chat.<eof>", item),
                              item => Assert.Equal("someUser: Initial message<eof>", item));

            string trimmedReceivedData = "newUser<sep>sentMessage<sep>lastMessageReceived<eof>";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.True(server.IsNewUser("newUser"));
            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.True(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Equal("sentMessage", server.CheckMessage(mockSocket));
            Assert.False(server.IsNewUser("newUser"));

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("server: someUser joined the chat.<eof>", item),
                              item => Assert.Equal("someUser: Initial message<eof>", item),
                              item => Assert.Equal("server: newUser joined the chat.<eof>", item));
        }
Esempio n. 3
0
        public void CheckMessageWhenNullMessageShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            mockSocket.TextToReceive = null;

            Assert.Throws <InvalidOperationException>(() => server.CheckMessage(mockSocket));
        }
Esempio n. 4
0
        public void CheckMessageWhenInvalidFormatShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string invalidFormatData = "userName_sentMessage_lastMessage";

            mockSocket.TextToReceive = invalidFormatData;

            Assert.Single(invalidFormatData.Split("<sep>"));
            Assert.Throws <InvalidOperationException>(() => server.CheckMessage(mockSocket));
        }
Esempio n. 5
0
        public void CheckMessageWhenEmptySecondArgumentShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string trimmedReceivedData = "userName<sep><sep>lastMessageReceived<eof>";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.True(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Throws <ArgumentException>(() => server.CheckMessage(mockSocket));
        }
Esempio n. 6
0
        public void CheckMessageWhenAnyShouldHaveValidFormat()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string trimmedReceivedData = "userName<sep>sentMessage<sep>lastMessageReceived<eof>";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.True(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Equal("sentMessage", server.CheckMessage(mockSocket));
        }
Esempio n. 7
0
        public void CheckMessageWhenNoEOFTagShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            string trimmedReceivedData = "userName<sep>sentMessage<sep>lastMessageReceived";

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.Equal(3, trimmedReceivedData.Split("<sep>").Length);
            Assert.False(trimmedReceivedData.IndexOf("<eof>") > -1);
            Assert.Throws <InvalidOperationException>(() => server.CheckMessage(mockSocket));
        }
Esempio n. 8
0
        public void CheckMessageWhenNullSocketShouldThrowException()
        {
            ChatServerSide server = new ChatServerSide();

            Assert.Throws <ArgumentNullException>(() => server.CheckMessage(null));
        }