-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from DFE-Digital/feature/152055-get-projects-…
…from-mfsp 152055: added support for calling external mfsp api to get projects
- Loading branch information
Showing
16 changed files
with
264 additions
and
131 deletions.
There are no files selected for viewing
57 changes: 0 additions & 57 deletions
57
TramsDataApi.Test/Controllers/FssProjectControllerTests.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using FluentValidation; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Http; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using TramsDataApi.ResponseModels; | ||
|
||
namespace TramsDataApi.Test.FakeApi | ||
{ | ||
public class FakeMfspApi | ||
{ | ||
private IWebHost _server; | ||
|
||
public void Start() | ||
{ | ||
_server = new WebHostBuilder().UseKestrel(x => x.ListenLocalhost(5003)).Configure(app => | ||
{ | ||
app.Run(async context => | ||
{ | ||
if (context.Request.Method == HttpMethods.Get && context.Request.Path == "/v2/fss/projects") | ||
{ | ||
var response = new List<FssProjectResponse>() | ||
{ | ||
new FssProjectResponse() | ||
{ | ||
CurrentFreeSchoolName = "This is my free school", | ||
AgeRange = "5-11", | ||
ProjectStatus = "Open", | ||
}, | ||
new FssProjectResponse() | ||
{ | ||
CurrentFreeSchoolName = "This is another free school", | ||
AgeRange = "11-16", | ||
ProjectStatus = "Open", | ||
}, | ||
}; | ||
|
||
await context.Response.WriteAsJsonAsync(response); | ||
} | ||
else | ||
{ | ||
context.Response.StatusCode = StatusCodes.Status404NotFound; | ||
await context.Response.WriteAsync("Not found"); | ||
} | ||
}); | ||
}).Build(); | ||
|
||
_server.Start(); | ||
} | ||
|
||
public void Stop() | ||
{ | ||
_server.StopAsync().Wait(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
TramsDataApi.Test/Integration/FssProjectIntegrationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using FluentAssertions; | ||
using Newtonsoft.Json; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using TramsDataApi.ResponseModels; | ||
using TramsDataApi.Test.Fixtures; | ||
using Xunit; | ||
|
||
namespace TramsDataApi.Test.Integration | ||
{ | ||
[Collection(ApiTestCollection.ApiTestCollectionName)] | ||
public class FssProjectIntegrationTests | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
public FssProjectIntegrationTests(ApiTestFixture fixture) | ||
{ | ||
_client = fixture.Client; | ||
} | ||
|
||
[Fact] | ||
public async Task GetFssProject() | ||
{ | ||
var response = await _client.GetAsync($"v2/fss/projects"); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
|
||
var responseContent = await response.Content.ReadAsStringAsync(); | ||
|
||
var fssProjectsResponse = JsonConvert.DeserializeObject<ApiResponseV2<FssProjectResponse>>(responseContent); | ||
|
||
var fssProjects = fssProjectsResponse.Data; | ||
|
||
fssProjects.Should().HaveCount(2); | ||
|
||
var firstProject = fssProjects.ElementAt(0); | ||
firstProject.CurrentFreeSchoolName.Should().Be("This is my free school"); | ||
firstProject.AgeRange.Should().Be("5-11"); | ||
firstProject.ProjectStatus.Should().Be("Open"); | ||
|
||
var secondProject = fssProjects.ElementAt(1); | ||
secondProject.CurrentFreeSchoolName.Should().Be("This is another free school"); | ||
secondProject.AgeRange.Should().Be("11-16"); | ||
secondProject.ProjectStatus.Should().Be("Open"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace TramsDataApi.Configuration | ||
{ | ||
public class MfspOptions | ||
{ | ||
public string ApiEndpoint { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
|
||
namespace TramsDataApi.Services | ||
{ | ||
public abstract class ApiClient | ||
{ | ||
private readonly IHttpClientFactory _clientFactory; | ||
private readonly ILogger<ApiClient> _logger; | ||
private string _httpClientName; | ||
|
||
protected ApiClient(IHttpClientFactory clientFactory, ILogger<ApiClient> logger, string httpClientName) | ||
{ | ||
_clientFactory = clientFactory; | ||
_logger = logger; | ||
_httpClientName = httpClientName; | ||
} | ||
|
||
public async Task<T> Get<T>(string endpoint) where T : class | ||
{ | ||
try | ||
{ | ||
var request = new HttpRequestMessage(HttpMethod.Get, endpoint); | ||
|
||
var client = CreateHttpClient(); | ||
|
||
var response = await client.SendAsync(request); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
var content = await response.Content.ReadAsStringAsync(); | ||
|
||
var result = JsonConvert.DeserializeObject<T>(content); | ||
|
||
return result; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, ex.Message); | ||
throw; | ||
} | ||
} | ||
|
||
private HttpClient CreateHttpClient() | ||
{ | ||
var client = _clientFactory.CreateClient(_httpClientName); | ||
|
||
return client; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.Extensions.Logging; | ||
using System.Net.Http; | ||
|
||
namespace TramsDataApi.Services | ||
{ | ||
public class MfspApiClient : ApiClient | ||
{ | ||
public MfspApiClient(IHttpClientFactory clientFactory, ILogger<ApiClient> logger, string httpClientName = "MfspApiClient") : base(clientFactory, logger, httpClientName) | ||
{ | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.