Files
csharp/tests/KubernetesClient.Tests/SerializationTests.cs
Boshi Lian f32cf9eab3 fix warnings for net8 (#1481)
* fix ca2007

* xUnit1031

* fix ca2007

* fix ca2007

* remove deprecated ctor

* fix xUnit1031

* fix missing doc

* fix missing dispose

* fix ex
2024-01-09 18:05:26 +01:00

80 lines
3.0 KiB
C#

using k8s.Tests.Mock;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace k8s.Tests
{
public class SerializationTests
{
private readonly ITestOutputHelper testOutput;
private enum Animals
{
Dog,
Cat,
Mouse,
}
public SerializationTests(ITestOutputHelper testOutput)
{
this.testOutput = testOutput;
}
[Fact]
public async Task SerializeEnumUsingPascalCase()
{
using var server = new MockKubeApiServer(testOutput);
var config = new KubernetesClientConfiguration { Host = server.Uri.ToString() };
config.AddJsonOptions(options =>
{
// Insert the converter at the front of the list so it overrides any others.
options.Converters.Insert(index: 0, new JsonStringEnumConverter());
});
var client = new Kubernetes(config);
var customObject = Animals.Dog;
var result = await client.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(customObject, "TestGroup", "TestVersion", "TestNamespace", "TestPlural").ConfigureAwait(true);
var content = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(true);
// Assert that the client serializes using the default options.
Assert.Equal(@"""Dog""", content);
// Assert that the underlying KubernetesJson serializes using the default options.
string animal = KubernetesJson.Serialize(Animals.Cat);
Assert.Equal(@"""Cat""", animal);
}
[Fact]
public async Task SerializeEnumUsingCamelCase()
{
using var server = new MockKubeApiServer(testOutput);
var config = new KubernetesClientConfiguration { Host = server.Uri.ToString() };
config.AddJsonOptions(options =>
{
// Insert the converter at the front of the list so it overrides
// the default JsonStringEnumConverter without namingPolicy.
options.Converters.Insert(index: 0, new JsonStringEnumConverter(JsonNamingPolicy.CamelCase));
});
var client = new Kubernetes(config);
var customObject = Animals.Dog;
var result = await client.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(customObject, "TestGroup", "TestVersion", "TestNamespace", "TestPlural").ConfigureAwait(true);
var content = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(true);
// Assert that the client serializes using the specified options.
Assert.Equal(@"""dog""", content);
// Assert that the underlying KubernetesJson serializes using the default options.
string animal = KubernetesJson.Serialize(Animals.Cat);
Assert.Equal(@"""Cat""", animal);
}
}
}