using System.Collections.Generic; using LCU.API.Collections.RepoApi.Controllers; using LCU.API.Collections.RepoApi.Models.Filters; using LCU.API.Collections.RepoApi.Models.Responses; using LCU.API.Collections.RepoApi.Services; using LCU.Shared.Collections.RepoApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Models.Results; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Moq; using Xunit; namespace LCU.API.Collections.RepoApiTests.Controllers { public class AuditRecordControllerTest { private readonly Mock mockAuditRecordService; private readonly AuditRecordController controller; private readonly Mock mockHttpContextAccessor; public AuditRecordControllerTest() { mockAuditRecordService = new Mock(); mockHttpContextAccessor = new Mock(); controller = new AuditRecordController(mockAuditRecordService.Object, mockHttpContextAccessor.Object); } // Get tests [Fact] public async void Get_OK() { mockAuditRecordService.Setup(service => service.ListAll(It.IsAny())).ReturnsAsync( new PagedResult() { Total = 1, Items = new List() { new AuditRecord() { Action = "action", ClassName = "Something", Description = "changed something field 1", PerformedBy = "Developer", KeyRepresentation = "111" } } }); // arrange var filter = new PageFilter(); // act var result = await controller.Get(filter); Assert.NotNull(result); var okResult = result as OkObjectResult; // assert Assert.NotNull(okResult); Assert.Equal(200, okResult.StatusCode); var okValue = okResult.Value as PagedResponse; Assert.NotNull(okValue); Assert.Equal(1, okValue.TotalItems); } // Post tests [Fact] public async void Post_OK() { // arrange var postAudit = new AuditRecord() { Action = "action", ClassName = "Something", Description = "changed something field 1", PerformedBy = "Developer", KeyRepresentation = "111" }; // act var result = await controller.Post(postAudit); Assert.NotNull(result); var okResult = result as OkObjectResult; // assert Assert.NotNull(okResult); Assert.Equal(200, okResult.StatusCode); } [Fact] public async void Post_Bad_Request() { // arrange var postAudit = new AuditRecord() { Action = "action", ClassName = "Something", Description = "changed something field 1", KeyRepresentation = "111" }; controller.ModelState.AddModelError("PerformedBy", "The performed by field is required."); // act var result = await controller.Post(postAudit); Assert.NotNull(result); var badRequestResult = result as BadRequestObjectResult; // assert Assert.NotNull(badRequestResult); Assert.Equal(StatusCodes.Status400BadRequest, badRequestResult.StatusCode); } } }