Beispiel #1
0
        public void AddMessageToChatRoomWithInvalidIdReturnsStatusCode404()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.AddMessageToChatRoom(It.IsAny <long>(), It.IsAny <Message>())).Throws(new KeyNotFoundException());
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);
            long    chatRoomId         = 1;
            Message message            = new Message("Carla Red", "Hello World");
            int     expectedStatusCode = 404;
            int     expectedErrorCount = 0;

            // Act
            SimulateValidation(message, chatRoomController);
            var result = chatRoomController.AddMessageToChatRoom(chatRoomId, message);

            // Assert
            Assert.IsNotNull(result);
            StatusCodeResult statusCodeResult = (StatusCodeResult)result;

            Assert.AreEqual(expectedStatusCode, statusCodeResult.StatusCode);

            var modelState = chatRoomController.ModelState;

            Assert.AreEqual(expectedErrorCount, modelState.ErrorCount);
        }
Beispiel #2
0
        public void AddChatRoomWithNameEmptyReturnsStatusCode400()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.AddChatRoom(It.IsAny <ChatRoom>())).Returns(1);
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);

            ChatRoom chatRoom           = new ChatRoom("");
            int      expectedStatusCode = 400;
            int      expectedErrorCount = 1;

            // Act
            SimulateValidation(chatRoom, chatRoomController);
            var result = chatRoomController.AddChatRoom(chatRoom);

            // Assert
            Assert.IsNotNull(result);
            StatusCodeResult statusCodeResult = (StatusCodeResult)result;

            Assert.AreEqual(expectedStatusCode, statusCodeResult.StatusCode);

            var modelState = chatRoomController.ModelState;

            Assert.AreEqual(expectedErrorCount, modelState.ErrorCount);
            Assert.AreEqual(modelState.First().Key, "Name");
        }
Beispiel #3
0
        public static void Run(string[] args)
        {
            ChatContext        = new ChatDbContext("db://localhost:3306");
            ChatUserController = new ChatUserController(ChatContext.ChatUsers);
            ChatRoomController = new ChatRoomController(ChatContext.ChatRooms);

            while (Running)
            {
                throw new NotImplementedException();
            }
        }
Beispiel #4
0
        private void SimulateValidation(object model, ChatRoomController chatRoomController)
        {
            // mimic the behaviour of the model binder which is responsible for Validating the Model
            var validationContext = new ValidationContext(model, null, null);
            var validationResults = new List <ValidationResult>();

            Validator.TryValidateObject(model, validationContext, validationResults, true);
            foreach (var validationResult in validationResults)
            {
                chatRoomController.ModelState.AddModelError(validationResult.MemberNames.First(), validationResult.ErrorMessage);
            }
        }
Beispiel #5
0
        public void Should_return_list_of_chat_rooms()
        {
            new ChatRoomBuilder().With(cr => cr.Name, "Room 1").BuildAndSave();
            new ChatRoomBuilder().With(cr => cr.Name, "Room 2").BuildAndSave();

            var controller = new ChatRoomController();

            var actionResult = controller.Get();
            var rooms        = actionResult as OkNegotiatedContentResult <IEnumerable <ChatRoomDetail> >;

            Assert.That(rooms, Is.Not.Null);
            Assert.That(rooms.Content, Has.Exactly(2).Items);
        }
Beispiel #6
0
        public void Should_return_bad_request_on_error()
        {
            var creator    = new UserBuilder().BuildAndSave();
            var controller = new ChatRoomController();

            controller.OverrideGetUserId(() => creator.Id);

            var actionResult     = controller.Put(new CreateChatRoom());
            var badRequestResult = actionResult as BadRequestErrorMessageResult;

            Assert.That(badRequestResult, Is.Not.Null);
            Assert.That(badRequestResult.Message, Is.Not.Empty);
        }
Beispiel #7
0
        public void GetAllChatRoomsCallChatRoomManager()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.GetAllChatRooms()).Returns(new List <ChatRoom>());
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);

            // Act
            var result = chatRoomController.GetAllChatRooms();

            // Assert
            chatRoomManagerMock.Verify(manager => manager.GetAllChatRooms(), Times.Once);
        }
Beispiel #8
0
        public void GetChatRoomByIdWithInvalidIdReturnsNotFoundResult()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.FindChatRoomById(It.IsAny <long>())).Throws(new KeyNotFoundException());
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);
            long chatRoomId = 1;
            // Act
            var result = chatRoomController.GetChatRoomById(chatRoomId);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(NotFoundResult));
        }
Beispiel #9
0
        public void GetAllChatRoomsReturnsIEnumerableOfChatRoom()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.GetAllChatRooms()).Returns(new List <ChatRoom>());
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);

            // Act
            var result = chatRoomController.GetAllChatRooms();

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IEnumerable <ChatRoom>));
        }
Beispiel #10
0
        public void Should_create_chat_room()
        {
            var creator    = new UserBuilder().BuildAndSave();
            var controller = new ChatRoomController();

            controller.OverrideGetUserId(() => creator.Id);

            var actionResult = controller.Put(new CreateChatRoom {
                RoomName = "Test_Room"
            });
            var createdResult = actionResult as CreatedAtRouteNegotiatedContentResult <ChatRoomDetail>;

            Assert.That(createdResult, Is.Not.Null);
            Assert.That(createdResult.RouteName, Is.EqualTo("DefaultApi"));
            Assert.That(createdResult.RouteValues["id"], Is.EqualTo("Test_Room"));
        }
Beispiel #11
0
        public void AddMessageToChatRoomReturnsStatusCodeResult()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.AddMessageToChatRoom(It.IsAny <long>(), It.IsAny <Message>()));
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);
            long    chatRoomId = 1;
            Message message    = new Message("Carla Red", "Hello World");
            // Act
            var result = chatRoomController.AddMessageToChatRoom(chatRoomId, message);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(StatusCodeResult));
        }
Beispiel #12
0
        public void PostWithValidChatRoomCallsChatRoomManager()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.AddChatRoom(It.IsAny <ChatRoom>())).Returns(1);
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);

            ChatRoom chatRoom = new ChatRoom("Room");

            // Act
            SimulateValidation(chatRoom, chatRoomController);
            chatRoomController.AddChatRoom(chatRoom);

            // Assert
            chatRoomManagerMock.Verify(manager => manager.AddChatRoom(It.IsAny <ChatRoom>()), Times.Once);
        }
Beispiel #13
0
        public void GetChatRoomByIdWithValidIdReturnsObjectResult()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.FindChatRoomById(It.IsAny <long>())).Returns(new ChatRoom());
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);
            long chatRoomId = 1;
            // Act
            var result = chatRoomController.GetChatRoomById(chatRoomId);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ObjectResult));
            ObjectResult objectResult = (ObjectResult)result;

            Assert.IsInstanceOfType(objectResult.Value, typeof(ChatRoom));
        }
Beispiel #14
0
        public void AddChatRoomReturnsStatusCodeResult()
        {
            // Arrange
            var chatRoomManagerMock = new Mock <IChatRoomManager>(MockBehavior.Strict);

            chatRoomManagerMock.Setup(manager => manager.AddChatRoom(It.IsAny <ChatRoom>())).Returns(1);
            ChatRoomController chatRoomController = new ChatRoomController(chatRoomManagerMock.Object);

            ChatRoom chatRoom = new ChatRoom("Room");

            // Act
            SimulateValidation(chatRoom, chatRoomController);
            var result = chatRoomController.AddChatRoom(chatRoom);

            // Assert
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(StatusCodeResult));
        }
Beispiel #15
0
        public MainWindow(ChatRoomController chatRoomController)
        {
            InitializeComponent();
            this.controller = chatRoomController;

            //Update participants list
            usersList.ItemsSource = controller.participants;
            chatList.ItemsSource  = controller.chatRooms;
            pairsList.ItemsSource = controller.nodes;

            //Init events
            controller.eventUpdateParticipants += updateParticipants;
            controller.eventUpdateChatRooms    += updateChatRooms;
            controller.eventUpdatePairs        += updatePairs;
            controller.eventReceiptedMessage   += onReceiveMessage;


            //Title
            Title = controller.ipAddress + " - " + controller.nickname;
        }
 public void Init()
 {
     controller = new ChatRoomController();;
 }