Skip to content

Commit

Permalink
refactor SyncItem model, adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Oct 29, 2023
1 parent 46bb20c commit c726a1b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IContentItem
/// <summary>
/// Represents system attributes of a content item.
/// </summary>
public IContentItemSystemAttributes System { get; set; }
public IContentItemSystemAttributes System { get; }
}
7 changes: 6 additions & 1 deletion Kontent.Ai.Delivery.Abstractions/Sync/ISyncItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ namespace Kontent.Ai.Delivery.Abstractions;
/// </summary>
public interface ISyncItem
{
/// <summary>
/// Retrieves runtime strongly typed item if CustomTypeProvider is registered, otherwise null.
/// </summary>
object StronglyTypedData { get; }

/// <summary>
/// Retrieves content item information and element values.
/// </summary>
object Data { get; }
ISyncItemData Data { get; }

/// <summary>
/// Gets the information whether the content item was modified or deleted since the last synchronization.
Expand Down
14 changes: 14 additions & 0 deletions Kontent.Ai.Delivery.Abstractions/Sync/ISyncItemData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Collections.Generic;

namespace Kontent.Ai.Delivery.Abstractions;

/// <summary>
/// Represents a delta update.
/// </summary>
public interface ISyncItemData : IContentItem
{
/// <summary>
/// Retrieves key:value pairs representing content item elements.
/// </summary>
Dictionary<string, object> Elements { get; }
}
58 changes: 24 additions & 34 deletions Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
Expand All @@ -14,7 +13,6 @@
using Kontent.Ai.Delivery.ContentItems;
using Kontent.Ai.Delivery.ContentItems.RichText.Blocks;
using Kontent.Ai.Delivery.SharedModels;
using Kontent.Ai.Delivery.Sync;
using Kontent.Ai.Delivery.Tests.Factories;
using Kontent.Ai.Delivery.Tests.Models;
using Kontent.Ai.Delivery.Tests.Models.ContentTypes;
Expand Down Expand Up @@ -1851,7 +1849,7 @@ public async Task SyncApi_PostSyncInitAsync_WithParameters_GetContinuationToken(
}

[Fact]
public async Task SyncApi_GetSyncAsync_GetSyncItems_WithTypeProvider()
public async Task SyncApi_GetSyncAsync_GetSyncItems_WithTypeProvider_ReturnsStronglyTypedData()
{
var mockedResponse = await File.ReadAllTextAsync(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}sync.json"));

Expand All @@ -1872,30 +1870,20 @@ public async Task SyncApi_GetSyncAsync_GetSyncItems_WithTypeProvider()

for (int i = 0; i < expectedItems.Count; i++)
{
var article = sync.SyncItems[i].StronglyTypedData as Article;
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);


AssertSystemPropertiesEquality(expectedItem["data"]["system"].ToObject<JObject>(), article.System);
Assert.NotNull(article);
Assert.Equal(expectedElementValues["title"]["value"].ToString(), article.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()
public async Task SyncApi_GetSyncAsync_GetSyncItems_WithoutTypeProvider_ReturnsGenericData()
{
var mockedResponse = await File.ReadAllTextAsync(Path.Combine(Environment.CurrentDirectory, $"Fixtures{Path.DirectorySeparatorChar}DeliveryClient{Path.DirectorySeparatorChar}sync.json"));

Expand All @@ -1916,28 +1904,30 @@ public async Task SyncApi_GetSyncAsync_GetSyncItems_WithoutTypeProvider()

for (int i = 0; i < expectedItems.Count; i++)
{
var syncItemData = sync.SyncItems[i].Data;
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"]);


AssertSystemPropertiesEquality(expectedItem["data"]["system"].ToObject<JObject>(), sync.SyncItems[i].Data.System);
Assert.Null(sync.SyncItems[i].StronglyTypedData);
Assert.NotNull(syncItemData.Elements["title"]);
Assert.Equal(expectedElementValues["title"], syncItemData.Elements["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()));
}
}

private void AssertSystemPropertiesEquality(JObject expectedSystemValues, IContentItemSystemAttributes system)
{
Assert.Equal(expectedSystemValues["codename"].ToString(), system.Codename.ToString());
Assert.Equal(expectedSystemValues["name"].ToString(), system.Name.ToString());
Assert.Equal(expectedSystemValues["id"].ToString(), system.Id.ToString());
Assert.Equal(expectedSystemValues["type"].ToString(), system.Type.ToString());
Assert.Equal(expectedSystemValues["language"].ToString(), system.Language.ToString());
Assert.Equal(expectedSystemValues["collection"].ToString(), system.Collection.ToString());
Assert.Equal(expectedSystemValues["workflow_step"].ToString(), system.WorkflowStep.ToString());
}


private DeliveryClient InitializeDeliveryClientWithACustomTypeProvider(MockHttpMessageHandler handler)
{
Expand Down
12 changes: 5 additions & 7 deletions Kontent.Ai.Delivery/DeliveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ public async Task<IDeliverySyncInitResponse> PostSyncInitAsync(IEnumerable<IQuer

var content = await response.GetJsonContentAsync();
var items = content["items"].ToObject<List<SyncItem>>(Serializer);

return new DeliverySyncInitResponse(response, items.ToList<ISyncItem>());
}

Expand All @@ -354,14 +355,11 @@ public async Task<IDeliverySyncResponse> GetSyncAsync(string continuationToken)
var itemModels = await Task.WhenAll(syncItems.Select(async syncItem =>
{
// use TypeProvider from DI container to select a model
var mappedModel = await ModelProvider.GetContentItemModelAsync<object>(syncItem.Data, new JObject());
if (mappedModel == null)
{
// return JObject if no suitable model is found
return new SyncItem(syncItem.Data, syncItem.ChangeType, syncItem.Timestamp);
}
return new SyncItem(mappedModel, syncItem.ChangeType, syncItem.Timestamp);
var mappedModel = await ModelProvider.GetContentItemModelAsync<object>(JToken.FromObject(syncItem.Data), new JObject());

return new SyncItem(mappedModel, syncItem.Data, syncItem.ChangeType, syncItem.Timestamp);
}));

return new DeliverySyncResponse(response, itemModels);
}

Expand Down
13 changes: 8 additions & 5 deletions Kontent.Ai.Delivery/Sync/SyncItem.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using System;
using System.Collections.Generic;
using Kontent.Ai.Delivery.Abstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Kontent.Ai.Delivery.Sync;

/// <inheritdoc/>
internal sealed class SyncItem : ISyncItem
{
/// <inheritdoc/>
public object StronglyTypedData { get; internal set; }

/// <inheritdoc/>
[JsonProperty("data")]
public object Data { get; internal set; }
public ISyncItemData Data { get; internal set; }

/// <inheritdoc/>
[JsonProperty("change_type")]
Expand All @@ -22,10 +23,12 @@ internal sealed class SyncItem : ISyncItem
public DateTime Timestamp { get; internal set; }

/// <summary>
/// Constructor used for deserialization (e.g. for caching purposes), contains no logic.
/// Initializes a new instance of <see cref="SyncItem"/> class.
/// </summary>
[JsonConstructor]
public SyncItem(object data, string changeType, DateTime timestamp) {
public SyncItem(object stronglyTypedData, ISyncItemData data, string changeType, DateTime timestamp)
{
StronglyTypedData = stronglyTypedData;
Data = data;
ChangeType = changeType;
Timestamp = timestamp;
Expand Down
25 changes: 25 additions & 0 deletions Kontent.Ai.Delivery/Sync/SyncItemData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using Kontent.Ai.Delivery.Abstractions;
using Newtonsoft.Json;

namespace Kontent.Ai.Delivery.Sync
{
internal sealed class SyncItemData : ISyncItemData
{
/// <inheritdoc/>
[JsonProperty("system")]
public IContentItemSystemAttributes System { get; internal set; }

/// <inheritdoc/>
[JsonProperty("elements")]
public Dictionary<string, object> Elements { get; internal set; }

/// <summary>
/// Constructor used for deserialization. Contains no logic.
/// </summary>
[JsonConstructor]
public SyncItemData()
{
}
}
}

0 comments on commit c726a1b

Please sign in to comment.