private static AutoSubstituteBuilder ConfigureServiceClient(string plateFile, int level, int x, int y, string expectedContainerName = null, string blobFormat = null) { // For all the non dss plate files var blobName = $"{plateFile.Replace(".plate", string.Empty)}/L{level}X{x}Y{y}.png"; var containerName = AzurePlateTilePyramidOptions.DefaultContainer; if (plateFile == "dssterrapixel.plate") { blobName = $"DSSTerraPixelL{level}X{x}Y{y}.png"; containerName = "dss"; } return(AutoSubstitute.Configure() .InjectProperties() .MakeUnregisteredTypesPerLifetime() .SubstituteFor <Response <BlobDownloadInfo> >() .SubstituteFor <DownloadResult>() .Provide(ctx => BlobsModelFactory.BlobDownloadInfo(content: ctx.Resolve <DownloadResult>())) .SubstituteFor <BlobClient>() .ResolveReturnValue(c => c.Download()) .SubstituteFor <BlobContainerClient>() .ResolveReturnValue(t => t.GetBlobClient(blobName)) .ConfigureSubstitute(c => { c.Configure() .CreateIfNotExists().Returns(Substitute.For <Response <BlobContainerInfo> >()); }) .SubstituteFor <BlobServiceClient>() .ResolveReturnValue(service => service.GetBlobContainerClient(containerName))); }
private static Mock <BlobBaseClient> SetupBlobMock(bool?isFetchSuccess = null, Dictionary <string, string> metadata = null) { if (metadata == null) { metadata = new Dictionary <string, string>(); } var blobMock = new Mock <BlobBaseClient>(MockBehavior.Strict); if (isFetchSuccess.HasValue) { var fetchAttributesSetup = blobMock.Setup(s => s.GetPropertiesAsync(null, It.IsAny <CancellationToken>())); if (isFetchSuccess.Value) { var blobProperties = BlobsModelFactory.BlobProperties(metadata: metadata); fetchAttributesSetup.Returns(Task.FromResult(Response.FromValue(blobProperties, null))); } else { var blobNotFoundException = new RequestFailedException(404, string.Empty); fetchAttributesSetup.Throws(blobNotFoundException); } fetchAttributesSetup.Verifiable(); } return(blobMock); }
private static AutoSubstituteBuilder ConfigureServiceClient(string plateFile, int level, int x, int y, string expectedContainerName = null, string blobFormat = null) { blobFormat ??= "L{0}X{1}Y{2}.png"; var blobName = string.Format(blobFormat, level, x, y); var containerName = expectedContainerName ?? plateFile.Replace(".plate", string.Empty); return(AutoSubstitute.Configure() .SubstituteFor2 <DownloadResult>().Provide(out var result).Configured() .SubstituteFor2 <BlobClient>().Provide(out var blob).Configure(b => { var response = Substitute.ForPartsOf <Response <BlobDownloadInfo> >(); response.Value.Returns(BlobsModelFactory.BlobDownloadInfo(content: result.Value)); b.Configure() .Download().Returns(response); b.WhenForAnyArgs(b => b.Upload(Arg.Any <Stream>(), Arg.Any <bool>(), Arg.Any <CancellationToken>())) .DoNotCallBase(); }) .SubstituteFor2 <BlobContainerClient>().Provide(out var container).Configure(c => { c.Configure() .GetBlobClient(blobName).Returns(blob.Value); c.Configure() .CreateIfNotExists().Returns(Substitute.For <Response <BlobContainerInfo> >()); }) .SubstituteFor2 <BlobServiceClient>().Provide(out var service).Configure(service => { service.Configure() .GetBlobContainerClient(containerName).Returns(container.Value); })); }
public AzureStorageBackedTests(ITestOutputHelper output) { _mockIALogger = new Mock <ILogger <IInstalledAppManager> >(); _mockStateLogger = new Mock <ILogger <IStateManager <string> > >(); _mockIALogger.Setup(log => log.Log(It.IsAny <Microsoft.Extensions.Logging.LogLevel>(), It.IsAny <EventId>(), It.IsAny <object>(), null, It.IsAny <Func <object, Exception, string> >())) .Callback <Microsoft.Extensions.Logging.LogLevel, EventId, object, Exception, Func <object, Exception, string> >((logLevel, e, state, ex, f) => { output.WriteLine($"{logLevel} logged: \"{state}\""); }); _mockStateLogger.Setup(log => log.Log(It.IsAny <Microsoft.Extensions.Logging.LogLevel>(), It.IsAny <EventId>(), It.IsAny <object>(), null, It.IsAny <Func <object, Exception, string> >())) .Callback <Microsoft.Extensions.Logging.LogLevel, EventId, object, Exception, Func <object, Exception, string> >((logLevel, e, state, ex, f) => { output.WriteLine($"{logLevel} logged: \"{state}\""); }); _mockSmartThingsAPIHelper = new Mock <ISmartThingsAPIHelper>(); _mockSmartThingsAPIHelper.Setup(api => api.RefreshTokensAsync(It.IsAny <InstalledAppInstance>())) .Returns(() => { return(Task.FromResult <InstalledAppInstance>(CommonUtils.GetValidInstalledAppInstance())); }); var iaBlobStream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(CommonUtils.GetIACache()))); iaBlobStream.Seek(0, SeekOrigin.Begin); var iaBlobStreamInfo = BlobsModelFactory.BlobDownloadInfo(DateTimeOffset.Now, 0, BlobType.Block, null, null, null, null, null, null, CopyStatus.Success, null, LeaseDurationType.Infinite, null, LeaseState.Available, null, LeaseStatus.Unlocked, null, null,
public async Task ClaimOwnershipForExistingPartitionLogsOwnershipClaimed() { var blobInfo = BlobsModelFactory.BlobInfo(new ETag($@"""{MatchingEtag}"""), DateTime.UtcNow); var partitionOwnership = new List <EventProcessorPartitionOwnership> { new EventProcessorPartitionOwnership { FullyQualifiedNamespace = FullyQualifiedNamespace, EventHubName = EventHubName, ConsumerGroup = ConsumerGroup, OwnerIdentifier = OwnershipIdentifier, PartitionId = PartitionId, LastModifiedTime = DateTime.UtcNow, Version = MatchingEtag } }; var mockContainerClient = new MockBlobContainerClient().AddBlobClient($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/ownership/1", client => client.BlobInfo = blobInfo); var target = new BlobCheckpointStoreInternal(mockContainerClient); var mockLog = new Mock <BlobEventStoreEventSource>(); target.Logger = mockLog.Object; var result = await target.ClaimOwnershipAsync(partitionOwnership, CancellationToken.None); mockLog.Verify(m => m.OwnershipClaimed(PartitionId, FullyQualifiedNamespace, EventHubName, ConsumerGroup, OwnershipIdentifier)); }
public async Task UpdateCheckpointLogsStartAndCompleteWhenTheBlobExists() { var blobInfo = BlobsModelFactory.BlobInfo(new ETag($@"""{MatchingEtag}"""), DateTime.UtcNow); var blobList = new List <BlobItem> { BlobsModelFactory.BlobItem($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/ownership/{Guid.NewGuid().ToString()}", false, BlobsModelFactory.BlobItemProperties(true, lastModified: DateTime.UtcNow, eTag: new ETag(MatchingEtag)), "snapshot", new Dictionary <string, string> { { BlobMetadataKey.OwnerIdentifier, Guid.NewGuid().ToString() } }) }; var mockContainerClient = new MockBlobContainerClient() { Blobs = blobList }; mockContainerClient.AddBlobClient($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/checkpoint/1", client => { client.BlobInfo = blobInfo; client.UploadBlobException = new Exception("Upload should not be called"); }); var target = new BlobCheckpointStoreInternal(mockContainerClient); var mockLog = new Mock <BlobEventStoreEventSource>(); target.Logger = mockLog.Object; await target.UpdateCheckpointAsync(FullyQualifiedNamespace, EventHubName, ConsumerGroup, PartitionId, 0, default, CancellationToken.None);
private static async Task <Response <BlobContentInfo> > WriteStreamToTestDataFolder(string filepath, Stream fileStream) { string dataPath = GetDataOutputBlobPath() + filepath; if (!Directory.Exists(Path.GetDirectoryName(dataPath))) { Directory.CreateDirectory(Path.GetDirectoryName(dataPath)); } int filesize; using (Stream streamToWriteTo = File.Open(dataPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { await fileStream.CopyToAsync(streamToWriteTo); streamToWriteTo.Flush(); filesize = (int)streamToWriteTo.Length; } BlobContentInfo mockedBlobInfo = BlobsModelFactory.BlobContentInfo(new ETag("ETagSuccess"), DateTime.Now, new byte[1], DateTime.Now.ToUniversalTime().ToString(), "encryptionKeySha256", "encryptionScope", 1); Mock <Response <BlobContentInfo> > mockResponse = new Mock <Response <BlobContentInfo> >(); mockResponse.SetupGet(r => r.Value).Returns(mockedBlobInfo); Mock <Response> responseMock = new Mock <Response>(); responseMock.SetupGet(r => r.Status).Returns((int)HttpStatusCode.Created); mockResponse.Setup(r => r.GetRawResponse()).Returns(responseMock.Object); return(mockResponse.Object); }
public async Task ClaimOwnershipForExistingPartitionWithWrongEtagLogsOwnershipNotClaimable() { var blobInfo = BlobsModelFactory.BlobInfo(new ETag($@"""{WrongEtag}"""), DateTime.UtcNow); var partitionOwnership = new List <EventProcessorPartitionOwnership> { new EventProcessorPartitionOwnership { FullyQualifiedNamespace = FullyQualifiedNamespace, EventHubName = EventHubName, ConsumerGroup = ConsumerGroup, OwnerIdentifier = OwnershipIdentifier, PartitionId = PartitionId, LastModifiedTime = DateTime.UtcNow, Version = MatchingEtag } }; var mockContainerClient = new MockBlobContainerClient().AddBlobClient($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/ownership/1", client => client.BlobInfo = blobInfo); var target = new BlobsCheckpointStore(mockContainerClient, DefaultRetryPolicy); var mockLog = new Mock <BlobEventStoreEventSource>(); target.Logger = mockLog.Object; var result = await target.ClaimOwnershipAsync(partitionOwnership, CancellationToken.None); mockLog.Verify(m => m.OwnershipNotClaimable(PartitionId, FullyQualifiedNamespace, EventHubName, ConsumerGroup, OwnershipIdentifier, It.Is <string>(e => e.Contains(BlobErrorCode.ConditionNotMet.ToString())))); }
public async Task RunFetchConcentratorFirmware_Succeeds() { var httpRequest = new Mock <HttpRequest>(); var queryCollection = new QueryCollection(new Dictionary <string, StringValues>() { { "StationEui", new StringValues(this.TestStationEui.ToString()) } }); httpRequest.SetupGet(x => x.Query).Returns(queryCollection); var twin = new Twin(); twin.Properties.Desired = new TwinCollection(JsonUtil.Strictify(@"{'cups': { 'package': '1.0.1', 'fwUrl': 'https://storage.blob.core.windows.net/container/blob', 'fwKeyChecksum': 123456, 'fwSignature': '123' }}")); this.registryManager.Setup(m => m.GetTwinAsync(It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(twin)); var blobBytes = Encoding.UTF8.GetBytes(BlobContent); using var blobContentStream = new MemoryStream(blobBytes); using var streamingResult = BlobsModelFactory.BlobDownloadStreamingResult(blobContentStream); this.blobClient.Setup(m => m.DownloadStreamingAsync(default, null, false, It.IsAny <CancellationToken>()))
public async Task ListCheckpointsAsync_LogsOnInvalidCheckpoints() { var testLoggerProvider = new TestLoggerProvider(); Mock <BlobContainerClient> containerClientMock = new Mock <BlobContainerClient>(MockBehavior.Strict); containerClientMock.Setup(c => c.GetBlobsAsync(It.IsAny <BlobTraits>(), It.IsAny <BlobStates>(), It.IsAny <string>(), It.IsAny <CancellationToken>())) .Returns(AsyncPageable <BlobItem> .FromPages(new[] { Page <BlobItem> .FromValues(new[] { BlobsModelFactory.BlobItem("testnamespace/testeventhubname/testconsumergroup/checkpoint/0", false, BlobsModelFactory.BlobItemProperties(false), metadata: new Dictionary <string, string>()) }, null, Mock.Of <Response>()) })); BlobsCheckpointStore store = new BlobsCheckpointStore( containerClientMock.Object, new BasicRetryPolicy(new EventHubsRetryOptions()), _functionId, testLoggerProvider.CreateLogger("TestLogger") ); await store.ListCheckpointsAsync(_namespace, _eventHubName, _consumerGroup, CancellationToken.None); var warning = testLoggerProvider.GetAllLogMessages().Single(p => p.Level == Extensions.Logging.LogLevel.Warning); var expectedWarning = "Function 'EventHubsTriggerFunction': An invalid checkpoint was found for partition: '0' of " + "FullyQualifiedNamespace: 'TestNamespace'; EventHubName: 'TestEventHubName'; ConsumerGroup: 'TestConsumerGroup'. " + "This checkpoint is not valid and will be ignored."; Assert.AreEqual(expectedWarning, warning.FormattedMessage); testLoggerProvider.ClearAllLogMessages(); }
private string CreateBlobAndUploadToContainer(Mock <BlobContainerClient> containerMock, List <BlobItem> blobItems, string blobContent = "test", DateTimeOffset lastModified = default) { string blobName = Path.GetRandomFileName().Replace(".", ""); Mock <BlobBaseClient> item = new Mock <BlobBaseClient>(); if (lastModified == default) { lastModified = DateTimeOffset.UtcNow; } var blobProperties = BlobsModelFactory.BlobProperties(lastModified: lastModified); item.Setup(x => x.GetPropertiesAsync(null, It.IsAny <CancellationToken>())).ReturnsAsync(Response.FromValue(blobProperties, null)); item.Setup(x => x.Name).Returns(blobName); BlobItemProperties blobItemProperties = BlobsModelFactory.BlobItemProperties(true, lastModified: lastModified); BlobItem blobItem = BlobsModelFactory.BlobItem( name: blobName, properties: blobItemProperties ); blobItems.Add(blobItem); Mock <BlobClient> blobClientMock = new Mock <BlobClient>(); blobClientMock.Setup(x => x.Name).Returns(blobName); blobClientMock.Setup(x => x.Download(It.IsAny <CancellationToken>())).Returns(() => Response.FromValue(BlobsModelFactory.BlobDownloadInfo(content: new MemoryStream(Encoding.UTF8.GetBytes(blobContent))), null)); blobClientMock.Setup(x => x.GetProperties(It.IsAny <BlobRequestConditions>(), It.IsAny <CancellationToken>())) .Returns(Response.FromValue(blobProperties, null)); containerMock.Setup(x => x.GetBlobClient(blobName)).Returns(blobClientMock.Object); return(blobName); }
public static Page <BlobHierarchyItem> GetSegmentsInYearFunc( string continuation, int?pageSizeHint) => new BlobHierarchyItemPage(new List <BlobHierarchyItem> { BlobsModelFactory.BlobHierarchyItem( null, BlobsModelFactory.BlobItem("idx/segments/2020/01/16/2300/meta.json", false, null)), BlobsModelFactory.BlobHierarchyItem( null, BlobsModelFactory.BlobItem("idx/segments/2020/03/02/2300/meta.json", false, null)), BlobsModelFactory.BlobHierarchyItem( null, BlobsModelFactory.BlobItem("idx/segments/2020/03/03/0000/meta.json", false, null)), BlobsModelFactory.BlobHierarchyItem( null, BlobsModelFactory.BlobItem("idx/segments/2020/03/03/1800/meta.json", false, null)), BlobsModelFactory.BlobHierarchyItem( null, BlobsModelFactory.BlobItem("idx/segments/2020/03/03/2000/meta.json", false, null)), BlobsModelFactory.BlobHierarchyItem( null, BlobsModelFactory.BlobItem("idx/segments/2020/03/03/2200/meta.json", false, null)), BlobsModelFactory.BlobHierarchyItem( null, BlobsModelFactory.BlobItem("idx/segments/2020/03/05/1700/meta.json", false, null)), });
public async Task ListOwnershipLogsStartAndComplete() { var blobList = new List <BlobItem> { BlobsModelFactory.BlobItem($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/ownership/{Guid.NewGuid().ToString()}", false, BlobsModelFactory.BlobItemProperties(true, lastModified: DateTime.UtcNow, eTag: new ETag(MatchingEtag)), "snapshot", new Dictionary <string, string> { { BlobMetadataKey.OwnerIdentifier, Guid.NewGuid().ToString() } }) }; var target = new BlobsCheckpointStore(new MockBlobContainerClient() { Blobs = blobList }, new BasicRetryPolicy(new EventHubsRetryOptions())); var mockLog = new Mock <BlobEventStoreEventSource>(); target.Logger = mockLog.Object; await target.ListOwnershipAsync(FullyQualifiedNamespace, EventHubName, ConsumerGroup, new CancellationToken()); mockLog.Verify(m => m.ListOwnershipAsyncStart(FullyQualifiedNamespace, EventHubName, ConsumerGroup)); mockLog.Verify(m => m.ListOwnershipAsyncComplete(FullyQualifiedNamespace, EventHubName, ConsumerGroup, blobList.Count)); }
public async Task GetCheckpointLogsInvalidCheckpoint() { var blobList = new List <BlobItem> { BlobsModelFactory.BlobItem($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/checkpoint/0", false, BlobsModelFactory.BlobItemProperties(true, lastModified: DateTime.UtcNow, eTag: new ETag(MatchingEtag)), "snapshot", new Dictionary <string, string> { { BlobMetadataKey.OwnerIdentifier, Guid.NewGuid().ToString() } }) }; var target = new BlobsCheckpointStore(new MockBlobContainerClient() { Blobs = blobList }, DefaultRetryPolicy); var mockLog = new Mock <BlobEventStoreEventSource>(); target.Logger = mockLog.Object; await target.ListCheckpointsAsync(FullyQualifiedNamespace, EventHubName, ConsumerGroup, CancellationToken.None); mockLog.Verify(m => m.InvalidCheckpointFound("0", FullyQualifiedNamespace, EventHubName, ConsumerGroup)); }
public StorageCheckpointTests() { _blobContainerClient = Substitute.For <BlobContainerClient>(); _storageCheckpointOptions = new StorageCheckpointOptions() { BlobPrefix = "Normalization", CheckpointBatchCount = "5" }; _eventHubClientOptions = new EventHubClientOptions(); _eventHubNamespaceFQDN = "test.servicebus.windows.net"; _eventHubName = "devicedata"; // Blob path corresponds to current event hub name _blobCheckpointPrefix = $"{_storageCheckpointOptions.BlobPrefix}/checkpoint/"; _blobPath = $"{_blobCheckpointPrefix}{_eventHubNamespaceFQDN}/{_eventHubName}/"; IReadOnlyList <BlobItem> mockBlobItems = new List <BlobItem>() { BlobsModelFactory.BlobItem(name: $"{_blobPath}1"), BlobsModelFactory.BlobItem(name: $"{_blobPath}10"), BlobsModelFactory.BlobItem(name: $"{_blobPath}20") }; var mockPageBlobItems = Page <BlobItem> .FromValues(mockBlobItems, "continuationToken", Substitute.For <Response>()); var mockPageableBlobItems = Pageable <BlobItem> .FromPages(new[] { mockPageBlobItems }); _blobContainerClient.GetBlobs(states: BlobStates.All, prefix: _blobCheckpointPrefix, cancellationToken: CancellationToken.None) .Returns(mockPageableBlobItems); _logger = Substitute.For <ITelemetryLogger>(); }
public static async IAsyncEnumerator <BlobItem> GetTestValues() { yield return(BlobsModelFactory.BlobItem("file1", false, null)); yield return(BlobsModelFactory.BlobItem("file2", false, null)); await Task.CompletedTask; }
private static Func <Response <BlobDownloadInfo> > ReturnBlob(string blobContent) => () => { var response = new Mock <Response <BlobDownloadInfo> >(); response.SetupGet(s => s.Value) .Returns(BlobsModelFactory.BlobDownloadInfo(content: new MemoryStream(Encoding.UTF8.GetBytes(blobContent)))); return(response.Object); };
private BlobDownloadInfo GetMockBlob() { byte[] firstString = Encoding.UTF8.GetBytes("doc"); MemoryStream str = new MemoryStream(firstString); BlobDownloadInfo blob = BlobsModelFactory.BlobDownloadInfo( DateTime.UtcNow, 0, BlobType.Block, null, null, null, null, null, null, CopyStatus.Pending, null, LeaseDurationType.Infinite, null, LeaseState.Available, null, LeaseStatus.Locked, null, null, default, 0, null, false, null, null,
public void BlobDownloadInfo_Dispose() { MockStream stream = new MockStream(); BlobDownloadInfo blobDownloadInfo = BlobsModelFactory.BlobDownloadInfo(content: stream); Assert.IsFalse(stream.IsDisposed); blobDownloadInfo.Dispose(); Assert.IsTrue(stream.IsDisposed); }
public static void ContentInfo_ReturnsExpectedValue() { using var stream = new MemoryStream(); var downloadInfo = BlobsModelFactory.BlobDownloadInfo(content: stream); var document = new AzureBlobDocument(downloadInfo); document.Content.Should().BeSameAs(downloadInfo.Content); }
public static void ContentType_ReturnsExpectedValue() { const string contentType = "test/content"; var downloadInfo = BlobsModelFactory.BlobDownloadInfo(contentType: contentType); var document = new AzureBlobDocument(downloadInfo); document.ContentType.Should().Be(downloadInfo.ContentType); }
public static Page <BlobHierarchyItem> GetYearPathFunc( string continuation, int?pageSizeHint) => new BlobHierarchyItemPage(new List <BlobHierarchyItem> { BlobsModelFactory.BlobHierarchyItem("idx/segments/1601/", null), BlobsModelFactory.BlobHierarchyItem("idx/segments/2019/", null), BlobsModelFactory.BlobHierarchyItem("idx/segments/2020/", null), BlobsModelFactory.BlobHierarchyItem("idx/segments/2022/", null), BlobsModelFactory.BlobHierarchyItem("idx/segments/2023/", null), });
internal MockSdk Returns(Stream content, string contentType) { var downloadInfo = BlobsModelFactory.BlobDownloadInfo( content: content, contentType: contentType); var mockResponse = new Mock <Response <BlobDownloadInfo> >(); mockResponse.Setup(r => r.Value).Returns(downloadInfo); mockSdk.mockBlobClient.Setup(c => c.DownloadAsync()) .ReturnsAsync(mockResponse.Object); return(mockSdk); }
public UploadTests(CourseDirectoryApplicationFactory factory) : base(factory) { DataUploadsContainerClient = new Mock <BlobContainerClient>(); DataUploadsContainerClient .Setup(mock => mock.CreateIfNotExistsAsync( It.IsAny <PublicAccessType>(), It.IsAny <IDictionary <string, string> >(), It.IsAny <CancellationToken>())) .ReturnsAsync(CreateMockResponse(BlobsModelFactory.BlobContainerInfo(new ETag(), Clock.UtcNow))); BlobServiceClient .Setup(mock => mock.GetBlobContainerClient("data-uploads")) .Returns(DataUploadsContainerClient.Object);
public async Task UploadToAzureStorageAsync_SuccessfullyUploads_ReturnsFilename() { using var context = new DataContext(ContextOptions); var invoiceService = FactoryService.CreateInvoiceService(context); Mock <Stream> mockMemoryStream = new Mock <Stream>(); Mock <BlobContainerClient> mockBlobContainerClient = new Mock <BlobContainerClient>(); var blobContentInfo = BlobsModelFactory.BlobContentInfo(It.IsAny <ETag>(), It.IsAny <DateTimeOffset>(), It.IsAny <byte[]>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <long>()); Mock <Task <Response <BlobContentInfo> > > mockResponse = new Mock <Task <Response <BlobContentInfo> > >(blobContentInfo); string html = "<html><body></body></html>"; mockBlobContainerClient.Setup(c => c.UploadBlobAsync(It.IsAny <string>(), mockMemoryStream.Object, CancellationToken.None)).Returns(mockResponse.Object); var result = await invoiceService.UploadToAzureStorageAsync(html, It.IsAny <string>(), It.IsAny <int>(), It.IsAny <int>()); Assert.True(true); }
public override Task <Response <BlobContentInfo> > UploadAsync(System.IO.Stream content, BlobHttpHeaders httpHeaders = null, IDictionary <string, string> metadata = null, BlobRequestConditions conditions = null, IProgress <long> progressHandler = null, AccessTier?accessTier = null, Storage.StorageTransferOptions transferOptions = default, CancellationToken cancellationToken = default) { if (BlobClientUploadBlobException != null) { throw BlobClientUploadBlobException; } if (BlobInfo != null) { throw new RequestFailedException(409, BlobErrorCode.BlobAlreadyExists.ToString(), BlobErrorCode.BlobAlreadyExists.ToString(), default); } return(Task.FromResult( Response.FromValue( BlobsModelFactory.BlobContentInfo(new ETag("etag"), DateTime.UtcNow, new byte[] { }, string.Empty, 0L), Mock.Of <Response>()))); }
public void StoreCreatesBlobWhenNotExist() { BlobRequestConditions uploadConditions = null; byte[] bytes = null; string contentType = null; var mock = new Mock <BlobClient>(); mock.Setup(c => c.UploadAsync( It.IsAny <Stream>(), It.IsAny <BlobHttpHeaders>(), It.IsAny <IDictionary <string, string> >(), It.IsAny <BlobRequestConditions>(), It.IsAny <IProgress <long> >(), It.IsAny <AccessTier?>(), It.IsAny <StorageTransferOptions>(), It.IsAny <CancellationToken>())) .Returns(async(Stream strm, BlobHttpHeaders headers, IDictionary <string, string> metaData, BlobRequestConditions conditions, IProgress <long> progress, AccessTier? access, StorageTransferOptions transfer, CancellationToken token) => { using var memoryStream = new MemoryStream(); strm.CopyTo(memoryStream); bytes = memoryStream.ToArray(); uploadConditions = conditions; contentType = headers?.ContentType; await Task.Yield(); var mockResponse = new Mock <Response <BlobContentInfo> >(); var blobContentInfo = BlobsModelFactory.BlobContentInfo(ETag.All, DateTimeOffset.Now.AddDays(-1), Array.Empty <byte>(), "", 1); mockResponse.Setup(c => c.Value).Returns(blobContentInfo); return(mockResponse.Object); }); var repository = new AzureBlobXmlRepository(mock.Object); repository.StoreElement(new XElement("Element"), null); Assert.AreEqual("*", uploadConditions.IfNoneMatch.ToString()); Assert.AreEqual("application/xml; charset=utf-8", contentType); var element = "<Element />"; Assert.AreEqual(bytes, GetEnvelopedContent(element)); }
public override BlobClient GetBlobClient(string blobName) { if (BlobClients.TryGetValue(blobName, out var client)) { return(client); } var blob = Blobs.SingleOrDefault(c => c.Name == blobName); if (blob != null) { return(new MockBlobClient(blobName) { Properties = BlobsModelFactory.BlobProperties(metadata: blob.Metadata) }); } return(new MockBlobClient(blobName)); }
public async Task GetCursor() { // Arrange string manifestPath = "idx/segments/2020/03/25/0200/meta.json"; Mock <BlobContainerClient> containerClient = new Mock <BlobContainerClient>(MockBehavior.Strict); Mock <BlobClient> blobClient = new Mock <BlobClient>(MockBehavior.Strict); Mock <ShardFactory> shardFactory = new Mock <ShardFactory>(MockBehavior.Strict); List <Mock <Shard> > shards = new List <Mock <Shard> >(); int shardCount = 3; for (int i = 0; i < shardCount; i++) { shards.Add(new Mock <Shard>(MockBehavior.Strict)); } DateTimeOffset dateTime = new DateTimeOffset(2020, 3, 25, 2, 0, 0, TimeSpan.Zero); string segmentPath = "idx/segments/2020/03/25/0200/meta.json"; string currentShardPath = "log/00/2020/03/25/0200/"; List <ShardCursor> shardCursors = new List <ShardCursor> { new ShardCursor("log/00/2020/03/25/0200/chunk1", 2, 3), new ShardCursor("log/01/2020/03/25/0200/chunk4", 5, 6), new ShardCursor("log/02/2020/03/25/0200/chunk7", 8, 9) }; SegmentCursor expectedCursor = new SegmentCursor( segmentPath, shardCursors, currentShardPath); containerClient.Setup(r => r.GetBlobClient(It.IsAny <string>())).Returns(blobClient.Object); using FileStream stream = File.OpenRead( $"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}{Path.DirectorySeparatorChar}Resources{Path.DirectorySeparatorChar}{"SegmentManifest.json"}"); BlobDownloadStreamingResult blobDownloadStreamingResult = BlobsModelFactory.BlobDownloadStreamingResult(content: stream); Response <BlobDownloadStreamingResult> downloadResponse = Response.FromValue(blobDownloadStreamingResult, new MockResponse(200)); if (IsAsync) { blobClient.Setup(r => r.DownloadStreamingAsync(default, default, default, default)).ReturnsAsync(downloadResponse);
public async Task ClaimOwnershipForExistingPartitionWithWongEtagLogsOwnershipNotClaimable() { var blobInfo = BlobsModelFactory.BlobInfo(new ETag($@"""{WrongEtag}"""), DateTime.UtcNow); var partitionOwnerships = new List <PartitionOwnership> { new PartitionOwnership(FullyQualifiedNamespace, EventHubName, ConsumerGroup, OwnershipIdentifier, PartitionId, DateTime.UtcNow, MatchingEtag) }; var target = new BlobsCheckpointStore(new MockBlobContainerClient { BlobInfo = blobInfo }, new BasicRetryPolicy(new EventHubsRetryOptions())); var mockLog = new Mock <BlobEventStoreEventSource>(); target.Logger = mockLog.Object; var result = (await target.ClaimOwnershipAsync(partitionOwnerships, new CancellationToken())).ToList(); CollectionAssert.IsEmpty(result); mockLog.Verify(m => m.OwnershipNotClaimable(PartitionId, OwnershipIdentifier, It.Is <string>(e => e.Contains(BlobErrorCode.ConditionNotMet.ToString())))); }