Exemple #1
0
        public void SendNewMessagesWhenThereAreNoNewMessagesShouldDoNothing()
        {
            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));

            server.SendNewMessages(mockSocket, 2);

            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));
        }
Exemple #2
0
        public void CheckMessageWhenNewUserAndUserNameAlreadyExistShouldRequestForNewUserName()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

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

            mockSocket.TextToReceive = trimmedReceivedData;

            Assert.True(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("server: userName joined the chat.<eof>", item));

            Assert.Equal("sentMessage", server.CheckMessage(mockSocket));

            Assert.Collection(mockSocket.SentMessages,
                              item => Assert.Equal("server: userName joined the chat.<eof>", item),
                              item => Assert.Equal("server: userName already exist, choose a different user name.<eof>", item));
        }
Exemple #3
0
        public void IsNewUserWhenUserExistShouldReturnFalse()
        {
            ChatServerSide server = new ChatServerSide();

            server.AddUser("newUser");

            Assert.False(server.IsNewUser("newUser"));
        }
Exemple #4
0
        public void SendNewMessagesWhenMessageNoExistShouldThrowException()
        {
            MockSocketCommunication socket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(socket);

            Assert.Throws <ArgumentException>(() => server.SendNewMessages(socket, -1));
        }
Exemple #5
0
        public void AddUserWhenAlreadyExistingUserShouldThrowException()
        {
            ChatServerSide server = new ChatServerSide();

            server.AddUser("newUser");

            Assert.Throws <ArgumentException>(() => server.AddUser("newUser"));
        }
Exemple #6
0
        public void CheckMessageWhenNullMessageShouldThrowException()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            mockSocket.TextToReceive = null;

            Assert.Throws <InvalidOperationException>(() => server.CheckMessage(mockSocket));
        }
Exemple #7
0
        public void AddUserWhenNewUserShouldAddUserToHashTable()
        {
            ChatServerSide server = new ChatServerSide();

            Assert.True(server.IsNewUser("newUser"));

            server.AddUser("newUser");

            Assert.False(server.IsNewUser("newUser"));
        }
Exemple #8
0
        public void StartWhenNotNullSocketShouldWaitForIncomingConnection()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            mockSocket.TextToReceive = "testUser<sep>testMessage<sep>lastMessageReceived<eof>";

            server.Start();

            Assert.True(mockSocket.ServerIsWaiting);
        }
Exemple #9
0
        public void TrimmedReceivedDataWhenStartShouldContainReceivedMessage()
        {
            MockSocketCommunication mockSocket = new MockSocketCommunication();

            ChatServerSide server = new ChatServerSide(mockSocket);

            mockSocket.TextToReceive = "testUser<sep>testMessage<sep>lastMessageReceived<eof>";

            server.Start();

            Assert.Equal("testUser<sep>testMessage<sep>lastMessageReceived<eof>", mockSocket.TrimmedReceivedData);
        }
Exemple #10
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));
        }
Exemple #11
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));
        }
Exemple #12
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));
        }
Exemple #13
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));
        }
Exemple #14
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));
        }
Exemple #15
0
        public void CheckMessageWhenNullSocketShouldThrowException()
        {
            ChatServerSide server = new ChatServerSide();

            Assert.Throws <ArgumentNullException>(() => server.CheckMessage(null));
        }
Exemple #16
0
        public void AddUserWhenEmptyUserShouldThrowException()
        {
            ChatServerSide server = new ChatServerSide();

            Assert.Throws <ArgumentException>(() => server.AddUser(""));
        }
Exemple #17
0
        public void StartWhenNullSocketShouldThrowException()
        {
            ChatServerSide server = new ChatServerSide(null);

            Assert.Throws <ArgumentNullException>(() => server.Start());
        }
Exemple #18
0
        public void IsNewUserWhenUserNoExistShouldReturnTrue()
        {
            ChatServerSide server = new ChatServerSide();

            Assert.True(server.IsNewUser("newUser"));
        }
Exemple #19
0
        public void AddUserWhenNullUserShouldThrowException()
        {
            ChatServerSide server = new ChatServerSide();

            Assert.Throws <ArgumentNullException>(() => server.AddUser(null));
        }
Exemple #20
0
        public void SendNewMessagesWhenNullSocketShouldThrowException()
        {
            ChatServerSide server = new ChatServerSide();

            Assert.Throws <ArgumentNullException>(() => server.SendNewMessages(null, 0));
        }