From e997b94b3b7434b9110322069ef5b89817ef5665 Mon Sep 17 00:00:00 2001 From: RaidMax Date: Thu, 12 Nov 2020 19:46:17 -0600 Subject: [PATCH] update unit tests --- Application/IO/GameLogEventDetection.cs | 2 +- .../ApplicationTests/BaseEventParserTests.cs | 2 +- Tests/ApplicationTests/ClientTests.cs | 1 + Tests/ApplicationTests/CommandTests.cs | 5 ++++ .../DependencyInjectionExtensions.cs | 18 +++--------- Tests/ApplicationTests/IOTests.cs | 14 ++++++---- Tests/ApplicationTests/IW4MServerTests.cs | 10 ++++--- Tests/ApplicationTests/PluginTests.cs | 2 +- Tests/ApplicationTests/ServerTests.cs | 28 ++++++------------- Tests/ApplicationTests/StatsTests.cs | 28 ++++++++++--------- 10 files changed, 52 insertions(+), 58 deletions(-) diff --git a/Application/IO/GameLogEventDetection.cs b/Application/IO/GameLogEventDetection.cs index 9a9a1f0b2..955a95c57 100644 --- a/Application/IO/GameLogEventDetection.cs +++ b/Application/IO/GameLogEventDetection.cs @@ -17,7 +17,7 @@ namespace IW4MAdmin.Application.IO private readonly bool _ignoreBots; private readonly ILogger _logger; - public GameLogEventDetection(ILogger logger, Server server, Uri[] gameLogUris, IGameLogReaderFactory gameLogReaderFactory) + public GameLogEventDetection(ILogger logger, IW4MServer server, Uri[] gameLogUris, IGameLogReaderFactory gameLogReaderFactory) { _reader = gameLogReaderFactory.CreateGameLogReader(gameLogUris, server.EventParser); _server = server; diff --git a/Tests/ApplicationTests/BaseEventParserTests.cs b/Tests/ApplicationTests/BaseEventParserTests.cs index 242878f95..40cd43745 100644 --- a/Tests/ApplicationTests/BaseEventParserTests.cs +++ b/Tests/ApplicationTests/BaseEventParserTests.cs @@ -9,8 +9,8 @@ using SharedLibraryCore; using SharedLibraryCore.Configuration; using SharedLibraryCore.Interfaces; using System; -using Microsoft.Extensions.Logging; using static SharedLibraryCore.GameEvent; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace ApplicationTests { diff --git a/Tests/ApplicationTests/ClientTests.cs b/Tests/ApplicationTests/ClientTests.cs index 4a2949680..788972cba 100644 --- a/Tests/ApplicationTests/ClientTests.cs +++ b/Tests/ApplicationTests/ClientTests.cs @@ -8,6 +8,7 @@ using SharedLibraryCore.Database.Models; using System; using System.Collections.Generic; using System.Text; +using SharedLibraryCore.Configuration; namespace ApplicationTests { diff --git a/Tests/ApplicationTests/CommandTests.cs b/Tests/ApplicationTests/CommandTests.cs index aa6ff8c21..8de324bec 100644 --- a/Tests/ApplicationTests/CommandTests.cs +++ b/Tests/ApplicationTests/CommandTests.cs @@ -16,6 +16,7 @@ using SharedLibraryCore.Services; using static SharedLibraryCore.Database.Models.EFClient; using FluentAssertions; using FluentAssertions.Extensions; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace ApplicationTests { @@ -50,6 +51,10 @@ namespace ApplicationTests appConfig = serviceProvider.GetRequiredService(); appConfig.MapChangeDelaySeconds = 1; cmdConfig = serviceProvider.GetRequiredService(); + serviceProvider.GetService().RconParser = + serviceProvider.GetService(); + + Utilities.DefaultLogger = serviceProvider.GetRequiredService(); A.CallTo(() => manager.GetClientService()) .Returns(clientService); diff --git a/Tests/ApplicationTests/DependencyInjectionExtensions.cs b/Tests/ApplicationTests/DependencyInjectionExtensions.cs index 61352967c..f24962601 100644 --- a/Tests/ApplicationTests/DependencyInjectionExtensions.cs +++ b/Tests/ApplicationTests/DependencyInjectionExtensions.cs @@ -6,8 +6,7 @@ using Microsoft.Extensions.DependencyInjection; using SharedLibraryCore.Configuration; using SharedLibraryCore.Interfaces; using System; -using Microsoft.Extensions.Logging; -using SharedLibraryCore; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace ApplicationTests { @@ -28,7 +27,6 @@ namespace ApplicationTests } var manager = A.Fake(); - var logger = A.Fake(); var transLookup = A.Fake(); A.CallTo(() => transLookup[A.Ignored]) @@ -37,8 +35,11 @@ namespace ApplicationTests serviceCollection .AddLogging() .AddSingleton(A.Fake()) + .AddSingleton(A.Fake()) + .AddSingleton(new ServerConfiguration { IPAddress = "127.0.0.1", Port = 28960 }) .AddSingleton(manager) .AddSingleton() + .AddSingleton() .AddSingleton(A.Fake()) .AddSingleton(A.Fake()) .AddSingleton(transLookup) @@ -52,17 +53,6 @@ namespace ApplicationTests .AddSingleton(ConfigurationGenerators.CreateCommandConfiguration()) .AddSingleton, ApplicationConfigurationHandlerMock>(); - serviceCollection.AddSingleton(_sp => new IW4MServer(_sp.GetRequiredService(), - ConfigurationGenerators.CreateServerConfiguration(), - _sp.GetRequiredService(), - _sp.GetRequiredService(), - _sp.GetRequiredService(), - _sp.GetRequiredService(), - _sp.GetRequiredService>()) - { - RconParser = _sp.GetRequiredService() - }); - return serviceCollection; } diff --git a/Tests/ApplicationTests/IOTests.cs b/Tests/ApplicationTests/IOTests.cs index 9c09767a2..d137e986b 100644 --- a/Tests/ApplicationTests/IOTests.cs +++ b/Tests/ApplicationTests/IOTests.cs @@ -1,12 +1,11 @@ using FakeItEasy; -using IW4MAdmin; using IW4MAdmin.Application.IO; using Microsoft.Extensions.DependencyInjection; using NUnit.Framework; -using SharedLibraryCore; using SharedLibraryCore.Interfaces; using System; using System.Threading.Tasks; +using SharedLibraryCore; namespace ApplicationTests { @@ -19,19 +18,24 @@ namespace ApplicationTests [SetUp] public void Setup() { - serviceProvider = new ServiceCollection().BuildBase().BuildServiceProvider(); + serviceProvider = new ServiceCollection() + .BuildBase() + .AddSingleton(new Uri[] { new Uri("C:\\test.log")}) + .AddSingleton(A.Fake()) + .AddSingleton() + .BuildServiceProvider(); } [Test] public async Task GameLogEventDetection_WorksAfterFileSizeReset() { var reader = A.Fake(); - var factory = A.Fake(); + var factory = serviceProvider.GetRequiredService(); A.CallTo(() => factory.CreateGameLogReader(A.Ignored, A.Ignored)) .Returns(reader); - var detect = new GameLogEventDetection(serviceProvider.GetService(), new Uri[] { new Uri("C:\\test.log") }, factory); + var detect = serviceProvider.GetRequiredService(); A.CallTo(() => reader.Length) .Returns(100) diff --git a/Tests/ApplicationTests/IW4MServerTests.cs b/Tests/ApplicationTests/IW4MServerTests.cs index 8cdda09a9..e1a1b1ddd 100644 --- a/Tests/ApplicationTests/IW4MServerTests.cs +++ b/Tests/ApplicationTests/IW4MServerTests.cs @@ -41,6 +41,8 @@ namespace ApplicationTests fakeRConParser = serviceProvider.GetRequiredService(); mockEventHandler = serviceProvider.GetRequiredService(); appConfig = serviceProvider.GetRequiredService(); + serviceProvider.GetService().RconParser = + serviceProvider.GetService(); var rconConnectionFactory = serviceProvider.GetRequiredService(); @@ -215,7 +217,7 @@ namespace ApplicationTests await server.Ban("test reason", target, origin); - A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) + A.CallTo(() => server.RconParser.ExecuteCommandAsync(A.Ignored, "kick")) .MustHaveHappenedOnceExactly(); } @@ -290,7 +292,7 @@ namespace ApplicationTests await server.TempBan("test reason", TimeSpan.Zero, target, origin); - A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) + A.CallTo(() => server.RconParser.ExecuteCommandAsync(A.Ignored, "kick")) .MustHaveHappenedOnceExactly(); } @@ -312,7 +314,7 @@ namespace ApplicationTests await server.TempBan("test reason", TimeSpan.Zero, target, origin); - A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) + A.CallTo(() => server.RconParser.ExecuteCommandAsync(A.Ignored, "kick")) .MustHaveHappenedOnceExactly(); } #endregion @@ -348,7 +350,7 @@ namespace ApplicationTests await server.Kick("test reason", target, origin); - A.CallTo(() => fakeRConParser.ExecuteCommandAsync(fakeRConConnection, "kick")) + A.CallTo(() => server.RconParser.ExecuteCommandAsync(A.Ignored, "kick")) .MustHaveHappenedOnceExactly(); } diff --git a/Tests/ApplicationTests/PluginTests.cs b/Tests/ApplicationTests/PluginTests.cs index 475501bc4..a05bd7504 100644 --- a/Tests/ApplicationTests/PluginTests.cs +++ b/Tests/ApplicationTests/PluginTests.cs @@ -13,7 +13,7 @@ using SharedLibraryCore.Services; using System; using System.IO; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace ApplicationTests { diff --git a/Tests/ApplicationTests/ServerTests.cs b/Tests/ApplicationTests/ServerTests.cs index 1cffc1f09..ea3833fb0 100644 --- a/Tests/ApplicationTests/ServerTests.cs +++ b/Tests/ApplicationTests/ServerTests.cs @@ -1,36 +1,32 @@ using FakeItEasy; using IW4MAdmin; -using IW4MAdmin.Application; using IW4MAdmin.Application.EventParsers; using NUnit.Framework; using SharedLibraryCore.Configuration; using SharedLibraryCore.Interfaces; using System; -using System.Diagnostics; -using Microsoft.Extensions.Logging; -using SharedLibraryCore; +using Microsoft.Extensions.DependencyInjection; +using ILogger = Microsoft.Extensions.Logging.ILogger; namespace ApplicationTests { [TestFixture] public class ServerTests { - + private IServiceProvider _serviceProvider; + [SetUp] public void Setup() { - + _serviceProvider = new ServiceCollection() + .BuildBase() + .BuildServiceProvider(); } [Test] public void GameTimeFalseQuitTest() { - var mgr = A.Fake(); - var server = new IW4MServer(mgr, - new SharedLibraryCore.Configuration.ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 }, - A.Fake(), A.Fake(), - A.Fake(), A.Fake(), A.Fake>()); - + var server = _serviceProvider.GetRequiredService(); var parser = new BaseEventParser(A.Fake(), A.Fake(), A.Fake()); parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer; @@ -50,13 +46,7 @@ namespace ApplicationTests [Test] public void LogFileReplay() { - var mgr = A.Fake(); - - var server = new IW4MServer(mgr, - new SharedLibraryCore.Configuration.ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 }, - A.Fake(), A.Fake(), A.Fake(), A.Fake(), - A.Fake>()); - + var server = _serviceProvider.GetRequiredService(); var parser = new BaseEventParser(A.Fake(), A.Fake(), A.Fake()); parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer; diff --git a/Tests/ApplicationTests/StatsTests.cs b/Tests/ApplicationTests/StatsTests.cs index 9f647dc86..7ac2b1dd4 100644 --- a/Tests/ApplicationTests/StatsTests.cs +++ b/Tests/ApplicationTests/StatsTests.cs @@ -13,8 +13,6 @@ using Microsoft.Extensions.DependencyInjection; using IW4MAdmin.Plugins.Stats.Helpers; using ApplicationTests.Fixtures; using System.Threading.Tasks; -using Microsoft.Extensions.Logging; -using SharedLibraryCore; using Stats.Helpers; using Stats.Dtos; using SharedLibraryCore.Configuration; @@ -37,8 +35,11 @@ namespace ApplicationTests handlerFactory = A.Fake(); serviceProvider = new ServiceCollection() - .AddSingleton() .BuildBase() + .AddSingleton(A.Fake>()) + .AddSingleton() + .AddSingleton(new ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 }) + .AddSingleton() .AddSingleton() .BuildServiceProvider(); @@ -61,11 +62,7 @@ namespace ApplicationTests A.CallTo(() => handlerFactory.GetConfigurationHandler(A.Ignored)) .Returns(config); - var server = new IW4MServer(mgr, - new SharedLibraryCore.Configuration.ServerConfiguration() { IPAddress = "127.0.0.1", Port = 28960 }, - A.Fake(), - A.Fake(), - A.Fake(), A.Fake(), A.Fake>()); + var server = serviceProvider.GetRequiredService(); var parser = new BaseEventParser(A.Fake(), A.Fake(), A.Fake()); parser.Configuration.GuidNumberStyle = System.Globalization.NumberStyles.Integer; @@ -128,8 +125,8 @@ namespace ApplicationTests public async Task Test_ConcurrentCallsToUpdateStatHistoryDoesNotCauseException() { var server = serviceProvider.GetRequiredService(); - var configHandler = A.Fake>(); - var mgr = new StatManager(serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService(), configHandler); + var configHandler = serviceProvider.GetRequiredService>(); + var mgr = serviceProvider.GetRequiredService(); var target = ClientGenerators.CreateDatabaseClient(); target.CurrentServer = server; @@ -141,10 +138,11 @@ namespace ApplicationTests var dbFactory = serviceProvider.GetRequiredService(); var db = dbFactory.CreateContext(true); - db.Set().Add(new EFServer() + var efServer = new EFServer() { EndPoint = server.EndPoint.ToString() - }); + }; + db.Set().Add(efServer); db.Clients.Add(target); db.SaveChanges(); @@ -154,6 +152,10 @@ namespace ApplicationTests var stats = target.GetAdditionalProperty("ClientStats"); await mgr.UpdateStatHistory(target, stats); + + db.Clients.Remove(target); + db.Set().Remove(efServer); + await db.SaveChangesAsync(); } #region QUERY_HELPER @@ -161,7 +163,7 @@ namespace ApplicationTests public async Task Test_StatsQueryHelper_Get() { var queryHelper = serviceProvider.GetRequiredService(); - using var context = contextFactory.CreateContext(); + await using var context = contextFactory.CreateContext(); var server = new EFServer() { ServerId = 1 }; var stats = new EFClientStatistics()