Skip to content

Commit 182774c

Browse files
committed
Enabled dependency injection in webapp
1 parent a1f7534 commit 182774c

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

PdfAValidatorWebApi/Controllers/PdfAValidatorController.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Http;
1+
using Codeuctivity;
2+
using Microsoft.AspNetCore.Http;
23
using Microsoft.AspNetCore.Mvc;
34
using System;
45
using System.IO;
@@ -15,6 +16,15 @@ namespace CodeuctivityWebApi.Controllers
1516
[ApiController]
1617
public class PdfAValidatorController : ControllerBase
1718
{
19+
private IPdfAValidator pdfAValidator { get; }
20+
/// <summary>
21+
/// Inject the validator.
22+
/// </summary>
23+
public PdfAValidatorController(IPdfAValidator PdfAValidator)
24+
{
25+
pdfAValidator = PdfAValidator;
26+
}
27+
1828
/// <summary>
1929
/// Validates the compliance of a PdfA.
2030
/// </summary>
@@ -42,7 +52,6 @@ private async Task<ActionResult> InternalValidate()
4252
try
4353
{
4454
using var fs = new FileStream(tempPdfFilePath, FileMode.CreateNew, FileAccess.Write);
45-
using var pdfAValidator = new Codeuctivity.PdfAValidator();
4655
await uploadedFile.CopyToAsync(fs).ConfigureAwait(false);
4756

4857
var result = await pdfAValidator.ValidateAsync(tempPdfFilePath).ConfigureAwait(false);
@@ -75,7 +84,6 @@ public async Task<ActionResult> ValidateWithDetailedReport(IFormFile pdfFile)
7584
try
7685
{
7786
using var fs = new FileStream(tempPdfFilePath, FileMode.CreateNew, FileAccess.Write);
78-
using var pdfAValidator = new Codeuctivity.PdfAValidator();
7987
await uploadedFile.CopyToAsync(fs).ConfigureAwait(false);
8088

8189
var result = pdfAValidator.ValidateWithDetailedReportAsync(tempPdfFilePath);

PdfAValidatorWebApi/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using Codeuctivity;
2+
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.AspNetCore.Mvc;
45
using Microsoft.Extensions.DependencyInjection;
@@ -24,6 +25,7 @@ public class Startup
2425
/// <param name="services"></param>
2526
public void ConfigureServices(IServiceCollection services)
2627
{
28+
services.AddSingleton<IPdfAValidator, PdfAValidator>();
2729
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
2830
// Register the Swagger generator, defining 1 or more Swagger documents
2931
services.AddSwaggerGen(c =>

PdfAValidatorWebApiTest/ShouldStartSuccessfull.cs renamed to PdfAValidatorWebApiTest/IntegrativeTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
namespace CodeuctivityWebApiTest
99
{
10-
public class ShouldStartSuccessfull : IClassFixture<WebApplicationFactory<CodeuctivityWebApi.Startup>>
10+
public class IntegrativeTests : IClassFixture<WebApplicationFactory<CodeuctivityWebApi.Startup>>
1111
{
1212
private readonly WebApplicationFactory<CodeuctivityWebApi.Startup> _factory;
1313

14-
public ShouldStartSuccessfull(WebApplicationFactory<CodeuctivityWebApi.Startup> factory)
14+
public IntegrativeTests(WebApplicationFactory<CodeuctivityWebApi.Startup> factory)
1515
{
1616
_factory = factory;
1717
}
@@ -30,8 +30,7 @@ public async Task ShouldAccessEndpointSuccessfull(string route, string contentTy
3030

3131
// Assert
3232
response.EnsureSuccessStatusCode();
33-
Assert.Equal(contentType,
34-
response.Content.Headers.ContentType.ToString());
33+
Assert.Equal(contentType, response.Content.Headers.ContentType.ToString());
3534
}
3635

3736
[Fact]
@@ -43,17 +42,18 @@ public async Task ShouldValidatePdf()
4342
using var request = new HttpRequestMessage(new HttpMethod("POST"), "http://localhost/api/PdfAValidator");
4443
request.Headers.TryAddWithoutValidation("accept", "*/*");
4544

46-
var multipartContent = new MultipartFormDataContent();
47-
var file1 = new ByteArrayContent(File.ReadAllBytes("../../../FromLibreOffice.pdf"));
45+
using var file1 = new ByteArrayContent(File.ReadAllBytes("../../../FromLibreOffice.pdf"));
4846
file1.Headers.Add("Content-Type", "application/pdf");
47+
var multipartContent = new MultipartFormDataContent();
4948
multipartContent.Add(file1, "pdfFile", Path.GetFileName("FromLibreOffice.pdf"));
5049
request.Content = multipartContent;
5150

5251
// Act
53-
var response = await client.SendAsync(request);
52+
var response = await client.SendAsync(request).ConfigureAwait(false);
5453

5554
// Assert
5655
response.EnsureSuccessStatusCode();
56+
Assert.Equal("true", await response.Content.ReadAsStringAsync().ConfigureAwait(false));
5757
}
5858
}
5959
}

0 commit comments

Comments
 (0)