public async Task <IActionResult> DownloadReport(string reportId)
        {
            Guard.ArgumentNotNull(reportId, nameof(reportId));

            SpecificationReportIdentifier specificationReportIdentifier = DecodeReportId(reportId);

            ReportType reportType    = GetReportType(specificationReportIdentifier.JobType);
            string     containerName = GetContainerName(reportType);
            string     blobName      = GenerateFileName(specificationReportIdentifier);

            ICloudBlob cloudBlob;

            try
            {
                cloudBlob = await _blobClient.GetBlobReferenceFromServerAsync(blobName, containerName);
            }
            catch (StorageException)
            {
                return(new StatusCodeResult((int)HttpStatusCode.NotFound));
            }

            cloudBlob.Metadata.TryGetValue("file_name", out string fileName);

            string blobUrl = _blobClient.GetBlobSasUrl(blobName, DateTimeOffset.Now.AddDays(1), SharedAccessBlobPermissions.Read, containerName);

            SpecificationsDownloadModel downloadModel = new SpecificationsDownloadModel {
                Url = blobUrl, FileName = fileName
            };

            return(new OkObjectResult(downloadModel));
        }
        public async Task DownloadSpecificationReport()
        {
            string reportId = NewRandomString();

            SpecificationsDownloadModel specificationsDownloadModel = new SpecificationsDownloadModel();

            string expectedUri = $"download-report/{reportId}";

            GivenTheResponse(expectedUri, specificationsDownloadModel, HttpMethod.Get);

            await AssertGetRequest(expectedUri,
                                   specificationsDownloadModel,
                                   () => _client.DownloadSpecificationReport(reportId));
        }
        public async Task DownloadReport_GivenFileNameAndType_ReturnsDownloadUrl(JobType jobType)
        {
            string sasUrl = "http://www.test.com/test.csv";

            string fundingLineCode = NewRandomString();
            string fundingPeriodId = NewRandomString();
            string fundingStreamId = NewRandomString();
            string specificationId = NewRandomString();

            SpecificationReportIdentifier id = new SpecificationReportIdentifier
            {
                JobType         = jobType,
                FundingLineCode = fundingLineCode,
                FundingPeriodId = fundingPeriodId,
                FundingStreamId = fundingStreamId,
                SpecificationId = specificationId
            };

            string expectedBlobName = jobType == JobType.HistoryPublishedProviderEstate ?
                                      $"funding-lines-{specificationId}-{jobType}-{fundingPeriodId}.csv" :
                                      $"funding-lines-{specificationId}-{jobType}-{fundingLineCode}-{fundingStreamId}.csv";

            _blobClient
            .GetBlobSasUrl(expectedBlobName, Arg.Any <DateTimeOffset>(), SharedAccessBlobPermissions.Read, PublishedProviderVersionsContainerName)
            .Returns(sasUrl);

            string fundingLineFileName = "Funding Lines";

            IDictionary <string, string> fundingLineFileMetadata = new Dictionary <string, string>
            {
                { "file_name", fundingLineFileName },
            };

            ICloudBlob fundingLineCloudBlob = Substitute.For <ICloudBlob>();

            fundingLineCloudBlob
            .Metadata
            .Returns(fundingLineFileMetadata);

            _blobClient
            .GetBlobReferenceFromServerAsync(expectedBlobName, PublishedProviderVersionsContainerName)
            .Returns(Task.FromResult(fundingLineCloudBlob));

            IActionResult result = await _service.DownloadReport(EncodeSpecificationReportIdentifier(id));

            result
            .Should()
            .BeOfType <OkObjectResult>()
            .Which
            .Value
            .Should()
            .BeOfType <SpecificationsDownloadModel>();

            SpecificationsDownloadModel downloadModel = ((OkObjectResult)result).Value as SpecificationsDownloadModel;

            downloadModel
            .Url
            .Should()
            .Be(sasUrl);

            downloadModel
            .FileName
            .Should()
            .Be(fundingLineFileName);
        }