public async Task Deliver_Message_Only_When_Referenced_Payloads_Are_Delivered()
        {
            AS4Message as4Message = await CreateAS4MessageFrom(deliveragent_message);

            string deliverLocation = DeliverPayloadLocationOf(as4Message.Attachments.First());

            CleanDirectoryAt(Path.GetDirectoryName(deliverLocation));

            // Act
            IPMode pmode = CreateReceivedPMode(
                deliverMessageLocation: DeliveryRoot,
                deliverPayloadLocation: @"%# \ (+_O) / -> Not a valid path");

            InMessage inMessage = CreateInMessageRepresentingUserMessage(as4Message.GetPrimaryMessageId(), as4Message, pmode);

            await InsertInMessageAsync(inMessage);

            // Assert
            var       spy    = DatabaseSpy.Create(_as4Msh.GetConfiguration());
            InMessage actual = await PollUntilPresent(
                () => spy.GetInMessageFor(im => im.Id == inMessage.Id && im.Status == InStatus.Exception.ToString()),
                TimeSpan.FromSeconds(10));

            Assert.Empty(Directory.EnumerateFiles(DeliveryRoot));
            Assert.Equal(InStatus.Exception, actual.Status.ToEnum <InStatus>());
            Assert.Equal(Operation.DeadLettered, actual.Operation);
        }
        public async Task Untouched_Forwarded_Message_Has_Still_Valid_Signature()
        {
            // Arrange
            string     ebmsMessageId = $"user-{Guid.NewGuid()}";
            IConfig    configuration = _msh.GetConfiguration();
            AS4Message tobeForwarded = CreateSignedAS4Message(ebmsMessageId, configuration);

            // Act
            InsertToBeForwardedMessage(
                msh: _msh,
                pmodeId: "Forwarding_Untouched_Push",
                mep: MessageExchangePattern.Push,
                tobeForwarded: tobeForwarded);

            // Assert

            var        databaseSpy    = DatabaseSpy.Create(configuration);
            OutMessage tobeSentRecord = await PollUntilPresent(
                () => databaseSpy.GetOutMessageFor(m => m.EbmsMessageId == ebmsMessageId &&
                                                   m.Operation == Operation.ToBeSent),
                timeout : TimeSpan.FromSeconds(20));

            Registry.Instance
            .MessageBodyStore
            .SaveAS4Message(
                configuration.InMessageStoreLocation,
                tobeForwarded);

            using (Stream tobeSentContents =
                       await tobeSentRecord.RetrieveMessageBody(Registry.Instance.MessageBodyStore))
            {
                AS4Message tobeSentMessage =
                    await SerializerProvider
                    .Default
                    .Get(tobeSentRecord.ContentType)
                    .DeserializeAsync(tobeSentContents, tobeSentRecord.ContentType);

                bool validSignature = tobeSentMessage.VerifySignature(
                    new VerifySignatureConfig(
                        allowUnknownRootCertificateAuthority: false,
                        attachments: tobeSentMessage.Attachments));

                Assert.True(validSignature,
                            "Forwarded AS4Message hasn't got a valid signature present while the message was forwaded 'untouched'");
            }
        }
        private static void InsertToBeForwardedMessage(
            AS4Component msh,
            string pmodeId,
            MessageExchangePattern mep,
            AS4Message tobeForwarded)
        {
            foreach (MessageUnit m in tobeForwarded.MessageUnits)
            {
                string location =
                    Registry.Instance
                    .MessageBodyStore
                    .SaveAS4Message(
                        msh.GetConfiguration().InMessageStoreLocation,
                        tobeForwarded);

                Operation operation =
                    m.MessageId == tobeForwarded.PrimaryMessageUnit.MessageId
                        ? Operation.ToBeForwarded
                        : Operation.NotApplicable;

                var inMessage = new InMessage(m.MessageId)
                {
                    Intermediary    = true,
                    Operation       = operation,
                    MessageLocation = location,
                    MEP             = mep,
                    ContentType     = tobeForwarded.ContentType
                };

                ReceivingProcessingMode forwardPMode =
                    msh.GetConfiguration()
                    .GetReceivingPModes()
                    .First(p => p.Id == pmodeId);

                inMessage.SetPModeInformation(forwardPMode);
                inMessage.SetStatus(InStatus.Received);
                inMessage.AssignAS4Properties(m);

                DatabaseSpy.Create(msh.GetConfiguration())
                .InsertInMessage(inMessage);
            }
        }