Skip to content

Commit ae132df

Browse files
authored
Merge pull request #724 from DFE-Digital/bugfix/215979-excel-exports-fail-if-too-many-UKPRNs-are-queried
Prepare/Academies API: Excel exports fail if too many UKPRNs are queried
2 parents ed700b3 + 446e881 commit ae132df

File tree

8 files changed

+601
-3
lines changed

8 files changed

+601
-3
lines changed

Dfe.Academies.TramsDataApi.Tests.Integration/Controllers/V4/EstablishmentsControllerTests.cs

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public async Task SearchEstablishmentsAsync_ShouldReturnAllEstablishments_WhenCl
2525

2626
// Assert
2727
Assert.NotNull(establishmentDtos);
28-
Assert.Equal(3, establishmentDtos.Count);
28+
Assert.Equal(5, establishmentDtos.Count);
2929
}
3030

3131
[Theory]
@@ -44,7 +44,7 @@ public async Task SearchEstablishmentsAsync_ShouldReturnAllEstablishments_WhenCl
4444

4545
// Assert
4646
Assert.NotNull(establishmentDtos);
47-
Assert.Equal(3, establishmentDtos.Count);
47+
Assert.Equal(5, establishmentDtos.Count);
4848
}
4949

5050
[Theory]
@@ -65,4 +65,98 @@ public async Task SearchEstablishmentsAsync_ShouldReturnAllEstablishments_WhenCl
6565
Assert.NotNull(establishmentDtos);
6666
Assert.Equal(2, establishmentDtos.Count);
6767
}
68+
69+
[Theory]
70+
[CustomAutoData(typeof(CustomWebApplicationDbContextFactoryCustomization<Startup>))]
71+
public async Task EstablishmentsByUrnsAsync_ShouldReturnAllEstablishments_By_UKPRN(
72+
CustomWebApplicationDbContextFactory<Startup> factory,
73+
IEstablishmentsV4Client establishmentsClient)
74+
{
75+
// Arrange
76+
factory.TestClaims = default;
77+
78+
var request = new UrnRequestModel
79+
{
80+
Urns = [22, 33]
81+
};
82+
83+
// Act
84+
var result = await establishmentsClient.GetEstablishmentsByUrnsAsync(request);
85+
86+
// Assert
87+
Assert.NotNull(result);
88+
Assert.Equal(2, result.Count);
89+
90+
var establishment = result.SingleOrDefault(x => x.Urn == request.Urns[0].ToString());
91+
Assert.NotNull(establishment);
92+
93+
establishment = result.SingleOrDefault(x => x.Urn == request.Urns[1].ToString());
94+
Assert.NotNull(establishment);
95+
}
96+
97+
[Theory]
98+
[CustomAutoData(typeof(CustomWebApplicationDbContextFactoryCustomization<Startup>))]
99+
public async Task EstablishmentsByUrnsAsync_ShouldReturnNotFound(
100+
CustomWebApplicationDbContextFactory<Startup> factory,
101+
IEstablishmentsV4Client establishmentsClient)
102+
{
103+
// Arrange
104+
factory.TestClaims = default;
105+
106+
var request = new UrnRequestModel
107+
{
108+
Urns = [10001, 10002] // those URNs do not exist
109+
};
110+
111+
AcademiesApiException exception = await Assert.ThrowsAsync<AcademiesApiException>(() => establishmentsClient.GetEstablishmentsByUrnsAsync(request));
112+
113+
Assert.Equal(404, exception.StatusCode);
114+
}
115+
116+
[Theory]
117+
[CustomAutoData(typeof(CustomWebApplicationDbContextFactoryCustomization<Startup>))]
118+
public async Task EstablishmentsByUkprnsAsync_ShouldReturnAllEstablishments_By_UKPRNs(
119+
CustomWebApplicationDbContextFactory<Startup> factory,
120+
IEstablishmentsV4Client establishmentsClient)
121+
{
122+
// Arrange
123+
factory.TestClaims = default;
124+
125+
var request = new UkprnRequestModel
126+
{
127+
Ukprns = ["10060367", "10067112"]
128+
};
129+
130+
// Act
131+
var result = await establishmentsClient.GetEstablishmentsByUkprnsAsync(request);
132+
133+
// Assert
134+
Assert.NotNull(result);
135+
Assert.Equal(2, result.Count);
136+
137+
var establishment = result.SingleOrDefault(x => x.Ukprn == request.Ukprns[0]);
138+
Assert.NotNull(establishment);
139+
140+
establishment = result.SingleOrDefault(x => x.Ukprn == request.Ukprns[1]);
141+
Assert.NotNull(establishment);
142+
}
143+
144+
[Theory]
145+
[CustomAutoData(typeof(CustomWebApplicationDbContextFactoryCustomization<Startup>))]
146+
public async Task EstablishmentsByUkprnsAsync_ShouldReturnNotFound(
147+
CustomWebApplicationDbContextFactory<Startup> factory,
148+
IEstablishmentsV4Client establishmentsClient)
149+
{
150+
// Arrange
151+
factory.TestClaims = default;
152+
153+
var request = new UkprnRequestModel
154+
{
155+
Ukprns = ["10000000", "4634523"] // those UKPRN do not exist
156+
};
157+
158+
AcademiesApiException exception = await Assert.ThrowsAsync<AcademiesApiException>(() => establishmentsClient.GetEstablishmentsByUkprnsAsync(request));
159+
160+
Assert.Equal(404, exception.StatusCode);
161+
}
68162
}

Dfe.AcademiesApi.Client/Generated/Client.g.cs

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,6 +3184,105 @@ public virtual async System.Threading.Tasks.Task<EstablishmentDto> GetEstablishm
31843184
}
31853185
}
31863186

3187+
/// <summary>
3188+
/// Retrieves a list of establishments by their Unique Reference Numbers (URNs).
3189+
/// </summary>
3190+
/// <param name="model">Contains Unique Reference Number (URNs) of the establishments.</param>
3191+
/// <returns>Successfully found and returned the establishments.</returns>
3192+
/// <exception cref="AcademiesApiException">A server side error occurred.</exception>
3193+
public virtual System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<EstablishmentDto>> GetEstablishmentsByUrnsAsync(UrnRequestModel model)
3194+
{
3195+
return GetEstablishmentsByUrnsAsync(model, System.Threading.CancellationToken.None);
3196+
}
3197+
3198+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
3199+
/// <summary>
3200+
/// Retrieves a list of establishments by their Unique Reference Numbers (URNs).
3201+
/// </summary>
3202+
/// <param name="model">Contains Unique Reference Number (URNs) of the establishments.</param>
3203+
/// <returns>Successfully found and returned the establishments.</returns>
3204+
/// <exception cref="AcademiesApiException">A server side error occurred.</exception>
3205+
public virtual async System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<EstablishmentDto>> GetEstablishmentsByUrnsAsync(UrnRequestModel model, System.Threading.CancellationToken cancellationToken)
3206+
{
3207+
if (model == null)
3208+
throw new System.ArgumentNullException("model");
3209+
3210+
var client_ = _httpClient;
3211+
var disposeClient_ = false;
3212+
try
3213+
{
3214+
using (var request_ = new System.Net.Http.HttpRequestMessage())
3215+
{
3216+
var json_ = Newtonsoft.Json.JsonConvert.SerializeObject(model, JsonSerializerSettings);
3217+
var content_ = new System.Net.Http.StringContent(json_);
3218+
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
3219+
request_.Content = content_;
3220+
request_.Method = new System.Net.Http.HttpMethod("POST");
3221+
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));
3222+
3223+
var urlBuilder_ = new System.Text.StringBuilder();
3224+
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
3225+
// Operation Path: "v4/establishments/bulk/urns"
3226+
urlBuilder_.Append("v4/establishments/bulk/urns");
3227+
3228+
PrepareRequest(client_, request_, urlBuilder_);
3229+
3230+
var url_ = urlBuilder_.ToString();
3231+
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
3232+
3233+
PrepareRequest(client_, request_, url_);
3234+
3235+
var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
3236+
var disposeResponse_ = true;
3237+
try
3238+
{
3239+
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
3240+
foreach (var item_ in response_.Headers)
3241+
headers_[item_.Key] = item_.Value;
3242+
if (response_.Content != null && response_.Content.Headers != null)
3243+
{
3244+
foreach (var item_ in response_.Content.Headers)
3245+
headers_[item_.Key] = item_.Value;
3246+
}
3247+
3248+
ProcessResponse(client_, response_);
3249+
3250+
var status_ = (int)response_.StatusCode;
3251+
if (status_ == 200)
3252+
{
3253+
var objectResponse_ = await ReadObjectResponseAsync<System.Collections.ObjectModel.ObservableCollection<EstablishmentDto>>(response_, headers_, cancellationToken).ConfigureAwait(false);
3254+
if (objectResponse_.Object == null)
3255+
{
3256+
throw new AcademiesApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
3257+
}
3258+
return objectResponse_.Object;
3259+
}
3260+
else
3261+
if (status_ == 404)
3262+
{
3263+
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
3264+
throw new AcademiesApiException("Establishments with specified Unique Reference Numbers (URNs) not found.", status_, responseText_, headers_, null);
3265+
}
3266+
else
3267+
{
3268+
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
3269+
throw new AcademiesApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
3270+
}
3271+
}
3272+
finally
3273+
{
3274+
if (disposeResponse_)
3275+
response_.Dispose();
3276+
}
3277+
}
3278+
}
3279+
finally
3280+
{
3281+
if (disposeClient_)
3282+
client_.Dispose();
3283+
}
3284+
}
3285+
31873286
/// <summary>
31883287
/// Retrieves a list of establishments by their Trust UK Provider Reference Number (UKPRN) identifier.
31893288
/// </summary>
@@ -3380,6 +3479,105 @@ public virtual async System.Threading.Tasks.Task<EstablishmentDto> GetEstablishm
33803479
}
33813480
}
33823481

3482+
/// <summary>
3483+
/// Retrieves a list of establishments by their UKPRNs.
3484+
/// </summary>
3485+
/// <param name="model">Contains UKPRNs of the establishments.</param>
3486+
/// <returns>Successfully found and returned the establishments.</returns>
3487+
/// <exception cref="AcademiesApiException">A server side error occurred.</exception>
3488+
public virtual System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<EstablishmentResponse>> GetEstablishmentsByUkprnsAsync(UkprnRequestModel model)
3489+
{
3490+
return GetEstablishmentsByUkprnsAsync(model, System.Threading.CancellationToken.None);
3491+
}
3492+
3493+
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
3494+
/// <summary>
3495+
/// Retrieves a list of establishments by their UKPRNs.
3496+
/// </summary>
3497+
/// <param name="model">Contains UKPRNs of the establishments.</param>
3498+
/// <returns>Successfully found and returned the establishments.</returns>
3499+
/// <exception cref="AcademiesApiException">A server side error occurred.</exception>
3500+
public virtual async System.Threading.Tasks.Task<System.Collections.ObjectModel.ObservableCollection<EstablishmentResponse>> GetEstablishmentsByUkprnsAsync(UkprnRequestModel model, System.Threading.CancellationToken cancellationToken)
3501+
{
3502+
if (model == null)
3503+
throw new System.ArgumentNullException("model");
3504+
3505+
var client_ = _httpClient;
3506+
var disposeClient_ = false;
3507+
try
3508+
{
3509+
using (var request_ = new System.Net.Http.HttpRequestMessage())
3510+
{
3511+
var json_ = Newtonsoft.Json.JsonConvert.SerializeObject(model, JsonSerializerSettings);
3512+
var content_ = new System.Net.Http.StringContent(json_);
3513+
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
3514+
request_.Content = content_;
3515+
request_.Method = new System.Net.Http.HttpMethod("POST");
3516+
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));
3517+
3518+
var urlBuilder_ = new System.Text.StringBuilder();
3519+
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
3520+
// Operation Path: "v4/establishments/bulk/ukprns"
3521+
urlBuilder_.Append("v4/establishments/bulk/ukprns");
3522+
3523+
PrepareRequest(client_, request_, urlBuilder_);
3524+
3525+
var url_ = urlBuilder_.ToString();
3526+
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);
3527+
3528+
PrepareRequest(client_, request_, url_);
3529+
3530+
var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
3531+
var disposeResponse_ = true;
3532+
try
3533+
{
3534+
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
3535+
foreach (var item_ in response_.Headers)
3536+
headers_[item_.Key] = item_.Value;
3537+
if (response_.Content != null && response_.Content.Headers != null)
3538+
{
3539+
foreach (var item_ in response_.Content.Headers)
3540+
headers_[item_.Key] = item_.Value;
3541+
}
3542+
3543+
ProcessResponse(client_, response_);
3544+
3545+
var status_ = (int)response_.StatusCode;
3546+
if (status_ == 200)
3547+
{
3548+
var objectResponse_ = await ReadObjectResponseAsync<System.Collections.ObjectModel.ObservableCollection<EstablishmentResponse>>(response_, headers_, cancellationToken).ConfigureAwait(false);
3549+
if (objectResponse_.Object == null)
3550+
{
3551+
throw new AcademiesApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
3552+
}
3553+
return objectResponse_.Object;
3554+
}
3555+
else
3556+
if (status_ == 404)
3557+
{
3558+
string responseText_ = ( response_.Content == null ) ? string.Empty : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
3559+
throw new AcademiesApiException("Establishments with specified UKPRNs not found.", status_, responseText_, headers_, null);
3560+
}
3561+
else
3562+
{
3563+
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
3564+
throw new AcademiesApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
3565+
}
3566+
}
3567+
finally
3568+
{
3569+
if (disposeResponse_)
3570+
response_.Dispose();
3571+
}
3572+
}
3573+
}
3574+
finally
3575+
{
3576+
if (disposeClient_)
3577+
client_.Dispose();
3578+
}
3579+
}
3580+
33833581
protected struct ObjectResponseResult<T>
33843582
{
33853583
public ObjectResponseResult(T responseObject, string responseText)

0 commit comments

Comments
 (0)