Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Paymentsense.Coding.Challenge.Api.Clients;
using Paymentsense.Coding.Challenge.Api.Models;
using Xunit;

namespace Paymentsense.Coding.Challenge.Api.Tests.Clients
{
public class CountryCacheTests
{
private readonly List<CountryModel> _countriesMock;

public CountryCacheTests()
{
var country = new CountryModel
{
Name = "Test Country",
Flag = "flag",
Population = 123456,
Timezones = new[] {"UTC", "UTC+01:00"},
Languages = new[] {new Language() {Name = "English"}},
Currencies = new[] {new CurrencyModel() {Name = "GBP"}},
Capital = "London",
Borders = new[] {"IRL"}
};

_countriesMock = Enumerable.Range(1, 100).Select(x => country).ToList();
}

[Fact]
public void GetCountries_ReturnsEmptyList()
{
var cache = new CountryCache();
var result = cache.GetCountries();

result.Should().BeOfType<List<CountryModel>>();
result.Should().HaveCount(0);
}

[Fact]
public void PopulateCountries_PopulatesCountries()
{
var cache = new CountryCache();
cache.PopulateCountries(_countriesMock);
var result = cache.GetCountries();

result.Should().BeOfType<List<CountryModel>>();
result.Should().HaveCount(_countriesMock.Count);
result.Should().BeSameAs(_countriesMock);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using FluentAssertions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Moq;
using Paymentsense.Coding.Challenge.Api.Controllers;
using Paymentsense.Coding.Challenge.Api.Models;
using Paymentsense.Coding.Challenge.Api.Services;
using Xunit;

namespace Paymentsense.Coding.Challenge.Api.Tests.Controllers
{
public class CountriesControllerTests
{
[Fact]
public async void Get_OnInvoke_ReturnsCountries()
{
var country = new CountryModel
{
Name = "Test Country",
Flag = "flag",
Population = 123456,
Timezones = new[] {"UTC", "UTC+01:00"},
Languages = new[] {new Language() {Name = "English"}},
Currencies = new[] {new CurrencyModel() {Name = "GBP"}},
Capital = "London",
Borders = new[] {"IRL"}
};

var countriesMock = new List<CountryModel>() {country};

var mockCountriesService = new Mock<ICountriesService>();
mockCountriesService.Setup(c => c.GetCountries(null, null)).ReturnsAsync(countriesMock);

var controller = new CountriesController(mockCountriesService.Object);

var result = (await controller.Get(null, null)).Result as OkObjectResult;

result.StatusCode.Should().Be(StatusCodes.Status200OK);
result.Value.Should().BeOfType<List<CountryModel>>();
result.Value.Should().Be(countriesMock);
mockCountriesService.Verify(c => c.GetCountries(null, null), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="3.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -21,10 +22,6 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Services\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Paymentsense.Coding.Challenge.Api\Paymentsense.Coding.Challenge.Api.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Moq;
using Paymentsense.Coding.Challenge.Api.Clients;
using Paymentsense.Coding.Challenge.Api.Models;
using Paymentsense.Coding.Challenge.Api.Services;
using Xunit;

namespace Paymentsense.Coding.Challenge.Api.Tests.Services
{
public class CountriesServiceTests
{
private readonly List<CountryModel> _countriesMock;

public CountriesServiceTests()
{
var country = new CountryModel
{
Name = "Test Country",
Flag = "flag",
Population = 123456,
Timezones = new[] {"UTC", "UTC+01:00"},
Languages = new[] {new Language() {Name = "English"}},
Currencies = new[] {new CurrencyModel() {Name = "GBP"}},
Capital = "London",
Borders = new[] {"IRL"}
};

_countriesMock = Enumerable.Range(1, 100).Select(x => country).ToList();
}

[Fact]
public async void GetCountries_ReturnsCountriesFromApiClientFirstCall_NoPagination()
{
var mockCountriesApiClient = new Mock<ICountriesApiClient>();
mockCountriesApiClient.Setup(c => c.GetCountries()).ReturnsAsync(_countriesMock);

var mockCache = new Mock<ICountryCache>();
mockCache.Setup(c => c.GetCountries()).Returns(new List<CountryModel>());
var countriesService = new CountriesService(mockCache.Object, mockCountriesApiClient.Object);

var result = await countriesService.GetCountries(null, null);

result.Should().BeOfType<List<CountryModel>>();
result.Should().BeSameAs(_countriesMock);
mockCountriesApiClient.Verify(c => c.GetCountries(), Times.Once);
mockCache.Verify(c => c.GetCountries(), Times.Once);
}

[Fact]
public async void GetCountries_PopulatesCacheOnFirstCall_NoPagination()
{
var mockCountriesApiClient = new Mock<ICountriesApiClient>();
mockCountriesApiClient.Setup(c => c.GetCountries()).ReturnsAsync(_countriesMock);

var mockCache = new Mock<ICountryCache>();
mockCache.Setup(c => c.GetCountries()).Returns(new List<CountryModel>());
var countriesService = new CountriesService(mockCache.Object, mockCountriesApiClient.Object);

var result = await countriesService.GetCountries(null, null);
mockCache.Verify(c => c.PopulateCountries(result), Times.Once);

mockCache.Verify(c => c.GetCountries(), Times.Once);
}

[Fact]
public async void GetCountries_ReturnsCountriesFromCacheSecondCall_NoPagination()
{
var mockCountriesApiClient = new Mock<ICountriesApiClient>();

var mockCache = new Mock<ICountryCache>();
mockCache.Setup(c => c.GetCountries()).Returns(_countriesMock);
var countriesService = new CountriesService(mockCache.Object, mockCountriesApiClient.Object);

var result = await countriesService.GetCountries(null, null);

result.Should().BeOfType<List<CountryModel>>();
result.Should().BeSameAs(_countriesMock);
mockCountriesApiClient.Verify(c => c.GetCountries(), Times.Never);
mockCache.Verify(c => c.GetCountries(), Times.Once);
}

[Fact]
public async void GetCountries_Returns10Countries_Pagination()
{
var mockCountriesApiClient = new Mock<ICountriesApiClient>();
mockCountriesApiClient.Setup(c => c.GetCountries()).ReturnsAsync(_countriesMock);

var mockCache = new Mock<ICountryCache>();
mockCache.Setup(c => c.GetCountries()).Returns(new List<CountryModel>());
var countriesService = new CountriesService(mockCache.Object, mockCountriesApiClient.Object);

var result = await countriesService.GetCountries(10, 0);

result.Should().HaveCount(10);
}

[Fact]
public async void GetCountries_ReturnsDifferent10Countries_Pagination()
{
var mockCountriesApiClient = new Mock<ICountriesApiClient>();
mockCountriesApiClient.Setup(c => c.GetCountries()).ReturnsAsync(_countriesMock);

var mockCache = new Mock<ICountryCache>();
mockCache.Setup(c => c.GetCountries()).Returns(new List<CountryModel>());
var countriesService = new CountriesService(mockCache.Object, mockCountriesApiClient.Object);

var result = await countriesService.GetCountries(10, 0);
var resultNextPage = await countriesService.GetCountries(10, 1);
result.Should().HaveCount(10);
resultNextPage.Should().HaveCount(10);
resultNextPage.Should().NotBeSameAs(result);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using Paymentsense.Coding.Challenge.Api.Models;

namespace Paymentsense.Coding.Challenge.Api.Clients
{
internal class CountriesApiClient : ICountriesApiClient
{
private readonly HttpClient _clientFactory;

public CountriesApiClient(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory.CreateClient();
}

public async Task<List<CountryModel>> GetCountries()
{
using var client = _clientFactory;
var responseStream = client.GetStreamAsync(
"https://restcountries.eu/rest/v2/all?fields=name;flag;population;timezones;currencies;languages;capital;borders");

return await JsonSerializer.DeserializeAsync<List<CountryModel>>(await responseStream);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Collections.Generic;
using Paymentsense.Coding.Challenge.Api.Models;

namespace Paymentsense.Coding.Challenge.Api.Clients
{
public class CountryCache : ICountryCache
{
private static List<CountryModel> Countries { get; set; } = new List<CountryModel>();
private static readonly object CountriesLock = new object();

public List<CountryModel> GetCountries()
{
lock (CountriesLock)
{
return Countries;
}
}

public void PopulateCountries(List<CountryModel> countries)
{
lock (CountriesLock)
{
Countries = countries;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Paymentsense.Coding.Challenge.Api.Models;

namespace Paymentsense.Coding.Challenge.Api.Clients
{
public interface ICountriesApiClient
{
public Task<List<CountryModel>> GetCountries();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Paymentsense.Coding.Challenge.Api.Models;

namespace Paymentsense.Coding.Challenge.Api.Clients
{
public interface ICountryCache
{
public List<CountryModel> GetCountries();
public void PopulateCountries(List<CountryModel> countries);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Paymentsense.Coding.Challenge.Api.Models;
using Paymentsense.Coding.Challenge.Api.Services;

namespace Paymentsense.Coding.Challenge.Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class CountriesController : ControllerBase
{
private readonly ICountriesService _countriesService;

public CountriesController(ICountriesService countriesService)
{
_countriesService = countriesService;
}


[HttpGet]
public async Task<ActionResult<List<CountryModel>>> Get(int? pageNumber, int? page)
{
var countries = await _countriesService.GetCountries(null, null);

return Ok(countries);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public ActionResult<string> Get()
return Ok("Paymentsense Coding Challenge!");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc;

namespace Paymentsense.Coding.Challenge.Api.Models
{
[BindProperties(SupportsGet = true)]
public class CountryModel
{
[JsonPropertyName("currencies")] public CurrencyModel[] Currencies { get; set; }
[JsonPropertyName("flag")] public string Flag { get; set; }
[JsonPropertyName("name")] public string Name { get; set; }
[JsonPropertyName("capital")] public string Capital { get; set; }
[JsonPropertyName("population")] public int Population { get; set; }
[JsonPropertyName("timezones")] public string[] Timezones { get; set; }
[JsonPropertyName("borders")] public string[] Borders { get; set; }
[JsonPropertyName("languages")] public Language[] Languages { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Text.Json.Serialization;

namespace Paymentsense.Coding.Challenge.Api.Models
{
public class CurrencyModel
{
[JsonPropertyName("code")] public string Code { get; set; }
[JsonPropertyName("name")] public string Name { get; set; }
[JsonPropertyName("symbol")] public string Symbol { get; set; }
}
}
Loading