Skip to content

Commit

Permalink
Enabled dependency injection in webapp
Browse files Browse the repository at this point in the history
  • Loading branch information
stesee committed Jun 22, 2020
1 parent a1f7534 commit 182774c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
14 changes: 11 additions & 3 deletions PdfAValidatorWebApi/Controllers/PdfAValidatorController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Http;
using Codeuctivity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
Expand All @@ -15,6 +16,15 @@ namespace CodeuctivityWebApi.Controllers
[ApiController]
public class PdfAValidatorController : ControllerBase
{
private IPdfAValidator pdfAValidator { get; }
/// <summary>
/// Inject the validator.
/// </summary>
public PdfAValidatorController(IPdfAValidator PdfAValidator)
{
pdfAValidator = PdfAValidator;
}

/// <summary>
/// Validates the compliance of a PdfA.
/// </summary>
Expand Down Expand Up @@ -42,7 +52,6 @@ private async Task<ActionResult> InternalValidate()
try
{
using var fs = new FileStream(tempPdfFilePath, FileMode.CreateNew, FileAccess.Write);
using var pdfAValidator = new Codeuctivity.PdfAValidator();
await uploadedFile.CopyToAsync(fs).ConfigureAwait(false);

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

var result = pdfAValidator.ValidateWithDetailedReportAsync(tempPdfFilePath);
Expand Down
4 changes: 3 additions & 1 deletion PdfAValidatorWebApi/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using Codeuctivity;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -24,6 +25,7 @@ public class Startup
/// <param name="services"></param>
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IPdfAValidator, PdfAValidator>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Latest);
// Register the Swagger generator, defining 1 or more Swagger documents
services.AddSwaggerGen(c =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace CodeuctivityWebApiTest
{
public class ShouldStartSuccessfull : IClassFixture<WebApplicationFactory<CodeuctivityWebApi.Startup>>
public class IntegrativeTests : IClassFixture<WebApplicationFactory<CodeuctivityWebApi.Startup>>
{
private readonly WebApplicationFactory<CodeuctivityWebApi.Startup> _factory;

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

// Assert
response.EnsureSuccessStatusCode();
Assert.Equal(contentType,
response.Content.Headers.ContentType.ToString());
Assert.Equal(contentType, response.Content.Headers.ContentType.ToString());
}

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

var multipartContent = new MultipartFormDataContent();
var file1 = new ByteArrayContent(File.ReadAllBytes("../../../FromLibreOffice.pdf"));
using var file1 = new ByteArrayContent(File.ReadAllBytes("../../../FromLibreOffice.pdf"));
file1.Headers.Add("Content-Type", "application/pdf");
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(file1, "pdfFile", Path.GetFileName("FromLibreOffice.pdf"));
request.Content = multipartContent;

// Act
var response = await client.SendAsync(request);
var response = await client.SendAsync(request).ConfigureAwait(false);

// Assert
response.EnsureSuccessStatusCode();
Assert.Equal("true", await response.Content.ReadAsStringAsync().ConfigureAwait(false));
}
}
}

0 comments on commit 182774c

Please sign in to comment.