public void ReceiveWithResult_WhenCalledOnNoDestinations_ShouldThrow()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var sut = InboxBuilder.New()
                .Build();

            Assert.Throws<SendException>(() => sut.ReceiveWithResult(typeof(SimpleMessage), envelope));
        }
        public void ReceiveWithResult_WhenCalledOnMultipleDestinations_ShouldThrow()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var destinations = _autoFixture.CreateMany<Mock<IDestination>>().ToArray();

            var sut = InboxBuilder.New()
                .WithDestinationsForType<SimpleMessage>(destinations.Select(x => x.Object))
                .Build();

            Assert.Throws<SendException>(() => sut.ReceiveWithResult(typeof (SimpleMessage), envelope));
        }
        public void ReceiveWithResult_WhenCalledWithCompatibleEnvelopeAndDeclaredTypes_ShouldNotThrow()
        {
            var envelope = new StandardEnvelope(new DerivedSimpleMessage());
            var destination = new Mock<IDestination>();
            destination.Setup(x => x.ReceiveWithResult(envelope)).Returns(new object());

            var sut = InboxBuilder.New()
                .WithDestinationForType<SimpleMessage>(destination.Object)
                .Build();

            Assert.DoesNotThrow(() => sut.ReceiveWithResult(typeof(SimpleMessage), envelope));
        }
        public void Deliver_WhenCalled_ShouldCallReceiveOnceForMessageType()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var inbox = new Mock<IInbox>(MockBehavior.Strict);
            inbox.Setup(x => x.Receive(typeof (SimpleMessage), envelope)).Verifiable();

            var sut = AnInMemoryCourier.Default(inbox.Object);

            sut.Deliver(envelope);

            inbox.Verify(x => x.Receive(typeof(SimpleMessage), envelope), Times.Once());
        }
        public void SendWithReply_OnCourierException_ShouldWrapIt()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var specificCourier = _autoFixture.CreateAnonymous<Mock<ICourier>>();
            specificCourier.Setup(x => x.DeliverWithReply<int>(envelope)).Throws<NullReferenceException>().Verifiable();

            var sut = OutboxBuilder.New()
                .WithCourierForType<SimpleMessage>(specificCourier.Object)
                .Build();

            Assert.Throws<SendException>(() => sut.SendWithReply<int>(envelope));
        }
        public void DeliverWithReply_WhenCalled_ShouldCallReceiveOnceForMessageTypeAndReturnItsResult()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var expected = 7;

            var inbox = new Mock<IInbox>(MockBehavior.Strict);
            inbox.Setup(x => x.ReceiveWithResult(typeof(SimpleMessage), envelope)).Returns(expected).Verifiable();

            var sut = AnInMemoryCourier.Default(inbox.Object);

            var actual = sut.DeliverWithReply<int>(envelope);

            Assert.Equal(expected, actual);
        }
        public void ReceiveWithResult_WhenCalledOnASingleDestination_ShouldReturnItsResult()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var expected = new object();

            var destination = new Mock<IDestination>();
            destination.Setup(x => x.ReceiveWithResult(envelope)).Returns(expected);

            var sut = InboxBuilder.New()
                .WithDestinationForType<SimpleMessage>(destination.Object)
                .Build();

            var actual = sut.ReceiveWithResult(typeof (SimpleMessage), envelope);

            Assert.Same(expected, actual);
        }
        public void SendWithReply_WhenBothDefaultAndSpecificCouriersRegisteredForMessage_ShouldUseSpecificCourier()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var defaultCourierMocks = _autoFixture.CreateMany<Mock<ICourier>>().ToList();
            var specificCourier = _autoFixture.CreateAnonymous<Mock<ICourier>>();

            specificCourier.Setup(x => x.DeliverWithReply<int>(envelope)).Returns(1).Verifiable();

            var sut = OutboxBuilder.New()
                .WithDefaultCouriers(defaultCourierMocks.Select(x => x.Object))
                .WithCourierForType<SimpleMessage>(specificCourier.Object)
                .Build();

            sut.SendWithReply<int>(envelope);

            specificCourier.Verify(x => x.DeliverWithReply<int>(envelope));
        }
        public void SendWithReply_WhenMultipleDefaultCouriersSetupAndGivenNonRegisteredMessageType_ShouldThrow()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var defaultCourierMocks = _autoFixture.CreateMany<Mock<ICourier>>().ToList();

            var sut = OutboxBuilder.New()
                .WithDefaultCouriers(defaultCourierMocks.Select(x => x.Object))
                .Build();

            Assert.Throws<SendException>(() => sut.SendWithReply<int>(envelope));
        }
        public void Send_WhenCalledForMessageThatIsNotPublished_ShouldCallDeliverOnlyOnce()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var courierMock = _autoFixture.CreateAnonymous<Mock<ICourier>>();
            courierMock.Setup(courier => courier.Deliver(envelope));

            var sut = OutboxBuilder.New()
                .WithCourierForType<SimpleMessage>(courierMock.Object)
                .Build();

            sut.Send(envelope);

            courierMock.Verify(courier => courier.Deliver(envelope), Times.Once());
        }
        public void Send_WhenCalledForMessageThatIsPublished_ShouldCallDeliverOnlyOnceOnEachCourier()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var courierMocks = _autoFixture.CreateMany<Mock<ICourier>>().ToList();
            courierMocks.ExpectEach(courier => courier.DeliverAsPublished(envelope));

            var sut = OutboxBuilder.New()
                .WithDefaultCouriers(courierMocks.Select(x => x.Object))
                .WithCouriersForType<SimpleMessage>(courierMocks.Select(x => x.Object))
                .Publish<SimpleMessage>()
                .Build();

            sut.Send(envelope);

            courierMocks.VerifyEach(courier => courier.DeliverAsPublished(envelope), Times.Once());
        }
        public void Receive_WhenCalled_ShouldDeliverEnvelopeToAllDestinationsForThatType()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var destinations = _autoFixture.CreateMany<Mock<IDestination>>().ToArray();
            destinations.ExpectEach(x => x.Receive(envelope));

            var sut = InboxBuilder.New()
                .WithDestinationsForType<SimpleMessage>(destinations.Select(x => x.Object))
                .Build();

            sut.Receive(typeof(SimpleMessage), envelope);

            destinations.VerifyEach(x => x.Receive(envelope));
        }
        public void Receive_WhenCalledWithNoExistingDestinations_ShouldNotThrow()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var sut = InboxBuilder.New()
                .Build();

            Assert.DoesNotThrow(() => sut.Receive(typeof(SimpleMessage), envelope));
        }
        public void Receive_WhenCalledWithIncompatibleEnvelopeAndDeclaredTypes_ShouldThrow()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());
            var destination = new Mock<IDestination>();

            var sut = InboxBuilder.New()
                .WithDestinationForType<DerivedSimpleMessage>(destination.Object)
                .Build();

            Assert.Throws<SendException>(() => sut.Receive(typeof(DerivedSimpleMessage), envelope));
        }
        public void Send_WhenMultipleDefaultCouriersSetupAndCalledForMessageThatIsNotPublished_ShouldThrow()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var courierMocks = _autoFixture.CreateMany<Mock<ICourier>>().ToList();
            courierMocks.ExpectEach(courier => courier.Deliver(envelope));

            var sut = OutboxBuilder.New()
                .WithDefaultCouriers(courierMocks.Select(x => x.Object))
                .Build();

            Assert.Throws<SendException>(() => sut.Send(envelope));
        }
        public void SendWithReply_WhenSingleDefaultCourierSetupAndGivenNonRegisteredMessageType_ShouldUseDefaultCourier()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var defaultCourier = _autoFixture.CreateAnonymous<Mock<ICourier>>();
            defaultCourier.Setup(x => x.DeliverWithReply<int>(envelope)).Returns(1).Verifiable();

            var sut = OutboxBuilder.New()
                .WithDefaultCourier(defaultCourier.Object)
                .Build();

            sut.SendWithReply<int>(envelope);

            defaultCourier.Verify(x => x.DeliverWithReply<int>(envelope));
        }
        public void Send_WhenSingeDefaultCourierSetupAndCalledForNonRegisteredMessageTypeThatIsNotPublished_ShouldCallDeliverOnDefaultCourier()
        {
            var envelope = new StandardEnvelope(new SimpleMessage());

            var courierMock = _autoFixture.CreateAnonymous<Mock<ICourier>>();
            courierMock.Setup(courier => courier.Deliver(envelope));

            var sut = OutboxBuilder.New()
                .WithDefaultCourier(courierMock.Object)
                .Build();

            sut.Send(envelope);

            courierMock.Verify(courier => courier.Deliver(envelope));
        }