Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pokornyd committed Oct 24, 2023
1 parent bf87c4a commit 2b17390
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace Kontent.Ai.Delivery.Abstractions;

/// <summary>
/// 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.
/// </summary>
public interface IDeliverySyncResponse : IResponse
{
/// <summary>
/// Gets list of delta update items.
/// Gets the list of items delta updates.
/// </summary>
IList<ISyncItem> SyncItems { get; }
}
90 changes: 71 additions & 19 deletions Kontent.Ai.Delivery.Tests/DeliveryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,42 +1851,94 @@ 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<IList<SyncItem>>();
var expectedItems = JObject.Parse(mockedResponse).SelectToken("items").ToObject<List<JObject>>();

_mockHttp
.When($"{_baseUrl}/sync")
.WithHeaders("X-Continuation", "token")
.Respond(new[] { new KeyValuePair<string, string>("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<List<JObject>>();

_mockHttp
.When($"{_baseUrl}/sync")
.WithHeaders("X-Continuation", "token")
.Respond(new[] { new KeyValuePair<string, string>("X-Continuation", "token"), }, "application/json", mockedResponse);

var client = InitializeDeliveryClientWithCustomModelProvider(_mockHttp, new PropertyMapper(), new TypeProvider());

var sync = await client.GetSyncAsync("token");

Assert.NotNull(sync.ApiResponse.ContinuationToken);

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();
Expand Down
48 changes: 37 additions & 11 deletions Kontent.Ai.Delivery.Tests/Fixtures/DeliveryClient/sync.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
23 changes: 14 additions & 9 deletions Kontent.Ai.Delivery/Sync/SyncItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@
namespace Kontent.Ai.Delivery.Sync;

/// <inheritdoc/>
/// <summary>
/// Constructor used for deserialization (e.g. for caching purposes), contains no logic.
/// </summary>
[method: JsonConstructor]
/// <<inheritdoc/>>
internal sealed class SyncItem(object data, string changeType, DateTime timestamp) : ISyncItem
internal sealed class SyncItem : ISyncItem
{
/// <inheritdoc/>
[JsonProperty("data")]
public object Data { get; internal set; } = data;
public object Data { get; internal set; }

/// <inheritdoc/>
[JsonProperty("change_type")]
public string ChangeType { get; internal set; } = changeType;
public string ChangeType { get; internal set; }

/// <inheritdoc/>
[JsonProperty("timestamp")]
public DateTime Timestamp { get; internal set; } = timestamp;
public DateTime Timestamp { get; internal set; }

/// <summary>
/// Constructor used for deserialization (e.g. for caching purposes), contains no logic.
/// </summary>
[JsonConstructor]
public SyncItem(object data, string changeType, DateTime timestamp) {
Data = data;
ChangeType = changeType;
Timestamp = timestamp;
}
}

0 comments on commit 2b17390

Please sign in to comment.