Skip to content

Commit

Permalink
#124 Implemented GetDocuments on DocumentController API
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGeering committed Aug 29, 2020
1 parent 54631fa commit 6eb75c6
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AdminAssistant.DomainModel.Modules.DocumentsModule.CQRS;
using AdminAssistant.Framework.Providers;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace AdminAssistant.WebAPI.v1
{
[ApiController]
[Route("api/v1/core/[controller]")]
[ApiExplorerSettings(GroupName = "Core - Currency")]
public class DocumentController : WebAPIControllerBase
{
public DocumentController(IMapper mapper, IMediator mediator, ILoggingProvider loggingProvider)
: base(mapper, mediator, loggingProvider)
{
}

[HttpGet]
[SwaggerOperation("Lists all documents.", OperationId = "GetDocument")]
[SwaggerResponse(StatusCodes.Status200OK, "Ok - returns a list of DocumentResponseDto", type: typeof(IEnumerable<DocumentResponseDto>))]
public async Task<ActionResult<IEnumerable<DocumentResponseDto>>> GetDocuments()
{
Log.Start();

var result = await Mediator.Send(new DocumentQuery()).ConfigureAwait(false);
var response = Mapper.Map<IEnumerable<DocumentResponseDto>>(result.Value);

return Log.Finish(Ok(response));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using AdminAssistant.DomainModel.Modules.DocumentsModule;
using AdminAssistant.Framework.TypeMapping;

namespace AdminAssistant.WebAPI.v1
{
public class DocumentResponseDto : IMapFrom<Document>
{
public int DocumentID { get; set; }
public string FileName { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#pragma warning disable CA1707 // Identifiers should not contain underscores
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AdminAssistant.DomainModel;
using AdminAssistant.DomainModel.Modules.AccountsModule;
using AdminAssistant.DomainModel.Modules.DocumentsModule;
using AdminAssistant.DomainModel.Modules.DocumentsModule.CQRS;
using Ardalis.Result;
using AutoMapper;
using FluentAssertions;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace AdminAssistant.WebAPI.v1.Documents
{
public class DocumentController_UnitTest_Should
{
[Fact]
[Trait("Category", "Unit")]
public async Task Return_Status200OK_With_AListOfDocuments_Given_NoArguments()
{
// Arrange
var documents = new List<Document>()
{
Factory.Document.WithTestData(10).Build(),
Factory.Document.WithTestData(20).Build()
};

var services = new ServiceCollection();
services.AddMockServerSideLogging();
services.AddAutoMapper(typeof(MappingProfile));

var mockMediator = new Mock<IMediator>();
mockMediator.Setup(x => x.Send(It.IsAny<DocumentQuery>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(Result<IEnumerable<Document>>.Success(documents)));

services.AddTransient((sp) => mockMediator.Object);
services.AddTransient<DocumentController>();

// Act
var response = await services.BuildServiceProvider().GetRequiredService<DocumentController>().GetDocuments().ConfigureAwait(false);

// Assert
response.Result.Should().BeOfType<OkObjectResult>();
response.Value.Should().BeNull();

var result = (OkObjectResult)response.Result;
result.Value.Should().BeAssignableTo<IEnumerable<DocumentResponseDto>>();

//var value = ((IEnumerable<CurrencyResponseDto>)result.Value).ToArray();
//value.Should().HaveCount(currencies.Count);

//var expected = currencies.ToArray();
//for (int i = 0; i < expected.Length; i++)
//{
// value[i].CurrencyID.Should().Be(expected[i].CurrencyID);
// value[i].Symbol.Should().Be(expected[i].Symbol);
// value[i].DecimalFormat.Should().Be(expected[i].DecimalFormat);
//}
}
}
}
#pragma warning restore CA1707 // Identifiers should not contain underscores
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace AdminAssistant.DomainModel.Modules.DocumentsModule.Builders
{
public interface IDocumentBuilder
{
Document Build();
IDocumentBuilder WithTestData(int documentID = Constants.UnknownRecordID);
IDocumentBuilder WithFileName(string fileName);
}
internal class DocumentBuilder : Document, IDocumentBuilder
{
public static Document Default(IDocumentBuilder builder) => builder.Build();
public static Document Default(DocumentBuilder builder) => builder.Build();

public Document Build() => this;

public IDocumentBuilder WithTestData(int documentID = Constants.UnknownRecordID)
{
DocumentID = documentID;
FileName = "SomRandomFileName.txt";
return this;
}
public IDocumentBuilder WithFileName(string fileName)
{
FileName = fileName;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using Ardalis.Result;
using MediatR;

namespace AdminAssistant.DomainModel.Modules.DocumentsModule.CQRS
{
public class DocumentQuery : IRequest<Result<IEnumerable<Document>>>
{
}
}
14 changes: 14 additions & 0 deletions src/AdminAssistant/DomainModel/Modules/DocumentsModule/Document.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AdminAssistant.Infra.DAL;

namespace AdminAssistant.DomainModel.Modules.DocumentsModule
{
public class Document : IDatabasePersistable
{
public const int FileNameMaxLength = 255;

public int DocumentID { get; set; }
public string FileName { get; set; } = string.Empty;

public int PrimaryKey => DocumentID;
}
}

0 comments on commit 6eb75c6

Please sign in to comment.