|
| 1 | +using BeekmanLabs.UnitTesting; |
| 2 | +using Common.Logging; |
| 3 | +using IntegrationEngine.Core.Configuration; |
| 4 | +using IntegrationEngine.Core.Mail; |
| 5 | +using Moq; |
| 6 | +using NUnit.Framework; |
| 7 | +using System; |
| 8 | +using System.IO; |
| 9 | +using System.Linq.Expressions; |
| 10 | +using System.Net.Mail; |
| 11 | +using System.Net.Sockets; |
| 12 | + |
| 13 | +namespace IntegrationEngine.Core.Tests.Mail |
| 14 | +{ |
| 15 | + public class MailClientTest : TestBase<MailClient> |
| 16 | + { |
| 17 | + public Mock<ITcpClient> MockTcpClient { get; set; } |
| 18 | + public Mock<ILog> MockLog { get; set; } |
| 19 | + |
| 20 | + [SetUp] |
| 21 | + public void Setup() |
| 22 | + { |
| 23 | + MockLog = new Mock<ILog>(); |
| 24 | + Subject.Log = MockLog.Object; |
| 25 | + Subject.MailConfiguration = new MailConfiguration() { |
| 26 | + HostName = "hostName", |
| 27 | + Port = 0, |
| 28 | + }; |
| 29 | + MockTcpClient = new Mock<ITcpClient>(); |
| 30 | + Subject.TcpClient = MockTcpClient.Object; |
| 31 | + } |
| 32 | + |
| 33 | + [Test] |
| 34 | + public void ShouldLogExceptionAndReturnFalseIfMailServerIsNotAvailable() |
| 35 | + { |
| 36 | + MockTcpClient.Setup(x => x.Connect( |
| 37 | + Subject.MailConfiguration.HostName, |
| 38 | + Subject.MailConfiguration.Port)) |
| 39 | + .Throws(new SocketException()); |
| 40 | + MockLog.Setup(x => x.Error(It.IsAny<SocketException>())); |
| 41 | + |
| 42 | + var actual = Subject.IsServerAvailable(); |
| 43 | + |
| 44 | + Assert.That(actual, Is.False); |
| 45 | + MockLog.Verify(x => x.Error(It.IsAny<SocketException>())); |
| 46 | + } |
| 47 | + |
| 48 | + [Test] |
| 49 | + public void ShouldReturnTrueIfMailServerIsAvailable() |
| 50 | + { |
| 51 | + var expectedText = "Mail server status: Available"; |
| 52 | + MockLog.Setup(x => x.Debug(expectedText)); |
| 53 | + MockTcpClient.Setup(x => x.Connect( |
| 54 | + Subject.MailConfiguration.HostName, |
| 55 | + Subject.MailConfiguration.Port)); |
| 56 | + var stream = new MemoryStream(); |
| 57 | + var responseInBytes = System.Text.Encoding.UTF8.GetBytes("OK"); |
| 58 | + stream.Write(responseInBytes, 0, responseInBytes.Length); |
| 59 | + MockTcpClient.Setup(x => x.GetStream()).Returns(stream); |
| 60 | + MockTcpClient.Setup(x => x.Close()); |
| 61 | + |
| 62 | + var actual = Subject.IsServerAvailable(); |
| 63 | + |
| 64 | + Assert.That(actual, Is.True); |
| 65 | + MockTcpClient.Verify(x => x.Close(), Times.Once); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void ShouldSendMailMessage() |
| 70 | + { |
| 71 | + var expected = new MailMessage(); |
| 72 | + var smtpClient = new Mock<ISmtpClient>(); |
| 73 | + smtpClient.Setup(x => x.Send(expected)); |
| 74 | + Subject.SmtpClient = smtpClient.Object; |
| 75 | + |
| 76 | + Subject.Send(expected); |
| 77 | + |
| 78 | + smtpClient.Verify(x => x.Send(expected), Times.Once); |
| 79 | + } |
| 80 | + |
| 81 | + [Test] |
| 82 | + public void ShouldLogExceptionIfMailMessageFailsToSend() |
| 83 | + { |
| 84 | + var expected = new MailMessage(); |
| 85 | + var smtpClient = new Mock<ISmtpClient>(); |
| 86 | + var expectedException = new Exception(); |
| 87 | + smtpClient.Setup(x => x.Send(expected)) |
| 88 | + .Throws(expectedException); |
| 89 | + Subject.SmtpClient = smtpClient.Object; |
| 90 | + |
| 91 | + Subject.Send(expected); |
| 92 | + |
| 93 | + smtpClient.Verify(x => x.Send(expected), Times.Once); |
| 94 | + MockLog.Verify(x => x.Error(expectedException), Times.Once); |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
0 commit comments