Exemplo n.º 1
0
        public void Should_Convert_PropagateMessageCommand_To_PrivateMessageText()
        {
            string expectedMessage        = "Hey, how are you doin?";
            string expectedSenderNickname = "Joey";
            string expectedTargetNickname = "Jospeh";
            string expectedText           = $"{expectedSenderNickname} says privately to {expectedTargetNickname}: {expectedMessage}";

            PropagateMessageCommand messageCommand = new PropagateMessageCommand
            {
                Content = new TextMessageContent(expectedMessage),
                Private = true,
                Sender  = new Domain.Entities.Client
                {
                    Nickname = expectedSenderNickname
                },
                Target = new Domain.Entities.Client
                {
                    Nickname = expectedTargetNickname
                }
            };

            string actualText = Mapper.ToString(messageCommand);

            Assert.AreEqual(expectedText, actualText);
        }
Exemplo n.º 2
0
        public async Task Should_Broadcast_A_Targeted_Message_When_User_Sents_A_Targeted_Public_Message()
        {
            // arrage
            string expectedRoom = "secret-room";

            string          theTargetedNickname = "Carlton";
            Guid            theConnectionUid    = Guid.NewGuid();
            IMessageContent theMessageContent   = Mock.Of <IMessageContent>();

            Client senderClient = new Client
            {
                Nickname = "Will",
                Room     = expectedRoom
            };

            Client targetedClient = new Client
            {
                Nickname = theTargetedNickname,
                Room     = expectedRoom
            };

            Client destinationClient = new Client();

            ClientRepositoryMock.Setup(mock => mock.GetAllClientInTheRoomAsync(expectedRoom)).Returns(Task.FromResult(new List <Client> {
                destinationClient
            }.AsEnumerable()));

            ClientRepositoryMock.Setup(mock => mock.FindByNicknameAsync(theTargetedNickname)).Returns(Task.FromResult(targetedClient));

            ClientRepositoryMock.Setup(mock => mock.GetByUidAsync(theConnectionUid)).Returns(Task.FromResult(senderClient));

            Client actualDestinationClient        = null;
            PropagateMessageCommand actualCommand = null;

            DomainEvents.OnCommand += (destination, command) =>
            {
                actualDestinationClient = destination;
                actualCommand           = command as PropagateMessageCommand;
                return(Task.CompletedTask);
            };

            // act
            await ChatFacade.SendPublicTargetedMessageAsync(theConnectionUid, theTargetedNickname, theMessageContent);

            // assert
            Assert.AreEqual(destinationClient, actualDestinationClient);
            Assert.NotNull(actualCommand);

            Assert.AreEqual(theMessageContent, actualCommand.Content);
            Assert.AreEqual(senderClient, actualCommand.Sender);
            Assert.AreEqual(targetedClient, actualCommand.Target);
        }
Exemplo n.º 3
0
        public async Task Should_Send_Command_To_Client()
        {
            // arrange
            PropagateMessageCommand expectedCommand = new PropagateMessageCommand
            {
                Content = new TextualContent("Welcome to my world!")
            };

            PropagateMessageCommand actualCommand     = null;
            ManualResetEvent        connectionWaiter  = new ManualResetEvent(false);
            CancellationTokenSource cancellationToken = new CancellationTokenSource(TestTimeout);

            TextualContent expectedContent     = expectedCommand.Content as TextualContent;
            TextualContent actualContent       = null;
            Guid           actualConnectionUid = default;

            SocketCommunicator.OnClientConnected += (connectionUid) =>
            {
                actualConnectionUid = connectionUid;
                connectionWaiter.Set();
                return(Task.CompletedTask);
            };

            // act when server is ready
            WhenSocketIsReady(cancellationToken, () =>
            {
                ClientConnection client = new ClientConnection("localhost", 33000);

                connectionWaiter.WaitOne(ReceiveMessageTimeout);

                SocketCommunicator.PublishAsync(actualConnectionUid, expectedCommand);

                var receivedText = client.Receive();

                actualCommand = CommandSerializer.Deserialize(receivedText) as PropagateMessageCommand;
                actualContent = actualCommand.Content as TextualContent;
            });

            await SocketCommunicator.ListenAsync(cancellationToken.Token);

            // assert
            Assert.AreEqual(expectedContent.Text, actualContent.Text);
        }
Exemplo n.º 4
0
        public void Should_Convert_PropagateMessageCommand_To_Text()
        {
            string expectedMessage        = "What's app? No, chat!";
            string expectedSenderNickname = "Joey";
            string expectedText           = $"{expectedSenderNickname} says to everybody: {expectedMessage}";

            PropagateMessageCommand messageCommand = new PropagateMessageCommand
            {
                Content = new TextMessageContent(expectedMessage),
                Private = false,
                Sender  = new Domain.Entities.Client
                {
                    Nickname = expectedSenderNickname
                }
            };

            string actualText = Mapper.ToString(messageCommand);

            Assert.AreEqual(expectedText, actualText);
        }
Exemplo n.º 5
0
        public async Task Should_Send_A_Private_Message_To_ItSelf_And_Destination_When_User_Sents_A_Private_Message()
        {
            // arrage
            string expectedRoom = "secret-room";

            string          theTargetedUser   = "******";
            Guid            theConnectionUid  = Guid.NewGuid();
            IMessageContent theMessageContent = Mock.Of <IMessageContent>();

            Client senderClient = new Client
            {
                Nickname = "Will",
                Room     = expectedRoom
            };

            Client targetedClient = new Client
            {
                Nickname = "Carlton",
                Room     = expectedRoom
            };

            ClientRepositoryMock.Setup(mock => mock.FindByNicknameAsync(theTargetedUser)).Returns(Task.FromResult(targetedClient));

            ClientRepositoryMock.Setup(mock => mock.GetByUidAsync(theConnectionUid)).Returns(Task.FromResult(senderClient));

            Client actualDestinationClient = null;
            PropagateMessageCommand actualDestinationCommand = null;

            Client actualSenderClient = null;
            PropagateMessageCommand actualSenderCommand = null;

            DomainEvents.OnCommand += (destination, command) =>
            {
                if (actualDestinationClient == null)
                {
                    actualDestinationClient  = destination;
                    actualDestinationCommand = command as PropagateMessageCommand;
                }
                else
                {
                    actualSenderClient  = destination;
                    actualSenderCommand = command as PropagateMessageCommand;
                }
                return(Task.CompletedTask);
            };

            // act
            await ChatFacade.SendPrivateMessageAsync(theConnectionUid, theTargetedUser, theMessageContent);

            // assert
            Assert.AreEqual(targetedClient, actualDestinationClient);
            Assert.NotNull(actualDestinationCommand);

            Assert.AreEqual(theMessageContent, actualDestinationCommand.Content);
            Assert.AreEqual(senderClient, actualDestinationCommand.Sender);
            Assert.AreEqual(targetedClient, actualDestinationCommand.Target);

            Assert.AreEqual(actualSenderClient, senderClient);
            Assert.AreEqual(theMessageContent, actualSenderCommand.Content);
            Assert.AreEqual(senderClient, actualSenderCommand.Sender);
            Assert.AreEqual(targetedClient, actualSenderCommand.Target);
        }