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