Skip to content

Commit

Permalink
Merge pull request #40 from DFE-Digital/rationale-for-project
Browse files Browse the repository at this point in the history
Rationale for project
  • Loading branch information
madeviv authored Jun 11, 2021
2 parents 83345c1 + 7cb14d9 commit 53d05e8
Show file tree
Hide file tree
Showing 31 changed files with 749 additions and 383 deletions.
16 changes: 16 additions & 0 deletions ApplyToBecomeInternal/ApplyToBecome.Data/ApiResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Net;

namespace ApplyToBecome.Data
{
public class ApiResponse<TBody>
{
public ApiResponse(HttpStatusCode statusCode, TBody body)
{
Success = (int)statusCode >= 200 && (int)statusCode < 300;
Body = body;
}

public bool Success { get; }
public TBody Body { get; }
}
}
5 changes: 3 additions & 2 deletions ApplyToBecomeInternal/ApplyToBecome.Data/IProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace ApplyToBecome.Data
{
public interface IProjects
{
Task<IEnumerable<Project>> GetAllProjects();
Task<Project> GetProjectById(int id);
Task<ApiResponse<IEnumerable<Project>>> GetAllProjects();
Task<ApiResponse<Project>> GetProjectById(int id);
Task<ApiResponse<Project>> UpdateProject(int id, UpdateProject project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ApplyToBecome.Data.Models
{
public class UpdateProject
{
public string RationaleForProject { get; set; }
public string RationaleForTrust { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ApplyToBecome.Data.Models;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
Expand All @@ -15,14 +16,40 @@ public ProjectsService(IHttpClientFactory httpClientFactory)
_httpClient = httpClientFactory.CreateClient("TramsClient");
}

public async Task<IEnumerable<Project>> GetAllProjects()
public async Task<ApiResponse<IEnumerable<Project>>> GetAllProjects()
{
return await _httpClient.GetFromJsonAsync<IEnumerable<Project>>("conversion-projects");
var response = await _httpClient.GetAsync("conversion-projects");
if (!response.IsSuccessStatusCode)
{
return new ApiResponse<IEnumerable<Project>>(response.StatusCode, Enumerable.Empty<Project>());
}

var projects = await response.Content.ReadFromJsonAsync<IEnumerable<Project>>();
return new ApiResponse<IEnumerable<Project>>(response.StatusCode, projects);
}

public async Task<ApiResponse<Project>> GetProjectById(int id)
{
var response = await _httpClient.GetAsync($"conversion-projects/{id}");
if (!response.IsSuccessStatusCode)
{
return new ApiResponse<Project>(response.StatusCode, null);
}

var project = await response.Content.ReadFromJsonAsync<Project>();
return new ApiResponse<Project>(response.StatusCode, project);
}

public async Task<Project> GetProjectById(int id)
public async Task<ApiResponse<Project>> UpdateProject(int id, UpdateProject updateProject)
{
return await _httpClient.GetFromJsonAsync<Project>($"conversion-projects/{id}");
var response = await _httpClient.PatchAsync($"conversion-projects/{id}", JsonContent.Create(updateProject));
if (!response.IsSuccessStatusCode)
{
return new ApiResponse<Project>(response.StatusCode, null);
}

var project = await response.Content.ReadFromJsonAsync<Project>();
return new ApiResponse<Project>(response.StatusCode, project);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using AutoFixture.Kernel;
using System;
using System.Reflection;

namespace ApplyToBecomeInternal.Tests.Customisations
{
public class OfstedRatingSpecimenBuilder : ISpecimenBuilder
{
public object Create(object request, ISpecimenContext context)
{
var pi = request as PropertyInfo;
if (pi == null || pi.PropertyType != typeof(string))
return new NoSpecimen();

var i = new Random().Next();

return "12349".Substring(i % 5, 1);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using FluentAssertions.Primitives;

namespace FluentAssertions
{
public static class FluentAssertionExtensions
{
public static void BeUrl(this StringAssertions stringAssertions, string url)
{
stringAssertions.Be($"http://localhost{url}");
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
using ApplyToBecomeInternal.Tests.Helpers;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using WireMock.Matchers;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace ApplyToBecomeInternal.Tests
{
public class IntegrationTestingWebApplicationFactory : WebApplicationFactory<Startup>, IDisposable
{
private static int _currentPort = 5080;
private static object _sync = new object();

private readonly WireMockServer _server;
private readonly int _port;
private readonly int _port;

public IntegrationTestingWebApplicationFactory()
{
_port = PortHelper.AllocateNext();
_port = AllocateNext();
_server = WireMockServer.Start(_port);
}

public WireMockServer WMServer => _server;

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureAppConfiguration(config =>
Expand All @@ -40,6 +44,51 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
});
}

public void AddGetWithJsonResponse<TResponseBody>(string path, TResponseBody responseBody)
{
_server
.Given(Request.Create()
.WithPath(path)
.UsingGet())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody(JsonSerializer.Serialize(responseBody)));
}

public void AddPatchWithJsonRequest<TRequestBody, TResponseBody>(string path, TRequestBody requestBody, TResponseBody responseBody)
{
_server
.Given(Request.Create()
.WithPath(path)
.WithBody(new JsonMatcher(requestBody, true))
.UsingPatch())
.RespondWith(Response.Create()
.WithStatusCode(200)
.WithHeader("Content-Type", "application/json")
.WithBody(JsonSerializer.Serialize(responseBody)));
}

public void AddErrorResponse(string path, string method)
{
_server
.Given(Request.Create()
.WithPath(path)
.UsingMethod(method))
.RespondWith(Response.Create()
.WithStatusCode(500));
}

private static int AllocateNext()
{
lock (_sync)
{
var next = _currentPort;
_currentPort++;
return next;
}
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using ApplyToBecome.Data.Models;
using ApplyToBecomeInternal.Tests.Customisations;
using AutoFixture;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using SchoolPerformanceModel = ApplyToBecome.Data.Models.SchoolPerformance;

namespace ApplyToBecomeInternal.Tests.Pages
{
public abstract partial class BaseIntegrationTests
{
protected IEnumerable<Project> AddGetProjects()
{
var projects = _fixture.CreateMany<Project>();
_factory.AddGetWithJsonResponse("/conversion-projects", projects);
return projects;
}

public Project AddGetProject(Action<Project> postSetup = null)
{
var project = _fixture.Create<Project>();
if (postSetup != null)
{
postSetup(project);
}
_factory.AddGetWithJsonResponse($"/conversion-projects/{project.Id}", project);
return project;
}

public (Project, UpdateProject) AddGetAndPatchProject<TProperty>(Expression<Func<UpdateProject, TProperty>> expression)
{
var project = _fixture.Create<Project>();
var request = _fixture
.Build<UpdateProject>()
.OmitAutoProperties()
.With(expression)
.Create();

_factory.AddGetWithJsonResponse($"/conversion-projects/{project.Id}", project);
_factory.AddPatchWithJsonRequest($"/conversion-projects/{project.Id}", request, project);
return (project, request);
}

public void AddPatchError(int id)
{
_factory.AddErrorResponse($"/conversion-projects/{id}", "patch");
}

public SchoolPerformanceModel AddGetSchoolPerformance(string urn)
{
_fixture.Customizations.Add(new OfstedRatingSpecimenBuilder());
var establishmentMockData = _fixture.Create<EstablishmentMockData>();
_factory.AddGetWithJsonResponse($"/establishment/urn/{urn}", establishmentMockData);
return establishmentMockData.misEstablishment;
}

private class EstablishmentMockData
{
public SchoolPerformanceModel misEstablishment { get; set; }

public DateTime ofstedLastInspection { get; set; }
}
}
}
Loading

0 comments on commit 53d05e8

Please sign in to comment.