| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="MockMailService.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Threading; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | using MyNet.Mail.Models; |
| | | 12 | | using MyNet.Primitives; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Mail.Mock; |
| | | 15 | | |
| | | 16 | | public partial class MockMailService(ILogger? logger = null) : IMailService |
| | | 17 | | { |
| | | 18 | | [LoggerMessage(LogLevel.Information, "Simulate Sending Mail : {email}")] |
| | | 19 | | public static partial void LogSendingMail(ILogger logger, string email); |
| | | 20 | | |
| | 3 | 21 | | public bool CanConnect() => true; |
| | | 22 | | |
| | 3 | 23 | | public Task<bool> CanConnectAsync() => Task.FromResult(true); |
| | | 24 | | |
| | | 25 | | public SendResponse Send(IEmail email, CancellationToken? token = null) |
| | | 26 | | { |
| | 3 | 27 | | var response = new SendResponse(); |
| | | 28 | | |
| | | 29 | | try |
| | | 30 | | { |
| | 3 | 31 | | Task.Delay(1000, token.GetValueOrDefault()).GetAwaiter().GetResult(); |
| | 0 | 32 | | } |
| | 3 | 33 | | catch (OperationCanceledException) |
| | | 34 | | { |
| | 3 | 35 | | response.ErrorMessages.Add("Send canceled."); |
| | 3 | 36 | | return response; |
| | | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | if (logger is not null) |
| | 0 | 40 | | LogSendingMail(logger, email.ToString().OrEmpty()); |
| | | 41 | | |
| | 0 | 42 | | return response; |
| | 3 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<SendResponse> SendAsync(IEmail email, CancellationToken? token = null) |
| | | 46 | | { |
| | 3 | 47 | | var response = new SendResponse(); |
| | | 48 | | |
| | | 49 | | try |
| | | 50 | | { |
| | 3 | 51 | | await Task.Delay(1000, token.GetValueOrDefault()).ConfigureAwait(false); |
| | 0 | 52 | | } |
| | 3 | 53 | | catch (OperationCanceledException) |
| | | 54 | | { |
| | 3 | 55 | | response.ErrorMessages.Add("Send canceled."); |
| | 3 | 56 | | return response; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | if (logger is not null) |
| | 0 | 60 | | LogSendingMail(logger, email.ToString().OrEmpty()); |
| | | 61 | | |
| | 0 | 62 | | return response; |
| | 3 | 63 | | } |
| | | 64 | | } |
| | | 65 | | |