Пример #1
0
        public void AggregateReportInfoReturned()
        {
            A.CallTo(() => _mimeMessageFactory.Create(A <Stream> ._)).Returns(CreateEmailMessage());
            A.CallTo(() => _aggregateReportDeserialiser.Deserialise(A <AttachmentInfo> ._, A <EmailMetadata> ._)).Returns(CreateAggregateReportInfo());

            AggregateReportInfo aggregateReportInfo = _aggregateReportParser.Parse(new EmailMessageInfo(new EmailMetadata("", "", "", "", 100), Stream.Null));

            Assert.That(aggregateReportInfo, Is.Not.Null);
        }
Пример #2
0
        public async Task Parse(EmailMessageInfo messageInfo)
        {
            _log.Debug($"Processing {messageInfo.EmailMetadata.OriginalUri}.");
            Stopwatch             stopwatch   = Stopwatch.StartNew();
            List <AttachmentInfo> attachments = _mimeMessageFactory
                                                .Create(messageInfo.EmailStream)
                                                .BodyParts.OfType <MimePart>()
                                                .Select(_attachmentStreamNormaliser.Normalise)
                                                .Where(_ => !_.Equals(AttachmentInfo.EmptyAttachmentInfo))
                                                .ToList();

            if (attachments.Count == 0)
            {
                _log.Warn($"Didnt find any aggregate report attachments for {messageInfo.EmailMetadata.OriginalUri}.");
            }
            else
            {
                _log.Debug($"Found {attachments.Count} aggregate report attachment(s) for {messageInfo.EmailMetadata.OriginalUri}.");
            }
            _log.Debug($"Aggregate report attachment processing took {stopwatch.Elapsed}");
            stopwatch.Reset();

            stopwatch.Start();
            List <AggregateReportInfo> aggregateReports = attachments
                                                          .Select(_ => _aggregateReportDeserialiser.Deserialise(_, messageInfo.EmailMetadata))
                                                          .ToList();

            _log.Debug($"Deserialising aggregate reports took {stopwatch.Elapsed}");
            stopwatch.Stop();

            await Task.WhenAll(aggregateReports.Select(_ => _aggregateReportDao.Add(_aggregateReportToEntityConverter.ConvertToEntity(_))));

            attachments.ForEach(_ => _.Dispose());
        }
Пример #3
0
        public AggregateReportInfo Parse(EmailMessageInfo messageInfo)
        {
            List <AttachmentInfo> attachments = _mimeMessageFactory
                                                .Create(messageInfo.EmailStream)
                                                .BodyParts.OfType <MimePart>()
                                                .Select(_attachmentStreamNormaliser.Normalise)
                                                .Where(_ => !_.Equals(AttachmentInfo.EmptyAttachmentInfo))
                                                .ToList();

            string logString = $"{messageInfo.EmailMetadata.OriginalUri}, message Id: {messageInfo.EmailMetadata.MessageId}, request Id: {messageInfo.EmailMetadata.RequestId}";

            if (attachments.Count == 0)
            {
                throw new ApplicationException(
                          $"Failed to parse: no attachment found where one was expected for {logString}.");
            }

            if (attachments.Count > 1)
            {
                var attachmentFilenames       = attachments.Select(attachment => attachment.AttachmentMetadata.Filename).ToArray();
                var attachmentFilenamesString = string.Join(", ", attachmentFilenames);

                throw new ApplicationException(
                          $"Failed to parse: multiple attachments found where only one was expected for {logString}. {Environment.NewLine} Attachment filenames: {attachmentFilenamesString}");
            }

            AttachmentInfo attachmentInfo = attachments[0];

            AggregateReportInfo aggregateReportInfo = _aggregateReportDeserialiser.Deserialise(attachmentInfo, messageInfo.EmailMetadata);

            _log.LogInformation(
                $"Successfully processed attachment for {logString}.");

            return(aggregateReportInfo);
        }
        public void NoPersistorsActiveNoProcessingOccursUserIsWarned()
        {
            A.CallTo(() => _multiAttachmentPersistor.Active).Returns(false);
            A.CallTo(() => _multiDenormalisedRecordPersistor.Active).Returns(false);
            A.CallTo(() => _mulitAggregateReportPersistor.Active).Returns(false);
            A.CallTo(() => _mimeMessageFactory.Create(A <Stream> ._)).Returns(CreateEmailMessage());

            _aggregateReportParser.Parse(new EmailMessageInfo(new EmailMetadata("", "", "", 100), Stream.Null));

            A.CallTo(() => _attachmentStreamNormaliser.Normalise(A <MimePart> ._)).MustNotHaveHappened();
            A.CallTo(() => _aggregateReportDeserialiser.Deserialise(A <AttachmentInfo> ._, A <EmailMetadata> ._)).MustNotHaveHappened();
            A.CallTo(() => _log.Warn(A <string> ._)).MustHaveHappened(Repeated.Exactly.Once);
            A.CallTo(() => _denormalisedRecordConverter.ToDenormalisedRecord(A <Domain.Dmarc.AggregateReport> ._, A <string> ._)).MustNotHaveHappened();
            A.CallTo(() => _multiAttachmentPersistor.Persist(A <List <AttachmentInfo> > ._)).MustNotHaveHappened();
            A.CallTo(() => _multiDenormalisedRecordPersistor.Persist(A <IEnumerable <DenormalisedRecord> > ._)).MustNotHaveHappened();
            A.CallTo(() => _mulitAggregateReportPersistor.Persist(A <IEnumerable <AggregateReportInfo> > ._)).MustNotHaveHappened();
        }
Пример #5
0
        public AggregateReportInfo Parse(EmailMessageInfo messageInfo)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            _log.Debug($"Processing {messageInfo.EmailMetadata.OriginalUri}.");

            List <AttachmentInfo> attachments = _mimeMessageFactory
                                                .Create(messageInfo.EmailStream)
                                                .BodyParts.OfType <MimePart>()
                                                .Select(_attachmentStreamNormaliser.Normalise)
                                                .Where(_ => !_.Equals(AttachmentInfo.EmptyAttachmentInfo))
                                                .ToList();

            _log.Debug($"Aggregate report attachment processing took {stopwatch.Elapsed}");
            stopwatch.Reset();

            AttachmentInfo attachment = attachments.Single();

            stopwatch.Start();
            AggregateReportInfo aggregateReport = _aggregateReportDeserialiser.Deserialise(attachment, messageInfo.EmailMetadata);

            _log.Debug($"Deserialising aggregate report took {stopwatch.Elapsed}");
            stopwatch.Reset();

            _log.Debug($"Found {aggregateReport.AggregateReport.Records.Length} records in {aggregateReport.AttachmentMetadata.Filename}");

            if (_multiAttachmentPersistor.Active)
            {
                stopwatch.Start();
                _multiAttachmentPersistor.Persist(attachment);
                _log.Debug($"Persisting attachments took {stopwatch.Elapsed}");
                stopwatch.Reset();
            }

            attachment?.Dispose();

            return(aggregateReport);
        }
        public AggregateReportInfo Parse(EmailMessageInfo messageInfo)
        {
            _log.Info($"Processing attachments for {messageInfo.EmailMetadata.OriginalUri}, message Id: {messageInfo.EmailMetadata.MessageId}, request Id: {messageInfo.EmailMetadata.RequestId}.");
            List <AttachmentInfo> attachments = _mimeMessageFactory
                                                .Create(messageInfo.EmailStream)
                                                .BodyParts.OfType <MimePart>()
                                                .Select(_attachmentStreamNormaliser.Normalise)
                                                .Where(_ => !_.Equals(AttachmentInfo.EmptyAttachmentInfo))
                                                .ToList();

            _log.Info($"Successfully processed attachments for {messageInfo.EmailMetadata.OriginalUri}, message Id: {messageInfo.EmailMetadata.MessageId}, request Id: {messageInfo.EmailMetadata.RequestId}.");

            AttachmentInfo attachment = attachments.Single();

            _log.Debug($"Deserialising aggregate report {attachment.AttachmentMetadata.Filename} for {messageInfo.EmailMetadata.OriginalUri}, message Id: {messageInfo.EmailMetadata.MessageId}, request Id: {messageInfo.EmailMetadata.RequestId}.");
            AggregateReportInfo aggregateReportInfo = _aggregateReportDeserialiser.Deserialise(attachment, messageInfo.EmailMetadata);

            _log.Debug($"Successfully deserialised aggregate report with Id: {aggregateReportInfo.AggregateReport.ReportMetadata.ReportId} from file {attachment.AttachmentMetadata.Filename} from S3 bucket {messageInfo.EmailMetadata.OriginalUri}, message Id: {messageInfo.EmailMetadata.MessageId}, request Id: {messageInfo.EmailMetadata.RequestId}.");

            attachments.ForEach(_ => _.Dispose());

            return(aggregateReportInfo);
        }
Пример #7
0
        public void Parse(EmailMessageInfo messageInfo)
        {
            try
            {
                Stopwatch             stopwatch   = new Stopwatch();
                List <AttachmentInfo> attachments = null;
                if (_multiAttachmentPersistor.Active ||
                    _multiDenormalisedRecordPersistor.Active ||
                    _multiAggregateReportPersistor.Active)
                {
                    _log.Debug($"Processing {messageInfo.EmailMetadata.OriginalUri}.");
                    stopwatch.Start();

                    attachments = _mimeMessageFactory
                                  .Create(messageInfo.EmailStream)
                                  .BodyParts.OfType <MimePart>()
                                  .Select(_attachmentStreamNormaliser.Normalise)
                                  .Where(_ => !_.Equals(AttachmentInfo.EmptyAttachmentInfo))
                                  .ToList();

                    if (attachments.Count == 0)
                    {
                        _log.Warn($"Didnt find any aggregate report attachments for {messageInfo.EmailMetadata.OriginalUri}.");
                    }
                    else
                    {
                        _log.Debug($"Found {attachments.Count} aggregate report attachment(s) for {messageInfo.EmailMetadata.OriginalUri}.");
                    }
                    _log.Debug($"Aggregate report attachment processing took {stopwatch.Elapsed}");
                    stopwatch.Reset();
                }
                else
                {
                    _log.Warn($"Not Processing {messageInfo.EmailMetadata.OriginalUri} as no persistors found.");
                }

                List <AggregateReportInfo> aggregateReports = null;
                if (_multiDenormalisedRecordPersistor.Active ||
                    _multiAggregateReportPersistor.Active)
                {
                    stopwatch.Start();
                    aggregateReports = attachments
                                       .Select(_ => _aggregateReportDeserialiser.Deserialise(_, messageInfo.EmailMetadata))
                                       .ToList();
                    _log.Debug($"Deserialising aggregate reports took {stopwatch.Elapsed}");
                    stopwatch.Reset();

                    aggregateReports.ForEach(_ => _log.Debug($"Found {_.AggregateReport.Records.Length} records in {_.AttachmentMetadata.Filename}"));
                }

                List <DenormalisedRecord> denormalisedRecords = null;
                if (_multiDenormalisedRecordPersistor.Active)
                {
                    stopwatch.Start();
                    denormalisedRecords = aggregateReports
                                          .Select(_ => _denormalisedRecordConverter.ToDenormalisedRecord(_.AggregateReport, messageInfo.EmailMetadata.OriginalUri))
                                          .SelectMany(_ => _)
                                          .ToList();
                    _log.Debug($"Denormalised record processing took {stopwatch.Elapsed}");
                    stopwatch.Reset();
                }

                if (_multiAttachmentPersistor.Active)
                {
                    stopwatch.Start();
                    _multiAttachmentPersistor.Persist(attachments);
                    _log.Debug($"Persisting attachments took {stopwatch.Elapsed}");
                    stopwatch.Reset();
                }

                if (_multiDenormalisedRecordPersistor.Active)
                {
                    stopwatch.Start();
                    _multiDenormalisedRecordPersistor.Persist(denormalisedRecords);
                    _log.Debug($"Persisting denormalised records took {stopwatch.Elapsed}");
                    stopwatch.Reset();
                }

                if (_multiAggregateReportPersistor.Active)
                {
                    stopwatch.Start();
                    _multiAggregateReportPersistor.Persist(aggregateReports);
                    _log.Debug($"Persisting denormalised records took {stopwatch.Elapsed}");
                    stopwatch.Reset();
                }

                attachments?.ForEach(_ => _.Dispose());
            }
            catch (Exception e)
            {
                _log.Error($"An error occurred processing {messageInfo.EmailMetadata.OriginalUri}{Environment.NewLine}{e.Message}{Environment.NewLine}{Environment.NewLine}{e.StackTrace}");
            }
        }