Skip to content

Commit 2f830c9

Browse files
committed
Add mail client tests and refactor dir structure
1 parent 91627e9 commit 2f830c9

20 files changed

+257
-50
lines changed

IntegrationEngine.Core.Tests/IntegrationEngine.Core.Tests.csproj

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@
5757
<ItemGroup>
5858
<Compile Include="Properties\AssemblyInfo.cs" />
5959
<Compile Include="Storage\ElasticsearchRepositoryTest.cs" />
60-
<Compile Include="StubElasticClient.cs" />
61-
<Compile Include="StubSearchResponse.cs" />
62-
<Compile Include="StubHit.cs" />
63-
<Compile Include="StubGetResponse.cs" />
64-
<Compile Include="StubIndexResponse.cs" />
65-
<Compile Include="StubUpdateResponse.cs" />
66-
<Compile Include="StubDeleteRequest.cs" />
67-
<Compile Include="StubPingResponse.cs" />
68-
<Compile Include="StubElasticsearchResponse.cs" />
69-
<Compile Include="StubExistsResponse.cs" />
60+
<Compile Include="Mail\MailClientTest.cs" />
61+
<Compile Include="Storage\StubDeleteRequest.cs" />
62+
<Compile Include="Storage\StubElasticClient.cs" />
63+
<Compile Include="Storage\StubElasticsearchResponse.cs" />
64+
<Compile Include="Storage\StubExistsResponse.cs" />
65+
<Compile Include="Storage\StubGetResponse.cs" />
66+
<Compile Include="Storage\StubHit.cs" />
67+
<Compile Include="Storage\StubIndexResponse.cs" />
68+
<Compile Include="Storage\StubPingResponse.cs" />
69+
<Compile Include="Storage\StubSearchResponse.cs" />
70+
<Compile Include="Storage\StubUpdateResponse.cs" />
7071
</ItemGroup>
7172
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
7273
<ItemGroup>
@@ -81,6 +82,7 @@
8182
</ItemGroup>
8283
<ItemGroup>
8384
<Folder Include="Storage\" />
85+
<Folder Include="Mail\" />
8486
</ItemGroup>
8587
<ItemGroup>
8688
<None Include="packages.config" />
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+

IntegrationEngine.Core.Tests/StubDeleteRequest.cs renamed to IntegrationEngine.Core.Tests/Storage/StubDeleteRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Nest;
44
using System;
55

6-
namespace IntegrationEngine.Core.Tests
6+
namespace IntegrationEngine.Core.Tests.Storage
77
{
88
public class StubDeleteRequest : IDeleteRequest
99
{

IntegrationEngine.Core.Tests/StubElasticClient.cs renamed to IntegrationEngine.Core.Tests/Storage/StubElasticClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Nest;
22
using System;
33

4-
namespace IntegrationEngine.Core.Tests
4+
namespace IntegrationEngine.Core.Tests.Storage
55
{
66
public class StubElasticClient : IElasticClient
77
{

IntegrationEngine.Core.Tests/StubElasticsearchResponse.cs renamed to IntegrationEngine.Core.Tests/Storage/StubElasticsearchResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Elasticsearch.Net.Connection;
33
using System;
44

5-
namespace IntegrationEngine.Core.Tests
5+
namespace IntegrationEngine.Core.Tests.Storage
66
{
77
public class StubElasticsearchResponse : IElasticsearchResponse
88
{

IntegrationEngine.Core.Tests/StubExistsResponse.cs renamed to IntegrationEngine.Core.Tests/Storage/StubExistsResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Nest;
22
using System;
33

4-
namespace IntegrationEngine.Core.Tests
4+
namespace IntegrationEngine.Core.Tests.Storage
55
{
66
public class StubExistsResponse : IExistsResponse
77
{

IntegrationEngine.Core.Tests/StubGetResponse.cs renamed to IntegrationEngine.Core.Tests/Storage/StubGetResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Nest;
33

4-
namespace IntegrationEngine.Core.Tests
4+
namespace IntegrationEngine.Core.Tests.Storage
55
{
66
public class StubGetResponse<TItem> : IGetResponse<TItem> where TItem : class
77
{

IntegrationEngine.Core.Tests/StubHit.cs renamed to IntegrationEngine.Core.Tests/Storage/StubHit.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System;
44
using System.Collections.Generic;
55

6-
namespace IntegrationEngine.Core.Tests
6+
namespace IntegrationEngine.Core.Tests.Storage
77
{
88
public class StubHit<TItem> : IHit<TItem> where TItem : class
99
{

IntegrationEngine.Core.Tests/StubIndexResponse.cs renamed to IntegrationEngine.Core.Tests/Storage/StubIndexResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Nest;
33
using System;
44

5-
namespace IntegrationEngine.Core.Tests
5+
namespace IntegrationEngine.Core.Tests.Storage
66
{
77
public class StubIndexResponse : IIndexResponse
88
{

IntegrationEngine.Core.Tests/StubPingResponse.cs renamed to IntegrationEngine.Core.Tests/Storage/StubPingResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Nest;
33
using System;
44

5-
namespace IntegrationEngine.Core.Tests
5+
namespace IntegrationEngine.Core.Tests.Storage
66
{
77
public class StubPingResponse : IPingResponse
88
{

0 commit comments

Comments
 (0)