|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using FluentAssertions; |
| 4 | +using Sentry.Android.Extensions; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace Sentry.Tests.Platforms.Android; |
| 8 | + |
| 9 | +public class JsonExtensionsTests |
| 10 | + |
| 11 | +{ |
| 12 | + [Fact] |
| 13 | + public void ToJavaSentryEvent_Success() |
| 14 | + |
| 15 | + { |
| 16 | + var evt = new SentryEvent(new Exception("Test Exception")); |
| 17 | + |
| 18 | + evt.Level = SentryLevel.Debug; |
| 19 | + evt.ServerName = "test server name"; |
| 20 | + evt.Distribution = "test distribution"; |
| 21 | + evt.Logger = "test logger"; |
| 22 | + evt.Release = "test release"; |
| 23 | + evt.Environment = "test environment"; |
| 24 | + evt.TransactionName = "test transaction name"; |
| 25 | + evt.Message = new SentryMessage |
| 26 | + { |
| 27 | + Params = ["Test"] |
| 28 | + }; |
| 29 | + |
| 30 | + evt.SetTag("TestTagKey", "TestTagValue"); |
| 31 | + evt.AddBreadcrumb(new Breadcrumb("test breadcrumb", "test type")); |
| 32 | + evt.SetExtra("TestExtraKey", "TestExtraValue"); |
| 33 | + evt.User = new SentryUser |
| 34 | + { |
| 35 | + Id = "user id", |
| 36 | + Username = "test", |
| 37 | + |
| 38 | + IpAddress = "127.0.0.1" |
| 39 | + }; |
| 40 | + |
| 41 | + var native = evt.ToJavaSentryEvent(new SentryOptions(), new JavaSdk.SentryOptions()); |
| 42 | + |
| 43 | + AssertEqual(evt, native); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void ToSentryEvent_ConvertToManaged() |
| 49 | + { |
| 50 | + var native = new JavaSdk.SentryEvent(); |
| 51 | + |
| 52 | + native.Throwable = new Exception("Test Exception").ToThrowable(); |
| 53 | + native.Timestamp = DateTimeOffset.UtcNow.ToJavaDate(); |
| 54 | + native.Level = JavaSdk.SentryLevel.Debug; |
| 55 | + native.ServerName = "native server name"; |
| 56 | + native.Dist = "native dist"; |
| 57 | + native.Logger = "native logger"; |
| 58 | + native.Release = "native release"; |
| 59 | + native.Environment = "native env"; |
| 60 | + native.Transaction = "native transaction"; |
| 61 | + native.Message = new JavaSdk.Protocol.Message |
| 62 | + { |
| 63 | + Params = ["Test"] |
| 64 | + }; |
| 65 | + native.SetTag("TestTagKey", "TestTagValue"); |
| 66 | + native.SetExtra("TestExtraKey", "TestExtraValue"); |
| 67 | + native.Breadcrumbs = |
| 68 | + [ |
| 69 | + new JavaSdk.Breadcrumb |
| 70 | + { |
| 71 | + Category = "category", |
| 72 | + Level = JavaSdk.SentryLevel.Debug |
| 73 | + } |
| 74 | + ]; |
| 75 | + |
| 76 | + native.User = new JavaSdk.Protocol.User |
| 77 | + { |
| 78 | + Id = "user id", |
| 79 | + Username = "test", |
| 80 | + |
| 81 | + IpAddress = "127.0.0.1" |
| 82 | + }; |
| 83 | + |
| 84 | + var managed = native.ToSentryEvent(new JavaSdk.SentryOptions()); |
| 85 | + |
| 86 | + AssertEqual(managed, native); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + private static void AssertEqual(SentryEvent managed, JavaSdk.SentryEvent native) |
| 91 | + { |
| 92 | + native.ServerName.Should().Be(managed.ServerName, "Server Name"); |
| 93 | + native.Dist.Should().Be(managed.Distribution, "Distribution"); |
| 94 | + native.Logger.Should().Be(managed.Logger, "Logger"); |
| 95 | + native.Release.Should().Be(managed.Release, "Release"); |
| 96 | + native.Environment.Should().Be(managed.Environment, "Environment"); |
| 97 | + native.Transaction.Should().Be(managed.TransactionName!, "Transaction"); |
| 98 | + native.Level!.ToString().ToUpper().Should().Be(managed.Level!.ToString()!.ToUpper(), "Level"); |
| 99 | + // native.Throwable.Message.Should().Be(managed.Exception!.Message, "Message should match"); |
| 100 | + |
| 101 | + // extras |
| 102 | + native.Extras.Should().NotBeNull("No extras found"); |
| 103 | + native.Extras!.Count.Should().Be(1, "Extras should have 1 item"); |
| 104 | + native.Extras!.Keys!.First().Should().Be(managed.Extra.Keys.First(), "Extras key should match"); |
| 105 | + native.Extras!.Values!.First().ToString().Should().Be(managed.Extra.Values.First().ToString(), "Extra value should match"); |
| 106 | + |
| 107 | + // tags |
| 108 | + native.Tags.Should().NotBeNull("No tags found"); |
| 109 | + native.Tags!.Count.Should().Be(1, "Tags should have 1 item"); |
| 110 | + native.Tags!.Keys!.First().Should().Be(managed.Tags.Keys.First()); |
| 111 | + native.Tags!.Values!.First().Should().Be(managed.Tags.Values.First()); |
| 112 | + |
| 113 | + // breadcrumbs |
| 114 | + native.Breadcrumbs.Should().NotBeNull("No breadcrumbs found"); |
| 115 | + var nb = native.Breadcrumbs!.First(); |
| 116 | + var mb = managed.Breadcrumbs!.First(); |
| 117 | + nb.Message.Should().Be(mb.Message, "Breadcrumb message"); |
| 118 | + nb.Type.Should().Be(mb.Type, "Breadcrumb type"); |
| 119 | + |
| 120 | + // user |
| 121 | + native.User!.Id.Should().Be(managed.User.Id, "UserId should match"); |
| 122 | + native.User.Email.Should().Be(managed.User.Email, "Email should match"); |
| 123 | + native.User.Username.Should().Be(managed.User.Username, "Username should match"); |
| 124 | + native.User.IpAddress.Should().Be(managed.User.IpAddress, "IpAddress should match"); |
| 125 | + } |
| 126 | +} |
0 commit comments