public void AttachmentProviderGetsUploader(
            string expectedKey,
            IAttachmentUploader expectedUploader)
        {
            // Arrange
            var provider = AttachmentUploaderProvider.Instance;

            // Act
            IAttachmentUploader actualUploader = provider.Get(expectedKey);

            // Assert
            Assert.IsType(expectedUploader.GetType(), actualUploader);
        }
        public void ReturnsSameInstance_EvenWhenAcceptedOtherwise()
        {
            // Arrange
            var expectedUploader = Mock.Of <IAttachmentUploader>();
            var sut = new StubAttachmentUploaderProvider(expectedUploader);

            sut.Accept(condition: null, uploader: new StubAttachmentUploader(downloadUrl: null));

            // Act
            IAttachmentUploader actualUploader = sut.Get(type: null);

            // Assert
            Assert.Equal(expectedUploader, actualUploader);
        }
        /// <summary>
        /// Adds a new <see cref="IAttachmentUploader" /> implementation
        /// for a given <paramref name="condition" />
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="uploader"></param>
        public void Accept(Func <string, bool> condition, IAttachmentUploader uploader)
        {
            if (condition == null)
            {
                throw new ArgumentNullException(nameof(condition));
            }

            if (uploader == null)
            {
                throw new ArgumentNullException(nameof(uploader));
            }

            _uploaders.Add(new UploaderEntry(condition, uploader));
        }
        private IAttachmentUploader GetAttachmentUploader(ReceivingProcessingMode pmode)
        {
            Method payloadReferenceMethod = pmode.MessageHandling.DeliverInformation.PayloadReferenceMethod;
            IAttachmentUploader uploader  = _provider.Get(payloadReferenceMethod.Type);

            if (uploader == null)
            {
                throw new ArgumentNullException(
                          nameof(uploader),
                          $@"No {nameof(IAttachmentUploader)} can be found for PayloadReferenceMethod.Type = {payloadReferenceMethod.Type}");
            }

            uploader.Configure(payloadReferenceMethod);
            return(uploader);
        }
        private static async Task <UploadResult> TryUploadAttachmentAsync(
            Attachment attachment,
            DeliverMessageEnvelope deliverMessage,
            IAttachmentUploader uploader)
        {
            try
            {
                Logger.Trace($"Start Uploading Attachment {attachment.Id}...");
                Task <UploadResult> uploadAsync = uploader.UploadAsync(attachment, deliverMessage.Message.MessageInfo);
                if (uploadAsync == null)
                {
                    throw new ArgumentNullException(
                              nameof(uploadAsync),
                              $@"{uploader.GetType().Name} returns 'null' for Attachment {attachment.Id}");
                }

                UploadResult attachmentResult = await uploadAsync.ConfigureAwait(false);

                attachment.ResetContentPosition();

                Payload referencedPayload =
                    deliverMessage.Message.Payloads.FirstOrDefault(attachment.Matches);

                if (referencedPayload == null)
                {
                    throw new InvalidOperationException(
                              $"No referenced <Payload/> element found in DeliverMessage to assign the upload location to with attachment Id = {attachment.Id}");
                }

                referencedPayload.Location = attachmentResult.DownloadUrl;

                Logger.Trace($"Attachment {attachment.Id} uploaded succesfully");
                return(attachmentResult);
            }
            catch (Exception exception)
            {
                Logger.Error($"Attachment {attachment.Id} cannot be uploaded because of an exception: {Environment.NewLine}" + exception);
                return(UploadResult.FatalFail);
            }
        }
Exemplo n.º 6
0
        public async Task Retries_Uploading_When_Uploader_Returns_RetryableFail_Result(UploadRetry input)
        {
            // Arrange
            string    id = "deliver-" + Guid.NewGuid();
            InMessage im = InsertInMessage(id);

            var r = RetryReliability.CreateForInMessage(
                refToInMessageId: im.Id,
                maxRetryCount: input.MaxRetryCount,
                retryInterval: default(TimeSpan),
                type: RetryType.Delivery);

            r.CurrentRetryCount = input.CurrentRetryCount;
            GetDataStoreContext.InsertRetryReliability(r);

            var        a           = new FilledAttachment();
            var        userMessage = new FilledUserMessage(id, a.Id);
            AS4Message as4Msg      = AS4Message.Create(userMessage);

            as4Msg.AddAttachment(a);

            MessagingContext fixture = await PrepareAS4MessageForDeliveryAsync(as4Msg, CreateReceivingPModeWithPayloadMethod());

            IAttachmentUploader stub = CreateStubAttachmentUploader(fixture.DeliverMessage.Message.MessageInfo, input.UploadResult);

            // Act
            await CreateUploadStep(stub).ExecuteAsync(fixture);

            // Assert
            GetDataStoreContext.AssertInMessage(id, actual =>
            {
                Assert.NotNull(actual);
                Assert.Equal(input.ExpectedStatus, actual.Status.ToEnum <InStatus>());
                Assert.Equal(input.ExpectedOperation, actual.Operation);
            });
        }
        /// <summary>
        /// Start uploading the AS4 Message Payloads
        /// </summary>
        /// <param name="messagingContext"></param>
        /// <returns></returns>
        public async Task <StepResult> ExecuteAsync(MessagingContext messagingContext)
        {
            if (messagingContext == null)
            {
                throw new ArgumentNullException(nameof(messagingContext));
            }

            if (messagingContext.DeliverMessage == null)
            {
                throw new InvalidOperationException(
                          $"{nameof(UploadAttachmentsStep)} requires a DeliverMessage to upload the attachments from but no DeliverMessage is present in the MessagingContext");
            }

            DeliverMessageEnvelope deliverEnvelope = messagingContext.DeliverMessage;

            if (!deliverEnvelope.Attachments.Any())
            {
                Logger.Debug("(Deliver) No attachments to upload for DeliverMessage");
                return(StepResult.Success(messagingContext));
            }

            if (messagingContext.ReceivingPMode == null)
            {
                throw new InvalidOperationException(
                          "Unable to send DeliverMessage: no ReceivingPMode is set");
            }

            if (messagingContext.ReceivingPMode.MessageHandling?.DeliverInformation?.PayloadReferenceMethod == null)
            {
                throw new InvalidOperationException(
                          $"Unable to send the DeliverMessage: the ReceivingPMode {messagingContext.ReceivingPMode.Id} "
                          + "does not contain any <PayloadReferenceMethod/> element in the MessageHandling.Deliver element. "
                          + "Please provide a correct <PayloadReferenceMethod/> tag to indicate where the attachments of the DeliverMessage should be sent to.");
            }

            if (messagingContext.ReceivingPMode.MessageHandling.DeliverInformation.PayloadReferenceMethod.Type == null)
            {
                throw new InvalidOperationException(
                          $"Unable to send the DeliverMessage: the ReceivingPMode {messagingContext.ReceivingPMode.Id} "
                          + "does not contain any <Type/> element in the MessageHandling.Deliver.PayloadReferenceMethod element "
                          + "that indicates which uploading strategy that must be used."
                          + "Default uploading strategies are: 'FILE' and 'HTTP'. See 'Deliver Uploading' for more information");
            }

            IAttachmentUploader uploader = GetAttachmentUploader(messagingContext.ReceivingPMode);
            var results = new Collection <UploadResult>();

            foreach (Attachment att in deliverEnvelope.Attachments)
            {
                UploadResult result = await TryUploadAttachmentAsync(att, deliverEnvelope, uploader).ConfigureAwait(false);

                results.Add(result);
            }

            SendResult accResult = results
                                   .Select(r => r.Status)
                                   .Aggregate(SendResultUtils.Reduce);

            await UpdateDeliverMessageAccordinglyToUploadResult(
                messageId : deliverEnvelope.Message.MessageInfo?.MessageId,
                status : accResult);

            if (accResult == SendResult.Success)
            {
                return(StepResult.Success(messagingContext));
            }

            return(StepResult.Failed(messagingContext));
        }
 /// <summary>
 /// Accept a <see cref="IAttachmentUploader"/> implementation in the <see cref="IAttachmentUploaderProvider"/>.
 /// </summary>
 /// <param name="condition">Condition for which the <see cref="IAttachmentUploader"/> must be used.</param>
 /// <param name="uploader"><see cref="IAttachmentUploader"/> implementation to be used.</param>
 public void Accept(Func <string, bool> condition, IAttachmentUploader uploader)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="StubAttachmentUploaderProvider"/> class.
 /// </summary>
 /// <param name="uploader"></param>
 public StubAttachmentUploaderProvider(IAttachmentUploader uploader)
 {
     _configedUploader = uploader;
 }
Exemplo n.º 10
0
 /// <summary>
 /// Creates the upload step.
 /// </summary>
 /// <param name="uploader">The uploader.</param>
 /// <returns></returns>
 private IStep CreateUploadStep(IAttachmentUploader uploader)
 {
     return(new UploadAttachmentsStep(new StubAttachmentUploaderProvider(uploader), GetDataStoreContext));
 }
 public UploaderEntry(Func <string, bool> condition, IAttachmentUploader uploader)
 {
     Condition = condition;
     Uploader  = uploader;
 }