Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix card serialization #977

Merged
merged 2 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/LondonTravel.Skill/Models/Card.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// Copyright (c) Martin Costello, 2017. All rights reserved.
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Text.Json.Serialization;

namespace MartinCostello.LondonTravel.Skill.Models;

[JsonDerivedType(typeof(LinkAccountCard))]
[JsonDerivedType(typeof(StandardCard))]
public abstract class Card
{
public abstract string Type { get; set; }
Expand Down
22 changes: 0 additions & 22 deletions test/LondonTravel.Skill.Tests/Samples/SkillResponse.json

This file was deleted.

93 changes: 88 additions & 5 deletions test/LondonTravel.Skill.Tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.

using System.Text.Json;
using MartinCostello.LondonTravel.Skill.Models;

namespace MartinCostello.LondonTravel.Skill;

Expand All @@ -25,17 +26,99 @@ public static async Task Can_Deserialize_Request(string name)
actual.Request.ShouldNotBeNull();
}

[Theory]
[InlineData("SkillResponse")]
public static async Task Can_Deserialize_Response(string name)
[Fact]
public static void Can_Serialize_Response_With_No_Card()
{
// Arrange
string json = await File.ReadAllTextAsync(Path.Combine("Samples", $"{name}.json"));
var response = new SkillResponse()
{
Response = new()
{
OutputSpeech = new()
{
Ssml = "<p>Hello, world!</p>",
},
},
};

// Act
var actual = JsonSerializer.Deserialize(json, AppJsonSerializerContext.Default.ResponseBody);
string actual = JsonSerializer.Serialize(response, AppJsonSerializerContext.Default.SkillResponse);

// Assert
actual.ShouldNotBeNull();
using var document = JsonDocument.Parse(actual);

document.RootElement
.GetProperty("response")
.TryGetProperty("card", out _)
.ShouldBeFalse();
}

[Fact]
public static void Can_Serialize_Response_With_Link_Account_Card()
{
// Arrange
var response = new SkillResponse()
{
Response = new()
{
Card = new LinkAccountCard(),
OutputSpeech = new()
{
Ssml = "<p>Hello, world!</p>",
},
},
};

// Act
string actual = JsonSerializer.Serialize(response, AppJsonSerializerContext.Default.SkillResponse);

// Assert
actual.ShouldNotBeNull();
using var document = JsonDocument.Parse(actual);

var card = document.RootElement
.GetProperty("response")
.GetProperty("card");

card.GetProperty("type").GetString().ShouldBe("LinkAccount");
card.EnumerateObject().Count().ShouldBe(1);
}

[Fact]
public static void Can_Serialize_Response_With_Standard_Card()
{
// Arrange
var response = new SkillResponse()
{
Response = new()
{
Card = new StandardCard()
{
Content = "Hello, world!",
Title = "Hello",
},
OutputSpeech = new()
{
Ssml = "<p>Hello, world!</p>",
},
},
};

// Act
string actual = JsonSerializer.Serialize(response, AppJsonSerializerContext.Default.SkillResponse);

// Assert
actual.ShouldNotBeNull();
using var document = JsonDocument.Parse(actual);

var card = document.RootElement
.GetProperty("response")
.GetProperty("card");

card.GetProperty("type").GetString().ShouldBe("Standard");
card.GetProperty("title").GetString().ShouldBe("Hello");
card.GetProperty("text").GetString().ShouldBe("Hello, world!");
card.EnumerateObject().Count().ShouldBe(3);
}
}