|
| 1 | +using Adyen.Checkout.Client; |
| 2 | +using Adyen.Checkout.Extensions; |
| 3 | +using Adyen.Checkout.Models; |
| 4 | +using Adyen.Core.Options; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using Microsoft.Extensions.Hosting; |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | +using System.Text.Json; |
| 9 | + |
| 10 | +namespace Adyen.Test.Checkout |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Tests for the IEnum pattern applied to generated Checkout models: |
| 14 | + /// - Result (standalone IEnum, generated by modelEnum.mustache) |
| 15 | + /// - PixPayByBankDetails (model with optional inner TypeEnum, generated by modelInnerEnum.mustache) |
| 16 | + /// </summary> |
| 17 | + [TestClass] |
| 18 | + public class IEnumCheckoutTest |
| 19 | + { |
| 20 | + private readonly JsonSerializerOptionsProvider _jsonSerializerOptionsProvider; |
| 21 | + |
| 22 | + public IEnumCheckoutTest() |
| 23 | + { |
| 24 | + IHost testHost = Host.CreateDefaultBuilder() |
| 25 | + .ConfigureCheckout((context, services, config) => |
| 26 | + { |
| 27 | + config.ConfigureAdyenOptions(options => |
| 28 | + { |
| 29 | + options.Environment = AdyenEnvironment.Test; |
| 30 | + }); |
| 31 | + }) |
| 32 | + .Build(); |
| 33 | + |
| 34 | + _jsonSerializerOptionsProvider = testHost.Services.GetRequiredService<JsonSerializerOptionsProvider>(); |
| 35 | + } |
| 36 | + |
| 37 | + // ── Result: standalone IEnum ────────────────────────────────────────────── |
| 38 | + |
| 39 | + [TestMethod] |
| 40 | + public void Given_Result_When_DeserializeKnownValue_Then_ReturnsStaticInstance() |
| 41 | + { |
| 42 | + Result? result = JsonSerializer.Deserialize<Result>("\"VALID\"", _jsonSerializerOptionsProvider.Options); |
| 43 | + Assert.AreEqual(Result.VALID, result); |
| 44 | + Assert.AreEqual("VALID", result?.Value); |
| 45 | + } |
| 46 | + |
| 47 | + [TestMethod] |
| 48 | + public void Given_Result_When_DeserializeAllKnownValues_Then_EachMatchesStaticInstance() |
| 49 | + { |
| 50 | + var opts = _jsonSerializerOptionsProvider.Options; |
| 51 | + Assert.AreEqual(Result.VALID, JsonSerializer.Deserialize<Result>("\"VALID\"", opts)); |
| 52 | + Assert.AreEqual(Result.INVALID, JsonSerializer.Deserialize<Result>("\"INVALID\"", opts)); |
| 53 | + Assert.AreEqual(Result.UNKNOWN, JsonSerializer.Deserialize<Result>("\"UNKNOWN\"", opts)); |
| 54 | + Assert.AreEqual(Result.NOTREQUIRED, JsonSerializer.Deserialize<Result>("\"NOT_REQUIRED\"", opts)); |
| 55 | + } |
| 56 | + |
| 57 | + [TestMethod] |
| 58 | + public void Given_Result_When_DeserializeUnknownValue_Then_PreservesRawString() |
| 59 | + { |
| 60 | + Result? result = JsonSerializer.Deserialize<Result>("\"FUTURE_VALUE\"", _jsonSerializerOptionsProvider.Options); |
| 61 | + Assert.IsNotNull(result); |
| 62 | + Assert.AreEqual("FUTURE_VALUE", result!.Value); |
| 63 | + Assert.AreNotEqual(Result.VALID, result); |
| 64 | + } |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void Given_Result_When_DeserializeUnknownValue_Then_DoesNotThrow() |
| 68 | + { |
| 69 | + Exception? ex = null; |
| 70 | + Result? result = null; |
| 71 | + try { result = JsonSerializer.Deserialize<Result>("\"NEW_RESULT_TYPE\"", _jsonSerializerOptionsProvider.Options); } |
| 72 | + catch (Exception e) { ex = e; } |
| 73 | + Assert.IsNull(ex); |
| 74 | + Assert.IsNotNull(result); |
| 75 | + } |
| 76 | + |
| 77 | + [TestMethod] |
| 78 | + public void Given_Result_When_DeserializeNull_Then_ReturnsNull() |
| 79 | + { |
| 80 | + Result? result = JsonSerializer.Deserialize<Result>("null", _jsonSerializerOptionsProvider.Options); |
| 81 | + Assert.IsNull(result); |
| 82 | + } |
| 83 | + |
| 84 | + [TestMethod] |
| 85 | + public void Given_Result_When_SerializeKnownValue_Then_WritesCorrectString() |
| 86 | + { |
| 87 | + string json = JsonSerializer.Serialize(Result.VALID, _jsonSerializerOptionsProvider.Options); |
| 88 | + Assert.AreEqual("\"VALID\"", json); |
| 89 | + |
| 90 | + json = JsonSerializer.Serialize(Result.NOTREQUIRED, _jsonSerializerOptionsProvider.Options); |
| 91 | + Assert.AreEqual("\"NOT_REQUIRED\"", json); |
| 92 | + } |
| 93 | + |
| 94 | + [TestMethod] |
| 95 | + public void Given_Result_When_SerializeUnknownValue_Then_PreservesRawString() |
| 96 | + { |
| 97 | + Result unknown = (Result)"FUTURE_RESULT"; |
| 98 | + string json = JsonSerializer.Serialize(unknown, _jsonSerializerOptionsProvider.Options); |
| 99 | + Assert.AreEqual("\"FUTURE_RESULT\"", json); |
| 100 | + } |
| 101 | + |
| 102 | + [TestMethod] |
| 103 | + public void Given_Result_When_RoundTripUnknownValue_Then_ValueIsPreserved() |
| 104 | + { |
| 105 | + var opts = _jsonSerializerOptionsProvider.Options; |
| 106 | + Result original = (Result)"FUTURE_RESULT"; |
| 107 | + string serialized = JsonSerializer.Serialize(original, opts); |
| 108 | + Result? deserialized = JsonSerializer.Deserialize<Result>(serialized, opts); |
| 109 | + Assert.IsNotNull(deserialized); |
| 110 | + Assert.AreEqual("FUTURE_RESULT", deserialized!.Value); |
| 111 | + } |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + public void Given_Result_When_FromStringOrDefaultKnownValue_Then_ReturnsStaticInstance() |
| 115 | + { |
| 116 | + Assert.AreEqual(Result.VALID, Result.FromStringOrDefault("VALID")); |
| 117 | + Assert.AreEqual(Result.INVALID, Result.FromStringOrDefault("INVALID")); |
| 118 | + Assert.AreEqual(Result.UNKNOWN, Result.FromStringOrDefault("UNKNOWN")); |
| 119 | + Assert.AreEqual(Result.NOTREQUIRED, Result.FromStringOrDefault("NOT_REQUIRED")); |
| 120 | + } |
| 121 | + |
| 122 | + [TestMethod] |
| 123 | + public void Given_Result_When_FromStringOrDefaultUnknownValue_Then_ReturnsNull() |
| 124 | + { |
| 125 | + Assert.IsNull(Result.FromStringOrDefault("SOMETHING_ELSE")); |
| 126 | + } |
| 127 | + |
| 128 | + [TestMethod] |
| 129 | + public void Given_Result_When_ToJsonValueKnownEnum_Then_ReturnsString() |
| 130 | + { |
| 131 | + Assert.AreEqual("VALID", Result.ToJsonValue(Result.VALID)); |
| 132 | + Assert.AreEqual("NOT_REQUIRED", Result.ToJsonValue(Result.NOTREQUIRED)); |
| 133 | + } |
| 134 | + |
| 135 | + [TestMethod] |
| 136 | + public void Given_Result_When_ToJsonValueUnknown_Then_ReturnsRawString() |
| 137 | + { |
| 138 | + Result unknown = (Result)"FUTURE_RESULT"; |
| 139 | + Assert.AreEqual("FUTURE_RESULT", Result.ToJsonValue(unknown)); |
| 140 | + } |
| 141 | + |
| 142 | + [TestMethod] |
| 143 | + public void Given_Result_When_ToJsonValueNull_Then_ReturnsNull() |
| 144 | + { |
| 145 | + Assert.IsNull(Result.ToJsonValue(null)); |
| 146 | + } |
| 147 | + |
| 148 | + [TestMethod] |
| 149 | + public void Given_Result_When_EqualityBetweenKnownInstances_Then_Equal() |
| 150 | + { |
| 151 | + Assert.AreEqual(Result.VALID, Result.VALID); |
| 152 | + Assert.IsTrue(Result.VALID == Result.VALID); |
| 153 | + Assert.AreNotEqual(Result.VALID, Result.INVALID); |
| 154 | + } |
| 155 | + |
| 156 | + [TestMethod] |
| 157 | + public void Given_Result_When_EqualityIsCaseInsensitive_Then_Equal() |
| 158 | + { |
| 159 | + Result lower = (Result)"valid"; |
| 160 | + Result upper = (Result)"VALID"; |
| 161 | + Assert.AreEqual(lower, upper); |
| 162 | + Assert.IsTrue(lower == upper); |
| 163 | + } |
| 164 | + |
| 165 | + // ── PixPayByBankDetails: model with optional inner TypeEnum ─────────────── |
| 166 | + |
| 167 | + [TestMethod] |
| 168 | + public void Given_PixPayByBankDetails_When_DeserializeWithKnownType_Then_TypeMatchesStaticInstance() |
| 169 | + { |
| 170 | + string json = """{"type":"paybybank_pix"}"""; |
| 171 | + PixPayByBankDetails? result = JsonSerializer.Deserialize<PixPayByBankDetails>(json, _jsonSerializerOptionsProvider.Options); |
| 172 | + Assert.IsNotNull(result); |
| 173 | + Assert.AreEqual(PixPayByBankDetails.TypeEnum.PaybybankPix, result!.Type); |
| 174 | + Assert.AreEqual("paybybank_pix", result.Type?.Value); |
| 175 | + } |
| 176 | + |
| 177 | + [TestMethod] |
| 178 | + public void Given_PixPayByBankDetails_When_DeserializeWithUnknownType_Then_PreservesRawString() |
| 179 | + { |
| 180 | + string json = """{"type":"paybybank_future"}"""; |
| 181 | + PixPayByBankDetails? result = JsonSerializer.Deserialize<PixPayByBankDetails>(json, _jsonSerializerOptionsProvider.Options); |
| 182 | + Assert.IsNotNull(result); |
| 183 | + Assert.IsNotNull(result!.Type); |
| 184 | + Assert.AreEqual("paybybank_future", result.Type!.Value); |
| 185 | + Assert.AreNotEqual(PixPayByBankDetails.TypeEnum.PaybybankPix, result.Type); |
| 186 | + } |
| 187 | + |
| 188 | + [TestMethod] |
| 189 | + public void Given_PixPayByBankDetails_When_DeserializeWithUnknownType_Then_DoesNotThrow() |
| 190 | + { |
| 191 | + string json = """{"type":"completely_new_type","checkoutAttemptId":"abc"}"""; |
| 192 | + Exception? ex = null; |
| 193 | + PixPayByBankDetails? result = null; |
| 194 | + try { result = JsonSerializer.Deserialize<PixPayByBankDetails>(json, _jsonSerializerOptionsProvider.Options); } |
| 195 | + catch (Exception e) { ex = e; } |
| 196 | + Assert.IsNull(ex); |
| 197 | + Assert.IsNotNull(result); |
| 198 | + Assert.AreEqual("completely_new_type", result!.Type?.Value); |
| 199 | + } |
| 200 | + |
| 201 | + [TestMethod] |
| 202 | + public void Given_PixPayByBankDetails_When_DeserializeWithoutTypeField_Then_TypeUsesDefault() |
| 203 | + { |
| 204 | + string json = """{"checkoutAttemptId":"abc123"}"""; |
| 205 | + PixPayByBankDetails? result = JsonSerializer.Deserialize<PixPayByBankDetails>(json, _jsonSerializerOptionsProvider.Options); |
| 206 | + Assert.IsNotNull(result); |
| 207 | + // Constructor sets _TypeOption = TypeEnum.PaybybankPix (the default) |
| 208 | + Assert.AreEqual(PixPayByBankDetails.TypeEnum.PaybybankPix, result!.Type); |
| 209 | + } |
| 210 | + |
| 211 | + [TestMethod] |
| 212 | + public void Given_PixPayByBankDetails_When_SerializeWithKnownType_Then_WritesCorrectString() |
| 213 | + { |
| 214 | + string json = """{"type":"paybybank_pix"}"""; |
| 215 | + PixPayByBankDetails? deserialized = JsonSerializer.Deserialize<PixPayByBankDetails>(json, _jsonSerializerOptionsProvider.Options); |
| 216 | + string reserialized = JsonSerializer.Serialize(deserialized, _jsonSerializerOptionsProvider.Options); |
| 217 | + Assert.IsTrue(reserialized.Contains("\"paybybank_pix\""), $"Unexpected JSON: {reserialized}"); |
| 218 | + } |
| 219 | + |
| 220 | + [TestMethod] |
| 221 | + public void Given_PixPayByBankDetails_When_SerializeWithUnknownType_Then_PreservesRawString() |
| 222 | + { |
| 223 | + string json = """{"type":"paybybank_future"}"""; |
| 224 | + PixPayByBankDetails? deserialized = JsonSerializer.Deserialize<PixPayByBankDetails>(json, _jsonSerializerOptionsProvider.Options); |
| 225 | + string reserialized = JsonSerializer.Serialize(deserialized, _jsonSerializerOptionsProvider.Options); |
| 226 | + Assert.IsTrue(reserialized.Contains("\"paybybank_future\""), $"Unexpected JSON: {reserialized}"); |
| 227 | + } |
| 228 | + |
| 229 | + [TestMethod] |
| 230 | + public void Given_PixPayByBankDetails_When_RoundTripWithOtherFields_Then_AllFieldsPreserved() |
| 231 | + { |
| 232 | + string json = """{"type":"paybybank_pix","checkoutAttemptId":"attempt-123","issuer":"BankXYZ"}"""; |
| 233 | + var opts = _jsonSerializerOptionsProvider.Options; |
| 234 | + PixPayByBankDetails? deserialized = JsonSerializer.Deserialize<PixPayByBankDetails>(json, opts); |
| 235 | + Assert.IsNotNull(deserialized); |
| 236 | + Assert.AreEqual(PixPayByBankDetails.TypeEnum.PaybybankPix, deserialized!.Type); |
| 237 | + Assert.AreEqual("attempt-123", deserialized.CheckoutAttemptId); |
| 238 | + Assert.AreEqual("BankXYZ", deserialized.Issuer); |
| 239 | + } |
| 240 | + |
| 241 | + [TestMethod] |
| 242 | + public void Given_PixPayByBankDetails_When_TypeEnumFromStringOrDefaultKnown_Then_ReturnsStaticInstance() |
| 243 | + { |
| 244 | + Assert.AreEqual(PixPayByBankDetails.TypeEnum.PaybybankPix, |
| 245 | + PixPayByBankDetails.TypeEnum.FromStringOrDefault("paybybank_pix")); |
| 246 | + } |
| 247 | + |
| 248 | + [TestMethod] |
| 249 | + public void Given_PixPayByBankDetails_When_TypeEnumFromStringOrDefaultUnknown_Then_ReturnsNull() |
| 250 | + { |
| 251 | + Assert.IsNull(PixPayByBankDetails.TypeEnum.FromStringOrDefault("unknown_type")); |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments