Skip to content

Commit

Permalink
Added unit test to prove out enum serialization working as expected d…
Browse files Browse the repository at this point in the history
…uring event publish (dapr#1174)

Signed-off-by: Whit Waldo <[email protected]>

Co-authored-by: halspang <[email protected]>
  • Loading branch information
WhitWaldo and halspang authored Nov 28, 2023
1 parent bb3f97a commit e435efd
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/Dapr.Client.Test/PublishEventApiTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
// limitations under the License.
// ------------------------------------------------------------------------

using System.Collections.Immutable;
using System.Linq;
using System.Net.Http;
using System.Text.Json.Serialization;
using Grpc.Net.Client;

namespace Dapr.Client.Test
{
using System;
Expand Down Expand Up @@ -51,6 +57,44 @@ public async Task PublishEventAsync_CanPublishTopicWithData()
envelope.Metadata.Count.Should().Be(0);
}

[Fact]
public async Task PublishEvent_ShouldRespectJsonStringEnumConverter()
{
//The following mimics how the TestClient is built, but adds the JsonStringEnumConverter to the serialization options
var handler = new TestClient.CapturingHandler();
var httpClient = new HttpClient(handler);
var clientBuilder = new DaprClientBuilder()
.UseJsonSerializationOptions(new JsonSerializerOptions()
{
Converters = {new JsonStringEnumConverter(null, false)}
})
.UseHttpClientFactory(() => httpClient)
.UseGrpcChannelOptions(new GrpcChannelOptions()
{
HttpClient = httpClient, ThrowOperationCanceledOnCancellation = true
});
var client = new TestClient<DaprClient>(clientBuilder.Build(), handler);

//Ensure that the JsonStringEnumConverter is registered
client.InnerClient.JsonSerializerOptions.Converters.Count.Should().Be(1);
client.InnerClient.JsonSerializerOptions.Converters.First().GetType().Name.Should()
.Match(nameof(JsonStringEnumConverter));

var publishData = new Widget {Size = "Large", Color = WidgetColor.Red};
var request = await client.CaptureGrpcRequestAsync(async daprClient =>
{
await daprClient.PublishEventAsync<Widget>(TestPubsubName, "test", publishData);
});

request.Dismiss();

var envelope = await request.GetRequestEnvelopeAsync<PublishEventRequest>();
var jsonFromRequest = envelope.Data.ToStringUtf8();
jsonFromRequest.Should()
.Be(JsonSerializer.Serialize(publishData, client.InnerClient.JsonSerializerOptions));
jsonFromRequest.Should().Match("{\"Size\":\"Large\",\"Color\":\"Red\"}");
}

[Fact]
public async Task PublishEventAsync_CanPublishTopicWithData_WithMetadata()
{
Expand Down Expand Up @@ -259,5 +303,18 @@ private class PublishData
{
public string PublishObjectParameter { get; set; }
}

private class Widget
{
public string Size { get; set; }
public WidgetColor Color { get; set; }
}

private enum WidgetColor
{
Red,
Green,
Yellow
}
}
}

0 comments on commit e435efd

Please sign in to comment.