コード例 #1
0
        public void FileAggregate_RecordFileLineAsRejected_FileLineUpdatedAsRejected()
        {
            FileAggregate fileAggregate = FileAggregate.Create(TestData.FileId);

            fileAggregate.CreateFile(TestData.FileImportLogId, TestData.EstateId, TestData.MerchantId, TestData.UserId,
                                     TestData.FileProfileId, TestData.FileLocation, TestData.FileUploadedDateTime);
            fileAggregate.AddFileLine(TestData.FileLine);
            fileAggregate.RecordFileLineAsRejected(TestData.LineNumber, TestData.RejectionReason);

            FileDetails fileDetails = fileAggregate.GetFile();

            fileDetails.FileLines.ShouldNotBeNull();
            fileDetails.FileLines.ShouldNotBeEmpty();
            fileDetails.FileLines.ShouldHaveSingleItem();
            fileDetails.FileLines.Single().LineNumber.ShouldBe(1);
            fileDetails.FileLines.Single().LineData.ShouldBe(TestData.FileLine);
            fileDetails.FileLines.Single().ProcessingResult.ShouldBe(ProcessingResult.Rejected);
            fileDetails.FileLines.Single().RejectedReason.ShouldBe(TestData.RejectionReason);
            fileDetails.ProcessingCompleted.ShouldBeTrue();
            fileDetails.ProcessingSummary.ShouldNotBeNull();
            fileDetails.ProcessingSummary.TotalLines.ShouldBe(1);
            fileDetails.ProcessingSummary.NotProcessedLines.ShouldBe(0);
            fileDetails.ProcessingSummary.FailedLines.ShouldBe(0);
            fileDetails.ProcessingSummary.SuccessfullyProcessedLines.ShouldBe(0);
            fileDetails.ProcessingSummary.IgnoredLines.ShouldBe(0);
            fileDetails.ProcessingSummary.RejectedLines.ShouldBe(1);
        }
コード例 #2
0
        public static FileAggregate GetFileAggregateWithLinesAlreadyProcessed()
        {
            FileAggregate fileAggregate = new FileAggregate();

            fileAggregate.CreateFile(TestData.FileImportLogId, TestData.EstateId, TestData.MerchantId, TestData.UserId, TestData.FileProfileId, TestData.OriginalFileName, TestData.FileUploadedDateTime);
            fileAggregate.AddFileLine("D,1,2");
            fileAggregate.AddFileLine("D,1,2");
            fileAggregate.AddFileLine("D,1,2");
            fileAggregate.AddFileLine("D,1,2");
            fileAggregate.RecordFileLineAsSuccessful(1, TestData.TransactionId);
            fileAggregate.RecordFileLineAsRejected(2, TestData.RejectionReason);
            fileAggregate.RecordFileLineAsFailed(3, TestData.TransactionId, "-1", "Failed");
            fileAggregate.RecordFileLineAsIgnored(4);

            return(fileAggregate);
        }
コード例 #3
0
        public static FileAggregate GetFileAggregateWithBlankLine()
        {
            FileAggregate fileAggregate = new FileAggregate();

            fileAggregate.CreateFile(TestData.FileImportLogId, TestData.EstateId, TestData.MerchantId, TestData.UserId, TestData.FileProfileId, TestData.OriginalFileName, TestData.FileUploadedDateTime);
            fileAggregate.AddFileLine(String.Empty);

            return(fileAggregate);
        }
コード例 #4
0
        public void FileAggregate_AddFileLine_FileNotCreated_FileLineAdded()
        {
            FileAggregate fileAggregate = FileAggregate.Create(TestData.FileId);

            Should.Throw <InvalidOperationException>(() =>
            {
                fileAggregate.AddFileLine(TestData.FileLine);
            });
        }
コード例 #5
0
        public void FileAggregate_RecordFileLineAsRejected_LineNotFound_ErrorThrown()
        {
            FileAggregate fileAggregate = FileAggregate.Create(TestData.FileId);

            fileAggregate.CreateFile(TestData.FileImportLogId, TestData.EstateId, TestData.MerchantId, TestData.UserId,
                                     TestData.FileProfileId, TestData.FileLocation, TestData.FileUploadedDateTime);
            fileAggregate.AddFileLine(TestData.FileLine);
            Should.Throw <NotFoundException>(() =>
            {
                fileAggregate.RecordFileLineAsRejected(TestData.NotFoundLineNumber, TestData.RejectionReason);
            });
        }
コード例 #6
0
        public void FileAggregate_AddFileLine_FileLineAdded()
        {
            FileAggregate fileAggregate = FileAggregate.Create(TestData.FileId);

            fileAggregate.CreateFile(TestData.FileImportLogId, TestData.EstateId, TestData.MerchantId, TestData.UserId,
                                     TestData.FileProfileId, TestData.FileLocation, TestData.FileUploadedDateTime);
            fileAggregate.AddFileLine(TestData.FileLine);

            FileDetails fileDetails = fileAggregate.GetFile();

            fileDetails.FileLines.ShouldNotBeNull();
            fileDetails.FileLines.ShouldNotBeEmpty();
            fileDetails.FileLines.ShouldHaveSingleItem();
            fileDetails.FileLines.Single().LineData.ShouldBe(TestData.FileLine);
            fileDetails.ProcessingCompleted.ShouldBeFalse();
            fileDetails.ProcessingSummary.ShouldNotBeNull();
            fileDetails.ProcessingSummary.TotalLines.ShouldBe(1);
            fileDetails.ProcessingSummary.NotProcessedLines.ShouldBe(1);
            fileDetails.ProcessingSummary.FailedLines.ShouldBe(0);
            fileDetails.ProcessingSummary.SuccessfullyProcessedLines.ShouldBe(0);
            fileDetails.ProcessingSummary.IgnoredLines.ShouldBe(0);
        }
コード例 #7
0
        private async Task <Unit> ProcessFile(Guid fileId,
                                              Guid fileProfileId,
                                              String fileName,
                                              CancellationToken cancellationToken)
        {
            IFileInfo   inProgressFile = null;
            FileProfile fileProfile    = null;

            try
            {
                fileProfile = await this.FileProcessorManager.GetFileProfile(fileProfileId, cancellationToken);

                if (fileProfile == null)
                {
                    throw new NotFoundException($"No file profile found with Id {fileProfileId}");
                }

                // Check the processed/failed directories exist
                if (this.FileSystem.Directory.Exists(fileProfile.ProcessedDirectory) == false)
                {
                    Logger.LogWarning($"Creating Directory {fileProfile.ProcessedDirectory} as not found");
                    this.FileSystem.Directory.CreateDirectory(fileProfile.ProcessedDirectory);
                }

                if (this.FileSystem.Directory.Exists(fileProfile.FailedDirectory) == false)
                {
                    Logger.LogWarning($"Creating Directory {fileProfile.FailedDirectory} as not found");
                    this.FileSystem.Directory.CreateDirectory(fileProfile.FailedDirectory);
                }

                inProgressFile = this.FileSystem.FileInfo.FromFileName(fileName);

                if (inProgressFile.Exists == false)
                {
                    throw new FileNotFoundException($"File {inProgressFile.FullName} not found");
                }

                FileAggregate fileAggregate =
                    await this.FileAggregateRepository.GetLatestVersion(fileId, cancellationToken);

                if (fileAggregate.IsCreated == false)
                {
                    throw new InvalidOperationException($"File with Id {fileId} not created");
                }

                String fileContent = null;
                //Open file for Read\Write
                using (Stream fs = inProgressFile.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read))
                {
                    //Create object of StreamReader by passing FileStream object on which it needs to operates on
                    using (StreamReader sr = new StreamReader(fs))
                    {
                        //Use ReadToEnd method to read all the content from file
                        fileContent = await sr.ReadToEndAsync();
                    }
                }

                if (String.IsNullOrEmpty(fileContent) == false)
                {
                    String[] fileLines = fileContent.Split(fileProfile.LineTerminator);

                    foreach (String fileLine in fileLines)
                    {
                        fileAggregate.AddFileLine(fileLine);
                    }

                    await this.FileAggregateRepository.SaveChanges(fileAggregate, cancellationToken);
                }

                Logger.LogInformation(
                    $"About to move file {inProgressFile.Name} to [{fileProfile.ProcessedDirectory}]");

                // TODO: Move file now
                inProgressFile.MoveTo($"{fileProfile.ProcessedDirectory}/{inProgressFile.Name}");

                return(new Unit());
            }
            catch (Exception e)
            {
                if (inProgressFile != null && fileProfile != null)
                {
                    inProgressFile.MoveTo($"{fileProfile.FailedDirectory}/{inProgressFile.Name}");
                }

                Logger.LogError(e);
                throw;
            }
        }