diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeTextMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserialize200-response.json similarity index 86% rename from Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeTextMessage-response.json rename to Vonage.Test/Conversations/GetEvent/Data/ShouldDeserialize200-response.json index dbc3cc43..39db6beb 100644 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeTextMessage-response.json +++ b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserialize200-response.json @@ -13,7 +13,10 @@ "name": "my_user_name", "display_name": "My User Name", "image_url": "https://example.com/image.png", - "custom_data": {} + "custom_data": { + "field_1": "value_1", + "field_2": "value_2" + } }, "from_member": { "id": "string" diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeAudioMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeAudioMessage-response.json deleted file mode 100644 index 3bd31dee..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeAudioMessage-response.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "audio", - "audio": { - "url": "https://example.com/audio.mp3" - } - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeCustomMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeCustomMessage-response.json deleted file mode 100644 index b23eab7a..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeCustomMessage-response.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "custom", - "custom": {} - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeFileMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeFileMessage-response.json deleted file mode 100644 index 67f55ac4..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeFileMessage-response.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "file", - "file": { - "url": "https://example.com/file.txt" - } - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeImageMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeImageMessage-response.json deleted file mode 100644 index 4221eb88..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeImageMessage-response.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "image", - "image": { - "url": "https://example.com/image.png" - } - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeLocationMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeLocationMessage-response.json deleted file mode 100644 index fc1e7856..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeLocationMessage-response.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "location", - "location": { - "longitude": "Longitude", - "latitude": "Latitude", - "name": "Name", - "address": "Address" - } - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeRandomMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeRandomMessage-response.json deleted file mode 100644 index 21304b26..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeRandomMessage-response.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "random" - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeTemplateMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeTemplateMessage-response.json deleted file mode 100644 index 6b6446d0..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeTemplateMessage-response.json +++ /dev/null @@ -1,34 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "template", - "template": { - "name": "Template", - "parameters": [], - "whatsapp": { - "policy": "deterministic", - "locale": "en-US" - } - } - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeVcardMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeVcardMessage-response.json deleted file mode 100644 index 705a4f43..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeVcardMessage-response.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "vcard", - "vcard": { - "url": "https://example.com/file.txt" - }, - "image": { - "url": "https://example.com/image.png" - } - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeVideoMessage-response.json b/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeVideoMessage-response.json deleted file mode 100644 index 7a48762f..00000000 --- a/Vonage.Test/Conversations/GetEvent/Data/ShouldDeserializeVideoMessage-response.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "id": 100, - "type": "message", - "from": "string", - "body": { - "message_type": "video", - "video": { - "url": "https://example.com/video.mkv" - } - }, - "timestamp": "2020-01-01T14:00:00.00Z", - "_embedded": { - "from_user": { - "id": "USR-82e028d9-5201-4f1e-8188-604b2d3471ec", - "name": "my_user_name", - "display_name": "My User Name", - "image_url": "https://example.com/image.png", - "custom_data": {} - }, - "from_member": { - "id": "string" - } - }, - "_links": { - "self": { - "href": "https://api.nexmo.com/v0.1/conversations/CON-1234/events/100" - } - } -} \ No newline at end of file diff --git a/Vonage.Test/Conversations/GetEvent/SerializationTest.cs b/Vonage.Test/Conversations/GetEvent/SerializationTest.cs index cac9c3a7..1da8e564 100644 --- a/Vonage.Test/Conversations/GetEvent/SerializationTest.cs +++ b/Vonage.Test/Conversations/GetEvent/SerializationTest.cs @@ -1,10 +1,12 @@ using System; +using FluentAssertions; using Vonage.Common; using Vonage.Conversations; using Vonage.Serialization; using Vonage.Test.Common; using Vonage.Test.Common.Extensions; using Xunit; +using JsonSerializer = System.Text.Json.JsonSerializer; namespace Vonage.Test.Conversations.GetEvent; @@ -16,75 +18,23 @@ public class SerializationTest JsonSerializerBuilder.BuildWithSnakeCase()); [Fact] - public void ShouldDeserializeTextMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess(BuildExpectedEvent(new EventBodyTextMessage("string"))); - - private static Event BuildExpectedEvent(EventBodyBase body) => new Event(100, "message", "string", body, - DateTimeOffset.Parse("2020-01-01T14:00:00.00Z"), - new EmbeddedEventData( - new EmbeddedEventUser("USR-82e028d9-5201-4f1e-8188-604b2d3471ec", "my_user_name", "My User Name", - "https://example.com/image.png", new { }), new EmbeddedEventMember("string")), - new Links(new HalLink(new Uri("https://api.nexmo.com/v0.1/conversations/CON-1234/events/100")))); - - [Fact] - public void ShouldDeserializeImageMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess( - BuildExpectedEvent(new EventBodyImageMessage(new EventBodyImageUrl("https://example.com/image.png")))); - - [Fact] - public void ShouldDeserializeAudioMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess( - BuildExpectedEvent(new EventBodyAudioMessage(new EventBodyAudioUrl("https://example.com/audio.mp3")))); - - [Fact] - public void ShouldDeserializeVideoMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess( - BuildExpectedEvent(new EventBodyVideoMessage(new EventBodyVideoUrl("https://example.com/video.mkv")))); - - [Fact] - public void ShouldDeserializeFileMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess(BuildExpectedEvent(new EventBodyFileMessage(new EventBodyFileUrl("https://example.com/file.txt")))); - - [Fact] - public void ShouldDeserializeTemplateMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess(BuildExpectedEvent(new EventBodyTemplateMessage("Template", Array.Empty(), - new EventBodyTemplateWhatsApp("Deterministic", "en-US")))); - - [Fact] - public void ShouldDeserializeCustomMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess(BuildExpectedEvent(new EventBodyCustomMessage(new { }))); - - [Fact] - public void ShouldDeserializeVcardMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess(BuildExpectedEvent(new EventBodyVcardMessage(new EventBodyVcardUrl("https://example.com/file.txt"), - new EventBodyImageUrl("https://example.com/image.png")))); - - [Fact] - public void ShouldDeserializeLocationMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess(BuildExpectedEvent( - new EventBodyLocationMessage(new EventBodyLocation("Longitude", "Latitude", "Name", "Address")))); - - [Fact] - public void ShouldDeserializeRandomMessage() => this.helper.Serializer - .DeserializeObject(this.helper.GetResponseJson()) - .Should() - .BeSuccess(BuildExpectedEvent(new EventBodyRandomMessage())); + public void ShouldDeserialize200() => this.helper.Serializer + .DeserializeObject(this.helper.GetResponseJson()) + .Should() + .BeSuccess(success => + { + success.Id.Should().Be(100); + success.Type.Should().Be("message"); + success.From.Should().Be("string"); + success.Body.Should().Be(JsonSerializer.SerializeToElement(new {message_type = "text", text = "string"})); + success.Embedded.Member.Should().Be(new EmbeddedEventMember("string")); + success.Embedded.User.Id.Should().Be("USR-82e028d9-5201-4f1e-8188-604b2d3471ec"); + success.Embedded.User.Name.Should().Be("my_user_name"); + success.Embedded.User.DisplayName.Should().Be("My User Name"); + success.Embedded.User.ImageUrl.Should().Be("https://example.com/image.png"); + success.Embedded.User.CustomData.Should() + .Be(JsonSerializer.SerializeToElement(new {field_1 = "value_1", field_2 = "value_2"})); + success.Links.Should() + .Be(new Links(new HalLink(new Uri("https://api.nexmo.com/v0.1/conversations/CON-1234/events/100")))); + }); } \ No newline at end of file diff --git a/Vonage.Test/Vonage.Test.csproj b/Vonage.Test/Vonage.Test.csproj index 8f018385..9743393b 100644 --- a/Vonage.Test/Vonage.Test.csproj +++ b/Vonage.Test/Vonage.Test.csproj @@ -1197,34 +1197,7 @@ PreserveNewest - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - - PreserveNewest - - + PreserveNewest diff --git a/Vonage/Conversations/Event.cs b/Vonage/Conversations/Event.cs index da54d3b0..c4104638 100644 --- a/Vonage/Conversations/Event.cs +++ b/Vonage/Conversations/Event.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json; using System.Text.Json.Serialization; #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member @@ -9,7 +10,7 @@ public record Event( int Id, string Type, string From, - EventBodyBase Body, + JsonElement Body, DateTimeOffset Timestamp, [property: JsonPropertyName("_embedded")] EmbeddedEventData Embedded, @@ -28,41 +29,6 @@ public record EmbeddedEventUser( string DisplayName, [property: JsonPropertyName("image_url")] string ImageUrl, - object CustomData); + JsonElement CustomData); -public record EmbeddedEventMember(string Id); - -[JsonPolymorphic(TypeDiscriminatorPropertyName = "message_type")] -[JsonDerivedType(typeof(EventBodyBase))] -[JsonDerivedType(typeof(EventBodyTextMessage), "text")] -[JsonDerivedType(typeof(EventBodyImageMessage), "image")] -[JsonDerivedType(typeof(EventBodyAudioMessage), "audio")] -[JsonDerivedType(typeof(EventBodyVideoMessage), "video")] -[JsonDerivedType(typeof(EventBodyFileMessage), "file")] -[JsonDerivedType(typeof(EventBodyTemplateMessage), "template")] -[JsonDerivedType(typeof(EventBodyCustomMessage), "custom")] -[JsonDerivedType(typeof(EventBodyVcardMessage), "vcard")] -[JsonDerivedType(typeof(EventBodyLocationMessage), "location")] -[JsonDerivedType(typeof(EventBodyRandomMessage), "random")] -public record EventBodyBase; - -public record EventBodyTextMessage(string Text) : EventBodyBase; -public record EventBodyImageMessage(EventBodyImageUrl Image) : EventBodyBase; -public record EventBodyImageUrl(string Url); -public record EventBodyAudioMessage(EventBodyAudioUrl Audio) : EventBodyBase; -public record EventBodyAudioUrl(string Url); -public record EventBodyVideoMessage(EventBodyVideoUrl Video) : EventBodyBase; -public record EventBodyVideoUrl(string Url); -public record EventBodyFileMessage(EventBodyFileUrl File) : EventBodyBase; -public record EventBodyFileUrl(string Url); - -public record EventBodyTemplateMessage(string Name, object[] Parameters, EventBodyTemplateWhatsApp WhatsApp) - : EventBodyBase; - -public record EventBodyTemplateWhatsApp(string Policy, string Locale); -public record EventBodyCustomMessage(object Custom) : EventBodyBase; -public record EventBodyVcardMessage(EventBodyVcardUrl Vcard, EventBodyImageUrl Image) : EventBodyBase; -public record EventBodyVcardUrl(string Url); -public record EventBodyLocationMessage(EventBodyLocation File) : EventBodyBase; -public record EventBodyLocation(string Longitude, string Latitude, string Name, string Address); -public record EventBodyRandomMessage : EventBodyBase; \ No newline at end of file +public record EmbeddedEventMember(string Id); \ No newline at end of file