Implemented DeploymentsClient and unit tests

This commit is contained in:
pmacnaughton
2014-01-10 15:11:02 -07:00
parent 524fb377af
commit c8d99c30f4
14 changed files with 274 additions and 1 deletions
@@ -0,0 +1,163 @@
using NSubstitute;
using Octokit;
using Octokit.Tests.Helpers;
using System;
using System.Collections.Generic;
using Xunit;
using Xunit.Extensions;
public class DeploymentsClientTests
{
const string expectedAcceptsHeader = "application/vnd.github.cannonball-preview+json";
public class TheGetAllMethod
{
[Fact]
public async void EnsuresNonNullArguments()
{
var client = new DeploymentsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.GetAll(null, "name"));
await AssertEx.Throws<ArgumentNullException>(async () => await client.GetAll("owner", null));
}
[Fact]
public async void EnsuresNonEmptyArguments()
{
var client = new DeploymentsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentException>(async () => await client.GetAll("", "name"));
await AssertEx.Throws<ArgumentException>(async () => await client.GetAll("owner", ""));
}
[Theory]
[InlineData(" ")]
[InlineData("\n")]
[InlineData("\t")]
[InlineData(" ")]
[InlineData("\n\r")]
public async void EnsuresNonWhitespaceArguments(string whitespace)
{
var client = new DeploymentsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentException>(async () => await client.GetAll(whitespace, "name"));
await AssertEx.Throws<ArgumentException>(async () => await client.GetAll("owner", whitespace));
}
[Fact]
public void RequestsCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new DeploymentsClient(connection);
var expectedUrl = "repos/owner/name/deployments";
client.GetAll("owner", "name");
connection.Received().GetAll<GitDeployment>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
Arg.Any<IDictionary<string, string>>(),
Arg.Any<string>());
}
[Fact]
public void UsesPreviewAcceptsHeader()
{
var connection = Substitute.For<IApiConnection>();
var client = new DeploymentsClient(connection);
client.GetAll("owner", "name");
connection.Received().GetAll<GitDeployment>(Arg.Any<Uri>(),
Arg.Any<IDictionary<string, string>>(),
expectedAcceptsHeader);
}
}
public class TheCreateMethod
{
readonly NewDeployment newDeployment = new NewDeployment { Ref = "aRef" };
[Fact]
public async void EnsuresNonNullArguments()
{
var client = new DeploymentsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentNullException>(async () => await client.Create(null, "name", newDeployment));
await AssertEx.Throws<ArgumentNullException>(async () => await client.Create("owner", null, newDeployment));
await AssertEx.Throws<ArgumentNullException>(async () => await client.Create("owner", "name", null));
}
[Fact]
public async void EnsuresNonEmptyArguments()
{
var client = new DeploymentsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentException>(async () => await client.Create("", "name", newDeployment));
await AssertEx.Throws<ArgumentException>(async () => await client.Create("owner", "", newDeployment));
}
[Theory]
[InlineData(" ")]
[InlineData("\n")]
[InlineData("\t")]
[InlineData(" ")]
[InlineData("\n\r")]
public async void EnsuresNonWhitespaceArguments(string whitespace)
{
var client = new DeploymentsClient(Substitute.For<IApiConnection>());
await AssertEx.Throws<ArgumentException>(async () => await client.Create(whitespace, "name", newDeployment));
await AssertEx.Throws<ArgumentException>(async () => await client.Create("owner", whitespace, newDeployment));
}
[Fact]
public void PostsToDeploymentsUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new DeploymentsClient(connection);
var expectedUrl = "repos/owner/name/deployments";
client.Create("owner", "name", newDeployment);
connection.Received().Post<GitDeployment>(Arg.Is<Uri>(u => u.ToString() == expectedUrl),
Arg.Any<NewDeployment>(),
Arg.Any<string>());
}
[Fact]
public void PassesNewDeploymentRequest()
{
var connection = Substitute.For<IApiConnection>();
var client = new DeploymentsClient(connection);
client.Create("owner", "name", newDeployment);
connection.Received().Post<GitDeployment>(Arg.Any<Uri>(),
newDeployment,
Arg.Any<string>());
}
[Fact]
public void UsesPreviewAcceptsHeader()
{
var connection = Substitute.For<IApiConnection>();
var client = new DeploymentsClient(connection);
client.Create("owner", "name", newDeployment);
connection.Received().Post<GitDeployment>(Arg.Any<Uri>(),
Arg.Any<NewDeployment>(),
Arg.Is(expectedAcceptsHeader));
}
}
public class TheCtor
{
[Fact]
public void EnsuresArgument()
{
Assert.Throws<ArgumentNullException>(() => new DeploymentsClient(null));
}
[Fact]
public void SetsStatusesClient()
{
var client = new DeploymentsClient(Substitute.For<IApiConnection>());
Assert.NotNull(client.Status);
}
}
}
+1
View File
@@ -62,6 +62,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Authentication\CredentialsTests.cs" />
<Compile Include="Clients\DeploymentsClientTests.cs" />
<Compile Include="Clients\SearchClientTests.cs" />
<Compile Include="Clients\GistCommentsClientTests.cs" />
<Compile Include="Clients\GistsClientTests.cs" />
+34
View File
@@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
public class DeploymentsClient : ApiClient, IDeploymentsClient
{
const string acceptsHeader = "application/vnd.github.cannonball-preview+json";
public DeploymentsClient(IApiConnection apiConnection)
: base(apiConnection)
{
}
public Task<IReadOnlyList<GitDeployment>> GetAll(string owner, string name)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "login");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
return ApiConnection.GetAll<GitDeployment>(ApiUrls.Deployments(owner, name),
null, acceptsHeader);
}
public Task<GitDeployment> Create(string owner, string name, NewDeployment newDeployment)
{
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(newDeployment, "deployment");
return ApiConnection.Post<GitDeployment>(ApiUrls.Deployments(owner, name),
newDeployment, acceptsHeader);
}
}
}
+11
View File
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Octokit
{
public interface IDeploymentsClient
{
Task<IReadOnlyList<GitDeployment>> GetAll(string owner, string name);
Task<GitDeployment> Create(string owner, string name, NewDeployment newDeployment);
}
}
+2
View File
@@ -94,6 +94,7 @@ namespace Octokit
GitDatabase = new GitDatabaseClient(apiConnection);
Tree = new TreesClient(apiConnection);
Search = new SearchClient(apiConnection);
Deployment = new DeploymentsClient(apiConnection);
}
/// <summary>
@@ -145,6 +146,7 @@ namespace Octokit
public IGitDatabaseClient GitDatabase { get; private set; }
public ITreesClient Tree { get; private set; }
public ISearchClient Search { get; private set; }
public IDeploymentsClient Deployment { get; private set; }
static Uri FixUpBaseUri(Uri uri)
{
+4
View File
@@ -782,5 +782,9 @@ namespace Octokit
{
return "search/code".FormatUri();
}
public static Uri Deployments(string owner, string name)
{
return "repos/{0}/{1}/deployments".FormatUri(owner, name);
}
}
}
+2 -1
View File
@@ -24,5 +24,6 @@ namespace Octokit
IGitDatabaseClient GitDatabase { get; }
ITreesClient Tree { get; }
ISearchClient Search { get; }
IDeploymentsClient Deployment { get; }
}
}
}
+12
View File
@@ -0,0 +1,12 @@

namespace Octokit
{
public class NewDeployment
{
public string Ref { get; set; }
public bool? Force { get; set; }
public string Payload { get; set; }
public bool? AutoMerge { get; set; }
public string Description { get; set; }
}
}
+25
View File
@@ -0,0 +1,25 @@
using System;
namespace Octokit
{
public class GitDeployment
{
public int Id { get; set; }
public string Sha { get; set; }
public string Url { get; set; }
public User Creator { get; set; }
public string Payload { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public string Description { get; set; }
public string StatusesUrl { get; set; }
}
}
+4
View File
@@ -246,6 +246,10 @@
<Compile Include="Clients\IGistCommentsClient.cs" />
<Compile Include="Models\Response\GistComment.cs" />
<Compile Include="Models\Request\BodyWrapper.cs" />
<Compile Include="Clients\DeploymentsClient.cs" />
<Compile Include="Clients\IDeploymentsClient.cs" />
<Compile Include="Models\Request\NewDeployment.cs" />
<Compile Include="Models\Response\GitDeployment.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+4
View File
@@ -256,6 +256,10 @@
<Compile Include="Models\Request\LabelUpdate.cs" />
<Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Response\Branch.cs" />
<Compile Include="Clients\DeploymentsClient.cs" />
<Compile Include="Clients\IDeploymentsClient.cs" />
<Compile Include="Models\Request\NewDeployment.cs" />
<Compile Include="Models\Response\GitDeployment.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
</Project>
+4
View File
@@ -251,6 +251,10 @@
<Compile Include="Models\Request\LabelUpdate.cs" />
<Compile Include="Models\Request\NewLabel.cs" />
<Compile Include="Models\Response\Branch.cs" />
<Compile Include="Clients\DeploymentsClient.cs" />
<Compile Include="Clients\IDeploymentsClient.cs" />
<Compile Include="Models\Request\NewDeployment.cs" />
<Compile Include="Models\Response\GitDeployment.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
+4
View File
@@ -244,6 +244,10 @@
<Compile Include="Clients\IGistCommentsClient.cs" />
<Compile Include="Models\Response\GistComment.cs" />
<Compile Include="Models\Request\BodyWrapper.cs" />
<Compile Include="Clients\DeploymentsClient.cs" />
<Compile Include="Clients\IDeploymentsClient.cs" />
<Compile Include="Models\Request\NewDeployment.cs" />
<Compile Include="Models\Response\GitDeployment.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
+4
View File
@@ -54,6 +54,10 @@
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Clients\ActivitiesClient.cs" />
<Compile Include="Models\Request\NewDeployment.cs" />
<Compile Include="Models\Response\GitDeployment.cs" />
<Compile Include="Clients\DeploymentsClient.cs" />
<Compile Include="Clients\IDeploymentsClient.cs" />
<Compile Include="Clients\SearchClient.cs" />
<Compile Include="Clients\ISearchClient.cs" />
<Compile Include="Models\Request\SearchCodeRequest.cs" />