private void AndTheFileSystemCacheFolderWasEnsuredToExist()
 {
     _fileSystemCache
     .Received(1)
     .Add(Arg.Any <FileSystemCacheKey>(),
          Arg.Any <Stream>(),
          Arg.Is(CancellationToken.None),
          Arg.Is(true));
 }
        public void EnsureFoldersExistDelegatesToCache()
        {
            GivenTheSettings(1, 1, true, true);

            _preLoader.EnsureFoldersExists();

            _cache
            .Received(1)
            .EnsureFoldersExist(FundingFileSystemCacheKey.Folder, ProviderFundingFileSystemCacheKey.Folder);
        }
        public async Task GetProviderFundingVersionsBody_GivenReturnsFromBlobStorage_ReturnsOK(bool isFileSystemCacheEnabled,
                                                                                               int expectedCacheAddCount)
        {
            //Arrange
            string template = "just a string";

            byte[] bytes = Encoding.UTF8.GetBytes(template);

            Stream memoryStream = new MemoryStream(bytes);

            ILogger          logger          = CreateLogger();
            ICloudBlob       cloudBlob       = CreateBlob();
            IFileSystemCache fileSystemCache = CreateFileSystemCache();
            IExternalApiFileSystemCacheSettings cacheSettings = Substitute.For <IExternalApiFileSystemCacheSettings>();

            cacheSettings.IsEnabled.Returns(isFileSystemCacheEnabled);

            IBlobClient blobClient = CreateBlobClient();

            blobClient
            .BlobExistsAsync(blobName)
            .Returns(true);

            blobClient
            .GetBlobReferenceFromServerAsync(blobName)
            .Returns(cloudBlob);

            blobClient
            .DownloadToStreamAsync(cloudBlob)
            .Returns(memoryStream);

            ProviderFundingVersionService service = CreateProviderFundingVersionService(logger, blobClient, fileSystemCache, cacheSettings);

            //Act
            IActionResult result = await service.GetProviderFundingVersion(providerFundingVersion);

            //Assert
            result
            .Should()
            .BeOfType <ContentResult>();

            ContentResult contentResult = result as ContentResult;

            contentResult
            .ContentType
            .Should()
            .Be("application/json");

            contentResult
            .StatusCode
            .Should()
            .Be((int)HttpStatusCode.OK);

            contentResult
            .Content
            .Should()
            .Be(template);

            fileSystemCache
            .Received(expectedCacheAddCount)
            .Add(Arg.Is <ProviderFundingFileSystemCacheKey>(_ => _.Key == providerFundingVersion),
                 memoryStream,
                 CancellationToken.None);
        }
        public async Task GetProviderFundingVersionsBody_GivenReturnsFromFileSystemCacheAndSkipBlobClientFetch_ReturnsOK()
        {
            //Arrange
            string template = "just a string";

            byte[] bytes = Encoding.UTF8.GetBytes(template);

            Stream memoryStream = new MemoryStream(bytes);

            ILogger          logger          = CreateLogger();
            IFileSystemCache fileSystemCache = CreateFileSystemCache();
            IBlobClient      blobClient      = CreateBlobClient();

            fileSystemCache.Exists(Arg.Is <ProviderFundingFileSystemCacheKey>(
                                       _ => _.Key == providerFundingVersion))
            .Returns(true);

            fileSystemCache.Get(Arg.Is <ProviderFundingFileSystemCacheKey>(
                                    _ => _.Key == providerFundingVersion))
            .Returns(memoryStream);

            ProviderFundingVersionService service = CreateProviderFundingVersionService(logger, blobClient, fileSystemCache);

            //Act
            IActionResult result = await service.GetProviderFundingVersion(providerFundingVersion);

            //Assert
            result
            .Should()
            .BeOfType <ContentResult>();

            ContentResult contentResult = result as ContentResult;

            contentResult
            .ContentType
            .Should()
            .Be("application/json");

            contentResult
            .StatusCode
            .Should()
            .Be((int)HttpStatusCode.OK);

            contentResult
            .Content
            .Should()
            .Be(template);

            await blobClient
            .Received(0)
            .BlobExistsAsync(providerFundingVersion);

            await blobClient
            .Received(0)
            .GetAsync(Arg.Any <string>());

            fileSystemCache
            .Received(0)
            .Add(Arg.Is <ProviderFundingFileSystemCacheKey>(_ => _.Key == providerFundingVersion),
                 memoryStream,
                 CancellationToken.None);
        }
Пример #5
0
        public async Task GetFundingFeedDocument_WhenReturnsFromBlobStorage_ReturnsOK(bool fileSystemCacheEnabled,
                                                                                      int expectedCacheAccessCount)
        {
            //Arrange
            string template     = new RandomString();
            string documentPath = "cromulent.json";
            string uri          = $"https://cfs/test/{documentPath}";

            Stream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(template));

            ILogger          logger          = CreateLogger();
            ICloudBlob       cloudBlob       = CreateBlob();
            IFileSystemCache fileSystemCache = CreateFileSystemCache();
            IExternalApiFileSystemCacheSettings cacheSettings = Substitute.For <IExternalApiFileSystemCacheSettings>();

            cacheSettings.IsEnabled.Returns(fileSystemCacheEnabled);

            cloudBlob
            .Exists()
            .Returns(true);
            cloudBlob
            .Exists(Arg.Any <BlobRequestOptions>(), Arg.Any <OperationContext>())
            .Returns(true);

            IBlobClient blobClient = CreateBlobClient();

            blobClient
            .GetBlockBlobReference(Arg.Is(documentPath))
            .Returns(cloudBlob);

            blobClient
            .DownloadToStreamAsync(Arg.Is(cloudBlob))
            .Returns(memoryStream);

            PublishedFundingRetrievalService service = CreatePublishedFundingRetrievalService(
                blobClient: blobClient,
                logger: logger,
                fileSystemCache: fileSystemCache,
                cacheSettings: cacheSettings);

            //Act
            string result = await service.GetFundingFeedDocument(uri);

            //Assert
            result
            .Should()
            .Be(template);

            blobClient
            .Received(1)
            .GetBlockBlobReference(documentPath);

            cloudBlob
            .Received(1)
            .Exists();

            logger
            .Received(0)
            .Error(Arg.Any <string>());

            await blobClient
            .Received(1)
            .DownloadToStreamAsync(cloudBlob);

            fileSystemCache
            .Received(0)
            .Get(Arg.Any <FundingFileSystemCacheKey>());

            string documentName = Path.GetFileNameWithoutExtension(documentPath);

            fileSystemCache
            .Received(expectedCacheAccessCount)
            .Add(Arg.Is <FundingFileSystemCacheKey>(
                     _ => _.Key == documentName),
                 memoryStream);
        }