diff --git a/src/Whipstaff.Core/Logging/EventIdFactory.cs b/src/Whipstaff.Core/Logging/EventIdFactory.cs index 7365c1af9..4b5243d1b 100644 --- a/src/Whipstaff.Core/Logging/EventIdFactory.cs +++ b/src/Whipstaff.Core/Logging/EventIdFactory.cs @@ -57,12 +57,30 @@ public static class EventIdFactory /// Gets the Event Id for the Middleware Exception event. /// /// Event Id. - public static EventId MiddlewareException() => new(7, "Middleware Exception"); + public static EventId MiddlewareException() => new(8, "Middleware Exception"); /// /// Gets the Event Id for the Middleware Finished event. /// /// Event Id. - public static EventId MiddlewareFinished() => new(7, "Middleware Finished"); + public static EventId MiddlewareFinished() => new(9, "Middleware Finished"); + + /// + /// Gets the Event Id for the "Starting Test Of DbSet" event which is used in the EF Core smoke test process. + /// + /// Event Id. + public static EventId TestOfDbSetStarting() => new(10, "Starting Test Of DbSet"); + + /// + /// Gets the Event Id for the "Test Of DbSet Failed" event which is used in the EF Core smoke test process. + /// + /// Event Id. + public static EventId TestOfDbSetFailed() => new(11, "Test Of DbSet Failed"); + + /// + /// Gets the Event Id for the "Completed Test Of DbSet" event which is used in the EF Core smoke test process. + /// + /// Event Id. + public static EventId TestOfDbSetCompleted() => new(11, "Test Of DbSet Completed"); } } diff --git a/src/Whipstaff.EntityFramework/SmokeTest/AbstractDbSetChecker.cs b/src/Whipstaff.EntityFramework/SmokeTest/AbstractDbSetChecker.cs new file mode 100644 index 000000000..1e195afe9 --- /dev/null +++ b/src/Whipstaff.EntityFramework/SmokeTest/AbstractDbSetChecker.cs @@ -0,0 +1,59 @@ +// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. +// This file is licensed to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace Whipstaff.EntityFramework.SmokeTest +{ + /// + /// Abstract class for checking DBSets. + /// + /// The type of the DbContext being tested. + public abstract class AbstractDbSetChecker + where TDbContext : DbContext + { + private readonly DbSetCheckerLogMessageActionsWrapper _logMessageActionsWrapper; + + /// + /// Initializes a new instance of the class. + /// + /// Log Message Wrapper instance. + protected AbstractDbSetChecker(DbSetCheckerLogMessageActionsWrapper logMessageActionsWrapper) + { + ArgumentNullException.ThrowIfNull(logMessageActionsWrapper); + _logMessageActionsWrapper = logMessageActionsWrapper; + } + + /// + /// Carries out the testing of DB Sets. + /// + /// Instance of the DbContext to test. + /// A representing the asynchronous operation. + public abstract Task CheckDbSets(TDbContext dbContext); + + /// + /// Carries out the test of an individual Db Set. + /// + /// The type of the entity being tested. + /// The Db Set to test. + /// A representing the asynchronous operation. + protected async Task CheckDbSet(DbSet dbSet) + where TEntity : class + { + try + { + _logMessageActionsWrapper.StartingTestOfDbSet(typeof(TEntity)); + var result = await dbSet.FirstOrDefaultAsync().ConfigureAwait(false); + } +#pragma warning disable CA1031 + catch (Exception ex) +#pragma warning restore CA1031 + { + _logMessageActionsWrapper.FailedToTestDbSet(ex, typeof(TEntity)); + } + } + } +} diff --git a/src/Whipstaff.EntityFramework/SmokeTest/DbSetCheckerLogMessageActions.cs b/src/Whipstaff.EntityFramework/SmokeTest/DbSetCheckerLogMessageActions.cs new file mode 100644 index 000000000..2eb2a2895 --- /dev/null +++ b/src/Whipstaff.EntityFramework/SmokeTest/DbSetCheckerLogMessageActions.cs @@ -0,0 +1,59 @@ +// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. +// This file is licensed to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Whipstaff.Core.Logging; + +namespace Whipstaff.EntityFramework.SmokeTest +{ + /// + /// Log Message Actions for DBSet checks. + /// + /// The type for the DBContext being tested. + public sealed class DbSetCheckerLogMessageActions : ILogMessageActions> + where TDbContext : DbContext + { + private readonly Action _startingTestOfDbSet; + private readonly Action _testOfDbSetFailed; + + /// + /// Initializes a new instance of the class. + /// + public DbSetCheckerLogMessageActions() + { + _startingTestOfDbSet = LoggerMessage.Define( + Microsoft.Extensions.Logging.LogLevel.Information, + EventIdFactory.TestOfDbSetStarting(), + "Starting Test of DBSet for {EntityType}"); + + _testOfDbSetFailed = LoggerMessage.Define( + Microsoft.Extensions.Logging.LogLevel.Error, + EventIdFactory.TestOfDbSetFailed(), + "Failed during test of DBSet for {EntityType}"); + } + + /// + /// Logs the start of a test of a DBSet. + /// + /// Logging framework instance. + /// The type of the db set being tested. + public void StartingTestOfDbSet(ILogger logger, Type type) + { + _startingTestOfDbSet(logger, type, null); + } + + /// + /// Logs a failure during a test of a DBSet. + /// + /// Logging framework instance. + /// The type of the db set being tested. + /// The exception that occurred. + public void TestOfDbSetFailed(ILogger logger, Type type, Exception exception) + { + _testOfDbSetFailed(logger, type, exception); + } + } +} diff --git a/src/Whipstaff.EntityFramework/SmokeTest/DbSetCheckerLogMessageActionsWrapper.cs b/src/Whipstaff.EntityFramework/SmokeTest/DbSetCheckerLogMessageActionsWrapper.cs new file mode 100644 index 000000000..933b0d976 --- /dev/null +++ b/src/Whipstaff.EntityFramework/SmokeTest/DbSetCheckerLogMessageActionsWrapper.cs @@ -0,0 +1,50 @@ +// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. +// This file is licensed to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Whipstaff.Core.Logging; + +namespace Whipstaff.EntityFramework.SmokeTest +{ + /// + /// Log Message Actions for DBSet checks. + /// + /// The type for the DbContext. + public sealed class DbSetCheckerLogMessageActionsWrapper : AbstractLogMessageActionsWrapper, DbSetCheckerLogMessageActions> + where TDbContext : DbContext + { + /// + /// Initializes a new instance of the class. + /// + /// Log Message Actions instance. + /// Logging framework instance. + public DbSetCheckerLogMessageActionsWrapper( + DbSetCheckerLogMessageActions logMessageActions, + ILogger> logger) + : base(logMessageActions, logger) + { + } + + /// + /// Logs the start of a test of a DBSet. + /// + /// The type of the DbSet being tested. + public void StartingTestOfDbSet(Type type) + { + LogMessageActions.StartingTestOfDbSet(Logger, type); + } + + /// + /// Logs a failure during a test of a DBSet. + /// + /// The exception that occurred. + /// The type of the DbSet being tested. + public void FailedToTestDbSet(Exception exception, Type type) + { + LogMessageActions.TestOfDbSetFailed(Logger, type, exception); + } + } +} diff --git a/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorker.cs b/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorker.cs new file mode 100644 index 000000000..e6ae70c43 --- /dev/null +++ b/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorker.cs @@ -0,0 +1,66 @@ +// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. +// This file is licensed to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; + +namespace Whipstaff.EntityFramework.SmokeTest +{ + /// + /// Background Worker for running smoke tests on a DBContext. + /// + /// The type for the DbContext. + /// The type for the DbSet checker. + public sealed class SmokeTestBackgroundWorker : BackgroundService + where TDbContext : DbContext + where TDbSetChecker : AbstractDbSetChecker + { + private readonly IDbContextFactory _dbContextFactory; + private readonly TDbSetChecker _dbSetChecker; + private readonly SmokeTestBackgroundWorkerLogMessageActionsWrapper _logMessageActionsWrappper; + + /// + /// Initializes a new instance of the class. + /// + /// DBContext factory for connecting to the database to test. + /// The logic for running the test of Db Sets. + /// Log Message Actions Wrapper instance. + public SmokeTestBackgroundWorker( + IDbContextFactory dbContextFactory, + TDbSetChecker dbSetChecker, + SmokeTestBackgroundWorkerLogMessageActionsWrapper logMessageActionsWrappper) + { + ArgumentNullException.ThrowIfNull(dbContextFactory); + ArgumentNullException.ThrowIfNull(dbSetChecker); + ArgumentNullException.ThrowIfNull(logMessageActionsWrappper); + + _dbContextFactory = dbContextFactory; + _dbSetChecker = dbSetChecker; + _logMessageActionsWrappper = logMessageActionsWrappper; + } + + /// + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + using (var dbContext = await _dbContextFactory.CreateDbContextAsync(stoppingToken).ConfigureAwait(false)) + { + try + { + _logMessageActionsWrappper.StartingDbSetChecker(); + var dbSets = _dbSetChecker.CheckDbSets(dbContext); + _logMessageActionsWrappper.CompletedDbSetChecker(); + } +#pragma warning disable CA1031 + catch (Exception ex) +#pragma warning restore CA1031 + { + _logMessageActionsWrappper.FailureOfDbSetChecker(ex); + } + } + } + } +} diff --git a/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorkerLogMessageActions.cs b/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorkerLogMessageActions.cs new file mode 100644 index 000000000..eb1918320 --- /dev/null +++ b/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorkerLogMessageActions.cs @@ -0,0 +1,74 @@ +// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. +// This file is licensed to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Whipstaff.Core.Logging; + +namespace Whipstaff.EntityFramework.SmokeTest +{ + /// + /// Log Message Actions for . + /// + /// The type for the DbContext. + /// The type for the DbSet checker. + public sealed class SmokeTestBackgroundWorkerLogMessageActions : ILogMessageActions> + where TDbContext : DbContext + where TDbSetChecker : AbstractDbSetChecker + { + private readonly Action _startingTestOfDbSet; + private readonly Action _completedTestOfDbSet; + private readonly Action _testOfDbSetFailed; + + /// + /// Initializes a new instance of the class. + /// + public SmokeTestBackgroundWorkerLogMessageActions() + { + _startingTestOfDbSet = LoggerMessage.Define( + LogLevel.Information, + EventIdFactory.TestOfDbSetStarting(), + "Starting test of DBSet"); + + _completedTestOfDbSet = LoggerMessage.Define( + LogLevel.Information, + EventIdFactory.TestOfDbSetCompleted(), + "Completed test of DBSet"); + + _testOfDbSetFailed = LoggerMessage.Define( + LogLevel.Information, + EventIdFactory.TestOfDbSetFailed(), + "Starting test of DBSet"); + } + + /// + /// Logging event for when the test of DB Sets is starting. + /// + /// Logging framework instance. + public void StartingTestOfDbSet(ILogger> logger) + { + _startingTestOfDbSet(logger, null); + } + + /// + /// Logging event for when the test of DB Sets has completed. + /// + /// Logging framework instance. + public void CompletedTestOfDbSet(ILogger> logger) + { + _completedTestOfDbSet(logger, null); + } + + /// + /// Logging event for when the test of DB Sets has failed. + /// + /// Logging framework instance. + /// The exception that occurred. + public void TestOfDbSetFailed(ILogger> logger, Exception exception) + { + _testOfDbSetFailed(logger, exception); + } + } +} diff --git a/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorkerLogMessageActionsWrapper.cs b/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorkerLogMessageActionsWrapper.cs new file mode 100644 index 000000000..39048633c --- /dev/null +++ b/src/Whipstaff.EntityFramework/SmokeTest/SmokeTestBackgroundWorkerLogMessageActionsWrapper.cs @@ -0,0 +1,58 @@ +// Copyright (c) 2022 DHGMS Solutions and Contributors. All rights reserved. +// This file is licensed to you under the MIT license. +// See the LICENSE file in the project root for full license information. + +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Whipstaff.Core.Logging; + +namespace Whipstaff.EntityFramework.SmokeTest +{ + /// + /// Log Message Actions Wrapper for DBSet checks. + /// + /// The type for the DbContext. + /// The type for the DbSet checker. + public sealed class SmokeTestBackgroundWorkerLogMessageActionsWrapper : AbstractLogMessageActionsWrapper, SmokeTestBackgroundWorkerLogMessageActions> + where TDbContext : DbContext + where TDbSetChecker : AbstractDbSetChecker + { + /// + /// Initializes a new instance of the class. + /// + /// Log Message Actions instance. + /// Logging framework instance. + public SmokeTestBackgroundWorkerLogMessageActionsWrapper( + SmokeTestBackgroundWorkerLogMessageActions logMessageActions, + ILogger> logger) + : base(logMessageActions, logger) + { + } + + /// + /// Logs the start of a test of a DBSet. + /// + public void StartingDbSetChecker() + { + LogMessageActions.StartingTestOfDbSet(Logger); + } + + /// + /// Logs the completion of a test of a DBSet. + /// + public void CompletedDbSetChecker() + { + LogMessageActions.CompletedTestOfDbSet(Logger); + } + + /// + /// Logs a failure during a test of a DBSet. + /// + /// The exception that occurred. + public void FailureOfDbSetChecker(Exception exception) + { + LogMessageActions.TestOfDbSetFailed(Logger, exception); + } + } +} diff --git a/src/Whipstaff.EntityFramework/Whipstaff.EntityFramework.csproj b/src/Whipstaff.EntityFramework/Whipstaff.EntityFramework.csproj index ee8982673..63ab9e7ea 100644 --- a/src/Whipstaff.EntityFramework/Whipstaff.EntityFramework.csproj +++ b/src/Whipstaff.EntityFramework/Whipstaff.EntityFramework.csproj @@ -6,6 +6,7 @@ +