microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/sub-pr-199

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

Tests/Microsoft.Teams.Api.Tests/Entities/EntitiesTests.cs

69lines · modecode

1using System.Text.Json;
2
3using Microsoft.Teams.Api.Entities;
4
5namespace Microsoft.Teams.Api.Tests.Entities;
6
7public class EntitiesTests
8{
9 private static IEntity? DeserializeEntity(string json) => JsonSerializer.Deserialize<IEntity>(json);
10 private static Entity? DeserializeBase(string json) => JsonSerializer.Deserialize<Entity>(json);
11 private static OMessageEntity? DeserializeOMessage(string json) => JsonSerializer.Deserialize<OMessageEntity>(json);
12
13 [Fact]
14 public void Entity_MissingType_Throws()
15 {
16 var json = "{}";
17 var ex = Assert.Throws<JsonException>(() => DeserializeEntity(json));
18 Assert.Contains("entity must have a 'type'", ex.Message);
19 }
20
21 [Fact]
22 public void Entity_NullType_Throws()
23 {
24 var json = "{\"type\":null}";
25 var ex = Assert.Throws<JsonException>(() => DeserializeEntity(json));
26 Assert.Contains("failed to deserialize entity 'type' property", ex.Message);
27 }
28
29 // Base Entity converter path
30 [Fact]
31 public void BaseEntity_Message_DispatchesToMessageEntity()
32 {
33 var json = "{\"type\":\"message\"}";
34 var entity = DeserializeBase(json);
35 Assert.NotNull(entity);
36 }
37
38 [Fact]
39 public void OMessage_MissingOType_Throws()
40 {
41 var json = "{\"type\":\"https://schema.org/Message\"}";
42 var ex = Assert.Throws<JsonException>(() => DeserializeOMessage(json));
43 Assert.Contains("must have a '@type'", ex.Message);
44 }
45
46 [Fact]
47 public void OMessage_NullOType_Throws()
48 {
49 var json = "{\"type\":\"https://schema.org/Message\",\"@type\":null}";
50 var ex = Assert.Throws<JsonException>(() => DeserializeOMessage(json));
51 Assert.Contains("failed to deserialize 'https://schema.org/Message' entity '@type' property", ex.Message);
52 }
53
54 [Fact]
55 public void OMessage_UnknownOType_Throws()
56 {
57 var json = "{\"type\":\"https://schema.org/Message\",\"@type\":\"Other\"}";
58 var ex = Assert.Throws<JsonException>(() => DeserializeOMessage(json));
59 Assert.Contains("doesn't match any known types", ex.Message);
60 }
61
62 [Fact]
63 public void Entity_JsonSerialize_Class()
64 {
65 var a = JsonSerializer.Deserialize<Entity>(@"{ ""type"": ""unknown"", ""hello"": ""world"" }");
66 var b = JsonSerializer.Deserialize<IEntity>(@"{ ""type"": ""unknown"", ""hello"": ""world"" }");
67 Assert.Equal(a?.ToString(), b?.ToString());
68 }
69}