public async Task DeserializedPModeWithDefaultValuesContainsDefaultValues()
        {
            var sendingPMode = new SendingProcessingMode();

            var serialized = await AS4XmlSerializer.ToStringAsync(sendingPMode);

            var deserialized = await AS4XmlSerializer.FromStringAsync <SendingProcessingMode>(serialized);

            Assert.Equal(sendingPMode.Security.Encryption.Algorithm, deserialized.Security.Encryption.Algorithm);
            Assert.Equal(sendingPMode.Security.Encryption.AlgorithmKeySize, deserialized.Security.Encryption.AlgorithmKeySize);
        }
        private async Task <SendPMode> DetermineSendingPModeForSignalMessageAsync(AS4Message as4Message)
        {
            var firstNonPrSignalMessage = as4Message.SignalMessages.First(s => !(s is Model.Core.PullRequest));

            async Task <SendPMode> SelectSendingPModeBasedOnSendUserMessage()
            {
                if (String.IsNullOrWhiteSpace(firstNonPrSignalMessage.RefToMessageId))
                {
                    Logger.Warn(
                        $"Cannot determine SendingPMode for received {firstNonPrSignalMessage.GetType().Name} SignalMessage "
                        + "because it doesn't contain a RefToMessageId to link a UserMessage from which the SendingPMode needs to be selected");

                    return(null);
                }

                using (DatastoreContext dbContext = _createContext())
                {
                    var repository = new DatastoreRepository(dbContext);

                    // We must take into account that it is possible that we have an OutMessage that has
                    // been forwarded; in that case, we must not retrieve the sending - pmode since we
                    // will have to forward the signal message.
                    var referenced = repository.GetOutMessageData(
                        where : m => m.EbmsMessageId == firstNonPrSignalMessage.RefToMessageId && m.Intermediary == false,
                        selection: m => new { m.PMode, m.ModificationTime })
                                     .OrderByDescending(m => m.ModificationTime)
                                     .FirstOrDefault();

                    if (referenced == null)
                    {
                        Logger.Warn($"No referenced UserMessage record found for SignalMessage {firstNonPrSignalMessage.MessageId}");
                        return(null);
                    }

                    string pmodeString = referenced?.PMode;
                    if (String.IsNullOrWhiteSpace(pmodeString))
                    {
                        Logger.Warn($"No SendingPMode found in stored referenced UserMessage record for SignalMessage {firstNonPrSignalMessage.MessageId}");
                        return(null);
                    }

                    return(await AS4XmlSerializer.FromStringAsync <SendPMode>(pmodeString));
                }
            }

            SendPMode pmode = await SelectSendingPModeBasedOnSendUserMessage();

            if (pmode != null)
            {
                Logger.Debug($"Determined SendingPMode {pmode.Id} for received SignalMessages");
                return(pmode);
            }

            if (as4Message.IsMultiHopMessage == false)
            {
                throw new InvalidOperationException(
                          "Cannot determine SendingPMode for incoming SignalMessage because no "
                          + $"referenced UserMessage {firstNonPrSignalMessage.RefToMessageId} was found on this MSH");
            }

            return(null);
        }