Skip to content

Commit

Permalink
Merge pull request #6 from merodriguezblanco/hipchat-integration
Browse files Browse the repository at this point in the history
Hipchat integration
  • Loading branch information
merodriguezblanco authored Dec 31, 2018
2 parents 76671e8 + f8b960f commit 465f2ed
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 155 deletions.
18 changes: 9 additions & 9 deletions ExceptionNotification.Core.Tests/Email/EmailBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
using Microsoft.AspNetCore.Http.Internal;
using Xunit;

namespace ExceptionNotification.Core.Tests
namespace ExceptionNotification.Core.Tests.Email
{
public class EmailBuilderTests
{
private readonly Exception _exception;

private readonly IEmailConfiguration _emailConfiguration;
private readonly EmailConfiguration _emailConfiguration;

private readonly NotifierOptions _notifierOptions;

Expand Down Expand Up @@ -46,7 +46,7 @@ public void ComposeEmailThrowsExceptionWhenSenderIsNull()
{
_emailConfiguration.Sender = null;

var exception = Assert.Throws<SenderNullException>(() => EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions));
var exception = Assert.Throws<SenderNullException>(() => EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions, null));
Assert.Equal("ComposeEmail failure: Sender is null.", exception.Message);
}

Expand All @@ -55,20 +55,20 @@ public void ComposeEmailThrowsExceptionWhenRecipientsCollectionIsEmpty()
{
_emailConfiguration.Recipients = null;

var exception = Assert.Throws<EmptyRecipientsException>(() => EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions));
var exception = Assert.Throws<EmptyRecipientsException>(() => EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions, null));
Assert.Equal("ComposeEmail failure: Recipients collection is empty.", exception.Message);

_emailConfiguration.Recipients = new List<EmailAddress>();

exception = Assert.Throws<EmptyRecipientsException>(() => EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions));
exception = Assert.Throws<EmptyRecipientsException>(() => EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions, null));
Assert.Equal("ComposeEmail failure: Recipients collection is empty.", exception.Message);

}

[Fact]
public void ComposeEmailReturnsMailMessage()
{
var email = EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions);
var email = EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions, null);

Assert.IsType<MailMessage>(email);
Assert.Equal("[Fried Chicken - Test] EXCEPTION!", email.Subject);
Expand All @@ -84,7 +84,7 @@ public void ComposeEmailReturnsMailMessage()
[Fact]
public void ComposeEmailBuildsMessageBody()
{
var email = EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions);
var email = EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions, null);

Assert.IsType<MailMessage>(email);
Assert.Contains("------------------\nException Message:\n------------------\n\nThis is an exception!", email.Body);
Expand All @@ -104,8 +104,8 @@ public void ComposeEmailBuildsMessageBodyWithRequest()
Method = HttpMethod.Get.ToString()
}
};
_notifierOptions.Request = new DefaultHttpRequest(httpContext);
var email = EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions);
var request = new DefaultHttpRequest(httpContext);
var email = EmailBuilder.ComposeEmail(_exception, _emailConfiguration, _notifierOptions, request);

Assert.IsType<MailMessage>(email);
Assert.Contains("------------------\nException Message:\n------------------\n\nThis is an exception!", email.Body);
Expand Down

This file was deleted.

28 changes: 28 additions & 0 deletions ExceptionNotification.Core.Tests/Email/EmailNotifierTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using ExceptionNotification.Core.Email;
using ExceptionNotification.Core.Exceptions.Email;
using Xunit;

namespace ExceptionNotification.Core.Tests.Email
{
public class EmailNotifierTests
{
[Fact]
public void FireExceptionThrowsExceptionWhenConfigurationIsNull()
{
var notifier = new EmailNotifier(null);

var exception = Assert.Throws<ConfigurationMissingException>(() => notifier.FireNotification(new Exception(), null));
Assert.Equal("FireNotification failure: configuration is null.", exception.Message);
}

[Fact]
public void FireExceptionThrowsExceptionWhenExceptionIsNull()
{
var notifier = new EmailNotifier(new EmailConfiguration());

var exception = Assert.Throws<ExceptionMissingException>(() => notifier.FireNotification(null, null));
Assert.Equal("FireNotification failure: exception is null.", exception.Message);
}
}
}
28 changes: 28 additions & 0 deletions ExceptionNotification.Core.Tests/Hipchat/HipchatNotifierTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using ExceptionNotification.Core.Exceptions.Email;
using ExceptionNotification.Core.Hipchat;
using Xunit;

namespace ExceptionNotification.Core.Tests.Hipchat
{
public class HipchatNotifierTests
{
[Fact]
public void FireExceptionThrowsExceptionWhenConfigurationIsNull()
{
var notifier = new HipchatNotifier(null);

var exception = Assert.Throws<ConfigurationMissingException>(() => notifier.FireNotification(new Exception(), null));
Assert.Equal("FireNotification failure: configuration is null.", exception.Message);
}

[Fact]
public void FireExceptionThrowsExceptionWhenExceptionIsNull()
{
var notifier = new HipchatNotifier(new HipchatConfiguration());

var exception = Assert.Throws<ExceptionMissingException>(() => notifier.FireNotification(null, null));
Assert.Equal("FireNotification failure: exception is null.", exception.Message);
}
}
}
26 changes: 26 additions & 0 deletions ExceptionNotification.Core/BaseNotifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Reflection;
using Microsoft.AspNetCore.Http;

namespace ExceptionNotification.Core
{
public class BaseNotifier
{
protected NotifierOptions NotifierOptions;

public BaseNotifier()
{
NotifierOptions = new NotifierOptions
{
ProjectName = Assembly.GetEntryAssembly().GetName().Name,
Environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
};
}

public virtual void FireNotification(Exception exception)
{}

public virtual void FireNotification(Exception exception, HttpRequest request)
{}
}
}
4 changes: 2 additions & 2 deletions ExceptionNotification.Core/Email/EmailBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ExceptionNotification.Core.Email
{
public static class EmailBuilder
{
public static MailMessage ComposeEmail(Exception exception, IEmailConfiguration emailConfiguration, NotifierOptions notifierOptions)
public static MailMessage ComposeEmail(Exception exception, EmailConfiguration emailConfiguration, NotifierOptions notifierOptions, HttpRequest request)
{
if (IsSenderNull(emailConfiguration.Sender))
{
Expand All @@ -24,7 +24,7 @@ public static MailMessage ComposeEmail(Exception exception, IEmailConfiguration
{
Subject = ComposeSubject(notifierOptions),
From = new MailAddress(emailConfiguration.Sender.Address, emailConfiguration.Sender.DisplayName),
Body = ComposeContent(exception, notifierOptions.Request)
Body = ComposeContent(exception, request)
};

emailConfiguration.Recipients.ForEach(r =>
Expand Down
2 changes: 1 addition & 1 deletion ExceptionNotification.Core/Email/EmailConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace ExceptionNotification.Core.Email
{
public class EmailConfiguration : IEmailConfiguration
public class EmailConfiguration
{
public string SmtpServer { get; set; }

Expand Down
57 changes: 0 additions & 57 deletions ExceptionNotification.Core/Email/EmailExceptionNotifier.cs

This file was deleted.

59 changes: 59 additions & 0 deletions ExceptionNotification.Core/Email/EmailNotifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Net;
using System.Net.Mail;
using ExceptionNotification.Core.Exceptions.Email;
using Microsoft.AspNetCore.Http;

namespace ExceptionNotification.Core.Email
{
public class EmailNotifier : BaseNotifier
{
private readonly EmailConfiguration _configuration;

public EmailNotifier(EmailConfiguration configuration)
{
_configuration = configuration;
}

public override void FireNotification(Exception exception)
{
FireNotification(exception, null);
}

public override void FireNotification(Exception exception, HttpRequest request)
{
if (_configuration == null)
{
throw new ConfigurationMissingException("FireNotification failure: configuration is null.");
}

if (exception == null)
{
throw new ExceptionMissingException("FireNotification failure: exception is null.");
}

var message = EmailBuilder.ComposeEmail(exception, _configuration, NotifierOptions, request);

try
{
using (var client = new SmtpClient(_configuration.SmtpServer, _configuration.SmtpPort))
{
client.UseDefaultCredentials = false;

if (_configuration.UseCredentials)
{
client.Credentials =
new NetworkCredential(_configuration.SmtpUser, _configuration.SmtpPassword);
}

client.EnableSsl = _configuration.EnableSsl;
client.Send(message);
}
}
catch (Exception)
{
//
}
}
}
}
23 changes: 0 additions & 23 deletions ExceptionNotification.Core/Email/IEmailConfiguration.cs

This file was deleted.

3 changes: 2 additions & 1 deletion ExceptionNotification.Core/ExceptionNotification.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<RepositoryUrl>https://github.com/merodriguezblanco/ExceptionNotification.Core</RepositoryUrl>
<PackageTags>dotnet dotnetcore-2 exception-handling</PackageTags>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HipChat.Net.Portable" Version="0.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.0.3" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 465f2ed

Please sign in to comment.