Skip to content

Commit

Permalink
Add strongly typed response for listing variants by type
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoFaFe committed Jun 16, 2024
1 parent 2d3c739 commit 8dcac4f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,43 @@ await client.Invoking(x => x.ListLanguageVariantsByItemAsync(null))
.Should().ThrowExactlyAsync<ArgumentNullException>();
}

[Fact]
public async Task ListLanguageVariantsByTypeAsync_StronglyTyped_ListsVariants()
{
var client = _scenario
.WithResponses("LanguageVariantsPage1.json", "LanguageVariantsPage2.json", "LanguageVariantsPage3.json")
.CreateManagementClient();

var expected = new[]
{
(itemId: "00000000-0000-0000-0000-000000000000", languageId: "00000000-0000-0000-0000-000000000000"),
(itemId: "00000000-0000-0000-0000-000000000000", languageId: "10000000-0000-0000-0000-000000000000"),
(itemId: "10000000-0000-0000-0000-000000000000", languageId: "00000000-0000-0000-0000-000000000000"),
(itemId: "10000000-0000-0000-0000-000000000000", languageId: "10000000-0000-0000-0000-000000000000"),
(itemId: "20000000-0000-0000-0000-000000000000", languageId: "00000000-0000-0000-0000-000000000000"),
(itemId: "20000000-0000-0000-0000-000000000000", languageId: "10000000-0000-0000-0000-000000000000")
}.Select(x => GetExpectedComplexTestModel(x.languageId, x.itemId));

var identifier = Reference.ById(Guid.Parse("17ff8a28-ebe6-5c9d-95ea-18fe1ff86d2d"));
var response = await client.ListLanguageVariantsByTypeAsync<ComplexTestModel>(identifier).GetAllAsync();

_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Get)
.Response(response, expected)
.Url($"{Endpoint}/projects/{ENVIRONMENT_ID}/types/{identifier.Id}/variants")
.Validate();
}

[Fact]
public async Task ListLanguageVariantsByTypeAsync_StronglyTyped_IdentifierIsNull_Throws()
{
var client = _scenario.CreateManagementClient();

await client.Invoking(x => x.ListLanguageVariantsByTypeAsync<ComplexTestModel>(null))
.Should().ThrowExactlyAsync<ArgumentNullException>();
}

[Fact]
public async void ListLanguageVariantsByTypeAsync_DynamicallyTyped_ListsVariants()
{
Expand Down Expand Up @@ -120,7 +157,6 @@ await client.Invoking(x => x.ListLanguageVariantsByTypeAsync(null))
.Should().ThrowExactlyAsync<ArgumentNullException>();
}


[Fact]
public async Task ListLanguageVariantsOfContentTypeWithComponentsAsync_DynamicallyTyped_ListsVariants()
{
Expand Down Expand Up @@ -496,18 +532,20 @@ private static LanguageVariantModel GetExpectedLanguageVariantModel(
};

private static List<LanguageVariantModel<ComplexTestModel>> GetExpectedComplexTestModels(params string[] languageIds)
=> languageIds.Select(GetExpectedComplexTestModel).ToList();
=> languageIds.Select(x => GetExpectedComplexTestModel(x)).ToList();

private static LanguageVariantModel<ComplexTestModel> GetExpectedComplexTestModel(string languageId = "78dbefe8-831b-457e-9352-f4c4eacd5024") => new()
{
Item = Reference.ById(Guid.Parse("4b628214-e4fe-4fe0-b1ff-955df33e1515")),
Language = Reference.ById(Guid.Parse(languageId)),
LastModified = DateTimeOffset.Parse("2021-11-06T13:57:26.7069564Z").UtcDateTime,
Workflow = new WorkflowStepIdentifier(Reference.ById(Guid.Parse("00000000-0000-0000-0000-000000000000")), Reference.ById(Guid.Parse("eee6db3b-545a-4785-8e86-e3772c8756f9"))),
Schedule = GetExpectedScheduleResponseModel(),
DueDate = GetExpectedDueDateModel(),
Elements = ElementsData.GetExpectedStronglyTypedElementsModel(),
};
private static LanguageVariantModel<ComplexTestModel> GetExpectedComplexTestModel(
string languageId = "78dbefe8-831b-457e-9352-f4c4eacd5024",
string itemId = "4b628214-e4fe-4fe0-b1ff-955df33e1515") => new()
{
Item = Reference.ById(Guid.Parse(itemId)),
Language = Reference.ById(Guid.Parse(languageId)),
LastModified = DateTimeOffset.Parse("2021-11-06T13:57:26.7069564Z").UtcDateTime,
Workflow = new WorkflowStepIdentifier(Reference.ById(Guid.Parse("00000000-0000-0000-0000-000000000000")), Reference.ById(Guid.Parse("eee6db3b-545a-4785-8e86-e3772c8756f9"))),
Schedule = GetExpectedScheduleResponseModel(),
DueDate = GetExpectedDueDateModel(),
Elements = ElementsData.GetExpectedStronglyTypedElementsModel(),
};

private static ScheduleResponseModel GetExpectedScheduleResponseModel() => new()
{
Expand Down
11 changes: 11 additions & 0 deletions Kontent.Ai.Management/IManagementClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,17 @@ public interface IManagementClient
/// <returns>The <see cref="IEnumerable{LanguageVariantModel}"/> instance that represents the listing of language variants.</returns>
Task<IListingResponseModel<LanguageVariantModel>> ListLanguageVariantsByTypeAsync(Reference identifier);

/// <summary>
/// Returns strongly typed listing of language variants with strongly typed elements for the specified content type.
/// The Content management API returns a dynamically paginated listing response limited to up to 100 objects.
/// To check if the next page is available use <see cref="IListingResponseModel{T}.HasNextPage"/>.
/// For getting next page use <see cref="IListingResponseModel{T}.GetNextPage"/>.
/// </summary>
/// <typeparam name="T">Type of the content item elements</typeparam>
/// <param name="identifier">The identifier of the content type.</param>
/// <returns>The <see cref="IEnumerable{LanguageVariantModel}"/> instance that represents the listing of language variants.</returns>
Task<IListingResponseModel<LanguageVariantModel<T>>> ListLanguageVariantsByTypeAsync<T>(Reference identifier) where T : new();

/// <summary>
/// Returns strongly typed listing of language variants containing components by type.
/// The Content management API returns a dynamically paginated listing response limited to up to 100 objects.
Expand Down
19 changes: 19 additions & 0 deletions Kontent.Ai.Management/ManagementClient.LanguageVariant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ public async Task<IListingResponseModel<LanguageVariantModel>> ListLanguageVaria
response.Variants);
}

/// <inheritdoc />
public async Task<IListingResponseModel<LanguageVariantModel<T>>> ListLanguageVariantsByTypeAsync<T>(Reference identifier) where T : new()
{
if (identifier == null)
{
throw new ArgumentNullException(nameof(identifier));
}

var endpointUrl = _urlBuilder.BuildListVariantsByTypeUrl(identifier);
var response = await _actionInvoker.InvokeReadOnlyMethodAsync<LanguageVariantsListingResponseServerModel>(endpointUrl, HttpMethod.Get);

return new ListingResponseMappedModel<LanguageVariantModel, LanguageVariantModel<T>>(
(token, url) => GetNextListingPageAsync<LanguageVariantsListingResponseServerModel, LanguageVariantModel>(token, url),
response.Pagination?.Token,
endpointUrl,
response.Variants,
_modelProvider.GetLanguageVariantModel<T>);
}

/// <inheritdoc />
public async Task<IListingResponseModel<LanguageVariantModel>> ListLanguageVariantsOfContentTypeWithComponentsAsync(Reference identifier)
{
Expand Down

0 comments on commit 8dcac4f

Please sign in to comment.