|
| 1 | +/* |
| 2 | + * Copyright (c) Cloud Software Group, Inc. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * 1) Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2) Redistributions in binary form must reproduce the above |
| 12 | + * copyright notice, this list of conditions and the following |
| 13 | + * disclaimer in the documentation and/or other materials |
| 14 | + * provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 19 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 20 | + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 21 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 22 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 25 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 27 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +using System.Reflection; |
| 31 | +using Newtonsoft.Json; |
| 32 | +using XenAPI; |
| 33 | +using Console = System.Console; |
| 34 | + |
| 35 | +namespace XenServerTest; |
| 36 | + |
| 37 | +internal class DateTimeObject |
| 38 | +{ |
| 39 | + [JsonConverter(typeof(XenDateTimeConverter))] |
| 40 | + public DateTime Date { get; set; } |
| 41 | +} |
| 42 | + |
| 43 | +[TestClass] |
| 44 | +public class DateTimeTests |
| 45 | +{ |
| 46 | + private readonly JsonSerializerSettings _settings = new() |
| 47 | + { |
| 48 | + Converters = new List<JsonConverter> { new XenDateTimeConverter() } |
| 49 | + }; |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + [DynamicData(nameof(GetTestData), DynamicDataSourceType.Method, |
| 53 | + DynamicDataDisplayName = nameof(GetCustomDynamicDataDisplayName))] |
| 54 | + public void TestXenDateTimeConverter(string dateString, DateTime expectedDateTime, DateTimeKind expectedDateTimeKind) |
| 55 | + { |
| 56 | + try |
| 57 | + { |
| 58 | + var jsonDateString = "{ \"Date\" : \"" + dateString + "\" }"; |
| 59 | + var actualDateTimeObject = JsonConvert.DeserializeObject<DateTimeObject>(jsonDateString, _settings); |
| 60 | + |
| 61 | + |
| 62 | + Assert.IsNotNull(actualDateTimeObject?.Date, $"Failed to convert '{dateString}'"); |
| 63 | + var actualDateTime = actualDateTimeObject.Date; |
| 64 | + Assert.IsTrue(expectedDateTimeKind.Equals(actualDateTime.Kind)); |
| 65 | + |
| 66 | + // expected times are in UTC to ensure these tests do |
| 67 | + // not fail when running in other timezones |
| 68 | + if (expectedDateTimeKind == DateTimeKind.Local) |
| 69 | + actualDateTime = actualDateTime.ToUniversalTime(); |
| 70 | + |
| 71 | + Assert.IsTrue(expectedDateTime.Equals(actualDateTime), |
| 72 | + $"Conversion of '{dateString}' resulted in an incorrect DateTime value. Expected '{expectedDateTime} but instead received '{actualDateTime}'"); |
| 73 | + } |
| 74 | + catch (Exception ex) |
| 75 | + { |
| 76 | + // Log the error or mark this specific data entry as failed |
| 77 | + Console.WriteLine($@"Error processing dateString '{dateString}': {ex.Message}"); |
| 78 | + Assert.Fail($"An error occurred while processing '{dateString}'"); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static string GetCustomDynamicDataDisplayName(MethodInfo methodInfo, object[] data) |
| 83 | + { |
| 84 | + return $"{methodInfo.Name}: '{data[0] as string}'"; |
| 85 | + } |
| 86 | + |
| 87 | + public static IEnumerable<object[]> GetTestData() |
| 88 | + { |
| 89 | + // no dashes, no colons |
| 90 | + yield return new object[] { "20220101T123045", new DateTime(2022, 1, 1, 12, 30, 45, DateTimeKind.Utc), DateTimeKind.Unspecified }; |
| 91 | + yield return new object[] { "20220101T123045Z", new DateTime(2022, 1, 1, 12, 30, 45, DateTimeKind.Utc), DateTimeKind.Utc }; |
| 92 | + yield return new object[] { "20220101T123045+03", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 93 | + yield return new object[] { "20220101T123045+0300", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 94 | + yield return new object[] { "20220101T123045+03:00", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 95 | + |
| 96 | + yield return new object[] |
| 97 | + { "20220101T123045.123", new DateTime(2022, 1, 1, 12, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Unspecified }; |
| 98 | + yield return new object[] |
| 99 | + { "20220101T123045.123Z", new DateTime(2022, 1, 1, 12, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Utc }; |
| 100 | + yield return new object[] |
| 101 | + { "20220101T123045.123+03", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 102 | + yield return new object[] |
| 103 | + { "20220101T123045.123+0300", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 104 | + yield return new object[] |
| 105 | + { "20220101T123045.123+03:00", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 106 | + |
| 107 | + // no dashes, with colons |
| 108 | + yield return new object[] |
| 109 | + { "20220101T12:30:45", new DateTime(2022, 1, 1, 12, 30, 45, DateTimeKind.Utc), DateTimeKind.Unspecified }; |
| 110 | + yield return new object[] { "20220101T12:30:45Z", new DateTime(2022, 1, 1, 12, 30, 45, DateTimeKind.Utc), DateTimeKind.Utc }; |
| 111 | + yield return new object[] { "20220101T12:30:45+03", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 112 | + yield return new object[] { "20220101T12:30:45+0300", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 113 | + yield return new object[] |
| 114 | + { "20220101T12:30:45+03:00", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 115 | + |
| 116 | + yield return new object[] |
| 117 | + { "20220101T12:30:45.123", new DateTime(2022, 1, 1, 12, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Unspecified }; |
| 118 | + yield return new object[] |
| 119 | + { "20220101T12:30:45.123Z", new DateTime(2022, 1, 1, 12, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Utc }; |
| 120 | + yield return new object[] |
| 121 | + { "20220101T12:30:45.123+03", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 122 | + yield return new object[] |
| 123 | + { "20220101T12:30:45.123+0300", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 124 | + yield return new object[] |
| 125 | + { "20220101T12:30:45.123+03:00", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 126 | + |
| 127 | + // dashes and colons |
| 128 | + yield return new object[] |
| 129 | + { "2022-01-01T12:30:45", new DateTime(2022, 1, 1, 12, 30, 45, DateTimeKind.Utc), DateTimeKind.Unspecified }; |
| 130 | + yield return new object[] { "2022-01-01T12:30:45Z", new DateTime(2022, 1, 1, 12, 30, 45, DateTimeKind.Utc), DateTimeKind.Utc }; |
| 131 | + yield return new object[] { "2022-01-01T12:30:45+03", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 132 | + yield return new object[] |
| 133 | + { "2022-01-01T12:30:45+0300", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 134 | + yield return new object[] |
| 135 | + { "2022-01-01T12:30:45+03:00", new DateTime(2022, 1, 1, 9, 30, 45, DateTimeKind.Utc), DateTimeKind.Local }; |
| 136 | + |
| 137 | + yield return new object[] |
| 138 | + { "2022-01-01T12:30:45.123", new DateTime(2022, 1, 1, 12, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Unspecified }; |
| 139 | + yield return new object[] |
| 140 | + { "2022-01-01T12:30:45.123Z", new DateTime(2022, 1, 1, 12, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Utc }; |
| 141 | + yield return new object[] |
| 142 | + { "2022-01-01T12:30:45.123+03", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 143 | + yield return new object[] |
| 144 | + { "2022-01-01T12:30:45.123+0300", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 145 | + yield return new object[] |
| 146 | + { "2022-01-01T12:30:45.123+03:00", new DateTime(2022, 1, 1, 9, 30, 45, 123, DateTimeKind.Utc), DateTimeKind.Local }; |
| 147 | + } |
| 148 | +} |
0 commit comments