|
| 1 | +using k8s.Tests.Mock; |
| 2 | +using System.Text.Json; |
| 3 | +using System.Text.Json.Serialization; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Xunit; |
| 6 | +using Xunit.Abstractions; |
| 7 | + |
| 8 | +namespace k8s.Tests |
| 9 | +{ |
| 10 | + public class SerializationTests |
| 11 | + { |
| 12 | + private readonly ITestOutputHelper testOutput; |
| 13 | + |
| 14 | + private enum Animals |
| 15 | + { |
| 16 | + Dog, |
| 17 | + Cat, |
| 18 | + Mouse, |
| 19 | + } |
| 20 | + |
| 21 | + public SerializationTests(ITestOutputHelper testOutput) |
| 22 | + { |
| 23 | + this.testOutput = testOutput; |
| 24 | + } |
| 25 | + |
| 26 | + [Fact] |
| 27 | + public async Task SerializeEnumUsingPascalCase() |
| 28 | + { |
| 29 | + using var server = new MockKubeApiServer(testOutput); |
| 30 | + |
| 31 | + var config = new KubernetesClientConfiguration { Host = server.Uri.ToString() }; |
| 32 | + config.AddJsonOptions(options => |
| 33 | + { |
| 34 | + // Insert the converter at the front of the list so it overrides any others. |
| 35 | + options.Converters.Insert(index: 0, new JsonStringEnumConverter()); |
| 36 | + }); |
| 37 | + var client = new Kubernetes(config); |
| 38 | + |
| 39 | + var customObject = Animals.Dog; |
| 40 | + |
| 41 | + var result = await client.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(customObject, "TestGroup", "TestVersion", "TestNamespace", "TestPlural").ConfigureAwait(false); |
| 42 | + var content = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 43 | + |
| 44 | + // Assert that the client serializes using the default options. |
| 45 | + Assert.Equal(@"""Dog""", content); |
| 46 | + |
| 47 | + // Assert that the underlying KubernetesJson serializes using the default options. |
| 48 | + string animal = KubernetesJson.Serialize(Animals.Cat); |
| 49 | + Assert.Equal(@"""Cat""", animal); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public async Task SerializeEnumUsingCamelCase() |
| 54 | + { |
| 55 | + using var server = new MockKubeApiServer(testOutput); |
| 56 | + |
| 57 | + var config = new KubernetesClientConfiguration { Host = server.Uri.ToString() }; |
| 58 | + config.AddJsonOptions(options => |
| 59 | + { |
| 60 | + // Insert the converter at the front of the list so it overrides |
| 61 | + // the default JsonStringEnumConverter without namingPolicy. |
| 62 | + options.Converters.Insert(index: 0, new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); |
| 63 | + }); |
| 64 | + var client = new Kubernetes(config); |
| 65 | + |
| 66 | + var customObject = Animals.Dog; |
| 67 | + |
| 68 | + var result = await client.CustomObjects.CreateNamespacedCustomObjectWithHttpMessagesAsync(customObject, "TestGroup", "TestVersion", "TestNamespace", "TestPlural").ConfigureAwait(false); |
| 69 | + var content = await result.Request.Content.ReadAsStringAsync().ConfigureAwait(false); |
| 70 | + |
| 71 | + // Assert that the client serializes using the specified options. |
| 72 | + Assert.Equal(@"""dog""", content); |
| 73 | + |
| 74 | + // Assert that the underlying KubernetesJson serializes using the default options. |
| 75 | + string animal = KubernetesJson.Serialize(Animals.Cat); |
| 76 | + Assert.Equal(@"""Cat""", animal); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments