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 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 async Task When_UpdateAsync()
        {
            // 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);

            toDoEntity.Description = _faker.Lorem.Paragraph(1);

            // Action

            await toDoEntityDataStore.UpdateAsync(
                toDoEntity);

            // Assert

            var itemResponse =
                await cosmosContainer.ReadItemAsync <ToDoEntity>(
                    toDoEntity.Id,
                    new PartitionKey(toDoEntity.Id));

            itemResponse.StatusCode.Should().Be(HttpStatusCode.OK);

            var toDoEntityUpdated =
                itemResponse.Resource;

            toDoEntityUpdated.Should().NotBeNull();
            toDoEntityUpdated.Description.Should().Be(toDoEntity.Description);
        }
        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_AddAsync()
        {
            // Arrange

            var entityDataStoreOptions =
                new EntityDataStoreOptions
            {
                PrimaryCloudTableClient = _cloudTableClient
            };

            var toDoEntityDataStore =
                new ToDoEntityDataStore(
                    entityDataStoreOptions);

            var toDoEntity =
                _faker.GenerateToDoEntity();

            // Action

            await toDoEntityDataStore.AddAsync(
                toDoEntity);

            // Assert

            var cloudTable =
                _cloudTableClient.GetTableReference("todos");

            var tableOperation =
                TableOperation.Retrieve <ToDoEntity>(toDoEntity.PartitionKey, toDoEntity.RowKey);

            var tableResult =
                await cloudTable.ExecuteAsync(tableOperation);

            tableResult.HttpStatusCode.Should().Be((int)HttpStatusCode.OK);

            var toDoEntityFetched =
                tableResult.Result as ToDoEntity;

            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_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_ListAsync()
        {
            // Arrange

            var cosmosDatabase =
                _cosmosClient.GetDatabase(
                    _configuration["AzureCosmosOptions:DatabaseId"]);

            var cosmosContainer =
                cosmosDatabase.GetContainer("todos");

            for (var i = 0; i < 3; i++)
            {
                var toDoEntity =
                    _faker.GenerateToDoEntity();

                await cosmosContainer.CreateItemAsync(
                    toDoEntity,
                    new PartitionKey(toDoEntity.ToDoId));
            }

            var entityDataStoreOptions =
                new EntityDataStoreOptions
            {
                CosmosClient = _cosmosClient,
                DatabaseId   = _configuration["AzureCosmosOptions:DatabaseId"]
            };

            var toDoEntityDataStore =
                new ToDoEntityDataStore(
                    entityDataStoreOptions);

            // Action

            var toDoEntityFetchedList =
                await toDoEntityDataStore.ListAsync();

            // Assert

            toDoEntityFetchedList.Should().NotBeNull();
            toDoEntityFetchedList.Count().Should().BeGreaterOrEqualTo(3);
        }
示例#10
0
        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);
        }
示例#11
0
        public async Task When_ListAsync()
        {
            // Arrange

            for (var i = 0; i < 3; i++)
            {
                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 toDoEntityFetchedList =
                await toDoEntityDataStore.ListAsync();

            // Assert

            toDoEntityFetchedList.Should().NotBeNull();
            toDoEntityFetchedList.Count().Should().BeGreaterOrEqualTo(3);
        }
示例#12
0
        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);
        }
示例#13
0
        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();
        }
示例#14
0
        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);
        }
示例#15
0
        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);
        }