public void SendInvitationToDB_MessageIDCreated_Pass()
        {
            //Arrange
            IMessage invitation = new Invitation(receiverID, 2, GetDateTime.GetUTCNow());
            bool     isSuccess  = false;

            //Act
            try
            {
                RoomAid.QueueConsumer.QueueConsumer.SendToDB(invitation); // using directive doesn't work for some reason
                // NOTE: All three commands below will return the first column of the first row of data, but since we cleared the database first there is only one entry
                int incomingSysID               = (int)_messageDAO.RetrieveOneColumn(new SqlCommand("SELECT SysID FROM dbo.InboxMessages"));
                int incomingMessageID           = (int)_messageDAO.RetrieveOneColumn(new SqlCommand("SELECT MessageID FROM dbo.InboxMessages"));
                int incomingInvitationMessageID = (int)_messageDAO.RetrieveOneColumn(new SqlCommand("SELECT MessageID FROM dbo.Invitations"));
                if (incomingSysID == receiverID && incomingMessageID == incomingInvitationMessageID)
                {
                    isSuccess = true;
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }

            //Assert
            Assert.IsTrue(isSuccess);
        }
        public void GetMessageCount_Pass()
        {
            //Arrange
            bool expected = true;
            bool actual   = false;
            IList <GeneralMessage> messages = new List <GeneralMessage>();

            //Act
            try
            {
                for (int i = 0; i < _numMessages; i++) // Creating 3 general messages to send to database
                {
                    messages.Add(new GeneralMessage(receiverID, i + 2, GetDateTime.GetUTCNow(), "Test message" + i));
                    RoomAid.QueueConsumer.QueueConsumer.SendToDB((IMessage)messages[i]);
                }
                if (_messageDAO.GetCount(receiverID, true) == _numMessages)
                {
                    actual = true;
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }

            //Assert
            Assert.AreEqual(expected, actual);
        }
        public void PurgeQueue_Pass()
        {
            //Arrange
            var      expected = true;
            var      actual   = false;
            IMessage message  = new GeneralMessage(1, 2, GetDateTime.GetUTCNow(), "Test message");

            //Act
            try
            {
                _msmqHandler.Send(message);
                if (_msmqHandler.Peek(1))      // If message is sent to queue (a message exists on queue)
                {
                    _msmqHandler.Purge();      // Then purge
                    if (!_msmqHandler.Peek(1)) // If queue is now empty
                    {
                        actual = true;         // Actual = true;
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }

            //Assert
            Assert.IsTrue(expected == actual);
        }
        public void SendAndReadMultipleRows_GeneralMessage_Pass()
        {
            //Arrange
            bool isSuccess = false;
            IList <GeneralMessage> messages = new List <GeneralMessage>();

            //Act
            try
            {
                for (int i = 0; i < _numMessages; i++) // Creating 3 general messages to send to database
                {
                    messages.Add(new GeneralMessage(receiverID, i + 2, GetDateTime.GetUTCNow(), "Test message" + i));
                    RoomAid.QueueConsumer.QueueConsumer.SendToDB((IMessage)messages[i]);
                }
                IList <IList <string> > incomingMessages      = _messageDAO.RetrieveMultipleRows(new SqlCommand("SELECT * FROM dbo.InboxMessages"));
                IList <IList <string> > generalMessageContent = _messageDAO.RetrieveMultipleRows(new SqlCommand("SELECT MessageBody FROM dbo.GeneralMessages"));
                for (int i = 0; i < incomingMessages.Count; i++)
                {
                    // InboxMessage[0..6] = [ReceiverID, MessageID, PrevMessageID, SenderID, IsRead, SentDate, IsGeneral]
                    GeneralMessage incomingMessage = new GeneralMessage(Int32.Parse(incomingMessages[i][0]),
                                                                        Int32.Parse(incomingMessages[i][2]),
                                                                        Int32.Parse(incomingMessages[i][3]),
                                                                        DateTime.Parse(incomingMessages[i][5]),
                                                                        generalMessageContent[i][0])
                    {
                        IsRead = bool.Parse(incomingMessages[i][4]) // IsRead [4]
                    };
                    //incomingMessage.IsAccepted = bool.Parse(generalMessageContent[i][0]);

                    if (incomingMessage.SenderID == messages[i].SenderID &&
                        incomingMessage.ReceiverID == messages[i].ReceiverID &&
                        incomingMessage.PrevMessageID == messages[i].PrevMessageID &&
                        incomingMessage.IsRead == messages[i].IsRead &&
                        incomingMessage.SentDate.ToString().Equals(messages[i].SentDate.ToString()) && // HACK: SQL & .NET DateTime precision is off, so comparing strings
                        incomingMessage.MessageBody.Equals(messages[i].MessageBody) &&
                        incomingMessage.IsGeneral == true)
                    {
                        isSuccess = true;
                    }
                    else
                    {
                        isSuccess = false; // Since there are multiple messages to be checked, must set false if any are not equal
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }

            //Assert
            Assert.IsTrue(isSuccess);
        }
 public bool SendInvitation(int receiverID, int senderID)
 {
     try
     {
         var invitation = new Invitation(receiverID, senderID, GetDateTime.GetUTCNow());
         Send(invitation);
         return(true);
     }
     catch (Exception e)
     {
         _errorHandler.Handle(e);
         return(false);
     }
 }
 public bool ReplyInvitation(int receiverID, int prevMessageID, int senderID, bool accepted)
 {
     try
     {
         var reply = new Invitation(receiverID, prevMessageID, senderID, GetDateTime.GetUTCNow());
         if (accepted)
         {
             reply.IsAccepted = true;
         }
         return(Send(reply));
     }
     catch (Exception e)
     {
         _errorHandler.Handle(e);
         return(false);
     }
 }
 public bool ReplyMessage(int receiverID, int prevMessageID, int senderID, string messageBody)
 {
     try
     {
         if (messageBody == "" || messageBody == null) // NOTE: do check on front=end?
         {
             throw new Exception("Message body cannot be empty");
         }
         var reply = new GeneralMessage(receiverID, prevMessageID, senderID, GetDateTime.GetUTCNow(), messageBody);
         return(Send(reply));
     }
     catch (Exception e)
     {
         _errorHandler.Handle(e);
         return(false);
     }
 }
        private void MakeTestData()
        {
            IList <GeneralMessage> messages     = new List <GeneralMessage>();
            IList <User>           sendingUsers = new List <User>();

            try
            {
                //CreateUser(receiverID, email, "Michell", "Kuang"); // Creating a user for the first account created in test initialization
                for (int i = 0; i < _numMessages; i++) // Creating the same number of users as the number of messages that will be sent
                {
                    // Create email
                    StringBuilder userEmail = new StringBuilder("email");
                    userEmail.Append(i);
                    userEmail.Append("@gmail.com");

                    // Create first name
                    StringBuilder firstName = new StringBuilder("FirstName");
                    firstName.Append(i);

                    // Create last name
                    StringBuilder lastName = new StringBuilder("LastName");
                    lastName.Append(i);

                    CreateAccount(userEmail.ToString(), "TestPassword", "TestSalt"); // Create user
                    var command = new SqlCommand("SELECT SysID FROM dbo.Users WHERE UserEmail = @email");
                    command.Parameters.AddWithValue("@email", userEmail.ToString());
                    int rcvid = (int)_messageDAO.RetrieveOneColumn(command); // Get SysID of user just created
                    var user  = CreateUser(rcvid, userEmail.ToString(), firstName.ToString(), lastName.ToString());
                    sendingUsers.Add(user);

                    messages.Add(new GeneralMessage(2211, rcvid, GetDateTime.GetUTCNow(), "Test message" + i)); // All messages will be sent to the first user created
                    RoomAid.QueueConsumer.QueueConsumer.SendToDB((IMessage)messages[i]);

                    command = new SqlCommand("SELECT MessageID FROM dbo.InboxMessages WHERE SenderID = @sendid");
                    command.Parameters.AddWithValue("@sendid", rcvid);
                    int incomingMessageID = (int)_messageDAO.RetrieveOneColumn(command);
                    messages[i].MessageID = incomingMessageID;
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }
        }
        public void SendMultipleMessages_RetrieveMessagesInSameOrder_GeneralMessages_Pass()
        {
            //Arrange
            bool  isSuccess = false;
            IList messages  = new List <IMessage>();
            var   j         = 0; // Counter to check number of incoming messages against number of messages sent

            //Act
            try
            {
                for (int i = 0; i < _numMessages; i++) // Creating 3 general messages to send to queue
                {
                    messages.Add(new GeneralMessage(i + 1, i + 2, GetDateTime.GetUTCNow(), "Test message" + i));
                    _msmqHandler.Send((IMessage)messages[i]);
                }
                while (_msmqHandler.Peek(1)) // 1 second should be enough to retrieve each message
                {
                    var incoming = _msmqHandler.Receive();
                    if (incoming.ToString().Equals(messages[j].ToString())) // Check each incoming message against messages list
                    {
                        isSuccess = true;
                    }
                    else
                    {
                        isSuccess = false; // If any are not equal, the messages are not in the same order or did not send properly
                    }
                    j++;                   // Increment incoming messages counter
                }
                if (j != _numMessages)     // If the number of incoming messages doesn't equal the number of messages sent to the queue
                {
                    isSuccess = false;
                    Trace.WriteLine("Incorrect number of incoming messages");
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }
            //Cleanup
            _msmqHandler.Purge();

            //Assert
            Assert.IsTrue(isSuccess);
        }
        public void SendAndReadOneRow_Invitation_Pass()
        {
            //Arrange
            Invitation invitation = new Invitation(receiverID, 2, GetDateTime.GetUTCNow());
            bool       isSuccess  = false;

            //Act
            try
            {
                RoomAid.QueueConsumer.QueueConsumer.SendToDB(invitation); // using directive doesn't work for some reason
                List <string> inboxContent      = (List <string>)_messageDAO.RetrieveOneRow(new SqlCommand("SELECT * FROM dbo.InboxMessages"));
                bool          invitationContent = bool.Parse(_messageDAO.RetrieveOneColumn(new SqlCommand("SELECT IsAccepted FROM dbo.Invitations")).ToString());

                // InboxMessage[0..6] = [ReceiverID, MessageID, PrevMessageID, SenderID, IsRead, SentDate, IsGeneral]
                Invitation incomingInvitation = new Invitation(Int32.Parse(inboxContent[0]),
                                                               Int32.Parse(inboxContent[2]),
                                                               Int32.Parse(inboxContent[3]),
                                                               DateTime.Parse(inboxContent[5]))
                {
                    IsRead     = bool.Parse(inboxContent[4]),
                    IsAccepted = invitationContent
                };

                if (incomingInvitation.SenderID == invitation.SenderID &&
                    incomingInvitation.ReceiverID == invitation.ReceiverID &&
                    incomingInvitation.PrevMessageID == invitation.PrevMessageID &&
                    incomingInvitation.IsRead == invitation.IsRead &&
                    incomingInvitation.SentDate.ToString().Equals(invitation.SentDate.ToString()) && // HACK: SQL & .NET DateTime precision is off, so comparing strings
                    incomingInvitation.IsAccepted.Equals(invitation.IsAccepted) &&
                    incomingInvitation.IsGeneral == false)
                {
                    isSuccess = true;
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }

            //Assert
            Assert.IsTrue(isSuccess);
        }
        public void SendAndReadOneRow_GeneralMessage_Pass()
        {
            //Arrange
            GeneralMessage message   = new GeneralMessage(receiverID, 2, GetDateTime.GetUTCNow(), "Test message");
            bool           isSuccess = false;

            //Act
            try
            {
                RoomAid.QueueConsumer.QueueConsumer.SendToDB(message); // using directive doesn't work for some reason
                List <string> inboxContent          = (List <string>)_messageDAO.RetrieveOneRow(new SqlCommand("SELECT * FROM dbo.InboxMessages"));
                string        generalMessageContent = _messageDAO.RetrieveOneColumn(new SqlCommand("SELECT MessageBody FROM dbo.GeneralMessages")).ToString();

                // InboxMessage[0..6] = [ReceiverID, MessageID, PrevMessageID, SenderID, IsRead, SentDate, IsGeneral]
                GeneralMessage incomingMessage = new GeneralMessage(Int32.Parse(inboxContent[0]),
                                                                    Int32.Parse(inboxContent[2]),
                                                                    Int32.Parse(inboxContent[3]),
                                                                    DateTime.Parse(inboxContent[5]),
                                                                    generalMessageContent)
                {
                    IsRead = bool.Parse(inboxContent[4])
                };

                if (incomingMessage.SenderID == message.SenderID &&
                    incomingMessage.ReceiverID == message.ReceiverID &&
                    incomingMessage.PrevMessageID == message.PrevMessageID &&
                    incomingMessage.IsRead == message.IsRead &&
                    incomingMessage.SentDate.ToString().Equals(message.SentDate.ToString()) && // HACK: SQL & .NET DateTime precision is off, so comparing strings
                    incomingMessage.MessageBody.Equals(message.MessageBody) &&
                    incomingMessage.IsGeneral == true)
                {
                    isSuccess = true;
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }

            //Assert
            Assert.IsTrue(isSuccess);
        }
        public void SendAndReceiveOneMessage_Invitation_Pass()
        {
            //Arrange
            IMessage invitation = new Invitation(1, 2, GetDateTime.GetUTCNow());
            var      incoming   = new Invitation();

            //Act
            try
            {
                _msmqHandler.Send(invitation);
                incoming = (Invitation)_msmqHandler.Receive();
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }
            //Cleanup
            _msmqHandler.Purge();

            //Assert
            Assert.AreEqual(incoming.ToString(), invitation.ToString()); // Doesn't pass if not converted to String (even though seemingly equal)
        }
        public void SendAndReceiveOneMessage_GeneralMessage_Pass()
        {
            //Arrange
            IMessage message  = new GeneralMessage(1, 2, GetDateTime.GetUTCNow(), "Test message");
            var      incoming = new GeneralMessage();

            //Act
            try
            {
                _msmqHandler.Send(message);
                incoming = (GeneralMessage)_msmqHandler.Receive();
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.ToString());
            }
            //Cleanup
            _msmqHandler.Purge();

            //Assert
            Assert.AreEqual(incoming.ToString(), message.ToString()); // Doesn't pass if not converted to String (even though seemingly equal)
        }
 /// <summary>
 /// Creates a user associated with an account for testing
 /// </summary>
 /// <param name="rcvid">SystemID of the user to be created</param>
 /// <param name="userEmail">Email of the user to be created</param>
 /// <param name="firstname">First name of user to be created</param>
 /// <param name="lastname">Last name of user to be created</param>
 /// <returns></returns>
 private User CreateUser(int rcvid, string userEmail, string firstname, string lastname)
 {
     try
     {
         var user   = new User(rcvid, userEmail, firstname, lastname, "Enable", GetDateTime.GetUTCNow(), "Female");
         var update = new UpdateAccountSqlService(user, _dao);
         update.Update();
         return(user);
     }
     catch (Exception e)
     {
         Trace.WriteLine(e);
         return(null);
     }
 }
        public void GetInvitationInbox_Pass()
        {
            //Arrange
            bool isSuccess = false;
            IList <Invitation> invitations  = new List <Invitation>();
            IList <User>       sendingUsers = new List <User>();

            try
            {
                CreateUser(receiverID, email, "Michell", "Kuang"); // Creating a user for the first account created in test initialization
                for (int i = 0; i < _numMessages; i++)             // Creating the same number of users as the number of messages that will be sent
                {
                    // Create email
                    StringBuilder userEmail = new StringBuilder("email");
                    userEmail.Append(i);
                    userEmail.Append("@gmail.com");

                    // Create first name
                    StringBuilder firstName = new StringBuilder("FirstName");
                    firstName.Append(i);

                    // Create last name
                    StringBuilder lastName = new StringBuilder("LastName");
                    lastName.Append(i);

                    CreateAccount(userEmail.ToString(), "TestPassword", "TestSalt"); // Create user
                    var command = new SqlCommand("SELECT SysID FROM dbo.Users WHERE UserEmail = @email");
                    command.Parameters.AddWithValue("@email", userEmail.ToString());
                    int rcvid = (int)_messageDAO.RetrieveOneColumn(command); // Get SysID of user just created
                    var user  = CreateUser(rcvid, userEmail.ToString(), firstName.ToString(), lastName.ToString());
                    sendingUsers.Add(user);

                    invitations.Add(new Invitation(receiverID, rcvid, GetDateTime.GetUTCNow())); // All messages will be sent to the first user created
                    RoomAid.QueueConsumer.QueueConsumer.SendToDB((IMessage)invitations[i]);

                    command = new SqlCommand("SELECT MessageID FROM dbo.InboxMessages WHERE SenderID = @sendid");
                    command.Parameters.AddWithValue("@sendid", rcvid);
                    int incomingMessageID = (int)_messageDAO.RetrieveOneColumn(command);
                    invitations[i].MessageID = incomingMessageID;
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }

            //Act
            try
            {
                IList <MessageListing> inbox = manager.GetAllInvitations(receiverID); // Retrieve all messages in inbox for user "Michell Kuang"
                for (int i = 0; i < inbox.Count; i++)
                {
                    // Combine first and last to get full name
                    StringBuilder sb = new StringBuilder(sendingUsers[i].FirstName);
                    sb.Append(" ");
                    sb.Append(sendingUsers[i].LastName);
                    if (inbox[i].MessageID == invitations[i].MessageID &&
                        inbox[i].SentDate.ToString().Equals(invitations[i].SentDate.ToString("f", CultureInfo.CreateSpecificCulture("en-US"))) &&
                        inbox[i].FullName.Equals(sb.ToString()))
                    {
                        isSuccess = true;
                    }
                    else
                    {
                        isSuccess = false; // Since there are multiple messages to be checked, must set false if any are not equal
                    }
                }
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }
            //Cleanup other users created
            foreach (User u in sendingUsers)
            {
                DeleteAccounts(u.UserEmail);
            }

            //Assert
            Assert.IsTrue(isSuccess);
        }