Skip to content
This repository has been archived by the owner on Jun 1, 2024. It is now read-only.

Commit

Permalink
Template V7 test
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadaj committed May 29, 2019
1 parent 549f791 commit f62bc73
Showing 1 changed file with 66 additions and 0 deletions.
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);
}
}
}

0 comments on commit f62bc73

Please sign in to comment.