예제 #1
0
        public async Task WhenStorageIsBrokenThenAnAppropriateExceptionIsThrownOnGetState()
        {
            Mock <ICloudBlobContainerWrapper> cloudBlobContainerWrapperMock = new Mock <ICloudBlobContainerWrapper>();

            cloudBlobContainerWrapperMock
            .Setup(m => m.DownloadBlobContentAsync(It.IsAny <string>(), CancellationToken.None))
            .Returns(() => throw new StorageException(new RequestResult(), string.Empty, null));

            ICloudStorageProviderFactory cloudStorageProviderFactoryMock = Mock.Of <ICloudStorageProviderFactory>(m => m.GetSmartDetectorStateStorageContainer() == cloudBlobContainerWrapperMock.Object);

            BlobStateRepository blobStateRepository = new BlobStateRepository("TestSignal", cloudStorageProviderFactoryMock, (new Mock <ITracer>()).Object);

            await blobStateRepository.GetStateAsync <TestState>("key", CancellationToken.None);
        }
예제 #2
0
        public async Task WhenExecutingBasicStateActionsThenFlowCompletesSuccesfully()
        {
            Dictionary <string, string>       repository = new Dictionary <string, string>();
            Mock <ICloudBlobContainerWrapper> cloudBlobContainerWrapperMock = new Mock <ICloudBlobContainerWrapper>();

            cloudBlobContainerWrapperMock
            .Setup(m => m.UploadBlobAsync(It.IsAny <string>(), It.IsAny <string>(), CancellationToken.None))
            .Returns <string, string, CancellationToken>((blobName, blobContent, token) => { repository[blobName] = blobContent; return(Task.FromResult <ICloudBlob>(null)); });
            cloudBlobContainerWrapperMock
            .Setup(m => m.DownloadBlobContentAsync(It.IsAny <string>(), CancellationToken.None))
            .Returns <string, CancellationToken>((blobName, token) =>
            {
                if (repository.ContainsKey(blobName))
                {
                    return(Task.FromResult <string>(repository[blobName]));
                }
                else
                {
                    StorageException ex = new StorageException(new RequestResult(), string.Empty, null);
                    ex.RequestInformation.HttpStatusCode = (int)HttpStatusCode.NotFound;
                    throw ex;
                }
            });
            cloudBlobContainerWrapperMock
            .Setup(m => m.DeleteBlobIfExistsAsync(It.IsAny <string>(), CancellationToken.None))
            .Returns <string, CancellationToken>((blobName, token) => { repository.Remove(blobName); return(Task.CompletedTask); });

            ICloudStorageProviderFactory cloudStorageProviderFactoryMock = Mock.Of <ICloudStorageProviderFactory>(m => m.GetSmartDetectorStateStorageContainer() == cloudBlobContainerWrapperMock.Object);

            BlobStateRepository blobStateRepository = new BlobStateRepository("TestSignal", cloudStorageProviderFactoryMock, (new Mock <ITracer>()).Object);

            var originalState = new TestState
            {
                Field1 = "testdata",
                Field2 = new List <DateTime> {
                    new DateTime(2018, 02, 15)
                },
                Field3 = true
            };

            await blobStateRepository.StoreStateAsync("key", originalState, CancellationToken.None);

            var retrievedState = await blobStateRepository.GetStateAsync <TestState>("key", CancellationToken.None);

            // validate
            Assert.AreEqual(originalState.Field1, retrievedState.Field1);
            CollectionAssert.AreEquivalent(originalState.Field2, retrievedState.Field2);
            Assert.AreEqual(originalState.Field3, retrievedState.Field3);
            Assert.AreEqual(originalState.Field4, retrievedState.Field4);
        }