public void Should_Give_Registrations_To_Transport_Layer()
        {
            Mock<ITransportProvider> txMock = _mocker.Create<ITransportProvider>();
            Mock<IRegistration> regMock = _mocker.Create<IRegistration>();

            DefaultEnvelopeBus bus = new DefaultEnvelopeBus(txMock.Object);
            bus.Register(regMock.Object);

            txMock.Verify(tx => tx.Register(regMock.Object), Times.Once());
        }
        public void Should_Dispatch_Envelopes_Even_When_InboundChain_Is_Null()
        {
            // create a mostly empty envelope
            Envelope env = new Envelope() { Payload = Encoding.UTF8.GetBytes("Test") };

            // create the mock dispatcher and transport provider
            Mock<IEnvelopeDispatcher> dispatcherMock = _mocker.Create<IEnvelopeDispatcher>();
            Mock<ITransportProvider> txMock = _mocker.Create<ITransportProvider>();

            // setup the dispatcher to return the envelope when the getter is called
            dispatcherMock.Setup(d => d.Envelope).Returns(env);

            // create the unit under test with the mock transport and null processing chains
            DefaultEnvelopeBus bus = new DefaultEnvelopeBus(txMock.Object);
            bus.InboundChain = null;
            bus.OutboundChain = null;

            // have the transport mock raise its envelope event
            txMock.Raise(tx => tx.OnEnvelopeReceived += null, dispatcherMock.Object);

            // verify that the dispatcher's dispatch method was called
            // (this implies successful inbound chain processing)
            dispatcherMock.Verify(d => d.Dispatch(env), Times.Once());
        }
        public void Should_Send_Incoming_Envelopes_Through_the_Chain()
        {
            // create an envelope and context
            Envelope env = new Envelope() { Payload = Encoding.UTF8.GetBytes("Test") };
            EnvelopeContext ctx = new EnvelopeContext(EnvelopeContext.Directions.In, env);

            // mock a transport provider and envelope processor
            Mock<ITransportProvider> txMock = _mocker.Create<ITransportProvider>();
            Mock<IEnvelopeProcessor> procMock = _mocker.Create<IEnvelopeProcessor>();
            Mock<IEnvelopeDispatcher> dispatcherMock = _mocker.Create<IEnvelopeDispatcher>();

            dispatcherMock.SetupGet<Envelope>(disp => disp.Envelope).Returns(env);

            DefaultEnvelopeBus bus = new DefaultEnvelopeBus(txMock.Object);
            bus.OutboundChain = null;
            bus.InboundChain = new Dictionary<int, IEnvelopeProcessor>();
            bus.InboundChain.Add(0, procMock.Object);

            txMock.Raise(tx => tx.OnEnvelopeReceived += null, dispatcherMock.Object);

            procMock.Verify(
                proc => proc.ProcessEnvelope(It.IsAny<EnvelopeContext>(), It.IsAny<Action>()), Times.Once());
        }
        public void Should_Send_Outgoing_Envelopes_Through_the_Chain()
        {
            // create an envelope
            Envelope env = new Envelope() { Payload = Encoding.UTF8.GetBytes("Test") };

            // mock a transport provider and envelope processor
            Mock<ITransportProvider> txMock = _mocker.Create<ITransportProvider>();
            Mock<IEnvelopeProcessor> procMock = _mocker.Create<IEnvelopeProcessor>();

            // setup the processor to call its continuation
            procMock
                .Setup(
                    proc => proc.ProcessEnvelope(It.IsAny<EnvelopeContext>(), It.IsAny<Action>()))
                .Callback<EnvelopeContext, Action>(
                    (newCtx, continuation) => continuation());

            // create the unit under test and give it the transport provider and processor chain
            DefaultEnvelopeBus bus = new DefaultEnvelopeBus(txMock.Object);
            bus.OutboundChain = new Dictionary<int, IEnvelopeProcessor>();
            bus.OutboundChain.Add(0, procMock.Object);

            // send the envelope
            bus.Send(env);

            // verify the expected calls
            procMock.Verify(
                proc => proc.ProcessEnvelope(It.IsAny<EnvelopeContext>(), It.IsAny<Action>()), Times.Once());
            txMock.Verify(tx => tx.Send(env), Times.Once());
        }
        public void Should_Send_Envelopes_Even_When_OutboundChain_Is_Null()
        {
            Envelope env = new Envelope() { Payload = Encoding.UTF8.GetBytes("Test") };

            Mock<ITransportProvider> txMock = _mocker.Create<ITransportProvider>();

            DefaultEnvelopeBus bus = new DefaultEnvelopeBus(txMock.Object);
            bus.InboundChain = null;
            bus.OutboundChain = null;

            bus.Send(env);

            txMock.Verify(tx => tx.Send(env), Times.Once());
        }