using ApplicationTests.Fixtures; using FakeItEasy; using FluentValidation; using FluentValidation.AspNetCore; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; using SharedLibraryCore.Dtos; using SharedLibraryCore.Helpers; using SharedLibraryCore.Interfaces; using Stats.Dtos; using StatsWeb.Dtos; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WebfrontCore.Controllers.API; using WebfrontCore.Controllers.API.Dtos; using WebfrontCore.Controllers.API.Validation; namespace ApplicationTests { [TestFixture] public class ApiTests { private IServiceProvider serviceProvider; private IDatabaseContextFactory contextFactory; private ClientController clientController; private StatsWeb.API.StatsController statsController; private IResourceQueryHelper fakeClientQueryHelper; private IResourceQueryHelper fakeStatsQueryHelper; [SetUp] public void Setup() { var collection = new ServiceCollection(); collection.AddMvc() .AddFluentValidation(); serviceProvider = collection.AddSingleton() .AddSingleton() .AddSingleton(A.Fake>()) .AddSingleton(A.Fake>()) .AddTransient, FindClientRequestValidator>() .BuildBase() .BuildServiceProvider(); clientController = serviceProvider.GetRequiredService(); statsController = serviceProvider.GetRequiredService(); contextFactory = serviceProvider.GetRequiredService(); fakeClientQueryHelper = serviceProvider.GetRequiredService>(); fakeStatsQueryHelper = serviceProvider.GetRequiredService>(); } #region CLIENT_CONTROLLER [Test] public async Task Test_ClientController_FindAsync_Happy() { var query = new FindClientRequest() { Name = "test" }; int expectedClientId = 123; A.CallTo(() => fakeClientQueryHelper.QueryResource(A.Ignored)) .Returns(Task.FromResult(new ResourceQueryHelperResult() { Results = new[] { new FindClientResult() { ClientId = expectedClientId } } })); var result = await clientController.FindAsync(query); Assert.IsInstanceOf(result); var viewResult = (result as OkObjectResult).Value as FindClientResponse; Assert.NotNull(viewResult); Assert.AreEqual(expectedClientId, viewResult.Clients.First().ClientId); } [Test] public async Task Test_ClientController_FindAsync_InvalidModelState() { var query = new FindClientRequest(); clientController.ModelState.AddModelError("test", "test"); var result = await clientController.FindAsync(query); Assert.IsInstanceOf(result); clientController.ModelState.Clear(); } [Test] public async Task Test_ClientController_FindAsync_Exception() { string expectedExceptionMessage = "failure"; int expectedStatusCode = 500; var query = new FindClientRequest(); A.CallTo(() => fakeClientQueryHelper.QueryResource(A.Ignored)) .Throws(new Exception(expectedExceptionMessage)); var result = await clientController.FindAsync(query); Assert.IsInstanceOf(result); var statusResult = (result as ObjectResult); Assert.AreEqual(expectedStatusCode, statusResult.StatusCode); //Assert.IsTrue((statusResult.Value as ErrorResponse).Messages.Contains(expectedExceptionMessage)); } #endregion #region STATS_CONTROLLER [Test] public async Task Test_StatsController_ClientStats_Happy() { var client = ClientGenerators.CreateBasicClient(null); var query = new StatsInfoRequest { ClientId = client.ClientId }; var queryResult = new ResourceQueryHelperResult() { Results = new[] { new StatsInfoResult { Deaths = 1, Kills = 1, LastPlayed = DateTime.Now, Performance = 100, Ranking = 10, ScorePerMinute = 500, ServerGame = "IW4", ServerId = 123, ServerName = "IW4Host", TotalSecondsPlayed = 100 } }, TotalResultCount = 1, RetrievedResultCount = 1 }; A.CallTo(() => fakeStatsQueryHelper.QueryResource(A.Ignored)) .Returns(Task.FromResult(queryResult)); var result = await statsController.ClientStats(query.ClientId.Value); Assert.IsInstanceOf(result); var viewResult = (result as OkObjectResult).Value as IEnumerable; Assert.NotNull(viewResult); Assert.AreEqual(queryResult.Results, viewResult); } [Test] public async Task Test_StatsController_ClientStats_InvalidModelState() { statsController.ModelState.AddModelError("test", "test"); var result = await statsController.ClientStats(1); Assert.IsInstanceOf(result); statsController.ModelState.Clear(); } [Test] public async Task Test_StatsController_ClientStats_Exception() { string expectedExceptionMessage = "failure"; int expectedStatusCode = 500; A.CallTo(() => fakeStatsQueryHelper.QueryResource(A.Ignored)) .Throws(new Exception(expectedExceptionMessage)); var result = await statsController.ClientStats(1); Assert.IsInstanceOf(result); var statusResult = (result as ObjectResult); Assert.AreEqual(expectedStatusCode, statusResult.StatusCode); Assert.IsTrue((statusResult.Value as ErrorResponse).Messages.Contains(expectedExceptionMessage)); } [Test] public async Task Test_StatsController_ClientStats_NotFound() { var queryResult = new ResourceQueryHelperResult() { Results = new List() }; A.CallTo(() => fakeStatsQueryHelper.QueryResource(A.Ignored)) .Returns(Task.FromResult(queryResult)); var result = await statsController.ClientStats(1); Assert.IsInstanceOf(result); } #endregion } }