Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database API endpoints to retrieve the list of databases. #30

Merged
merged 3 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions arangodb-net-standard.Test/DatabaseApi/DatabaseApiClientTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using ArangoDBNetStandard;
using ArangoDBNetStandard.DatabaseApi;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Xunit;

namespace ArangoDBNetStandardTest.DatabaseApi
{
/// <summary>
/// Test class for <see cref="DatabaseApiClient"/>.
/// </summary>
public class DatabaseApiClientTest : IClassFixture<DatabaseApiClientTestFixture>
{
private DatabaseApiClientTestFixture _fixture;

public DatabaseApiClientTest(DatabaseApiClientTestFixture fixture)
{
_fixture = fixture;
}

[Fact]
public async Task ListDatabasesAsync_ShouldSucceed()
{
ListDatabaseResult result = await _fixture.DatabaseClientSystem.ListDatabasesAsync();

Assert.False(result.Error);
Assert.Equal(HttpStatusCode.OK, result.Code);
Assert.True(result.Result.Count() > 0);
}

[Fact]
public async Task ListDatabasesAsync_ShouldThrow_WhenDatabaseIsNotSystem()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _fixture.DatabaseClientOther.ListDatabasesAsync();
});

ApiErrorResponse apiError = ex.ApiError;

Assert.Equal(HttpStatusCode.Forbidden, apiError.Code);
Assert.Equal(1230, apiError.ErrorNum);
}

[Fact]
public async Task ListDatabasesAsync_ShouldThrow_WhenDatabaseDoesNotExist()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _fixture.DatabaseClientNonExistent.ListDatabasesAsync();
});

ApiErrorResponse apiError = ex.ApiError;

Assert.Equal(HttpStatusCode.NotFound, apiError.Code);
Assert.Equal(1228, apiError.ErrorNum);
}

[Fact]
public async Task ListUserDatabasesAsync_ShouldSucceed()
{
ListDatabaseResult result = await _fixture.DatabaseClientOther.ListUserDatabasesAsync();

Assert.False(result.Error);
Assert.Equal(HttpStatusCode.OK, result.Code);
Assert.True(result.Result.Count() > 0);
}

[Fact]
public async Task ListUserDatabasesAsync_ShouldThrow_WhenDatabaseDoesNotExist()
{
var ex = await Assert.ThrowsAsync<ApiErrorException>(async () =>
{
await _fixture.DatabaseClientNonExistent.ListDatabasesAsync();
});

ApiErrorResponse apiError = ex.ApiError;

Assert.Equal(HttpStatusCode.NotFound, apiError.Code);
Assert.Equal(1228, apiError.ErrorNum);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System.Threading.Tasks;
using ArangoDBNetStandard;
using ArangoDBNetStandard.DatabaseApi;

namespace ArangoDBNetStandardTest.DatabaseApi
{
/// <summary>
/// Provides per-test-class fixture data for <see cref="DatabaseApiClientTest"/>.
/// </summary>
public class DatabaseApiClientTestFixture : ApiClientTestFixtureBase
{
/// <summary>
/// A <see cref="DatabaseApiClient"/> targeting the _system database.
/// </summary>
public DatabaseApiClient DatabaseClientSystem { get; internal set; }

/// <summary>
/// A <see cref="DatabaseApiClient"/> targeting a database that does not exist.
/// </summary>
public DatabaseApiClient DatabaseClientNonExistent { get; internal set; }

/// <summary>
/// A <see cref="DatabaseApiClient"/> targeting an existing database other than _system.
/// </summary>
public DatabaseApiClient DatabaseClientOther { get; internal set; }

public DatabaseApiClientTestFixture()
{
DatabaseClientSystem = GetArangoDBClient("_system").Database;
DatabaseClientNonExistent = GetArangoDBClient("DatabaseThatDoesNotExist").Database;
}

public override async Task InitializeAsync()
{
await base.InitializeAsync();

string dbName = nameof(DatabaseApiClientTestFixture);

await CreateDatabase(dbName);

DatabaseClientOther = GetArangoDBClient(dbName).Database;
}
}
}
41 changes: 40 additions & 1 deletion arangodb-net-standard/DatabaseApi/DatabaseApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace ArangoDBNetStandard.DatabaseApi
{
public class DatabaseApiClient: ApiClientBase
public class DatabaseApiClient : ApiClientBase
{
private IApiClientTransport _client;
private readonly string _databaseApiPath = "_api/database";
Expand Down Expand Up @@ -48,5 +48,44 @@ public async Task<DeleteDatabaseResult> DeleteDatabaseAsync(string dbName)
throw new ApiErrorException(apiError);
}
}

/// <summary>
/// Retrieves the list of all existing databases.
/// (Only possible from within the _system database)
/// </summary>
/// <remarks>
/// You should use <see cref="ListUserDatabasesAsync"/> to fetch the list of the databases
/// available for the current user.
/// </remarks>
/// <returns></returns>
public async Task<ListDatabaseResult> ListDatabasesAsync()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rossmills99 Maybe this should be named GetDatabasesAsync?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think I agree

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I will create a new issue for that.

{
using (var response = await _client.GetAsync(_databaseApiPath))
{
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync();
return DeserializeJsonFromStream<ListDatabaseResult>(stream, true, false);
}
throw await GetApiErrorException(response);
}
}

/// <summary>
/// Retrieves the list of all databases the current user can access.
/// </summary>
/// <returns></returns>
public async Task<ListDatabaseResult> ListUserDatabasesAsync()
{
using (var response = await _client.GetAsync(_databaseApiPath + "/user"))
{
if (response.IsSuccessStatusCode)
{
var stream = await response.Content.ReadAsStreamAsync();
return DeserializeJsonFromStream<ListDatabaseResult>(stream, true, false);
}
throw await GetApiErrorException(response);
}
}
}
}
27 changes: 27 additions & 0 deletions arangodb-net-standard/DatabaseApi/ListDatabaseResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Net;

namespace ArangoDBNetStandard.DatabaseApi
{
/// <summary>
/// Represents the content of the response returned
/// by an endpoint that gets the list of databases.
/// </summary>
public class ListDatabaseResult
{
/// <summary>
/// Indicates whether an error occurred (false in this case).
/// </summary>
public bool Error { get; set; }

/// <summary>
/// The HTTP status code.
/// </summary>
public HttpStatusCode Code { get; set; }

/// <summary>
/// The list of databases.
/// </summary>
public IEnumerable<string> Result { get; set; }
}
}
1 change: 0 additions & 1 deletion arangodb-net-standard/Transport/Http/HttpApiTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public async Task<IApiClientResponse> PutAsync(string requestUri, StringContent
/// <returns></returns>
public async Task<IApiClientResponse> GetAsync(string requestUri)
{

var response = await _client.GetAsync(requestUri);
return new HttpApiClientResponse(response);
}
Expand Down
6 changes: 3 additions & 3 deletions project/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ A tick indicates an item is implemented and has automated tests in place.

#### Database API

- [ ] GET/_api/database List of databases
- [ ] POST/_api/database Create database
- [X] GET/_api/database List of databases
- [X] POST/_api/database Create database
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests yet for Create database?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll answer my own question - as far as I can tell, we haven't done the proper implementation for Create database yet so we shouldn't check it off in the task list yet. Can you uncheck it for now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep I will do that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we do not have an explicit test for create database, most (if not all) the per-test-class fixtures use the method the create a test database.

Copy link
Collaborator

@rossmills99 rossmills99 Nov 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the criteria for marking a task complete is that we have implemented both the endpoint and tests of the endpoint. Those methods existed for use in other tests but there are no tests explicitly for them yet.

- [ ] GET/_api/database/current Information of the database
- [ ] GET/_api/database/user List of accessible databases
- [X] GET/_api/database/user List of accessible databases
- [ ] DELETE/_api/database/{database-name} Drop database

#### Document API
Expand Down