public void SendingRequestWithEndpointAndCallbackShouldSendMessageToTheSpecifiedEndPoint()
        {
            // Arrange
            var mockConfiguration        = new Mock <IConfiguration>();
            var mockProducer             = new Mock <IProducer>();
            var mockContainer            = new Mock <IBusContainer>();
            var mockRequestConfiguration = new Mock <IRequestConfiguration>();
            var mockSendMessagePipeline  = new Mock <ISendMessagePipeline>();

            mockConfiguration.Setup(x => x.GetSendMessagePipeline()).Returns(mockSendMessagePipeline.Object);
            mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
            mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
            mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
            mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny <Guid>())).Returns(mockRequestConfiguration.Object);
            var task = new Task(() => { });

            mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny <Action <object> >())).Returns(task);

            var message = new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            };


            // Act
            var bus = new ServiceConnect.Bus(mockConfiguration.Object);

            bus.SendRequest <FakeMessage1, FakeMessage2>("test", message, x => { }, null);

            // Assert
            mockSendMessagePipeline.Verify(x => x.ExecuteSendMessagePipeline(It.IsAny <Type>(), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >(), "test"), Times.Once);
        }
        public void SendingRequestSynchronouslyShouldSendCommand()
        {
            // Arrange
            var mockConfiguration        = new Mock <IConfiguration>();
            var mockProducer             = new Mock <IProducer>();
            var mockContainer            = new Mock <IBusContainer>();
            var mockRequestConfiguration = new Mock <IRequestConfiguration>();
            var mockSendMessagePipeline  = new Mock <ISendMessagePipeline>();

            mockConfiguration.Setup(x => x.GetSendMessagePipeline()).Returns(mockSendMessagePipeline.Object);
            mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
            mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
            mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
            mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny <Guid>())).Returns(mockRequestConfiguration.Object);
            var task = new Task(() => { });

            mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny <Action <object> >())).Returns(task);

            var message = new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            };

            mockSendMessagePipeline.Setup(x => x.ExecuteSendMessagePipeline(typeof(FakeMessage1), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <string>())).Callback(task.Start);

            // Act
            var          bus      = new ServiceConnect.Bus(mockConfiguration.Object);
            FakeMessage2 response = bus.SendRequest <FakeMessage1, FakeMessage2>(message, null, 1000);

            // Assert

            mockSendMessagePipeline.Verify(x => x.ExecuteSendMessagePipeline(typeof(FakeMessage1), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >(), null), Times.Once);
        }
        public void SendingRequestToMultipleEndpointsSynchronouslyShouldReturnResponses()
        {
            // Arrange
            var mockConfiguration        = new Mock <IConfiguration>();
            var mockProducer             = new Mock <IProducer>();
            var mockContainer            = new Mock <IBusContainer>();
            var mockRequestConfiguration = new Mock <IRequestConfiguration>();
            var mockSendMessagePipeline  = new Mock <ISendMessagePipeline>();

            mockConfiguration.Setup(x => x.GetSendMessagePipeline()).Returns(mockSendMessagePipeline.Object);
            mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
            mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
            mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
            mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny <Guid>())).Returns(mockRequestConfiguration.Object);
            var task = new Task(() => { });

            Action <FakeMessage2> action = null;

            mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny <Action <object> >())).Returns(task).Callback <Action <FakeMessage2> >(r =>
            {
                action = r;
            });

            var message = new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            };

            var r1 = new FakeMessage2(Guid.NewGuid());
            var r2 = new FakeMessage2(Guid.NewGuid());


            mockSendMessagePipeline.Setup(x => x.ExecuteSendMessagePipeline(It.IsAny <Type>(), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >(), "test1")).Callback(() =>
            {
                action(r1);
            });

            mockSendMessagePipeline.Setup(x => x.ExecuteSendMessagePipeline(It.IsAny <Type>(), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >(), "test2")).Callback(() =>
            {
                action(r2);
                task.Start();
            });

            // Act
            var bus = new ServiceConnect.Bus(mockConfiguration.Object);
            IList <FakeMessage2> responses = bus.SendRequest <FakeMessage1, FakeMessage2>(new List <string> {
                "test1", "test2"
            }, message, null, 1000);

            // Assert
            Assert.Equal(2, responses.Count);
            Assert.True(responses.Contains(r1));
            Assert.True(responses.Contains(r2));
        }
        public void SendingRequestToMultipleEndpointsShouldPassResponsesToCallbackHandler()
        {
            // Arrange
            var mockConfiguration        = new Mock <IConfiguration>();
            var mockProducer             = new Mock <IProducer>();
            var mockContainer            = new Mock <IBusContainer>();
            var mockRequestConfiguration = new Mock <IRequestConfiguration>();
            var mockSendMessagePipeline  = new Mock <ISendMessagePipeline>();

            mockConfiguration.Setup(x => x.GetSendMessagePipeline()).Returns(mockSendMessagePipeline.Object);
            mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
            mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
            mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
            mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny <Guid>())).Returns(mockRequestConfiguration.Object);
            var task = new Task(() => { });

            int count = 0;
            var r1    = new FakeMessage2(Guid.NewGuid());
            var r2    = new FakeMessage2(Guid.NewGuid());

            var responses = new List <FakeMessage2>();
            Action <FakeMessage2> action = message2 =>
            {
                count++;
                responses.Add(message2);
            };

            mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny <Action <object> >())).Callback <Action <object> >(a =>
            {
                a(r1);
                a(r2);
            }).Returns(task);

            var message = new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            };

            mockProducer.Setup(x => x.Send(It.IsAny <Type>(), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >()));

            // Act
            var bus = new ServiceConnect.Bus(mockConfiguration.Object);

            bus.SendRequest(message, action, null);

            // Assert
            mockRequestConfiguration.Verify(x => x.SetHandler(It.IsAny <Action <object> >()), Times.Exactly(1));
            Assert.Equal(2, count);
            Assert.True(responses.Contains(r1));
            Assert.True(responses.Contains(r2));
        }
        public void SendingRequestWithEndpointSynchronouslyShouldReturnResponse()
        {
            // Arrange
            var mockConfiguration        = new Mock <IConfiguration>();
            var mockProducer             = new Mock <IProducer>();
            var mockContainer            = new Mock <IBusContainer>();
            var mockRequestConfiguration = new Mock <IRequestConfiguration>();
            var mockSendMessagePipeline  = new Mock <ISendMessagePipeline>();

            mockConfiguration.Setup(x => x.GetSendMessagePipeline()).Returns(mockSendMessagePipeline.Object);
            mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
            mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
            mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
            mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny <Guid>())).Returns(mockRequestConfiguration.Object);
            var task = new Task(() => { });

            Action <FakeMessage2> action = null;

            mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny <Action <object> >())).Returns(task)
            .Callback <Action <FakeMessage2> >(r => action = r);

            var message = new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            };


            mockSendMessagePipeline.Setup(x => x.ExecuteSendMessagePipeline(It.IsAny <Type>(), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >(), "test")).Callback(() =>
            {
                action(new FakeMessage2(message.CorrelationId)
                {
                    DisplayName = "Tim Watson",
                    Email       = "*****@*****.**"
                });
                task.Start();
            });

            // Act
            var          bus      = new ServiceConnect.Bus(mockConfiguration.Object);
            FakeMessage2 response = bus.SendRequest <FakeMessage1, FakeMessage2>("test", message, null, 1000);

            // Assert
            Assert.Equal("Tim Watson", response.DisplayName);
            Assert.Equal("*****@*****.**", response.Email);
            Assert.Equal(message.CorrelationId, response.CorrelationId);
        }
        public void SendingRequestWithEndpointAndCallbackShouldPassCallbackToHandler()
        {
            // Arrange
            var mockConfiguration        = new Mock <IConfiguration>();
            var mockProducer             = new Mock <IProducer>();
            var mockContainer            = new Mock <IBusContainer>();
            var mockRequestConfiguration = new Mock <IRequestConfiguration>();
            var mockSendMessagePipeline  = new Mock <ISendMessagePipeline>();

            mockConfiguration.Setup(x => x.GetSendMessagePipeline()).Returns(mockSendMessagePipeline.Object);
            mockConfiguration.Setup(x => x.GetContainer()).Returns(mockContainer.Object);
            mockConfiguration.Setup(x => x.GetProducer()).Returns(mockProducer.Object);
            mockConfiguration.SetupGet(x => x.TransportSettings).Returns(new TransportSettings());
            mockConfiguration.Setup(x => x.GetRequestConfiguration(It.IsAny <Guid>())).Returns(mockRequestConfiguration.Object);
            var task = new Task(() => { });

            bool actionCalled            = false;
            Action <FakeMessage2> action = message2 => { actionCalled = true; };

            mockRequestConfiguration.Setup(x => x.SetHandler(It.IsAny <Action <object> >())).Callback <Action <object> >(a => a(new FakeMessage2(Guid.NewGuid()))).Returns(task);

            var message = new FakeMessage1(Guid.NewGuid())
            {
                Username = "******"
            };

            mockProducer.Setup(x => x.Send(It.IsAny <Type>(), It.IsAny <byte[]>(), It.IsAny <Dictionary <string, string> >()));

            // Act
            var bus = new ServiceConnect.Bus(mockConfiguration.Object);

            bus.SendRequest("test", message, action, null);

            // Assert
            mockRequestConfiguration.Verify(x => x.SetHandler(It.IsAny <Action <object> >()), Times.Once());
            Assert.True(actionCalled);
        }