using System; using System.Collections.Generic; using System.Linq; using LCU.API.Collections.RepoApi.Controllers; using LCU.API.Collections.RepoApi.Exceptions; using LCU.API.Collections.RepoApi.Models.Responses; using LCU.API.Collections.RepoApi.Services; using LCU.Shared.Collections.RepoApiLib.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Moq; using Xunit; namespace LCU.API.Collections.RepoApiTests.Controllers { public class RepoCompanySelectionControllerTest { private RepoCompanySelectionController _controller; private Mock _mockService; private Mock _mockHttpContextAccessor; public RepoCompanySelectionControllerTest() { _mockService = new Mock(); _mockHttpContextAccessor = new Mock(); _controller = new RepoCompanySelectionController( _mockService.Object, _mockHttpContextAccessor.Object); } [Fact] public async void Get_Ok() { // Arrange _mockService.Setup(service => service.ListAllAsync()).ReturnsAsync(new List() { new RepoCompanySelection(), new RepoCompanySelection() }); // Act var result = await _controller.Get(); // Assert Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.NotNull(okResult.Value); var repoCompanySelectionList = okResult.Value as IEnumerable; Assert.NotNull(repoCompanySelectionList); Assert.Equal(2, repoCompanySelectionList.Count()); } [Fact] public async void Get_ById_Ok() { // Arrange _mockService.Setup(service => service.GetAsync(It.IsAny())).ReturnsAsync(new RepoCompanySelection() { RepoCompanyId = 5 }); // Act var result = await _controller.Get(42); // Assert Assert.NotNull(result); var okResult = result as OkObjectResult; Assert.NotNull(okResult); Assert.NotNull(okResult.Value); var repoCompanySelection = okResult.Value as RepoCompanySelection; Assert.NotNull(repoCompanySelection); Assert.Equal(5, repoCompanySelection.RepoCompanyId); } [Fact] public async void Get_ById_Invalid_ID() { // Act var result = await _controller.Get(0); // Assert Assert.NotNull(result); var badRequestResult = result as BadRequestObjectResult; Assert.NotNull(badRequestResult); Assert.Equal("Invalid ID", badRequestResult.Value); } [Fact] public async void Get_ById_Not_Found() { // Arrange _mockService.Setup(service => service.GetAsync(It.IsAny())) .ThrowsAsync(new NotFoundException("Not Found in Get")); // Act var result = await _controller.Get(1); // Assert Assert.NotNull(result); var notFoundRequest = result as NotFoundObjectResult; Assert.NotNull(notFoundRequest); Assert.Equal("Not Found in Get", notFoundRequest.Value); } } }