This repository has been archived by the owner on Jun 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
test/Serilog.Sinks.Elasticsearch.Tests/Templating/Sendsv7TemplateTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Reflection; | ||
using FluentAssertions; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.Elasticsearch.Tests.Templating | ||
{ | ||
public class Sendsv7TemplateTests : ElasticsearchSinkTestsBase | ||
{ | ||
private readonly Tuple<Uri, string> _templatePut; | ||
|
||
public Sendsv7TemplateTests() | ||
{ | ||
_options.AutoRegisterTemplate = true; | ||
_options.AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7; | ||
|
||
var loggerConfig = new LoggerConfiguration() | ||
.MinimumLevel.Debug() | ||
.Enrich.WithMachineName() | ||
.WriteTo.ColoredConsole() | ||
.WriteTo.Elasticsearch(_options); | ||
|
||
var logger = loggerConfig.CreateLogger(); | ||
using (logger as IDisposable) | ||
{ | ||
logger.Error("Test exception. Should not contain an embedded exception object."); | ||
} | ||
|
||
this._seenHttpPosts.Should().NotBeNullOrEmpty().And.HaveCount(1); | ||
this._seenHttpPuts.Should().NotBeNullOrEmpty().And.HaveCount(1); | ||
_templatePut = this._seenHttpPuts[0]; | ||
} | ||
|
||
[Fact] | ||
public void ShouldRegisterTheCorrectTemplateOnRegistration() | ||
{ | ||
|
||
var method = typeof(Sendsv7TemplateTests).GetMethod(nameof(ShouldRegisterTheCorrectTemplateOnRegistration)); | ||
JsonEquals(_templatePut.Item2, method, "template_v7.json"); | ||
} | ||
|
||
[Fact] | ||
public void TemplatePutToCorrectUrl() | ||
{ | ||
var uri = _templatePut.Item1; | ||
uri.AbsolutePath.Should().Be("/_template/serilog-events-template"); | ||
} | ||
|
||
protected void JsonEquals(string json, MethodBase method, string fileName = null) | ||
{ | ||
#if DOTNETCORE | ||
var assembly = typeof(Sendsv7TemplateTests).GetTypeInfo().Assembly; | ||
#else | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
#endif | ||
var expected = TestDataHelper.ReadEmbeddedResource(assembly, fileName ?? "template.json"); | ||
|
||
var nJson = JObject.Parse(json); | ||
var nOtherJson = JObject.Parse(expected); | ||
var equals = JToken.DeepEquals(nJson, nOtherJson); | ||
if (equals) return; | ||
expected.Should().BeEquivalentTo(json); | ||
} | ||
} | ||
} |