Skip to content

Commit

Permalink
ShouldThrowValidationExceptionIfFlexiMessageIsNull -> PASS
Browse files Browse the repository at this point in the history
  • Loading branch information
mabroukmahdhi committed Aug 11, 2024
1 parent e1a5ad0 commit 59218cd
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace FlexiMail.Tests.Unit.Services
public partial class FlexiExchangeServiceTests
{
[Fact]
public void ShouldSendAndSaveCopyAsync()
public async void ShouldSendAndSaveCopyAsync()
{
// given
var randomAccessToken = GetRandomString();
var randomMessage = CreateRandomFlexiMessage();
var randomExchangeService = CreateExchangeService();
var randomExchangeService = CreateExchangeService();

this.exchangeBrokerMock.Setup(broker =>
broker.GetAccessTokenAsync())
Expand All @@ -27,10 +27,10 @@ public void ShouldSendAndSaveCopyAsync()
ExchangeVersion.Exchange2013,
randomAccessToken,
It.IsAny<ImpersonatedUserId>()))
.Returns(randomExchangeService);
.Returns(randomExchangeService);

// when
this.flexiExchangeService.SendAndSaveCopyAsync(randomMessage);
await this.flexiExchangeService.SendAndSaveCopyAsync(randomMessage);

// then
this.exchangeBrokerMock.Verify(broker =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Made with love for the .NET Community
// ---------------------------------------

using System;
using FlexiMail.Models.Foundations.Messages;
using FlexiMail.Models.Foundations.Messages.Exceptions;
using FluentAssertions;
Expand All @@ -15,7 +14,7 @@ namespace FlexiMail.Tests.Unit.Services
public partial class FlexiExchangeServiceTests
{
[Fact]
public void ShouldThrowValidationExceptionIfFlexiMessageIsNull()
public async void ShouldThrowValidationExceptionIfFlexiMessageIsNull()
{
// given
FlexiMessage nullMessage = null;
Expand All @@ -30,12 +29,16 @@ public void ShouldThrowValidationExceptionIfFlexiMessageIsNull()
innerException: nullFlexiMessageException);

// when
void SendMessageAction() => this.flexiExchangeService.SendAndSaveCopyAsync(nullMessage);
var sendMessageTask =
this.flexiExchangeService.SendAndSaveCopyAsync(nullMessage);

var actualException = Assert.Throws<FlexiMessageValidationException>((Action)SendMessageAction);
var actualFlexiMessageValidationException =
await Assert.ThrowsAsync<FlexiMessageValidationException>(
sendMessageTask.AsTask);
// then

actualException.Should().BeEquivalentTo(expectedFlexiMessageValidationException);
actualFlexiMessageValidationException.Should()
.BeEquivalentTo(expectedFlexiMessageValidationException);

this.exchangeBrokerMock.Verify(broker =>
broker.GetAccessTokenAsync(),
Expand Down
1 change: 0 additions & 1 deletion FlexiMail.Tests.Unit/Services/FlexiExchangeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using FlexiMail.Brokers.Exchanges;
using FlexiMail.Models.Configurations;
using FlexiMail.Models.Foundations.Attachments;
using FlexiMail.Models.Foundations.Messages;
using FlexiMail.Services;
using Microsoft.Exchange.WebServices.Data;
Expand Down
1 change: 1 addition & 0 deletions FlexiMail/FlexiMail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.Exchange.WebServices" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.63.0" />
<PackageReference Include="Xeption" Version="2.8.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
// Made with love for the .NET Community
// ---------------------------------------

using System;
using Xeptions;

namespace FlexiMail.Models.Foundations.Messages.Exceptions
{
public class FlexiMessageValidationException(string message, Exception innerException)
: Exception(message, innerException)
public class FlexiMessageValidationException(string message, Xeption innerException)
: Xeption(message, innerException)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
// Made with love for the .NET Community
// ---------------------------------------

using System;
using Xeptions;

namespace FlexiMail.Models.Foundations.Messages.Exceptions
{
public class NullFlexiMessageException(string message) : Exception(message)
public class NullFlexiMessageException(string message) : Xeption(message)
{
}
}
35 changes: 35 additions & 0 deletions FlexiMail/Services/FlexiExchangeService.Exceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// ---------------------------------------
// Copyright (c) 2024 Mabrouk Mahdhi.
// Made with love for the .NET Community
// ---------------------------------------

using System.Threading.Tasks;
using FlexiMail.Models.Foundations.Messages.Exceptions;
using Xeptions;

namespace FlexiMail.Services
{
internal partial class FlexiExchangeService
{
private delegate ValueTask SendFunction();

private static async ValueTask TryCatch(SendFunction sendFunction)
{
try
{
await sendFunction();
}
catch (NullFlexiMessageException nullFlexiMessageException)
{
throw CreateValidationException(nullFlexiMessageException);
}
}

private static FlexiMessageValidationException CreateValidationException(Xeption exception)
{
return new FlexiMessageValidationException(
message: "Flexi Message validation error occurred, fix errors and try again.",
innerException: exception);
}
}
}
24 changes: 24 additions & 0 deletions FlexiMail/Services/FlexiExchangeService.Validations.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// ---------------------------------------
// Copyright (c) 2024 Mabrouk Mahdhi.
// Made with love for the .NET Community
// ---------------------------------------

using FlexiMail.Models.Foundations.Messages;
using FlexiMail.Models.Foundations.Messages.Exceptions;

namespace FlexiMail.Services
{
internal partial class FlexiExchangeService
{
private static void ValidFlexiMessage(FlexiMessage flexiMessage)
{
ValidFlexiMessageIsNotNull(flexiMessage);
}

private static void ValidFlexiMessageIsNotNull(FlexiMessage flexiMessage)
{
if (flexiMessage == null)
throw new NullFlexiMessageException(message: "FlexiMessage is null.");
}
}
}
66 changes: 36 additions & 30 deletions FlexiMail/Services/FlexiExchangeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,53 @@

namespace FlexiMail.Services
{
internal class FlexiExchangeService(
ExchangeConfigurations configurations,
internal partial class FlexiExchangeService(
ExchangeConfigurations configurations,
IExchangeBroker exchangeBroker)
: IFlexiExchangeService
{
private readonly IExchangeBroker exchangeBroker = exchangeBroker;

public async void SendAndSaveCopyAsync(FlexiMessage flexiMessage)
{
var exchangeService = await CreateExchangeServiceAsync();
var emailMessage = new EmailMessage(service: exchangeService)

public ValueTask SendAndSaveCopyAsync(FlexiMessage flexiMessage) =>
TryCatch(async () =>
{
Subject = flexiMessage.Subject,
Body = new MessageBody(
bodyType: MapBodyType(bodyContentType: flexiMessage.Body.ContentType),
text: flexiMessage.Body.Content)
};
ValidFlexiMessage(flexiMessage);
emailMessage.ToRecipients.AddAddresses(flexiMessage.To);
emailMessage.CcRecipients.AddAddresses(flexiMessage.Cc);
emailMessage.BccRecipients.AddAddresses(flexiMessage.Bcc);
var exchangeService = await CreateExchangeServiceAsync();
var emailMessage = new EmailMessage(service: exchangeService)
{
Subject = flexiMessage.Subject,
Body = new MessageBody(bodyType: MapBodyType(bodyContentType: flexiMessage.Body.ContentType),
text: flexiMessage.Body.Content)
};
var tempFiles = new List<string>();
emailMessage.ToRecipients.AddAddresses(flexiMessage.To);
emailMessage.CcRecipients.AddAddresses(flexiMessage.Cc);
emailMessage.BccRecipients.AddAddresses(flexiMessage.Bcc);
if (flexiMessage.Attachments != null)
{
foreach (var attachment in flexiMessage.Attachments)
var tempFiles = new List<string>();
if (flexiMessage.Attachments != null)
{
var tempFilePath = Path.Combine(Path.GetTempPath(), attachment.Name);
await File.WriteAllBytesAsync(tempFilePath, attachment.Bytes);
foreach (var attachment in flexiMessage.Attachments)
{
var tempFilePath = Path.Combine(Path.GetTempPath(), attachment.Name);
await File.WriteAllBytesAsync(tempFilePath, attachment.Bytes);
tempFiles.Add(tempFilePath);
tempFiles.Add(tempFilePath);
emailMessage.Attachments.AddFileAttachment(attachment.Name, tempFilePath);
emailMessage.Attachments.AddFileAttachment(attachment.Name, tempFilePath);
}
}
}
this.exchangeBroker.SendAndSaveCopy(emailMessage: emailMessage);
this.exchangeBroker.SendAndSaveCopy(emailMessage: emailMessage);
foreach (var filePath in tempFiles)
{
File.Delete(filePath);
}
});

foreach (var filePath in tempFiles)
{
File.Delete(filePath);
}
}
private async ValueTask<ExchangeService> CreateExchangeServiceAsync()
{
var accessToken = await this.exchangeBroker.GetAccessTokenAsync();
Expand All @@ -71,13 +74,15 @@ private async ValueTask<ExchangeService> CreateExchangeServiceAsync()
return this.exchangeBroker.CreateExchangeService(ExchangeVersion.Exchange2013, accessToken,
impersonatedUserId);
}

private string GetUserId(ConnectingIdType idType) => idType switch
{
ConnectingIdType.PrincipalName => configurations.PrincipalName,
ConnectingIdType.SmtpAddress => configurations.SmtpAddress,
ConnectingIdType.SID => configurations.Sid,
_ => configurations.SmtpAddress
};

private ConnectingIdType GetConnectingIdType()
{
if (!string.IsNullOrWhiteSpace(configurations.SmtpAddress))
Expand All @@ -90,6 +95,7 @@ private ConnectingIdType GetConnectingIdType()
? ConnectingIdType.PrincipalName
: ConnectingIdType.SmtpAddress;
}

private static BodyType MapBodyType(BodyContentType bodyContentType) => bodyContentType switch
{
BodyContentType.Html => BodyType.HTML,
Expand Down
3 changes: 2 additions & 1 deletion FlexiMail/Services/IFlexiExchangeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
// Made with love for the .NET Community
// ---------------------------------------

using System.Threading.Tasks;
using FlexiMail.Models.Foundations.Messages;

namespace FlexiMail.Services
{
internal interface IFlexiExchangeService
{
void SendAndSaveCopyAsync(FlexiMessage flexiMessage);
ValueTask SendAndSaveCopyAsync(FlexiMessage flexiMessage);
}
}

0 comments on commit 59218cd

Please sign in to comment.