|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.IO; |
| 6 | +using System.Reflection; |
| 7 | +using System.Text.Json; |
| 8 | +using NUnit.Framework; |
| 9 | + |
| 10 | +namespace Azure.Security.KeyVault.Certificates.Tests |
| 11 | +{ |
| 12 | + public class ModelTests |
| 13 | + { |
| 14 | + [Test] |
| 15 | + public void ValidateCertificateIssuer() => |
| 16 | + AssertModel(() => new CertificateIssuer(nameof(CertificateIssuer.Name), nameof(CertificateIssuer.Provider))); |
| 17 | + |
| 18 | + [Test] |
| 19 | + public void ValidateCertificateContact() => |
| 20 | + AssertModel(() => new CertificateContact()); |
| 21 | + |
| 22 | + [Test] |
| 23 | + public void ValidateCertificatePolicy() => |
| 24 | + AssertModel(() => new CertificatePolicy()); |
| 25 | + |
| 26 | + [Test] |
| 27 | + public void ValidateIssueProperties() => |
| 28 | + AssertModel(() => new IssuerProperties("Name")); |
| 29 | + |
| 30 | + [Test] |
| 31 | + public void ValidateLifetimeAction() => |
| 32 | + AssertModel(() => new LifetimeAction(CertificatePolicyAction.AutoRenew)); |
| 33 | + |
| 34 | + #region Assertion helpers |
| 35 | + private static void AssertModel<T>(Func<T> factory) where T: IJsonSerializable, IJsonDeserializable |
| 36 | + { |
| 37 | + T obj = factory(); |
| 38 | + |
| 39 | + // Write intrinsic properties. |
| 40 | + foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) |
| 41 | + { |
| 42 | + if (property.CanWrite && ShouldSerialize(property, out var value)) |
| 43 | + { |
| 44 | + property.SetValue(obj, value); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Serialize. |
| 49 | + using MemoryStream ms = new(); |
| 50 | + using Utf8JsonWriter writer = new(ms); |
| 51 | + writer.WriteStartObject(); |
| 52 | + ((IJsonSerializable)obj).WriteProperties(writer); |
| 53 | + writer.WriteEndObject(); |
| 54 | + writer.Flush(); |
| 55 | + |
| 56 | + // Deserialize. |
| 57 | + ms.Seek(0, SeekOrigin.Begin); |
| 58 | + obj = factory(); |
| 59 | + using JsonDocument doc = JsonDocument.Parse(ms); |
| 60 | + ((IJsonDeserializable)obj).ReadProperties(doc.RootElement); |
| 61 | + |
| 62 | + // Assert intrinsic properties. |
| 63 | + foreach (PropertyInfo property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)) |
| 64 | + { |
| 65 | + // Check CanWrite to make sure we actually wrote it. |
| 66 | + if (property.CanWrite && property.CanRead && ShouldSerialize(property, out var value)) |
| 67 | + { |
| 68 | + Assert.AreEqual(value, property.GetValue(obj, null)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private static bool ShouldSerialize(PropertyInfo property, out object value) |
| 74 | + { |
| 75 | + if (property.PropertyType == typeof(string)) |
| 76 | + { |
| 77 | + value = property.Name; |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + if (property.PropertyType == typeof(int) || property.PropertyType == typeof(long)) |
| 82 | + { |
| 83 | + value = property.Name.GetHashCode(); |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + if (property.PropertyType == typeof(int?)) |
| 88 | + { |
| 89 | + value = new Nullable<int>(property.Name.GetHashCode()); |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + if (property.PropertyType == typeof(long?)) |
| 94 | + { |
| 95 | + value = new Nullable<long>(property.Name.GetHashCode()); |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + value = string.Empty; |
| 100 | + return false; |
| 101 | + } |
| 102 | + #endregion |
| 103 | + } |
| 104 | +} |
0 commit comments