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

Support for RSI's getOrgMembers #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
69 changes: 69 additions & 0 deletions Imbick.StarCitizen.Api.Tests/GetOrgMembersTests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,48 @@ namespace Imbick.StarCitizen.Api.Tests {
using Moq;
using RestSharp;

public class OrganisationsClientTests {
public class GetOrgsTests {

[Fact]
public async Task ListAsync_Always_ReturnsCorrectResultsAsync() {
public async Task ListOrgsAsync_Always_ReturnsCorrectResultsAsync() {
var response = new RestResponse<Response> {
Data = _dummyResponse,
StatusCode = HttpStatusCode.OK,
ResponseStatus = ResponseStatus.Completed
};
_mockRest.Setup(r => r.ExecutePostTaskAsync<Response>(It.IsAny<IRestRequest>()))
.Returns(Task.FromResult<IRestResponse<Response>>(response));
var client = new OrganisationsClient(_mockRest.Object);
var client = new RsiApiClient(_mockRest.Object);

var orgs = await client.ListAsync();
var orgs = await client.ListOrgsAsync();

Assert.Equal(32, orgs.Count());
Assert.Equal(_dummyOrg, orgs.First());
}

[Fact]
public async Task GetAsync_WithSearchTerm_ReturnsCorrectResultAsync() {
public async Task GetOrgsAsync_WithSearchTerm_ReturnsCorrectResultAsync() {
var response = new RestResponse<Response> {
Data = _dummyResponse,
StatusCode = HttpStatusCode.OK,
ResponseStatus = ResponseStatus.Completed
};
_mockRest.Setup(c => c.ExecutePostTaskAsync<Response>(It.IsAny<IRestRequest>()))
.Returns(Task.FromResult<IRestResponse<Response>>(response));
var client = new OrganisationsClient(_mockRest.Object);
var searchRequest = new SearchRequest {Search = "Empire De La Soumission" };
var orgs = await client.GetAsync(searchRequest);
var client = new RsiApiClient(_mockRest.Object);
var searchRequest = new OrgsSearchRequest {Search = "Empire De La Soumission" };
var orgs = await client.GetOrgsAsync(searchRequest);

Assert.Equal(32, orgs.Count());
Assert.Equal(_dummyOrg, orgs.First());
}

private readonly Organisation _dummyOrg = new Organisation {
private readonly Org _dummyOrg = new Org {
Name = "Rogue Pirates",
Symbol = "ROGPIRATES",
ThumbnailUrl = "/rsi/static/images/organization/defaults/logo/syndicate.jpg",
Archetype = Archetype.Syndicate,
Commitment = Commitment.Hardcore,
Archetype = OrgArchetype.Syndicate,
Commitment = OrgCommitment.Hardcore,
Language = "Italian",
IsRecruiting = true,
IsRolePlay = false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

namespace Imbick.StarCitizen.Api.Tests.Models {
using System;
using System.Collections.Generic;
using System.Linq;
using Imbick.StarCitizen.Api.Models;
using Xunit;

public class OrgMembersSearchRequestTests
{
[Fact]
public void Ctor_WithMembershipMainOrg_MapsCorrectly()
{
var request = new OrgMembersSearchRequest
{
Membership = OrgMembersSearchRequest.MembershipType.MainOrg
};
var result = new OrgMembersSearchRequestInternal(request);
Assert.True(result.Main_Org == 1);
}

[Fact]
public void Ctor_WithMembershipAffiliate_MapsCorrectly()
{
var request = new OrgMembersSearchRequest
{
Membership = OrgMembersSearchRequest.MembershipType.Affiliate
};
var result = new OrgMembersSearchRequestInternal(request);
Assert.True(result.Main_Org == 0);
}

[Fact]
public void Ctor_WithMembershipAny_MapsCorrectly()
{
var request = new OrgMembersSearchRequest
{
Membership = OrgMembersSearchRequest.MembershipType.Any
};
var result = new OrgMembersSearchRequestInternal(request);
Assert.True(result.Main_Org == null);
}

}
}
105 changes: 105 additions & 0 deletions Imbick.StarCitizen.Api.Tests/Models/OrgsSearchRequestTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

namespace Imbick.StarCitizen.Api.Tests.Models {
using System;
using System.Collections.Generic;
using System.Linq;
using Imbick.StarCitizen.Api.Models;
using Xunit;

public class OrgsSearchRequestTests
{
[Fact]
public void Ctor_WithBooleanYes_MapsCorrectly() {
var request = new OrgsSearchRequest {
Recruiting = OrgsSearchRequest.Boolean.Yes,
RolePlay = OrgsSearchRequest.Boolean.Yes
};
var result = new OrgsSearchRequestInternal(request);
Assert.True(result.Recruiting.Count() == 1);
Assert.True(result.Recruiting.First() == 1);
Assert.True(result.RolePlay.Count() == 1);
Assert.True(result.RolePlay.First() == 1);
}

[Fact]
public void Ctor_WithBooleanNo_MapsCorrectly() {
var request = new OrgsSearchRequest {
Recruiting = OrgsSearchRequest.Boolean.No,
RolePlay = OrgsSearchRequest.Boolean.No
};
var result = new OrgsSearchRequestInternal(request);
Assert.True(result.Recruiting.Count() == 1);
Assert.True(result.Recruiting.First() == 0);
Assert.True(result.RolePlay.Count() == 1);
Assert.True(result.RolePlay.First() == 0);
}

[Fact]
public void Ctor_WithBooleanBoth_MapsCorrectly() {
var request = new OrgsSearchRequest {
Recruiting = OrgsSearchRequest.Boolean.YesAndNo,
RolePlay = OrgsSearchRequest.Boolean.YesAndNo
};
var result = new OrgsSearchRequestInternal(request);
AssertContains(result.Recruiting, new List<int>());
AssertContains(result.RolePlay, new List<int>());
}

[Fact]
public void Ctor_WithSort_MapsCorrectly() {
var result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.ActiveAscending});
Assert.Equal(result.Sort, OrgsSearchRequestInternal.ActiveAsc);
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.ActiveDescending });
Assert.Equal(result.Sort, OrgsSearchRequestInternal.ActiveDesc);
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.CreatedAscending});
Assert.Equal(result.Sort, OrgsSearchRequestInternal.CreatedAsc);
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.CreatedDescending});
Assert.Equal(result.Sort, OrgsSearchRequestInternal.CreatedDesc);
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.NameAscending});
Assert.Equal(result.Sort, OrgsSearchRequestInternal.NameAsc);
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.NameDescending});
Assert.Equal(result.Sort, OrgsSearchRequestInternal.NameDesc);
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.SizeAscending});
Assert.Equal(result.Sort, OrgsSearchRequestInternal.SizeAsc);
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Sort = OrgsSearchRequest.OrganisationSorting.SizeDescending });
Assert.Equal(result.Sort, OrgsSearchRequestInternal.SizeDesc);
Assert.Throws<ArgumentOutOfRangeException>(() =>
result = new OrgsSearchRequestInternal(new OrgsSearchRequest {Sort = (OrgsSearchRequest.OrganisationSorting)123}));
}

[Fact]
public void Ctor_WithSize_MapsCorrectly() {
var result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Size = new[] { OrgSize.Large } });
AssertContains(result.Size, new[] { OrgSize.Large.ToString().ToLower() });
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Size = new[] { OrgSize.Medium } });
AssertContains(result.Size, new[] { OrgSize.Medium.ToString().ToLower() });
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Size = new[] { OrgSize.Small } });
AssertContains(result.Size, new[] { OrgSize.Small.ToString().ToLower() });
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Size = new[] { OrgSize.Small, OrgSize.Medium, OrgSize.Large } });
AssertContains(result.Size, new[] { OrgSize.Small.ToString().ToLower(), OrgSize.Medium.ToString().ToLower(), OrgSize.Large.ToString().ToLower() });
Assert.Throws<ArgumentOutOfRangeException>(() =>
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Size = new [] { (OrgSize)123} }));
}

[Fact]
public void Ctor_WithCommitment_MapsCorrectly() {
var result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Commitment = new[] { OrgCommitment.Regular } });
AssertContains(result.Commitment, new[] { "RE" });
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Commitment = new[] { OrgCommitment.Casual } });
AssertContains(result.Commitment, new[] { "CA" });
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Commitment = new[] { OrgCommitment.Hardcore } });
AssertContains(result.Commitment, new[] { "HA" });
result = new OrgsSearchRequestInternal(new OrgsSearchRequest { Commitment = new[] { OrgCommitment.Casual, OrgCommitment.Hardcore } });
AssertContains(result.Commitment, new[] { "CA", "HA" });
/*Assert.Throws<ArgumentOutOfRangeException>(() =>
result = new SearchRequestInternal(new SearchRequest { Commitment = new[] { (Commitment)123 } }));*/
}

private static void AssertContains<T>(IEnumerable<T> collection, IReadOnlyCollection<T> expectedContent) {
Assert.True(collection.Count() == expectedContent.Count);
foreach (var i in expectedContent) {
Assert.Contains(i, collection);
}
}
}
}
105 changes: 0 additions & 105 deletions Imbick.StarCitizen.Api.Tests/Models/SearchRequestTests.cs

This file was deleted.

17 changes: 17 additions & 0 deletions Imbick.StarCitizen.Api/GetOrgMembersRestRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Imbick.StarCitizen.Api {
using System.Linq;
using Imbick.StarCitizen.Api.HtmlSerializers;
using Models;
using RestSharp;

public class GetOrgMembersRestRequest
: RestRequest {
public static string GetOrgMembersResource = "api/orgs/getOrgMembers";

public GetOrgMembersRestRequest(OrgMembersSearchRequest searchRequest)
: base(GetOrgMembersResource) {
SimpleJson.CurrentJsonSerializerStrategy = new LowerCaseSerializerStrategy();
AddJsonBody(searchRequest == null ? null : new OrgMembersSearchRequestInternal(searchRequest));
}
}
}
15 changes: 4 additions & 11 deletions Imbick.StarCitizen.Api/GetOrgsRestRequest.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
namespace Imbick.StarCitizen.Api {
using System.Linq;
using Imbick.StarCitizen.Api.HtmlSerializers;
using Models;
using RestSharp;
using SimpleJson;

public class GetOrgsRestRequest
: RestRequest {
public static string GetOrgsResource = "api/orgs/getOrgs";

public GetOrgsRestRequest(SearchRequest searchRequest)
public GetOrgsRestRequest(OrgsSearchRequest searchRequest)
: base(GetOrgsResource) {
SimpleJson.CurrentJsonSerializerStrategy = new CamelCaseSerializerStrategy();
AddJsonBody(searchRequest == null ? null : new SearchRequestInternal(searchRequest));
}
}

internal class CamelCaseSerializerStrategy
: PocoJsonSerializerStrategy {
protected override string MapClrMemberNameToJsonFieldName(string clrPropertyName) {
return char.ToLower(clrPropertyName[0]) + clrPropertyName.Substring(1);
SimpleJson.CurrentJsonSerializerStrategy = new LowerCaseSerializerStrategy();
AddJsonBody(searchRequest == null ? null : new OrgsSearchRequestInternal(searchRequest));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Imbick.StarCitizen.Api.HtmlSerializers {
using System.Linq;
using System.Text.RegularExpressions;
using RestSharp;

internal class LowerCaseSerializerStrategy
: PocoJsonSerializerStrategy {
protected override string MapClrMemberNameToJsonFieldName(string clrPropertyName) {
return clrPropertyName.ToLower();
}
}
}
Loading