From cf340f16d1cb56ef5456319eebdbbfa50b144b20 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Wed, 30 Aug 2023 15:28:44 -0700 Subject: [PATCH] Use raw string literals to make json easier to read in tests. --- ...CommonSchemaJsonSerializationStateTests.cs | 10 ++--- ...ogRecordCommonSchemaJsonSerializerTests.cs | 38 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/test/OpenTelemetry.Exporter.OneCollector.Tests/CommonSchemaJsonSerializationStateTests.cs b/test/OpenTelemetry.Exporter.OneCollector.Tests/CommonSchemaJsonSerializationStateTests.cs index 4e2083bd42..cc9cad8eb1 100644 --- a/test/OpenTelemetry.Exporter.OneCollector.Tests/CommonSchemaJsonSerializationStateTests.cs +++ b/test/OpenTelemetry.Exporter.OneCollector.Tests/CommonSchemaJsonSerializationStateTests.cs @@ -50,7 +50,7 @@ public void AddExtensionAttributeTest() var json = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal( - "{\"ext\":{\"something\":{\"field1\":1,\"field2\":2,\"field3\":3,\"field4\":6},\"food\":{\"field1\":4,\"field2\":5}}}", + """{"ext":{"something":{"field1":1,"field2":2,"field3":3,"field4":6},"food":{"field1":4,"field2":5}}}""", json); stream.SetLength(0); @@ -74,7 +74,7 @@ public void AddExtensionAttributeTest() json = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal( - "{\"ext\":{\"something\":{\"field1\":1},\"food\":{\"field1\":1}}}", + """{"ext":{"something":{"field1":1},"food":{"field1":1}}}""", json); } @@ -107,7 +107,7 @@ public void AddExtensionAttributeWithComplexTypeTest() var json = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal( - "{\"ext\":{\"metadata\":{\"f\":{\"Id\":1234}}}}", + """{"ext":{"metadata":{"f":{"Id":1234}}}}""", json); } @@ -135,7 +135,7 @@ public void AddExtensionAttributeDuplicatesTest() var json = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal( - "{\"ext\":{\"something\":{\"field1\":1,\"field1\":2}}}", + """{"ext":{"something":{"field1":1,"field1":2}}}""", json); } @@ -177,7 +177,7 @@ public void AddExtensionAttributeMixedFieldAndPropertyTest() var json = Encoding.UTF8.GetString(stream.ToArray()); Assert.Equal( - "{\"ext\":{\"foo\":{\"foo_field1\":1,\"foo.field2\":2,\"foo_field3\":3},\"bar\":{\"bar_field1\":1,\"bar.field2\":2,\"bar_field3\":3}}}", + """{"ext":{"foo":{"foo_field1":1,"foo.field2":2,"foo_field3":3},"bar":{"bar_field1":1,"bar.field2":2,"bar_field3":3}}}""", json); } diff --git a/test/OpenTelemetry.Exporter.OneCollector.Tests/LogRecordCommonSchemaJsonSerializerTests.cs b/test/OpenTelemetry.Exporter.OneCollector.Tests/LogRecordCommonSchemaJsonSerializerTests.cs index cabe61439a..d410a7be08 100644 --- a/test/OpenTelemetry.Exporter.OneCollector.Tests/LogRecordCommonSchemaJsonSerializerTests.cs +++ b/test/OpenTelemetry.Exporter.OneCollector.Tests/LogRecordCommonSchemaJsonSerializerTests.cs @@ -32,7 +32,7 @@ public void EmptyLogRecordJsonTest() string json = GetLogRecordJson(1, (index, logRecord) => { }); Assert.Equal( - "{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n", json); } @@ -42,8 +42,8 @@ public void MultipleEmptyLogRecordJsonTest() string json = GetLogRecordJson(2, (index, logRecord) => { }); Assert.Equal( - "{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n" - + "{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n" + + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n", json); } @@ -63,7 +63,7 @@ public void LogRecordLogLevelJsonTest(LogLevel logLevel, string severityText, in }); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"{severityText}\",\"severityNumber\":{severityNumber}}}}}\n", + $$$"""{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"{{{severityText}}}","severityNumber":{{{severityNumber}}}}}""" + "\n", json); } @@ -79,7 +79,7 @@ public void LogRecordCategoryNameAndEventNameJsonTest(string categoryName, strin }); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"{categoryName}.{eventName ?? "Name"}\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1}}}}\n", + $$$"""{"ver":"4.0","name":"{{{categoryName}}}.{{{eventName ?? "Name"}}}","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n", json); } @@ -92,7 +92,7 @@ public void LogRecordEventIdJsonTest() }); Assert.Equal( - "{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"eventId\":18,\"severityText\":\"Trace\",\"severityNumber\":1}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"eventId":18,"severityText":"Trace","severityNumber":1}}""" + "\n", json); } @@ -105,7 +105,7 @@ public void LogRecordTimestampJsonTest() }); Assert.Equal( - "{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2023-01-18T10:18:00Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2023-01-18T10:18:00Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1}}""" + "\n", json); } @@ -119,7 +119,7 @@ public void LogRecordOriginalFormatBodyJsonTest() }); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"body\":\"hello world\",\"formattedMessage\":\"goodbye world\"}}}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"body":"hello world","formattedMessage":"goodbye world"}}""" + "\n", json); } @@ -133,7 +133,7 @@ public void LogRecordBodyJsonTest() }); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"body\":\"hello world\",\"formattedMessage\":\"goodbye world\"}}}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"body":"hello world","formattedMessage":"goodbye world"}}""" + "\n", json); } @@ -146,7 +146,7 @@ public void LogRecordFormattedMessageBodyJsonTest() }); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"body\":\"goodbye world\",\"formattedMessage\":\"goodbye world\"}}}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"body":"goodbye world","formattedMessage":"goodbye world"}}""" + "\n", json); } @@ -164,7 +164,7 @@ public void LogRecordResourceJsonTest() string json = GetLogRecordJson(1, (index, logRecord) => { }, resource); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"resourceKey1\":\"resourceValue1\",\"resourceKey2\":\"resourceValue2\"}}}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"resourceKey1":"resourceValue1","resourceKey2":"resourceValue2"}}""" + "\n", json); } @@ -178,7 +178,7 @@ public void LogRecordScopesJsonTest() string json = GetLogRecordJson(1, (index, logRecord) => { }, scopeProvider: scopeProvider); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"scope1Key1\":\"scope1Value1\",\"scope1Key2\":\"scope1Value2\",\"scope2Key1\":\"scope2Value1\"}}}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"scope1Key1":"scope1Value1","scope1Key2":"scope1Value2","scope2Key1":"scope2Value1"}}""" + "\n", json); } @@ -191,7 +191,7 @@ public void LogRecordStateValuesJsonTest() }); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1,\"stateKey1\":\"stateValue1\",\"stateKey2\":\"stateValue2\"}}}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1,"stateKey1":"stateValue1","stateKey2":"stateValue2"}}""" + "\n", json); } @@ -209,7 +209,9 @@ public void LogRecordTraceContextJsonTest() }); Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1}},\"ext\":{{\"dt\":{{\"traceId\":\"{traceId}\",\"spanId\":\"{spanId}\",\"traceFlags\":1}}}}}}\n", + $$$$""" + {"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1},"ext":{"dt":{"traceId":"{{{{traceId}}}}","spanId":"{{{{spanId}}}}","traceFlags":1}}} + """ + "\n", json); } @@ -227,11 +229,13 @@ public void LogRecordExceptionJsonTest(bool includeStackTraceAsString) includeStackTraceAsString: includeStackTraceAsString); var stackJson = includeStackTraceAsString - ? $",\"stack\":\"System.InvalidOperationException: Operation is not valid due to the current state of the object.\"" + ? ",\"stack\":\"System.InvalidOperationException: Operation is not valid due to the current state of the object.\"" : string.Empty; Assert.Equal( - $"{{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{{\"severityText\":\"Trace\",\"severityNumber\":1}},\"ext\":{{\"ex\":{{\"type\":\"System.InvalidOperationException\",\"msg\":\"Operation is not valid due to the current state of the object.\"{stackJson}}}}}}}\n", + $$$$""" + {"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1},"ext":{"ex":{"type":"System.InvalidOperationException","msg":"Operation is not valid due to the current state of the object."{{{{stackJson}}}}}}} + """ + "\n", json); } @@ -258,7 +262,7 @@ public void LogRecordExtensionsJsonTest() scopeProvider); Assert.Equal( - "{\"ver\":\"4.0\",\"name\":\"Namespace.Name\",\"time\":\"2032-01-18T10:11:12Z\",\"iKey\":\"o:tenant-token\",\"data\":{\"severityText\":\"Trace\",\"severityNumber\":1},\"ext\":{\"state\":{\"field\":\"stateValue1\"},\"resource\":{\"field\":\"resourceValue1\"},\"scope\":{\"field\":\"scopeValue1\"}}}\n", + """{"ver":"4.0","name":"Namespace.Name","time":"2032-01-18T10:11:12Z","iKey":"o:tenant-token","data":{"severityText":"Trace","severityNumber":1},"ext":{"state":{"field":"stateValue1"},"resource":{"field":"resourceValue1"},"scope":{"field":"scopeValue1"}}}""" + "\n", json); }