Skip to content

Commit b385e4c

Browse files
committed
Add more repo tests
1 parent 5990b42 commit b385e4c

File tree

10 files changed

+442
-12
lines changed

10 files changed

+442
-12
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
<Reference Include="Nest">
4848
<HintPath>..\packages\NEST.1.4.0\lib\net45\Nest.dll</HintPath>
4949
</Reference>
50+
<Reference Include="Common.Logging.Core">
51+
<HintPath>..\packages\Common.Logging.Core.3.0.0\lib\net40\Common.Logging.Core.dll</HintPath>
52+
</Reference>
53+
<Reference Include="Common.Logging">
54+
<HintPath>..\packages\Common.Logging.3.0.0\lib\net40\Common.Logging.dll</HintPath>
55+
</Reference>
5056
</ItemGroup>
5157
<ItemGroup>
5258
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -56,6 +62,10 @@
5662
<Compile Include="StubHit.cs" />
5763
<Compile Include="StubGetResponse.cs" />
5864
<Compile Include="StubIndexResponse.cs" />
65+
<Compile Include="StubUpdateResponse.cs" />
66+
<Compile Include="StubDeleteRequest.cs" />
67+
<Compile Include="StubPingResponse.cs" />
68+
<Compile Include="StubElasticsearchResponse.cs" />
5969
</ItemGroup>
6070
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
6171
<ItemGroup>

IntegrationEngine.Core.Tests/Storage/ElasticsearchRepositoryTest.cs

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using BeekmanLabs.UnitTesting;
2+
using Common.Logging;
3+
using Elasticsearch.Net;
24
using IntegrationEngine.Core.Storage;
35
using IntegrationEngine.Model;
46
using Moq;
@@ -33,20 +35,22 @@ public void ShouldReturnListOfDocumentsWithIdsFromElasticsearch()
3335

3436
Assert.That(actual, Is.Not.Empty);
3537
Assert.That(actual.First().Id, Is.EqualTo(expectedId));
38+
elasticClient.Verify(x => x.Search(It.IsAny<Func<SearchDescriptor<CronTrigger>, SearchDescriptor<CronTrigger>>>()), Times.Once);
3639
}
3740

3841
[Test]
3942
public void ShouldReturnNullIfDocumentIsNotFoundById()
4043
{
4144
var elasticClient = new Mock<StubElasticClient>();
4245
var getResponse = new Mock<StubGetResponse<CronTrigger>>();
43-
elasticClient.Setup(x => x.Get<CronTrigger>(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
46+
elasticClient.Setup(x => x.Get(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
4447
.Returns(getResponse.Object);
4548
Subject.ElasticClient = elasticClient.Object;
4649

4750
var actual = Subject.SelectById<CronTrigger>("1");
4851

4952
Assert.That(actual, Is.Null);
53+
elasticClient.Verify(x => x.Get(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()), Times.Once);
5054
}
5155

5256
[Test]
@@ -57,13 +61,125 @@ public void ShouldReturnSingleDocumentGivenAnId()
5761
var expectedId = "1";
5862
getResponse.SetupGet(x => x.Id).Returns(() => expectedId);
5963
getResponse.SetupGet(x => x.Source).Returns(() => new CronTrigger());
60-
elasticClient.Setup(x => x.Get<CronTrigger>(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
64+
elasticClient.Setup(x => x.Get(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
6165
.Returns(getResponse.Object);
6266
Subject.ElasticClient = elasticClient.Object;
6367

6468
var actual = Subject.SelectById<CronTrigger>(expectedId);
6569

6670
Assert.That(actual.Id, Is.EqualTo(expectedId));
71+
elasticClient.Verify(x => x.Get(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()), Times.Once);
72+
}
73+
74+
[Test]
75+
public void ShouldInsertAndReturnDocument()
76+
{
77+
var expected = new CronTrigger() {
78+
Id = "1",
79+
};
80+
var elasticClient = new Mock<StubElasticClient>();
81+
var getResponse = new Mock<StubGetResponse<CronTrigger>>();
82+
getResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
83+
getResponse.SetupGet(x => x.Source).Returns(() => new CronTrigger());
84+
elasticClient.Setup(x => x.Get<CronTrigger>(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
85+
.Returns(getResponse.Object);
86+
var indexResponse = new Mock<StubIndexResponse>();
87+
indexResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
88+
elasticClient.Setup(x => x.Index(expected, It.IsAny<Func<IndexDescriptor<CronTrigger>, IndexDescriptor<CronTrigger>>>()))
89+
.Returns(indexResponse.Object);
90+
Subject.ElasticClient = elasticClient.Object;
91+
92+
var actual = Subject.Insert(expected);
93+
94+
Assert.That(actual.Id, Is.EqualTo(expected.Id));
95+
elasticClient.Verify(x => x.Index(expected, It.IsAny<Func<IndexDescriptor<CronTrigger>, IndexDescriptor<CronTrigger>>>()), Times.Once);
96+
}
97+
98+
[Test]
99+
public void ShouldUpdateAndReturnDocument()
100+
{
101+
var expected = new CronTrigger() {
102+
Id = "1",
103+
};
104+
var elasticClient = new Mock<StubElasticClient>();
105+
var getResponse = new Mock<StubGetResponse<CronTrigger>>();
106+
getResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
107+
getResponse.SetupGet(x => x.Source).Returns(() => new CronTrigger());
108+
elasticClient.Setup(x => x.Get<CronTrigger>(It.IsAny<Func<GetDescriptor<CronTrigger>, GetDescriptor<CronTrigger>>>()))
109+
.Returns(getResponse.Object);
110+
var updateResponse = new Mock<StubUpdateResponse>();
111+
updateResponse.SetupGet(x => x.Id).Returns(() => expected.Id);
112+
elasticClient.Setup(x => x.Update<CronTrigger>(It.IsAny<Func<UpdateDescriptor<CronTrigger, CronTrigger>, UpdateDescriptor<CronTrigger, CronTrigger>>>()))
113+
.Returns(updateResponse.Object);
114+
Subject.ElasticClient = elasticClient.Object;
115+
116+
var actual = Subject.Update(expected);
117+
118+
Assert.That(actual.Id, Is.EqualTo(expected.Id));
119+
elasticClient.Verify(
120+
x => x.Update<CronTrigger>(It.IsAny<Func<UpdateDescriptor<CronTrigger, CronTrigger>, UpdateDescriptor<CronTrigger, CronTrigger>>>()),
121+
Times.Once);
122+
}
123+
124+
[Test]
125+
public void ShouldDeleteDocument()
126+
{
127+
var id = "1";
128+
var elasticClient = new Mock<StubElasticClient>();
129+
elasticClient.Setup(x => x.Delete(It.IsAny<Func<DeleteDescriptor<CronTrigger>, DeleteDescriptor<CronTrigger>>>()));
130+
Subject.ElasticClient = elasticClient.Object;
131+
132+
Subject.Delete<CronTrigger>(id);
133+
134+
elasticClient.Verify(x => x.Delete(It.IsAny<Func<DeleteDescriptor<CronTrigger>, DeleteDescriptor<CronTrigger>>>()), Times.Once);
135+
}
136+
137+
[Test]
138+
public void ShouldShouldReturnTrueIfServerIsAvailable()
139+
{
140+
var elasticClient = new Mock<StubElasticClient>();
141+
var elasticsearchResponse = new Mock<StubElasticsearchResponse>();
142+
elasticsearchResponse.SetupGet(x => x.Success).Returns(() => true);
143+
var pingResponse = new Mock<StubPingResponse>();
144+
pingResponse.SetupGet(x => x.ConnectionStatus).Returns(() => elasticsearchResponse.Object);
145+
elasticClient.Setup(x => x.Ping(It.IsAny<PingRequest>())).Returns(pingResponse.Object);
146+
Subject.ElasticClient = elasticClient.Object;
147+
148+
var actual = Subject.IsServerAvailable();
149+
150+
Assert.That(actual, Is.True);
151+
}
152+
153+
[Test]
154+
public void ShouldShouldReturnFalseIfServerIsUnavailable()
155+
{
156+
var elasticClient = new Mock<StubElasticClient>();
157+
var elasticsearchResponse = new Mock<StubElasticsearchResponse>();
158+
elasticsearchResponse.SetupGet(x => x.Success).Returns(() => false);
159+
var pingResponse = new Mock<StubPingResponse>();
160+
pingResponse.SetupGet(x => x.ConnectionStatus).Returns(() => elasticsearchResponse.Object);
161+
elasticClient.Setup(x => x.Ping(It.IsAny<PingRequest>())).Returns(pingResponse.Object);
162+
Subject.ElasticClient = elasticClient.Object;
163+
164+
var actual = Subject.IsServerAvailable();
165+
166+
Assert.That(actual, Is.False);
167+
}
168+
169+
[Test]
170+
public void ShouldShouldReturnFalseIfServerIsUnavailableBecauseExceptionOccured()
171+
{
172+
var elasticClient = new Mock<StubElasticClient>();
173+
elasticClient.Setup(x => x.Ping(It.IsAny<PingRequest>())).Returns<PingResponse>(null);
174+
Subject.ElasticClient = elasticClient.Object;
175+
var log = new Mock<ILog>();
176+
log.Setup(x => x.Error(It.IsAny<Exception>()));
177+
Subject.Log = log.Object;
178+
179+
var actual = Subject.IsServerAvailable();
180+
181+
Assert.That(actual, Is.False);
182+
log.Verify(x => x.Error(It.IsAny<Exception>()), Times.Once);
67183
}
68184
}
69185
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Elasticsearch.Net;
2+
using Elasticsearch.Net.Connection.Configuration;
3+
using Nest;
4+
using System;
5+
6+
namespace IntegrationEngine.Core.Tests
7+
{
8+
public class StubDeleteRequest : IDeleteRequest
9+
{
10+
public StubDeleteRequest()
11+
{
12+
}
13+
14+
#region IPathInfo implementation
15+
16+
public ElasticsearchPathInfo<DeleteRequestParameters> ToPathInfo(IConnectionSettingsValues settings)
17+
{
18+
throw new NotImplementedException();
19+
}
20+
21+
#endregion
22+
23+
#region IDocumentOptionalPath implementation
24+
25+
public IndexNameMarker Index {
26+
get {
27+
throw new NotImplementedException();
28+
}
29+
set {
30+
throw new NotImplementedException();
31+
}
32+
}
33+
34+
public TypeNameMarker Type {
35+
get {
36+
throw new NotImplementedException();
37+
}
38+
set {
39+
throw new NotImplementedException();
40+
}
41+
}
42+
43+
public string Id {
44+
get {
45+
throw new NotImplementedException();
46+
}
47+
set {
48+
throw new NotImplementedException();
49+
}
50+
}
51+
52+
#endregion
53+
54+
#region IRequest implementation
55+
56+
public Elasticsearch.Net.DeleteRequestParameters RequestParameters {
57+
get {
58+
throw new NotImplementedException();
59+
}
60+
set {
61+
throw new NotImplementedException();
62+
}
63+
}
64+
65+
#endregion
66+
67+
#region IRequest implementation
68+
69+
public IRequestConfiguration RequestConfiguration {
70+
get {
71+
throw new NotImplementedException();
72+
}
73+
set {
74+
throw new NotImplementedException();
75+
}
76+
}
77+
78+
#endregion
79+
}
80+
}
81+

IntegrationEngine.Core.Tests/StubElasticClient.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public System.Threading.Tasks.Task<ISearchResponse<T>> ScrollAsync<T>(Func<Scrol
3737
throw new NotImplementedException();
3838
}
3939

40-
public IUpdateResponse Update<T>(Func<UpdateDescriptor<T, T>, UpdateDescriptor<T, T>> updateSelector) where T : class
40+
public virtual IUpdateResponse Update<T>(Func<UpdateDescriptor<T, T>, UpdateDescriptor<T, T>> updateSelector) where T : class
4141
{
4242
throw new NotImplementedException();
4343
}
@@ -1057,12 +1057,12 @@ public System.Threading.Tasks.Task<IBulkResponse> BulkAsync(Func<BulkDescriptor,
10571057
throw new NotImplementedException();
10581058
}
10591059

1060-
public IIndexResponse Index<T>(T @object, Func<IndexDescriptor<T>, IndexDescriptor<T>> indexSelector = null) where T : class
1060+
public virtual IIndexResponse Index<T>(T @object, Func<IndexDescriptor<T>, IndexDescriptor<T>> indexSelector = null) where T : class
10611061
{
10621062
throw new NotImplementedException();
10631063
}
10641064

1065-
public IIndexResponse Index<T>(IIndexRequest<T> indexRequest) where T : class
1065+
public virtual IIndexResponse Index<T>(IIndexRequest<T> indexRequest) where T : class
10661066
{
10671067
throw new NotImplementedException();
10681068
}
@@ -1077,7 +1077,7 @@ public System.Threading.Tasks.Task<IIndexResponse> IndexAsync<T>(IIndexRequest<T
10771077
throw new NotImplementedException();
10781078
}
10791079

1080-
public IDeleteResponse Delete<T>(Func<DeleteDescriptor<T>, DeleteDescriptor<T>> deleteSelector) where T : class
1080+
public virtual IDeleteResponse Delete<T>(Func<DeleteDescriptor<T>, DeleteDescriptor<T>> deleteSelector) where T : class
10811081
{
10821082
throw new NotImplementedException();
10831083
}
@@ -1727,7 +1727,7 @@ public System.Threading.Tasks.Task<IPingResponse> PingAsync(Func<PingDescriptor,
17271727
throw new NotImplementedException();
17281728
}
17291729

1730-
public IPingResponse Ping(IPingRequest pingRequest)
1730+
public virtual IPingResponse Ping(IPingRequest pingRequest)
17311731
{
17321732
throw new NotImplementedException();
17331733
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using Elasticsearch.Net;
2+
using Elasticsearch.Net.Connection;
3+
using System;
4+
5+
namespace IntegrationEngine.Core.Tests
6+
{
7+
public class StubElasticsearchResponse : IElasticsearchResponse
8+
{
9+
public StubElasticsearchResponse()
10+
{
11+
}
12+
13+
#region IElasticsearchResponse implementation
14+
15+
public virtual bool Success {
16+
get {
17+
throw new NotImplementedException();
18+
}
19+
}
20+
21+
public IConnectionConfigurationValues Settings {
22+
get {
23+
throw new NotImplementedException();
24+
}
25+
}
26+
27+
public Exception OriginalException {
28+
get {
29+
throw new NotImplementedException();
30+
}
31+
}
32+
33+
public string RequestMethod {
34+
get {
35+
throw new NotImplementedException();
36+
}
37+
}
38+
39+
public string RequestUrl {
40+
get {
41+
throw new NotImplementedException();
42+
}
43+
}
44+
45+
public int? HttpStatusCode {
46+
get {
47+
throw new NotImplementedException();
48+
}
49+
}
50+
51+
public int NumberOfRetries {
52+
get {
53+
throw new NotImplementedException();
54+
}
55+
}
56+
57+
public CallMetrics Metrics {
58+
get {
59+
throw new NotImplementedException();
60+
}
61+
}
62+
63+
public byte[] ResponseRaw {
64+
get {
65+
throw new NotImplementedException();
66+
}
67+
}
68+
69+
public byte[] Request {
70+
get {
71+
throw new NotImplementedException();
72+
}
73+
}
74+
75+
#endregion
76+
}
77+
}
78+

IntegrationEngine.Core.Tests/StubIndexResponse.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public StubIndexResponse()
1212

1313
#region IIndexResponse implementation
1414

15-
public string Id {
15+
public virtual string Id {
1616
get {
1717
throw new NotImplementedException();
1818
}

0 commit comments

Comments
 (0)