|
| 1 | +using AwesomeAssertions; |
| 2 | +using Newtonsoft.Json.Linq; |
| 3 | +using ServiceNow.Api.Exceptions; |
| 4 | +using System.Globalization; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace ServiceNow.Api.Test; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Regression tests for how the ordering field is read out of a returned row when paging. |
| 11 | +/// |
| 12 | +/// These run entirely against a stubbed message handler, so they need no credentials and no network. |
| 13 | +/// |
| 14 | +/// The bug they were written for (issue #74, reported as #25): paging read the ordering field with |
| 15 | +/// ToString() and concatenated "Z" onto it. That breaks whenever sysparm_display_value is set: |
| 16 | +/// with "all" every field is returned as a { display_value, value } object, so ToString() yields JSON |
| 17 | +/// and the parse throws, taking the whole query with it. |
| 18 | +/// </summary> |
| 19 | +public class PagingFieldParsingTests |
| 20 | +{ |
| 21 | + private const string TableName = "cmdb_ci"; |
| 22 | + private const int PageSize = 1000; |
| 23 | + private static readonly DateTime _baseTime = new(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// The reported failure: with sysparm_display_value=all the ordering field is an object, not a scalar. |
| 27 | + /// </summary> |
| 28 | + [Fact] |
| 29 | + public async Task ObjectShapedPagingField_PagesInsteadOfThrowing() |
| 30 | + { |
| 31 | + using var handler = new StubServiceNowHandler(totalCount: 1_500, |
| 32 | + [ |
| 33 | + MakeObjectShapedPage(0, PageSize), |
| 34 | + MakeObjectShapedPage(PageSize, 500), |
| 35 | + [] |
| 36 | + ]); |
| 37 | + |
| 38 | + using var client = new ServiceNowClient(handler); |
| 39 | + |
| 40 | + var result = await client |
| 41 | + .GetAllByQueryAsync(TableName, cancellationToken: TestContext.Current.CancellationToken) |
| 42 | + .ConfigureAwait(true); |
| 43 | + |
| 44 | + result.Should().HaveCount(1_500, "an object-shaped ordering field must not break paging"); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// With both representations present the raw value is the one to page on, since it carries the |
| 49 | + /// underlying UTC timestamp rather than a timezone-and-format-dependent rendering of it. |
| 50 | + /// </summary> |
| 51 | + [Fact] |
| 52 | + public async Task ObjectShapedPagingField_PagesOnTheRawValueNotTheDisplayValue() |
| 53 | + { |
| 54 | + // value says 10:00 UTC; display_value says something entirely different. |
| 55 | + var fullPage = MakePageSharingOneTimestamp(new JObject |
| 56 | + { |
| 57 | + ["display_value"] = "31/12/2030 23:59:59", |
| 58 | + ["value"] = "2026-01-01 10:00:00" |
| 59 | + }); |
| 60 | + |
| 61 | + using var handler = new StubServiceNowHandler(totalCount: PageSize, [fullPage, []]); |
| 62 | + using var client = new ServiceNowClient(handler); |
| 63 | + |
| 64 | + _ = await client |
| 65 | + .GetAllByQueryAsync(TableName, cancellationToken: TestContext.Current.CancellationToken) |
| 66 | + .ConfigureAwait(true); |
| 67 | + |
| 68 | + // The second request carries the paging window, built from whichever representation was used. |
| 69 | + handler.RequestUris.Should().HaveCountGreaterThan(1); |
| 70 | + handler.RequestUris[1].Should().Contain("2026-01-01 10:00:00", "the raw value is the correct boundary"); |
| 71 | + handler.RequestUris[1].Should().NotContain("2030", "the display value must not be used as the boundary"); |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// A value carrying its own offset used to be corrupted by concatenating "Z" onto it. It should now be |
| 76 | + /// converted to UTC properly. |
| 77 | + /// </summary> |
| 78 | + [Fact] |
| 79 | + public async Task PagingFieldWithAnExplicitOffset_IsConvertedToUtc() |
| 80 | + { |
| 81 | + // 05:00 at +05:00 is midnight UTC. |
| 82 | + var fullPage = MakePageSharingOneTimestamp("2026-01-02T05:00:00+05:00"); |
| 83 | + |
| 84 | + using var handler = new StubServiceNowHandler(totalCount: PageSize, [fullPage, []]); |
| 85 | + using var client = new ServiceNowClient(handler); |
| 86 | + |
| 87 | + _ = await client |
| 88 | + .GetAllByQueryAsync(TableName, cancellationToken: TestContext.Current.CancellationToken) |
| 89 | + .ConfigureAwait(true); |
| 90 | + |
| 91 | + handler.RequestUris.Should().HaveCountGreaterThan(1); |
| 92 | + handler.RequestUris[1].Should().Contain("2026-01-02 00:00:00", "the offset should be applied, not ignored"); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// An ordering field that is not a date at all should say so clearly, naming the field and the value, |
| 97 | + /// rather than surfacing a bare FormatException from inside a LINQ Max(). |
| 98 | + /// </summary> |
| 99 | + [Fact] |
| 100 | + public async Task UnparseablePagingField_ThrowsNamingTheFieldAndValue() |
| 101 | + { |
| 102 | + var fullPage = MakePageSharingOneTimestamp("not a date at all"); |
| 103 | + |
| 104 | + using var handler = new StubServiceNowHandler(totalCount: PageSize, [fullPage, []]); |
| 105 | + using var client = new ServiceNowClient(handler); |
| 106 | + |
| 107 | + var act = async () => await client |
| 108 | + .GetAllByQueryAsync(TableName, cancellationToken: TestContext.Current.CancellationToken) |
| 109 | + .ConfigureAwait(true); |
| 110 | + |
| 111 | + (await act.Should().ThrowAsync<ServiceNowApiException>().ConfigureAwait(true)) |
| 112 | + .WithMessage("*sys_created_on*not a date at all*"); |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// A plain UTC string, which is what comes back with no sysparm_display_value, must keep working exactly |
| 117 | + /// as before. This is the overwhelmingly common case. |
| 118 | + /// </summary> |
| 119 | + [Fact] |
| 120 | + public async Task PlainUtcPagingField_StillPagesAsBefore() |
| 121 | + { |
| 122 | + using var handler = new StubServiceNowHandler(totalCount: 1_500, |
| 123 | + [ |
| 124 | + MakePlainPage(0, PageSize), |
| 125 | + MakePlainPage(PageSize, 500), |
| 126 | + [] |
| 127 | + ]); |
| 128 | + |
| 129 | + using var client = new ServiceNowClient(handler); |
| 130 | + |
| 131 | + var result = await client |
| 132 | + .GetAllByQueryAsync(TableName, cancellationToken: TestContext.Current.CancellationToken) |
| 133 | + .ConfigureAwait(true); |
| 134 | + |
| 135 | + result.Should().HaveCount(1_500); |
| 136 | + } |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// A full page of distinct records that all share one ordering-field value, so that the paging window the |
| 140 | + /// client derives from the page is deterministic and can be asserted on. The sys_ids must differ or the |
| 141 | + /// client's de-duplication would collapse the page to a single row. |
| 142 | + /// </summary> |
| 143 | + private static List<JObject> MakePageSharingOneTimestamp(JToken sharedCreatedOn) |
| 144 | + => [.. Enumerable.Range(0, PageSize).Select(i => new JObject |
| 145 | + { |
| 146 | + ["sys_id"] = $"sys{i:D8}", |
| 147 | + ["sys_created_on"] = sharedCreatedOn.DeepClone() |
| 148 | + })]; |
| 149 | + |
| 150 | + private static List<JObject> MakePlainPage(int startIndex, int count) |
| 151 | + => [.. Enumerable.Range(0, count).Select(i => new JObject |
| 152 | + { |
| 153 | + ["sys_id"] = $"sys{startIndex + i:D8}", |
| 154 | + ["sys_created_on"] = _baseTime.AddSeconds(startIndex + i).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) |
| 155 | + })]; |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// The shape returned when sysparm_display_value=all is requested. |
| 159 | + /// </summary> |
| 160 | + private static List<JObject> MakeObjectShapedPage(int startIndex, int count) |
| 161 | + => [.. Enumerable.Range(0, count).Select(i => |
| 162 | + { |
| 163 | + var created = _baseTime.AddSeconds(startIndex + i); |
| 164 | + return new JObject |
| 165 | + { |
| 166 | + ["sys_id"] = $"sys{startIndex + i:D8}", |
| 167 | + ["sys_created_on"] = new JObject |
| 168 | + { |
| 169 | + ["display_value"] = created.ToString("dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture), |
| 170 | + ["value"] = created.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) |
| 171 | + } |
| 172 | + }; |
| 173 | + })]; |
| 174 | +} |
0 commit comments