public async Task Should_discard_edit_when_different_edit_already_exists()
        {
            var failedMessageId = Guid.NewGuid().ToString();
            var previousEdit    = Guid.NewGuid().ToString();

            await CreateFailedMessage(failedMessageId);

            using (var session = Store.OpenAsyncSession())
            {
                await session.StoreAsync(new FailedMessageEdit
                {
                    Id = FailedMessageEdit.MakeDocumentId(failedMessageId),
                    FailedMessageId = failedMessageId,
                    EditId          = previousEdit
                });

                await session.SaveChangesAsync();
            }

            var message = CreateEditMessage(failedMessageId);
            await Handler.Handle(message, new TestableMessageHandlerContext());

            using (var session = Store.OpenAsyncSession())
            {
                var failedMessage = await session.LoadAsync <FailedMessage>(FailedMessage.MakeDocumentId(failedMessageId));

                var editOperation = await session.LoadAsync <FailedMessageEdit>(FailedMessageEdit.MakeDocumentId(failedMessageId));

                Assert.AreEqual(FailedMessageStatus.Unresolved, failedMessage.Status);
                Assert.AreEqual(previousEdit, editOperation.EditId);
            }

            Assert.IsEmpty(Dispatcher.DispatchedMessages);
        }
        public async Task Should_dispatch_edited_message_when_first_edit()
        {
            var failedMessage = await CreateFailedMessage();

            var newBodyContent = Encoding.UTF8.GetBytes("new body content");
            var newHeaders     = new Dictionary <string, string> {
                { "someKey", "someValue" }
            };
            var message = CreateEditMessage(failedMessage.UniqueMessageId, newBodyContent, newHeaders);

            var handlerContent = new TestableMessageHandlerContext();
            await Handler.Handle(message, handlerContent);

            var dispatchedMessage = Dispatcher.DispatchedMessages.Single();

            Assert.AreEqual(
                failedMessage.ProcessingAttempts.Last().FailureDetails.AddressOfFailingEndpoint,
                dispatchedMessage.Item1.Destination);
            Assert.AreEqual(newBodyContent, dispatchedMessage.Item1.Message.Body);
            Assert.AreEqual("someValue", dispatchedMessage.Item1.Message.Headers["someKey"]);
            using (var session = Store.OpenAsyncSession())
            {
                failedMessage = await session.LoadAsync <FailedMessage>(failedMessage.Id);

                var editOperation = await session.LoadAsync <FailedMessageEdit>(FailedMessageEdit.MakeDocumentId(failedMessage.UniqueMessageId));

                Assert.AreEqual(FailedMessageStatus.Resolved, failedMessage.Status);
                Assert.AreEqual(handlerContent.MessageId, editOperation.EditId);
            }
        }