Skip to content

Commit

Permalink
Add asset codename
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirO-kontent authored and pokornyd committed Sep 13, 2023
1 parent bd732d9 commit ec7910b
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 33 deletions.
1 change: 1 addition & 0 deletions Kontent.Ai.Management.Tests/Data/Asset/Asset.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"id": "01647205-c8c4-4b41-b524-1a98a7b12750",
"codename": "my_super_asset",
"external_id": "asset-1",
"file_name": "our-story.jpg",
"title": "My super asset",
Expand Down
1 change: 1 addition & 0 deletions Kontent.Ai.Management.Tests/Data/Asset/AssetsPage1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"assets": [
{
"id": "00000000-0000-0000-0000-000000000000",
"codename": "my_super_asset",
"external_id": "asset-1",
"file_name": "our-story.jpg",
"title": "My super asset",
Expand Down
1 change: 1 addition & 0 deletions Kontent.Ai.Management.Tests/Data/Asset/AssetsPage2.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"assets": [
{
"id": "10000000-0000-0000-0000-000000000000",
"codename": "my_super_asset",
"external_id": "asset-1",
"file_name": "our-story.jpg",
"title": "My super asset",
Expand Down
1 change: 1 addition & 0 deletions Kontent.Ai.Management.Tests/Data/Asset/AssetsPage3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"assets": [
{
"id": "20000000-0000-0000-0000-000000000000",
"codename": "my_super_asset",
"external_id": "asset-1",
"file_name": "our-story.jpg",
"title": "My super asset",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,21 @@ public async Task ListAssetRenditionsAsync_ById_ReturnsRenditions()
}

[Fact]
public async Task ListAssetRenditionsAsync_ByCodename_Throws()
public async Task ListAssetRenditionsAsync_ByCodename_ReturnsRenditions()
{
var client = _scenario.CreateManagementClient();
var client = _scenario
.WithResponses("AssetRenditionPage1.json", "AssetRenditionPage2.json", "AssetRenditionPage3.json")
.CreateManagementClient();

await client.Invoking(x => x.ListAssetRenditionsAsync(Reference.ByCodename("codename"))).Should().ThrowAsync<Exception>();
var identifier = Reference.ByCodename("codename");
var response = await client.ListAssetRenditionsAsync(identifier).GetAllAsync();

_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Get)
.ListingResponse(response)
.Url($"{Endpoint}/projects/{PROJECT_ID}/assets/codename/{identifier.Codename}/renditions")
.Validate();
}

[Fact]
Expand Down Expand Up @@ -143,16 +153,36 @@ public async Task CreateAssetRenditionAsync_ById_CreatesRenditions()
}

[Fact]
public async Task CreateAssetRenditionAsync_ByCodename_Throws()
public async Task CreateAssetRenditionAsync_ByCodename_CreatesRenditions()
{
var client = _scenario
.WithResponses("AssetRendition.json")
.CreateManagementClient();

var createModel = new AssetRenditionCreateModel
{
ExternalId = "rendition-1",
Transformation = new RectangleResizeTransformation
{
CustomWidth = 120,
CustomHeight = 240,
X = 300,
Y = 200,
Width = 360,
Height = 720,
}
};

var identifier = Reference.ByCodename("codename");
var createRenditionModel = new AssetRenditionCreateModel();
var response = await client.CreateAssetRenditionAsync(identifier, createModel);

await client.Invoking(x => x.CreateAssetRenditionAsync(identifier, createRenditionModel)).Should().ThrowAsync<Exception>();
_scenario
.CreateExpectations()
.HttpMethod(HttpMethod.Post)
.RequestPayload(createModel)
.Response(response)
.Url($"{Endpoint}/projects/{PROJECT_ID}/assets/codename/{identifier.Codename}/renditions")
.Validate();
}

[Fact]
Expand Down Expand Up @@ -284,7 +314,7 @@ public IEnumerator<object[]> GetEnumerator()

public IEnumerable<(AssetRenditionIdentifier Identifier, string Url)> GetPermutation()
{
var assetIdentifier = new[] { ById, ByExternalId };
var assetIdentifier = new[] { ById, ByCodename, ByExternalId };
var renditionIdentifiers = new[] { ById, ByExternalId };

foreach (var item in assetIdentifier)
Expand All @@ -299,6 +329,7 @@ public IEnumerator<object[]> GetEnumerator()
}

private static (Reference Identifier, string UrlSegment) ById => (Reference.ById(Guid.Parse("4b628214-e4fe-4fe0-b1ff-955df33e1515")), "4b628214-e4fe-4fe0-b1ff-955df33e1515");
private static (Reference Identifier, string UrlSegment) ByCodename => (Reference.ByCodename("codename"), "codename/codename");
private static (Reference Identifier, string UrlSegment) ByExternalId => (Reference.ByExternalId("external-id"), "external-id/external-id");
}
}
56 changes: 39 additions & 17 deletions Kontent.Ai.Management.Tests/ManagementClientTests/AssetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ public async Task GetAssetAsync_StronglyTyped_ById_GetsAsset()
}

[Fact]
public async Task GetAssetAsync_StronglyTyped_ByCodename_Throws()
public async Task GetAssetAsync_StronglyTyped_ByCodename_GetsAsset()
{
var client = _fileSystemFixture.CreateMockClientWithoutResponse();
var client = _fileSystemFixture.CreateMockClientWithResponse("Asset.json");

var expected = GetExpectedStronglyTypedAssetModel();

var response = await client.GetAssetAsync<ComplexTestModel>(Reference.ByCodename(expected.Codename));

await client.Invoking(c => c.GetAssetAsync<ComplexTestModel>(Reference.ByCodename("codename")))
.Should().ThrowExactlyAsync<InvalidOperationException>();
response.Should().BeEquivalentTo(expected);
}

[Fact]
Expand Down Expand Up @@ -111,12 +114,15 @@ public async Task GetAssetAsync_DynamicallyTyped_ById_GetsAsset()
}

[Fact]
public async Task GetAssetAsync_DynamicallyTyped_ByCodename_Throws()
public async Task GetAssetAsync_DynamicallyTyped_ByCodename_GetsAsset()
{
var client = _fileSystemFixture.CreateMockClientWithoutResponse();
var client = _fileSystemFixture.CreateMockClientWithResponse("Asset.json");

var expected = GetExpectedDynamicAssetModel();

await client.Invoking(c => c.GetAssetAsync(Reference.ByCodename("codename")))
.Should().ThrowExactlyAsync<InvalidOperationException>();
var response = await client.GetAssetAsync(Reference.ByCodename(expected.Codename));

response.Should().BeEquivalentTo(expected);
}

[Fact]
Expand Down Expand Up @@ -315,12 +321,19 @@ public async Task UpsertAssetAsync_StronglyTyped_ById_UpsertsAsset()
[Fact]
public async Task UpsertAssetAsync_StronglyTyped_ByCodename_UpsertsAsset()
{
var client = _fileSystemFixture.CreateMockClientWithoutResponse();
var client = _fileSystemFixture.CreateMockClientWithResponse("Asset.json");

var updateModel = new AssetUpsertModel<ComplexTestModel> { Title = "xxx" };
var expected = GetExpectedStronglyTypedAssetModel();

await client.Invoking(c => c.UpsertAssetAsync(Reference.ByCodename("c"), updateModel))
.Should().ThrowExactlyAsync<InvalidOperationException>();
var updateModel = new AssetUpsertModel<ComplexTestModel>
{
Title = expected.Title,
Elements = expected.Elements
};

var response = await client.UpsertAssetAsync(Reference.ByCodename(expected.Codename), updateModel);

response.Should().BeEquivalentTo(expected);
}

[Fact]
Expand Down Expand Up @@ -385,12 +398,19 @@ public async Task UpsertAssetAsync_DynamicallyTyped_ById_UpsertsAsset()
[Fact]
public async Task UpsertAssetAsync_DynamicallyTyped_ByCodename_UpsertsAsset()
{
var client = _fileSystemFixture.CreateMockClientWithoutResponse();
var client = _fileSystemFixture.CreateMockClientWithResponse("Asset.json");

var updateModel = new AssetUpsertModel { Title = "xxx" };
var expected = GetExpectedDynamicAssetModel();

await client.Invoking(c => c.UpsertAssetAsync(Reference.ByCodename("c"), updateModel))
.Should().ThrowExactlyAsync<InvalidOperationException>();
var updateModel = new AssetUpsertModel
{
Title = expected.Title,
Elements = expected.Elements
};

var response = await client.UpsertAssetAsync(Reference.ByCodename(expected.Codename), updateModel);

response.Should().BeEquivalentTo(expected);
}

[Fact]
Expand Down Expand Up @@ -580,7 +600,7 @@ public async Task DeleteAssetAsync_ByCodename_DeletesAsset()
var client = _fileSystemFixture.CreateMockClientWithoutResponse();

await client.Invoking(c => c.DeleteAssetAsync(Reference.ByCodename("c")))
.Should().ThrowExactlyAsync<InvalidOperationException>();
.Should().NotThrowAsync();
}

[Fact]
Expand Down Expand Up @@ -634,6 +654,7 @@ private static AssetModel GetExpectedDynamicAssetModel(string assetId = "0164720
return new AssetModel
{
Id = stronglyTyped.Id,
Codename = stronglyTyped.Codename,
ExternalId = stronglyTyped.ExternalId,
FileName = stronglyTyped.FileName,
Title = stronglyTyped.Title,
Expand All @@ -653,6 +674,7 @@ private static AssetModel GetExpectedDynamicAssetModel(string assetId = "0164720
private static AssetModel<ComplexTestModel> GetExpectedStronglyTypedAssetModel(string assetId = "01647205-c8c4-4b41-b524-1a98a7b12750") => new()
{
Id = Guid.Parse(assetId),
Codename = "my_super_asset",
ExternalId = "asset-1",
FileName = "our-story.jpg",
Title = "My super asset",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ public void BuildAssetRenditionsUrlFromAssetExternalIdAndRenditionExternalId_Wit
}

[Fact]
public void BuildAssetRenditionsUrlFromAssetCodenameAndRenditionId_AssetDoesNotSupportCodename_Throws()
public void BuildAssetRenditionsUrlFromAssetCodenameAndRenditionId_WithGivenAssetAndRenditionIds_ReturnsExpectedUrl()
{
_builder.Invoking(x => x.BuildAssetRenditionsUrl(new AssetRenditionIdentifier(Reference.ByCodename("not-supported"), Reference.ById(Guid.NewGuid()))))
.Should().ThrowExactly<InvalidOperationException>();
var assetCodename = "which_brewing_fits_you";
var renditionId = Guid.NewGuid();
var expectedResult = $"{ENDPOINT}/projects/{PROJECT_ID}/assets/codename/{assetCodename}/renditions/{renditionId}";
var actualResult = _builder.BuildAssetRenditionsUrl(new AssetRenditionIdentifier(Reference.ByCodename(assetCodename), Reference.ById(renditionId)));

Assert.Equal(expectedResult, actualResult);
}

[Fact]
Expand Down
8 changes: 6 additions & 2 deletions Kontent.Ai.Management.Tests/Modules/UrlBuilder/AssetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public void BuildAssetsUrlFromId_ById_ReturnsExpectedUrl()
}

[Fact]
public void BuildAssetsUrlFromExternalId_ByCodename_ReturnsExpectedUrl()
public void BuildAssetsUrlFromCodename_ByCodename_ReturnsExpectedUrl()
{
_builder.Invoking(c => c.BuildAssetsUrl(Reference.ByCodename("c"))).Should().Throw<InvalidOperationException>();
var codename = "which_brewing_fits_you";
var expectedResult = $"{ENDPOINT}/projects/{PROJECT_ID}/assets/codename/{codename}";
var actualResult = _builder.BuildAssetsUrl(Reference.ByCodename(codename));

Assert.Equal(expectedResult, actualResult);
}

[Fact]
Expand Down
6 changes: 6 additions & 0 deletions Kontent.Ai.Management/Models/Assets/AssetCreateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public sealed class AssetCreateModel
/// </summary>
[JsonProperty("elements")]
public IEnumerable<dynamic> Elements { get; set; }

/// <summary>
/// Gets or sets the codename of the asset.
/// </summary>
[JsonProperty("codename")]
public string Codename { set; get; }
}
6 changes: 6 additions & 0 deletions Kontent.Ai.Management/Models/Assets/AssetModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public sealed class AssetModel
[JsonProperty("id")]
public Guid Id { get; set; }

/// <summary>
/// Gets or sets the codename of the asset.
/// </summary>
[JsonProperty("codename")]
public string Codename { set; get; }

/// <summary>
/// Gets or sets the file name of the asset.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Kontent.Ai.Management/Models/Assets/AssetUpsertModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public sealed class AssetUpsertModel
/// </summary>
[JsonProperty("file_reference")]
public FileReference FileReference { get; set; }

/// <summary>
/// Gets or sets the codename of the asset.
/// </summary>
[JsonProperty("codename")]
public string Codename { set; get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace Kontent.Ai.Management.Models.StronglyTyped;
[JsonProperty("file_reference")]
public FileReference FileReference { get; set; }

/// <summary>
/// Gets or sets the codename of the asset.
/// </summary>
[JsonProperty("codename")]
public string Codename { set; get; }

/// <summary>
/// Gets or sets the description for the asset.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions Kontent.Ai.Management/Models/StronglyTyped/AssetModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ namespace Kontent.Ai.Management.Models.StronglyTyped;
[JsonProperty("id")]
public Guid Id { get; set; }

/// <summary>
/// Gets or sets the codename of the asset.
/// </summary>
[JsonProperty("codename")]
public string Codename { set; get; }

/// <summary>
/// Gets or sets the file name of the asset.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace Kontent.Ai.Management.Models.StronglyTyped;
/// </summary>
[JsonProperty("file_reference", Required = Required.Always)]
public FileReference FileReference { get; set; }

/// <summary>
/// Gets or sets the codename of the asset.
/// </summary>
[JsonProperty("codename")]
public string Codename { set; get; }

/// <summary>
/// Gets or sets the description for the asset.
Expand Down
3 changes: 3 additions & 0 deletions Kontent.Ai.Management/Modules/ModelBuilders/ModelProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal ModelProvider()
FileReference = asset.FileReference,
Descriptions = asset.Descriptions,
Title = asset.Title,
Codename = asset.Codename,
ExternalId = asset.ExternalId,
LastModified = asset.LastModified,
ImageHeight = asset.ImageHeight,
Expand All @@ -58,6 +59,7 @@ internal ModelProvider()
Descriptions = asset.Descriptions,
Title = asset.Title,
Folder = asset.Folder,
Codename = asset.Codename,
ExternalId = asset.ExternalId,
Collection = asset.Collection,
Elements = _elementModelProvider.GetDynamicElements(asset.Elements),
Expand All @@ -70,6 +72,7 @@ internal ModelProvider()
Descriptions = asset.Descriptions,
Title = asset.Title,
Folder = asset.Folder,
Codename = asset.Codename,
Collection = asset.Collection,
Elements = _elementModelProvider.GetDynamicElements(asset.Elements),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using System;

namespace Kontent.Ai.Management.Modules.UrlBuilder.Templates;
namespace Kontent.Ai.Management.Modules.UrlBuilder.Templates;

internal class AssetTemplate : UrlTemplate
{
public override string Url => "/assets";

public override string UrlId => "/assets/{0}";

public override string UrlCodename => throw new InvalidOperationException("assets do not have codename url");
public override string UrlCodename => "/assets/codename/{0}";

public override string UrlExternalId => "/assets/external-id/{0}";
}

0 comments on commit ec7910b

Please sign in to comment.