Skip to content

Commit 3e5355b

Browse files
committed
Add more product check tests
1 parent fed9499 commit 3e5355b

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Net;
8+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
9+
using Elasticsearch.Net;
10+
using FluentAssertions;
11+
using Nest;
12+
13+
namespace Tests.ClientConcepts
14+
{
15+
public class ProductCheckTests
16+
{
17+
[U] public void MissingProductNameCausesException()
18+
{
19+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
20+
productCheckResponse.Headers.Clear();
21+
22+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
23+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
24+
var client = new ElasticClient(connectionSettings);
25+
26+
client.Invoking(y => y.Cluster.Health())
27+
.Should()
28+
.Throw<InvalidProductException>();
29+
}
30+
31+
[U] public void InvalidProductNameCausesException()
32+
{
33+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
34+
productCheckResponse.Headers.Clear();
35+
productCheckResponse.Headers.Add("X-elastic-product", new List<string>{ "Something Unexpected" });
36+
37+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
38+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
39+
var client = new ElasticClient(connectionSettings);
40+
41+
client.Invoking(y => y.Cluster.Health())
42+
.Should()
43+
.Throw<InvalidProductException>();
44+
}
45+
46+
[U] public void UnauthorizedStatusCodeFromRootPathDoesNotThrowException_WithExpectedDataOnApiCall()
47+
{
48+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
49+
productCheckResponse.StatusCode = (int)HttpStatusCode.Unauthorized;
50+
51+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
52+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
53+
var client = new ElasticClient(connectionSettings);
54+
55+
var response = client.Cluster.Health();
56+
57+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckOnStartup);
58+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckSuccess);
59+
response.ApiCall.DebugInformation.Should().Contain(RequestPipeline.UndeterminedProductWarning);
60+
}
61+
62+
[U] public void ForbiddenStatusCodeFromRootPathDoesNotThrowException_WithExpectedDataOnApiCall()
63+
{
64+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
65+
productCheckResponse.StatusCode = (int)HttpStatusCode.Forbidden;
66+
67+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
68+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
69+
var client = new ElasticClient(connectionSettings);
70+
71+
var response = client.Cluster.Health();
72+
73+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckOnStartup);
74+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckSuccess);
75+
response.ApiCall.DebugInformation.Should().Contain(RequestPipeline.UndeterminedProductWarning);
76+
}
77+
78+
[U] public void OldVersionsThrowException()
79+
{
80+
var responseJson = new
81+
{
82+
version = new
83+
{
84+
number = "5.9.999",
85+
build_flavor = "default",
86+
},
87+
tagline = "You Know, for Search"
88+
};
89+
90+
using var ms = RecyclableMemoryStreamFactory.Default.Create();
91+
LowLevelRequestResponseSerializer.Instance.Serialize(responseJson, ms);
92+
93+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
94+
productCheckResponse.ResponseBytes = ms.ToArray();
95+
96+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
97+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
98+
var client = new ElasticClient(connectionSettings);
99+
100+
client.Invoking(y => y.Cluster.Health())
101+
.Should()
102+
.Throw<InvalidProductException>();
103+
}
104+
105+
[U] public void MissingTaglineOnVersionSixThrowsException()
106+
{
107+
var responseJson = new
108+
{
109+
version = new
110+
{
111+
number = "6.10.0",
112+
build_flavor = "default"
113+
}
114+
};
115+
116+
using var ms = RecyclableMemoryStreamFactory.Default.Create();
117+
LowLevelRequestResponseSerializer.Instance.Serialize(responseJson, ms);
118+
119+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
120+
productCheckResponse.ResponseBytes = ms.ToArray();
121+
122+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
123+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
124+
var client = new ElasticClient(connectionSettings);
125+
126+
client.Invoking(y => y.Cluster.Health())
127+
.Should()
128+
.Throw<InvalidProductException>();
129+
}
130+
131+
[U] public void InvalidTaglineOnVersionSixThrowsException()
132+
{
133+
var responseJson = new
134+
{
135+
version = new
136+
{
137+
number = "6.10.0",
138+
build_flavor = "default"
139+
},
140+
tagline = "unexpected"
141+
};
142+
143+
using var ms = RecyclableMemoryStreamFactory.Default.Create();
144+
LowLevelRequestResponseSerializer.Instance.Serialize(responseJson, ms);
145+
146+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
147+
productCheckResponse.ResponseBytes = ms.ToArray();
148+
149+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
150+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
151+
var client = new ElasticClient(connectionSettings);
152+
153+
client.Invoking(y => y.Cluster.Health())
154+
.Should()
155+
.Throw<InvalidProductException>();
156+
}
157+
158+
[U] public void ExpectedTaglineOnVersionSixDoesNotThrowException_WithExpectedDataOnApiCall()
159+
{
160+
var responseJson = new
161+
{
162+
version = new
163+
{
164+
number = "6.8.0",
165+
build_flavor = "default"
166+
},
167+
tagline = "You Know, for Search"
168+
};
169+
170+
using var ms = RecyclableMemoryStreamFactory.Default.Create();
171+
LowLevelRequestResponseSerializer.Instance.Serialize(responseJson, ms);
172+
173+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
174+
productCheckResponse.ResponseBytes = ms.ToArray();
175+
176+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
177+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
178+
var client = new ElasticClient(connectionSettings);
179+
180+
var response = client.Cluster.Health();
181+
182+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckOnStartup);
183+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckSuccess);
184+
}
185+
186+
[U] public void MissingBuildFlavorOnVersionSevenThrowsException()
187+
{
188+
var responseJson = new
189+
{
190+
version = new
191+
{
192+
number = "7.13.0"
193+
},
194+
tagline = "You Know, for Search"
195+
};
196+
197+
using var ms = RecyclableMemoryStreamFactory.Default.Create();
198+
LowLevelRequestResponseSerializer.Instance.Serialize(responseJson, ms);
199+
200+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
201+
productCheckResponse.ResponseBytes = ms.ToArray();
202+
203+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
204+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
205+
var client = new ElasticClient(connectionSettings);
206+
207+
client.Invoking(y => y.Cluster.Health())
208+
.Should()
209+
.Throw<InvalidProductException>();
210+
}
211+
212+
[U] public void InvalidBuildFlavorMissingOnVersionSevenThrowsException()
213+
{
214+
var responseJson = new
215+
{
216+
version = new
217+
{
218+
number = "7.13.0",
219+
build_flavor = "unexpected"
220+
},
221+
tagline = "You Know, for Search"
222+
};
223+
224+
using var ms = RecyclableMemoryStreamFactory.Default.Create();
225+
LowLevelRequestResponseSerializer.Instance.Serialize(responseJson, ms);
226+
227+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
228+
productCheckResponse.ResponseBytes = ms.ToArray();
229+
230+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
231+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
232+
var client = new ElasticClient(connectionSettings);
233+
234+
client.Invoking(y => y.Cluster.Health())
235+
.Should()
236+
.Throw<InvalidProductException>();
237+
}
238+
239+
[U] public void ExpectedBuildFlavorOnVersionSixDoesNotThrowException_WithExpectedDataOnApiCall()
240+
{
241+
var responseJson = new
242+
{
243+
version = new
244+
{
245+
number = "7.13.0",
246+
build_flavor = "default"
247+
},
248+
tagline = "You Know, for Search"
249+
};
250+
251+
using var ms = RecyclableMemoryStreamFactory.Default.Create();
252+
LowLevelRequestResponseSerializer.Instance.Serialize(responseJson, ms);
253+
254+
var productCheckResponse = InMemoryConnection.ValidProductCheckResponse();
255+
productCheckResponse.ResponseBytes = ms.ToArray();
256+
257+
var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
258+
var connectionSettings = new ConnectionSettings(connectionPool, new InMemoryConnection(productCheckResponse));
259+
var client = new ElasticClient(connectionSettings);
260+
261+
var response = client.Cluster.Health();
262+
263+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckOnStartup);
264+
response.ApiCall.AuditTrail.Should().Contain(x => x.Event == AuditEvent.ProductCheckSuccess);
265+
}
266+
}
267+
}

0 commit comments

Comments
 (0)