Skip to content

Commit

Permalink
Merge pull request #29 from IowaComputerGurus/feature/async
Browse files Browse the repository at this point in the history
Adjusted project to use Async Properly Fixes #28
  • Loading branch information
kenny-sellers committed Mar 21, 2024
2 parents 412fb57 + ab6e020 commit 663baad
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 264 deletions.
67 changes: 10 additions & 57 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,61 +37,14 @@ jobs:
- name: Build
run: dotnet build "${{ env.solution-path }}" --no-restore --configuration Release -p:version=${{ steps.gitversion.outputs.majorMinorPatch }}
- name: Test
run: dotnet test "${{ env.solution-path }}" --no-build --configuration Release


code-quality:
if: github.actor != 'dependabot[bot]'
runs-on: windows-latest
name: Analyze Code Quality
env:
solution-path: './src/NetCore.Utilities.Email.SendGrid.sln'
steps:
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: 11
distribution: zulu
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: Cache SonarCloud packages
uses: actions/[email protected]
with:
path: ~\sonar\cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar
- name: Cache SonarCloud scanner
id: cache-sonar-scanner
uses: actions/[email protected]
with:
path: .\.sonar\scanner
key: ${{ runner.os }}-sonar-scanner
restore-keys: ${{ runner.os }}-sonar-scanner
- name: Install SonarCloud scanner
if: steps.cache-sonar-scanner.outputs.cache-hit != 'true'
shell: powershell
run: |
New-Item -Path .\.sonar\scanner -ItemType Directory
dotnet tool update dotnet-sonarscanner --tool-path .\.sonar\scanner
- name: Install GitVersion
run: dotnet tool install --global GitVersion.Tool

- name: Determine Version
id: gitversion
uses: gittools/actions/gitversion/[email protected]
with:
useConfigFile: true
run: dotnet test "${{ env.solution-path }}" --no-build --configuration Release --collect "XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover --logger "trx;LogFileName=unittests.trx"

- name: Build and analyze
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
shell: powershell
run: |
.\.sonar\scanner\dotnet-sonarscanner begin /k:"IowaComputerGurus_netcore.utilities.email.sendgrid" /o:"iowacomputergurus-github" /d:sonar.login="${{ secrets.SONAR_TOKEN }}" /d:sonar.host.url="https://sonarcloud.io"
dotnet restore "${{ env.solution-path }}"
dotnet build "${{ env.solution-path }}" --no-restore --configuration Release -p:version=${{ steps.gitversion.outputs.majorMinorPatch }}
dotnet test "${{ env.solution-path }}" --no-build --configuration Release --collect "XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover --logger "trx;LogFileName=unittests.trx"
.\.sonar\scanner\dotnet-sonarscanner end /d:sonar.login="${{ secrets.SONAR_TOKEN }}"
- name: Push Coverage to Codacy
shell: bash
env:
CODACY_ORGANIZATION_PROVIDER: ${{ secrets.CODACY_ORGANIZATION_PROVIDER }}
CODACY_USERNAME: ${{ secrets.CODACY_USERNAME }}
CODACY_PROJECT_NAME: ${{ secrets.CODACY_PROJECT_NAME }}
CODACY_API_TOKEN: ${{ secrets.CODACY_API_TOKEN }}
run: |
bash <(curl -Ls https://coverage.codacy.com/get.sh) report $(find . -name '*.opencover.xml' -printf '-r %p ')
1 change: 1 addition & 0 deletions .github/workflows/release-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:

- name: Restore Packages
run: dotnet restore "${{ env.solution-path }}"

- name: Build
run: dotnet build "${{ env.solution-path }}" --no-restore --configuration Release -p:version=${{ steps.gitversion.outputs.majorMinorPatch }}

Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ This library provides an easy to use implementation of SendGrid based email deli

This package depends on the ICG.NetCore.Utilities.Email project for template implementation

## SonarCloud Analysis

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.email.sendgrid&metric=alert_status)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_netcore.utilities.email.sendgrid)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.email.sendgrid&metric=coverage)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_netcore.utilities.email.sendgrid)
[![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.email.sendgrid&metric=security_rating)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_netcore.utilities.email.sendgrid)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=IowaComputerGurus_netcore.utilities.email.sendgrid&metric=sqale_index)](https://sonarcloud.io/dashboard?id=IowaComputerGurus_netcore.utilities.email.sendgrid)
## Breaking Changes

Version 7.0 has a breaking change transitioning to Async for all methods!

## Dependencies
The following additional NuGet packages are installed with this extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
44 changes: 22 additions & 22 deletions src/NetCore.Utilities.Email.SendGrid.Tests/SmtpServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,48 +84,48 @@ public void AdminName_ShouldReturnNullWhenNoConfiguration()
}

[Fact]
public void SendToAdministrator_ShouldSend_DefaultingFromAndToAddress()
public void SendToAdministratorAsync_ShouldSend_DefaultingFromAndToAddress()
{
//Arrange
var subject = "Test";
var message = "Message";

//Act
_service.SendMessageToAdministrator(subject, message);
_service.SendMessageToAdministratorAsync(subject, message);

//Verify
}

[Fact]
public void SendToAdministrator_ShouldSend_DefaultingFromAndToAddress_WithCCRecipients()
public void SendToAdministratorAsync_ShouldSend_DefaultingFromAndToAddress_WithCCRecipients()
{
//Arrange
var subject = "Test";
var message = "Message";
var cc = new List<string> {"[email protected]"};

//Act
_service.SendMessageToAdministrator(cc, subject, message);
_service.SendMessageToAdministratorAsync(cc, subject, message);

//Verify
}

[Fact]
public void SendMessage_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
public void SendMessageAsync_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var to = "[email protected]";
var subject = "test";
var message = "message";

//Act
_service.SendMessage(to, subject, message);
_service.SendMessageAsync(to, subject, message);

//Verify
}

[Fact]
public void SendMessage_WithCCRecipients_ShouldSend_DefaultingFromAddress()
public void SendMessageAsync_WithCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var to = "[email protected]";
Expand All @@ -134,13 +134,13 @@ public void SendMessage_WithCCRecipients_ShouldSend_DefaultingFromAddress()
var message = "message";

//Act
_service.SendMessage(to, cc, subject, message);
_service.SendMessageAsync(to, cc, subject, message);

//Verify
}

[Fact]
public void SendMessageWithAttachment_ShouldSend_DefaultingFromAddress()
public void SendMessageWithAttachmentAsync_ShouldSend_DefaultingFromAddress()
{
//Arrange
var to = "[email protected]";
Expand All @@ -151,13 +151,13 @@ public void SendMessageWithAttachment_ShouldSend_DefaultingFromAddress()
var message = "message";

//Act
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message, null);
_service.SendMessageWithAttachmentAsync(to, cc, subject, fileContent, fileName, message, null);

//Assets
}

[Fact]
public void SendMessage_ShouldPassOptionalTemplateName_ToMessageMethods()
public void SendMessageAsync_ShouldPassOptionalTemplateName_ToMessageMethods()
{
//Arrange
var to = "[email protected]";
Expand All @@ -167,13 +167,13 @@ public void SendMessage_ShouldPassOptionalTemplateName_ToMessageMethods()
var requestedTemplate = "Test";

//Act
_service.SendMessage(to, cc, subject, message, null, requestedTemplate);
_service.SendMessageAsync(to, cc, subject, message, null, requestedTemplate);

//Assets
}

[Fact]
public void SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMessageMethods()
public void SendMessageWithAttachmentAsync_ShouldPassOptionalTemplateName_ToMessageMethods()
{
//Arrange
var to = "[email protected]";
Expand All @@ -185,25 +185,25 @@ public void SendMessageWithAttachment_ShouldPassOptionalTemplateName_ToMessageMe
var requestedTemplate = "Test";

//Act
_service.SendMessageWithAttachment(to, cc, subject, fileContent, fileName, message, null, requestedTemplate);
_service.SendMessageWithAttachmentAsync(to, cc, subject, fileContent, fileName, message, null, requestedTemplate);

//Assets
}

[Fact]
public void SendWithReplyTo_ShouldThrowArgumentException_WhenReplyToMissing()
public async void SendWithReplyToAsync_ShouldThrowArgumentException_WhenReplyToMissing()
{
//Arrange
var to = "[email protected]";
var subject = "test";
var message = "message";

//Act/Assert
Assert.Throws<ArgumentNullException>(() => _service.SendWithReplyTo("", "", to, subject, message));
var result = await Assert.ThrowsAsync<ArgumentNullException>(() => _service.SendWithReplyToAsync("", "", to, subject, message));
}

[Fact]
public void SendWithReplyTo_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
public void SendWithReplyToAsync_WithoutCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var replyTo = "[email protected]";
Expand All @@ -216,13 +216,13 @@ public void SendWithReplyTo_WithoutCCRecipients_ShouldSend_DefaultingFromAddress
"")).Returns(returnMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, "", to, subject, message);
_service.SendWithReplyToAsync(replyTo, "", to, subject, message);

//Verify
}

[Fact]
public void SendWithReplyTo_WithCCRecipients_ShouldSend_DefaultingFromAddress()
public void SendWithReplyToAsync_WithCCRecipients_ShouldSend_DefaultingFromAddress()
{
//Arrange
var replyTo = "[email protected]";
Expand All @@ -235,14 +235,14 @@ public void SendWithReplyTo_WithCCRecipients_ShouldSend_DefaultingFromAddress()
.Setup(s => s.CreateMessage(_options.AdminEmail, _options.AdminName, to, cc, subject, message, "")).Returns(returnMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, "", to, cc, subject, message);
_service.SendWithReplyToAsync(replyTo, "", to, cc, subject, message);

//Verify
_sendGridMessageBuilderMock.Verify();
}

[Fact]
public void SendWithReplyTo_ShouldPassOptionalTemplateName_ToMessageMethods()
public void SendWithReplyToAsync_ShouldPassOptionalTemplateName_ToMessageMethods()
{
//Arrange
var replyTo = "[email protected]";
Expand All @@ -257,7 +257,7 @@ public void SendWithReplyTo_ShouldPassOptionalTemplateName_ToMessageMethods()
requestedTemplate)).Returns(returnMessage).Verifiable();

//Act
_service.SendWithReplyTo(replyTo, "", to, cc, subject, message, null, requestedTemplate);
_service.SendWithReplyToAsync(replyTo, "", to, cc, subject, message, null, requestedTemplate);

//Assets
_sendGridMessageBuilderMock.Verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="icg.netcore.utilities.email" Version="6.2.0" />
<PackageReference Include="icg.netcore.utilities.email" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
Expand All @@ -44,7 +44,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SendGrid" Version="9.28.1" />
<PackageReference Include="SendGrid" Version="9.29.2" />
</ItemGroup>

</Project>
41 changes: 20 additions & 21 deletions src/NetCore.Utilities.Email.SendGrid/SendGridSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@
using SendGrid;
using SendGrid.Helpers.Mail;

namespace ICG.NetCore.Utilities.Email.SendGrid
namespace ICG.NetCore.Utilities.Email.SendGrid;

/// <summary>
/// Internal service to abstract the actual SendGrid implementation to allow for proper unit testing of the remainder
/// of the project. Not intended for external usage
/// </summary>
public interface ISendGridSender
{
/// <summary>
/// Internal service to abstract the actual SendGrid implementation to allow for proper unit testing of the remainder
/// of the project. Not intended for external usage
/// Sends a mail message using the provided API key
/// </summary>
public interface ISendGridSender
{
/// <summary>
/// Sends a mail message using the provided API key
/// </summary>
/// <param name="apiKey"></param>
/// <param name="message"></param>
/// <returns></returns>
Task<bool> SendMessage(string apiKey, SendGridMessage message);
}
/// <param name="apiKey"></param>
/// <param name="message"></param>
/// <returns></returns>
Task<bool> SendMessage(string apiKey, SendGridMessage message);
}

/// <inheritdoc />
public class SendGridSender : ISendGridSender
{
/// <inheritdoc />
public class SendGridSender : ISendGridSender
public async Task<bool> SendMessage(string apiKey, SendGridMessage message)
{
/// <inheritdoc />
public async Task<bool> SendMessage(string apiKey, SendGridMessage message)
{
var client = new SendGridClient(apiKey);
var result = await client.SendEmailAsync(message);
return result.IsSuccessStatusCode;
}
var client = new SendGridClient(apiKey);
var result = await client.SendEmailAsync(message);
return result.IsSuccessStatusCode;
}
}
Loading

0 comments on commit 663baad

Please sign in to comment.