From 35c30f7bf0d14aaba17426bbaf03ecb5d24d18dc Mon Sep 17 00:00:00 2001 From: Daniel Pokorny Date: Tue, 24 Oct 2023 08:34:01 +0200 Subject: [PATCH] update tests --- .../Sync/IDeliverySyncResponse.cs | 4 +- .../DeliveryClientTests.cs | 90 +++++++++++++++---- .../Fixtures/DeliveryClient/sync.json | 48 +++++++--- Kontent.Ai.Delivery/Sync/SyncItem.cs | 23 +++-- 4 files changed, 124 insertions(+), 41 deletions(-) diff --git a/Kontent.Ai.Delivery.Abstractions/Sync/IDeliverySyncResponse.cs b/Kontent.Ai.Delivery.Abstractions/Sync/IDeliverySyncResponse.cs index e5901236..c2735a34 100644 --- a/Kontent.Ai.Delivery.Abstractions/Sync/IDeliverySyncResponse.cs +++ b/Kontent.Ai.Delivery.Abstractions/Sync/IDeliverySyncResponse.cs @@ -3,12 +3,12 @@ namespace Kontent.Ai.Delivery.Abstractions; /// -/// Represents a response from Kontent.ai Delivery API that contains a taxonomy group. +/// Represents a response from Kontent.ai Sync API that contains recently updated items. Response includes continuation token for subsequent synchronization calls. /// public interface IDeliverySyncResponse : IResponse { /// - /// Gets list of delta update items. + /// Gets the list of items delta updates. /// IList SyncItems { get; } } \ No newline at end of file diff --git a/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs b/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs index 96596bf1..626bee26 100644 --- a/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs +++ b/Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs @@ -1851,18 +1851,62 @@ public async Task SyncApi_PostSyncInitAsync_WithParameters_GetContinuationToken( } [Fact] - public async Task SyncApi_GetSyncAsync_GetSyncItems() + public async Task SyncApi_GetSyncAsync_GetSyncItems_WithTypeProvider() { var mockedResponse = await File.ReadAllTextAsync(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}sync.json")); - var expectedValue = JObject.Parse(mockedResponse).SelectToken("items").ToObject>(); - + var expectedItems = JObject.Parse(mockedResponse).SelectToken("items").ToObject>(); + _mockHttp .When($"{_baseUrl}/sync") .WithHeaders("X-Continuation", "token") .Respond(new[] { new KeyValuePair("X-Continuation", "token"), }, "application/json", mockedResponse); - var client = InitializeDeliveryClientWithCustomModelProvider(_mockHttp); + var client = InitializeDeliveryClientWithACustomTypeProvider(_mockHttp); + + var sync = await client.GetSyncAsync("token"); + + Assert.NotNull(sync.ApiResponse.ContinuationToken); + + Assert.Equal(2, sync.SyncItems.Count); + + for (int i = 0; i < expectedItems.Count; i++) + { + var expectedItem = expectedItems[i]; + var syncItem = (Article)sync.SyncItems[i].Data; + + var expectedSystemValues = expectedItem["data"]["system"]; + var expectedElementValues = expectedItem["data"]["elements"]; + var syncItemSystemValues = syncItem.System; + + Assert.Equal(expectedSystemValues["codename"].ToString(), syncItemSystemValues.Codename.ToString()); + Assert.Equal(expectedSystemValues["name"].ToString(), syncItemSystemValues.Name.ToString()); + Assert.Equal(expectedSystemValues["id"].ToString(), syncItemSystemValues.Id.ToString()); + Assert.Equal(expectedSystemValues["type"].ToString(), syncItemSystemValues.Type.ToString()); + Assert.Equal(expectedSystemValues["language"].ToString(), syncItemSystemValues.Language.ToString()); + Assert.Equal(expectedSystemValues["collection"].ToString(), syncItemSystemValues.Collection.ToString()); + Assert.Equal(expectedSystemValues["workflow_step"].ToString(), syncItemSystemValues.WorkflowStep.ToString()); + + Assert.Equal(expectedElementValues["title"]["value"].ToString(), syncItem.Title); + + Assert.Equal(expectedItem["change_type"].ToString(), sync.SyncItems[i].ChangeType); + Assert.Equal(DateTime.Parse(expectedItem["timestamp"].ToString()), DateTime.Parse(sync.SyncItems[i].Timestamp.ToString())); + } + } + + [Fact] + public async Task SyncApi_GetSyncAsync_GetSyncItems_WithoutTypeProvider() + { + var mockedResponse = await File.ReadAllTextAsync(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}sync.json")); + + var expectedItems = JObject.Parse(mockedResponse).SelectToken("items").ToObject>(); + + _mockHttp + .When($"{_baseUrl}/sync") + .WithHeaders("X-Continuation", "token") + .Respond(new[] { new KeyValuePair("X-Continuation", "token"), }, "application/json", mockedResponse); + + var client = InitializeDeliveryClientWithCustomModelProvider(_mockHttp, new PropertyMapper(), new TypeProvider()); var sync = await client.GetSyncAsync("token"); @@ -1870,23 +1914,31 @@ public async Task SyncApi_GetSyncAsync_GetSyncItems() Assert.Equal(2, sync.SyncItems.Count); - Assert.Equal(expectedValue[0].Codename, sync.SyncItems[0].Codename); - Assert.Equal(expectedValue[0].Id, sync.SyncItems[0].Id); - Assert.Equal(expectedValue[0].Type, sync.SyncItems[0].Type); - Assert.Equal(expectedValue[0].Language, sync.SyncItems[0].Language); - Assert.Equal(expectedValue[0].Collection, sync.SyncItems[0].Collection); - Assert.Equal(expectedValue[0].ChangeType, sync.SyncItems[0].ChangeType); - Assert.Equal(expectedValue[0].Timestamp, sync.SyncItems[0].Timestamp); - - Assert.Equal(expectedValue[1].Codename, sync.SyncItems[1].Codename); - Assert.Equal(expectedValue[1].Id, sync.SyncItems[1].Id); - Assert.Equal(expectedValue[1].Type, sync.SyncItems[1].Type); - Assert.Equal(expectedValue[1].Language, sync.SyncItems[1].Language); - Assert.Equal(expectedValue[1].Collection, sync.SyncItems[1].Collection); - Assert.Equal(expectedValue[1].ChangeType, sync.SyncItems[1].ChangeType); - Assert.Equal(expectedValue[1].Timestamp, sync.SyncItems[1].Timestamp); + for (int i = 0; i < expectedItems.Count; i++) + { + var expectedItem = expectedItems[i]; + var syncItem = (JObject)sync.SyncItems[i].Data; + + var expectedSystemValues = expectedItem["data"]["system"]; + var expectedElementValues = expectedItem["data"]["elements"]; + var syncItemSystemValues = syncItem["system"]; + + Assert.Equal(expectedSystemValues["codename"].ToString(), syncItemSystemValues["codename"].ToString()); + Assert.Equal(expectedSystemValues["name"].ToString(), syncItemSystemValues["name"].ToString()); + Assert.Equal(expectedSystemValues["id"].ToString(), syncItemSystemValues["id"].ToString()); + Assert.Equal(expectedSystemValues["type"].ToString(), syncItemSystemValues["type"].ToString()); + Assert.Equal(expectedSystemValues["language"].ToString(), syncItemSystemValues["language"].ToString()); + Assert.Equal(expectedSystemValues["collection"].ToString(), syncItemSystemValues["collection"].ToString()); + Assert.Equal(expectedSystemValues["workflow_step"].ToString(), syncItemSystemValues["workflow_step"].ToString()); + + Assert.Equal(expectedElementValues["title"]["value"].ToString(), syncItem["elements"]["title"]["value"]); + + Assert.Equal(expectedItem["change_type"].ToString(), sync.SyncItems[i].ChangeType); + Assert.Equal(DateTime.Parse(expectedItem["timestamp"].ToString()), DateTime.Parse(sync.SyncItems[i].Timestamp.ToString())); + } } + private DeliveryClient InitializeDeliveryClientWithACustomTypeProvider(MockHttpMessageHandler handler) { var customTypeProvider = new CustomTypeProvider(); diff --git a/Kontent.Ai.Delivery.Tests/Fixtures/DeliveryClient/sync.json b/Kontent.Ai.Delivery.Tests/Fixtures/DeliveryClient/sync.json index 1b377b27..15332266 100644 --- a/Kontent.Ai.Delivery.Tests/Fixtures/DeliveryClient/sync.json +++ b/Kontent.Ai.Delivery.Tests/Fixtures/DeliveryClient/sync.json @@ -1,22 +1,48 @@ { "items": [ { - "codename": "hello_world", - "id": "7adfb82a-1386-4228-bcc2-45073a0355f6", - "type": "article", - "language": "default", - "collection": "default", + "data": { + "system": { + "codename": "hello_world", + "name": "Hello world!", + "id": "7adfb82a-1386-4228-bcc2-45073a0355f6", + "type": "article", + "language": "default", + "collection": "default", + "workflow_step": "published" + }, + "elements": { + "title": { + "type": "text", + "name": "Title", + "value": "Hello world!" + } + } + }, "change_type": "changed", "timestamp": "2022-10-06T08:38:40.0088127Z" }, { - "codename": "bye__world", - "id": "42a3cfbd-4967-43e7-987b-e1e69c483e26", - "type": "article", - "language": "default", - "collection": "default", + "data": { + "system": { + "codename": "bye_world", + "name": "Bye world!", + "id": "42a3cfbd-4967-43e7-987b-e1e69c483e26", + "type": "article", + "language": "default", + "collection": "default", + "workflow_step": "published" + }, + "elements": { + "title": { + "type": "text", + "name": "Title", + "value": "Bye world!" + } + } + }, "change_type": "deleted", "timestamp": "2022-10-06T08:38:47.3613558Z" } ] -} \ No newline at end of file +} diff --git a/Kontent.Ai.Delivery/Sync/SyncItem.cs b/Kontent.Ai.Delivery/Sync/SyncItem.cs index 6491ea09..589ec28d 100644 --- a/Kontent.Ai.Delivery/Sync/SyncItem.cs +++ b/Kontent.Ai.Delivery/Sync/SyncItem.cs @@ -7,22 +7,27 @@ namespace Kontent.Ai.Delivery.Sync; /// -/// -/// Constructor used for deserialization (e.g. for caching purposes), contains no logic. -/// -[method: JsonConstructor] -/// <> -internal sealed class SyncItem(object data, string changeType, DateTime timestamp) : ISyncItem +internal sealed class SyncItem : ISyncItem { /// [JsonProperty("data")] - public object Data { get; internal set; } = data; + public object Data { get; internal set; } /// [JsonProperty("change_type")] - public string ChangeType { get; internal set; } = changeType; + public string ChangeType { get; internal set; } /// [JsonProperty("timestamp")] - public DateTime Timestamp { get; internal set; } = timestamp; + public DateTime Timestamp { get; internal set; } + + /// + /// Constructor used for deserialization (e.g. for caching purposes), contains no logic. + /// + [JsonConstructor] + public SyncItem(object data, string changeType, DateTime timestamp) { + Data = data; + ChangeType = changeType; + Timestamp = timestamp; + } } \ No newline at end of file