public async Task When_GetByIdAsync_And_EntityDoesNotExist_Then_ReturnNull() { // Arrange var entityDataStoreOptions = new EntityDataStoreOptions { CosmosClient = _cosmosClient, DatabaseId = _configuration["AzureCosmosOptions:DatabaseId"] }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); // Action var toDoEntity = await toDoEntityDataStore.GetByIdAsync( "IMABADID"); // Assert toDoEntity.Should().BeNull(); }
public override void Configure( IFunctionsHostBuilder builder) { var services = builder.Services; var entityDataStoreOptions = new EntityDataStoreOptions(); var primaryCloudStorageAccount = CloudStorageAccount.Parse( Environment.GetEnvironmentVariable("AzureTableStorageOptions:PrimaryConnectionString")); entityDataStoreOptions.PrimaryCloudTableClient = primaryCloudStorageAccount.CreateCloudTableClient(); if (!string.IsNullOrWhiteSpace( Environment.GetEnvironmentVariable("AzureTableStorageOptions:SecondaryConnectionString"))) { var secondaryCloudStorageAccount = CloudStorageAccount.Parse( Environment.GetEnvironmentVariable("AzureTableStorageOptions:SecondaryConnectionString")); entityDataStoreOptions.SecondaryCloudTableClient = secondaryCloudStorageAccount.CreateCloudTableClient(); } services.AddSingleton(entityDataStoreOptions); services.AddTransient <IToDoEntityDataStore, ToDoEntityDataStore>(); services.AddTransient <IToDoCommentEntityDataStore, ToDoCommentEntityDataStore>(); }
public async Task Given_BadToken_When_AddAsync_ThrowException() { // Arrange var entityDataStoreOptions = new EntityDataStoreOptions { ConnectionString = _configuration["AzureSqlOptions:ConnectionString"] }; var azureTokenProvider = new Mock <IAzureTokenProvider>(); azureTokenProvider.Setup(x => x.GetTokenAsync()).ReturnsAsync("BADTOKEN"); var toDoEntityDataStore = new ToDoEntityDataStore( azureTokenProvider.Object, entityDataStoreOptions); var toDoEntity = _faker.GenerateToDoEntity(); // Action Func <Task> action = async() => await toDoEntityDataStore.AddAsync( toDoEntity); // Assert action.Should().Throw <SqlException>().WithMessage("Login failed for user 'NT AUTHORITY\\ANONYMOUS LOGON'."); }
public override void Configure( IFunctionsHostBuilder builder) { var services = builder.Services; var entityDataStoreOptions = new EntityDataStoreOptions { ConnectionString = Environment.GetEnvironmentVariable("AzureSqlOptions:ConnectionString") }; services.AddSingleton(entityDataStoreOptions); var azureTokenProviderOptions = new AzureTokenProviderOptions { Authority = Environment.GetEnvironmentVariable("TokenProviderOptions:Authority"), ClientId = Environment.GetEnvironmentVariable("TokenProviderOptions:ClientId"), ClientSecret = Environment.GetEnvironmentVariable("TokenProviderOptions:ClientSecret"), ResourceId = "https://database.windows.net/", TenantId = Environment.GetEnvironmentVariable("TokenProviderOptions:TenantId") }; services.AddSingleton(azureTokenProviderOptions); services.AddSingleton <IAzureTokenProvider, AzureTokenProvider>(); services.AddTransient <IToDoEntityDataStore, ToDoEntityDataStore>(); }
public override void Configure( IFunctionsHostBuilder builder) { var services = builder.Services; var entityDataStoreOptions = new EntityDataStoreOptions(); var cosmosClient = new CosmosClientBuilder( Environment.GetEnvironmentVariable("AzureCosmosOptions:ConnectionString")) .WithConnectionModeDirect() .Build(); entityDataStoreOptions.CosmosClient = cosmosClient; entityDataStoreOptions.DatabaseId = Environment.GetEnvironmentVariable("AzureCosmosOptions:DatabaseId"); services.AddSingleton(entityDataStoreOptions); services.AddTransient <IToDoEntityDataStore, ToDoEntityDataStore>(); services.AddTransient <IToDoCommentEntityDataStore, ToDoCommentEntityDataStore>(); }
public async Task When_AddAsync() { // Arrange var cosmosDatabase = _cosmosClient.GetDatabase( _configuration["AzureCosmosOptions:DatabaseId"]); var cosmosContainer = cosmosDatabase.GetContainer("todos"); var toDoEntity = _faker.GenerateToDoEntity(); await cosmosContainer.CreateItemAsync( toDoEntity, new PartitionKey(toDoEntity.Id)); var entityDataStoreOptions = new EntityDataStoreOptions { CosmosClient = _cosmosClient, DatabaseId = _configuration["AzureCosmosOptions:DatabaseId"] }; var toDoCommentEntityDataStore = new ToDoCommentEntityDataStore( entityDataStoreOptions); var toDoCommentEntity = _faker.GenerateToDoCommentEntity( toDoEntity.Id); // Action await toDoCommentEntityDataStore.AddAsync( toDoEntity.Id, toDoCommentEntity); // Assert var itemResponse = await cosmosContainer.ReadItemAsync <ToDoCommentEntity>( toDoCommentEntity.Id, new PartitionKey(toDoEntity.Id)); itemResponse.StatusCode.Should().Be(HttpStatusCode.OK); var toDoCommentEntityFetched = itemResponse.Resource; toDoCommentEntityFetched.Should().NotBeNull(); toDoCommentEntityFetched.Id.Should().Be(toDoCommentEntity.Id); toDoCommentEntityFetched.ToDoId.Should().Be(toDoCommentEntity.ToDoId); toDoCommentEntityFetched.Body.Should().Be(toDoCommentEntity.Body); }
public async Task When_AddAsync() { // Arrange var toDoEntity = _faker.GenerateToDoEntity(); var cloudTable = _cloudTableClient.GetTableReference("todos"); var tableOperation = TableOperation.Insert(toDoEntity); await cloudTable.ExecuteAsync(tableOperation); var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = _cloudTableClient }; var toDoCommentEntityDataStore = new ToDoCommentEntityDataStore( entityDataStoreOptions); var toDoCommentEntity = _faker.GenerateToDoCommentEntity(toDoEntity.Id); // Action await toDoCommentEntityDataStore.AddAsync( toDoEntity.Id, toDoCommentEntity); // Assert cloudTable = _cloudTableClient.GetTableReference("todos"); tableOperation = TableOperation.Retrieve <ToDoCommentEntity>(toDoCommentEntity.PartitionKey, toDoCommentEntity.RowKey); var tableResult = await cloudTable.ExecuteAsync(tableOperation); tableResult.HttpStatusCode.Should().Be((int)HttpStatusCode.OK); var toDoCommentEntityFetched = tableResult.Result as ToDoCommentEntity; toDoCommentEntityFetched.Should().NotBeNull(); toDoCommentEntityFetched.RowKey.Should().Be(toDoCommentEntity.RowKey); toDoCommentEntityFetched.PartitionKey.Should().Be(toDoCommentEntity.PartitionKey); toDoCommentEntityFetched.Body.Should().Be(toDoCommentEntity.Body); }
public async Task When_ListAsync() { // Arrange var cosmosDatabase = _cosmosClient.GetDatabase( _configuration["AzureCosmosOptions:DatabaseId"]); var cosmosContainer = cosmosDatabase.GetContainer("todos"); var toDoEntity = _faker.GenerateToDoEntity(); await cosmosContainer.CreateItemAsync( toDoEntity, new PartitionKey(toDoEntity.ToDoId)); for (var i = 0; i < 3; i++) { var toDoCommentEntity = _faker.GenerateToDoCommentEntity( toDoEntity.Id); await cosmosContainer.CreateItemAsync( toDoCommentEntity, new PartitionKey(toDoEntity.Id)); } var entityDataStoreOptions = new EntityDataStoreOptions { CosmosClient = _cosmosClient, DatabaseId = _configuration["AzureCosmosOptions:DatabaseId"] }; var toDoCommentEntityDataStore = new ToDoCommentEntityDataStore( entityDataStoreOptions); // Action var toDoCommentEntityList = await toDoCommentEntityDataStore.ListByToDoIdAsync( toDoEntity.Id); // Assert toDoCommentEntityList.Should().NotBeNull(); toDoCommentEntityList.Count().Should().Be(3); }
public async Task When_ListAsync() { // Arrange var cloudTable = _cloudTableClient.GetTableReference("todos"); var toDoEntity = _faker.GenerateToDoEntity(); var tableOperation = TableOperation.Insert(toDoEntity); await cloudTable.ExecuteAsync(tableOperation); for (var i = 0; i < 3; i++) { var toDoCommentEntity = _faker.GenerateToDoCommentEntity( toDoEntity.Id); tableOperation = TableOperation.Insert(toDoCommentEntity); await cloudTable.ExecuteAsync(tableOperation); } var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = _cloudTableClient }; var toDoCommentEntityDataStore = new ToDoCommentEntityDataStore( entityDataStoreOptions); // Action var toDoCommentEntityList = await toDoCommentEntityDataStore.ListByToDoIdAsync( toDoEntity.Id); // Assert toDoCommentEntityList.Should().NotBeNull(); toDoCommentEntityList.Count().Should().Be(3); }
public void When_Create_And_EntityDataStoreOptionsPrimaryCloudClientIsNull_Then_ThrowsArgumentNullException() { // Arrange var entityDataStoreOptions = new EntityDataStoreOptions(); // Action Action action = () => new FakeEntityDataStore( entityDataStoreOptions); // Assert action.Should().Throw <ArgumentNullException>(); }
public async Task When_UpdateAsync() { // Arrange var toDoEntity = _faker.GenerateToDoEntity(); var cloudTable = _cloudTableClient.GetTableReference("todos"); var tableOperation = TableOperation.Insert(toDoEntity); await cloudTable.ExecuteAsync(tableOperation); var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = _cloudTableClient }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); toDoEntity.Description = _faker.Lorem.Paragraph(1); // Action await toDoEntityDataStore.UpdateAsync( toDoEntity); // Assert tableOperation = TableOperation.Retrieve <ToDoEntity>( toDoEntity.PartitionKey, toDoEntity.RowKey); var tableResult = await cloudTable.ExecuteAsync(tableOperation); var toDoEntityUpdated = tableResult.Result as ToDoEntity; toDoEntityUpdated.Should().NotBeNull(); toDoEntityUpdated.Description.Should().Be(toDoEntity.Description); }
public async Task When_GetByIdAsync() { // Arrange var cosmosDatabase = _cosmosClient.GetDatabase( _configuration["AzureCosmosOptions:DatabaseId"]); var cosmosContainer = cosmosDatabase.GetContainer("todos"); var toDoEntity = _faker.GenerateToDoEntity(); await cosmosContainer.CreateItemAsync( toDoEntity, new PartitionKey(toDoEntity.ToDoId)); var entityDataStoreOptions = new EntityDataStoreOptions { CosmosClient = _cosmosClient, DatabaseId = cosmosDatabase.Id }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); // Action var toDoEntityFetched = await toDoEntityDataStore.GetByIdAsync( toDoEntity.Id); // Assert toDoEntityFetched.Should().NotBeNull(); toDoEntityFetched.Id.Should().Be(toDoEntity.Id); toDoEntityFetched.ToDoId.Should().Be(toDoEntity.ToDoId); toDoEntityFetched.ToDoId.Should().Be(toDoEntityFetched.Id); toDoEntityFetched.Status.Should().Be(toDoEntity.Status); toDoEntityFetched.Description.Should().Be(toDoEntity.Description); }
public void When_AddAsync_And_PrimaryStorageNotAvailable_Then_ThrowsHttpStatusException() { // Arrange var primaryCloudTableClient = new Mock <CloudTableClient>( new Uri("http://localhost.com"), null, null); var primaryCloudTable = new Mock <CloudTable>( new Uri("http://localhost.com"), null); var primaryCloudTableResult = new TableResult { HttpStatusCode = (int)HttpStatusCode.NotFound }; primaryCloudTable.Setup(x => x.ExecuteAsync(It.IsAny <TableOperation>())) .ReturnsAsync(primaryCloudTableResult); primaryCloudTableClient.Setup(x => x.GetTableReference("todos")) .Returns(primaryCloudTable.Object); var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = primaryCloudTableClient.Object }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); var toDoEntity = _faker.GenerateToDoEntity(); // Action Func <Task> action = async() => await toDoEntityDataStore.AddAsync(toDoEntity); // Assert action.Should().Throw <HttpRequestException>(); }
public async Task When_DeleteByIdAsync() { // Arrange var toDoEntity = _faker.GenerateToDoEntity(); var cosmosDatabase = _cosmosClient.GetDatabase( _configuration["AzureCosmosOptions:DatabaseId"]); var cosmosContainer = cosmosDatabase.GetContainer("todos"); await cosmosContainer.CreateItemAsync( toDoEntity, new PartitionKey(toDoEntity.ToDoId)); var entityDataStoreOptions = new EntityDataStoreOptions { CosmosClient = _cosmosClient, DatabaseId = _configuration["AzureCosmosOptions:DatabaseId"] }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); // Action await toDoEntityDataStore.DeleteByIdAsync( toDoEntity.Id); // Assert Func <Task> action = async() => await cosmosContainer.ReadItemAsync <ToDoEntity>( toDoEntity.Id, new PartitionKey(toDoEntity.Id)); action.Should().Throw <CosmosException>(); }
public async Task When_DeleteByIdAsync() { // Arrange var toDoEntity = _faker.GenerateToDoEntity(); var cloudTable = _cloudTableClient.GetTableReference("todos"); var tableOperation = TableOperation.Insert(toDoEntity); await cloudTable.ExecuteAsync(tableOperation); var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = _cloudTableClient }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); // Action await toDoEntityDataStore.DeleteByIdAsync( toDoEntity.Id); // Assert tableOperation = TableOperation.Retrieve <ToDoEntity>( toDoEntity.PartitionKey, toDoEntity.RowKey); var tableResult = await cloudTable.ExecuteAsync(tableOperation); tableResult.HttpStatusCode.Should().Be((int)HttpStatusCode.NotFound); }
public async Task When_GetByIdAsync() { // Arrange var toDoEntity = _faker.GenerateToDoEntity(); var cloudTable = _cloudTableClient.GetTableReference("todos"); var tableOperation = TableOperation.Insert(toDoEntity); await cloudTable.ExecuteAsync(tableOperation); var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = _cloudTableClient }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); // Action var toDoEntityFetched = await toDoEntityDataStore.GetByIdAsync( toDoEntity.Id); // Assert toDoEntityFetched.Should().NotBeNull(); toDoEntityFetched.RowKey.Should().Be(toDoEntity.RowKey); toDoEntityFetched.PartitionKey.Should().Be(toDoEntity.PartitionKey); toDoEntityFetched.Status.Should().Be(toDoEntity.Status); toDoEntityFetched.Description.Should().Be(toDoEntity.Description); }
public async Task When_GetByIdAsync_And_EntityDoesNotExist_Then_ReturnNull() { // Arrange var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = _cloudTableClient }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); // Action var toDoEntity = await toDoEntityDataStore.GetByIdAsync( Guid.NewGuid().ToString()); // Assert toDoEntity.Should().BeNull(); }
public async Task When_AddAsync_And_PrimaryStorageNotAvailable_Then_FailOverToSecondaryStorage() { // Arrange var primaryCloudTableClient = new Mock <CloudTableClient>( new Uri("http://localhost.com"), null, null); var primaryCloudTable = new Mock <CloudTable>( new Uri("https://localhost.com"), null); var primaryCloudTableResult = new TableResult { HttpStatusCode = (int)HttpStatusCode.TooManyRequests }; primaryCloudTable.Setup(x => x.ExecuteAsync(It.IsAny <TableOperation>())) .ReturnsAsync(primaryCloudTableResult); primaryCloudTableClient.Setup(x => x.GetTableReference("todos")) .Returns(primaryCloudTable.Object); var secondaryCloudTableClient = new Mock <CloudTableClient>( new Uri("http://localhost.com"), null, null); var secondaryCloudTable = new Mock <CloudTable>( new Uri("https://localhost.com/secondary"), null); var secondaryCloudTableResult = new TableResult { HttpStatusCode = (int)HttpStatusCode.OK }; secondaryCloudTable.Setup(x => x.ExecuteAsync(It.IsAny <TableOperation>())) .ReturnsAsync(secondaryCloudTableResult); secondaryCloudTableClient.Setup(x => x.GetTableReference("todos")) .Returns(secondaryCloudTable.Object); var entityDataStoreOptions = new EntityDataStoreOptions { PrimaryCloudTableClient = primaryCloudTableClient.Object, SecondaryCloudTableClient = secondaryCloudTableClient.Object }; var toDoEntityDataStore = new ToDoEntityDataStore( entityDataStoreOptions); var toDoEntity = _faker.GenerateToDoEntity(); // Action await toDoEntityDataStore.AddAsync(toDoEntity); // Assert secondaryCloudTable.Verify(x => x.ExecuteAsync(It.IsAny <TableOperation>()), Times.Once); }
public FakeEntityDataStore( EntityDataStoreOptions entityDataStoreOptions) : base("fakes", entityDataStoreOptions) { }
public async Task When_AddAsync_ThenToDoAdded() { // Arrange var entityDataStoreOptions = new EntityDataStoreOptions { ConnectionString = _configuration["AzureSqlOptions:ConnectionString"] }; var azureTokenProviderOptions = new AzureTokenProviderOptions { Authority = _configuration["TokenProviderOptions:Authority"], ClientId = _configuration["TokenProviderOptions:ClientId"], ClientSecret = _configuration["TokenProviderOptions:ClientSecret"], ResourceId = "https://database.windows.net/", TenantId = _configuration["TokenProviderOptions:TenantId"] }; var azureTokenProvider = new AzureTokenProvider( azureTokenProviderOptions); var toDoEntityDataStore = new ToDoEntityDataStore( azureTokenProvider, entityDataStoreOptions); var toDoEntity = _faker.GenerateToDoEntity(); // Action await toDoEntityDataStore.AddAsync( toDoEntity); // Assert var query = "SELECT * FROM todos WHERE Id = @Id"; using var cn = new SqlConnection(_configuration["AzureSqlOptions:ConnectionString"]); using var cmd = new SqlCommand(query, cn); cn.AccessToken = await azureTokenProvider.GetTokenAsync(); cn.Open(); cmd.Parameters.AddWithValue("@Id", toDoEntity.Id); var reader = await cmd.ExecuteReaderAsync(); ToDoEntity toDoEntityFetched = null; while (reader.Read()) { toDoEntityFetched = new ToDoEntity { Id = reader.GetGuid(0), Status = reader.GetString(1), Description = reader.GetString(2), CreatedOn = reader.GetDateTime(3) }; } toDoEntityFetched.Should().NotBeNull(); toDoEntityFetched.Id.Should().Be(toDoEntity.Id); toDoEntityFetched.Status.Should().Be(toDoEntity.Status); toDoEntityFetched.Description.Should().Be(toDoEntity.Description); toDoEntityFetched.CreatedOn.Should().BeCloseTo(toDoEntity.CreatedOn); }