microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Tests/Microsoft.Teams.Common.Tests/Json/TrueTypeJsonAttributeTests.cs
77lines · modecode
| 1 | using System.Text.Json; |
| 2 | |
| 3 | using Microsoft.Teams.Common.Json; |
| 4 | |
| 5 | namespace Microsoft.Teams.Common.Tests.Json; |
| 6 | |
| 7 | [TrueTypeJson<IValidateEvent>] |
| 8 | public interface IValidateEvent |
| 9 | { |
| 10 | public string Id { get; } |
| 11 | public string Type { get; } |
| 12 | public object? Body { get; } |
| 13 | public DateTime SentAt { get; } |
| 14 | } |
| 15 | |
| 16 | public class ValidateEvent : IValidateEvent |
| 17 | { |
| 18 | public required string Id { get; set; } |
| 19 | public string Type { get; } = "test"; |
| 20 | public object? Body { get; set; } |
| 21 | public DateTime SentAt { get; set; } = DateTime.UtcNow; |
| 22 | } |
| 23 | |
| 24 | public class TrueTypeJsonAttributeTests |
| 25 | { |
| 26 | [Fact] |
| 27 | public void TrueTypeJsonAttribute_SetsConverterType() |
| 28 | { |
| 29 | var attr = new TrueTypeJsonAttribute<string>(); |
| 30 | Assert.Equal(typeof(TrueTypeJsonConverter<string>), attr.ConverterType); |
| 31 | } |
| 32 | |
| 33 | [Fact] |
| 34 | public void TrueTypeJsonAttribute_SetsConverterTypeObject() |
| 35 | { |
| 36 | var attr = new TrueTypeJsonAttribute<IValidateEvent>(); |
| 37 | Assert.Equal(typeof(TrueTypeJsonConverter<IValidateEvent>), attr.ConverterType); |
| 38 | } |
| 39 | |
| 40 | [Fact] |
| 41 | public void TrueTypeJsonConverter_Serialize() |
| 42 | { |
| 43 | // Arrange |
| 44 | var validateEvent = new ValidateEvent |
| 45 | { |
| 46 | Id = "bodyGuid" |
| 47 | }; |
| 48 | Assert.True(validateEvent is ValidateEvent); |
| 49 | |
| 50 | // Act |
| 51 | var json = JsonSerializer.Serialize(validateEvent); |
| 52 | // Assert |
| 53 | Assert.Contains("\"Id\":\"bodyGuid\"", json); |
| 54 | Assert.Contains("\"Type\":\"test\"", json); |
| 55 | Assert.Contains("\"SentAt\":\"", json); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | [Fact] |
| 60 | public void TrueTypeJsonConverter_Deserialize() |
| 61 | { |
| 62 | // Arrange |
| 63 | var validateEvent = new ValidateEvent() |
| 64 | { |
| 65 | Id = "guid", |
| 66 | SentAt = DateTime.UtcNow |
| 67 | }; |
| 68 | var json = JsonSerializer.Serialize<IValidateEvent>(validateEvent); |
| 69 | |
| 70 | // Act |
| 71 | var ex = Assert.Throws<System.NotImplementedException>(() => JsonSerializer.Deserialize<IValidateEvent>(json)); |
| 72 | // Assert |
| 73 | var expectedSubmitException = "The method or operation is not implemented."; |
| 74 | Assert.Equal(expectedSubmitException, ex.Message); |
| 75 | } |
| 76 | } |