Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
gormal committed Sep 2, 2021
1 parent fec6aee commit 709603e
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public ContentTypeSnippetTests(ITestOutputHelper output)
[Fact]
public async void ListSnippet_WithContinuation_ListsSnippet()
{


var response = await _client.ListContentTypeSnippetsAsync();
Assert.NotNull(response);

Expand All @@ -61,8 +59,6 @@ public async void ListSnippet_WithContinuation_ListsSnippet()
[Fact]
public async void GetSnippet_ById_GetsSnippet()
{


var identifier = Reference.ById(EXISTING_SNIPPET_ID);

var response = await _client.GetContentTypeSnippetAsync(identifier);
Expand All @@ -72,8 +68,6 @@ public async void GetSnippet_ById_GetsSnippet()
[Fact]
public async void GetSnippet_ByCodename_GetsSnippet()
{


var identifier = Reference.ByCodename(EXISTING_SNIPPET_CODENAME);

var response = await _client.GetContentTypeSnippetAsync(identifier);
Expand All @@ -85,8 +79,6 @@ public async void GetSnippet_ByExternalId_GetsSnippet()
{
var externalId = "baf884be-531f-441f-ae88-64205efdd0f6";



var identifier = Reference.ByExternalId(externalId);

var response = await _client.GetContentTypeSnippetAsync(identifier);
Expand All @@ -96,8 +88,6 @@ public async void GetSnippet_ByExternalId_GetsSnippet()
[Fact]
public async void DeleteSnippet_ById_DeletesSnippet()
{


var responseType = await CreateSnippet();

var identifier = Reference.ById(responseType.Id);
Expand All @@ -114,8 +104,6 @@ public async void DeleteSnippet_ById_DeletesSnippet()
[Fact]
public async void DeleteSnippet_ByCodename_DeletesSnippet()
{


var responseType = await CreateSnippet();

var identifier = Reference.ByCodename(responseType.Codename);
Expand All @@ -133,8 +121,6 @@ public async void DeleteSnippet_ByCodename_DeletesSnippet()
[Fact]
public async void DeleteSnippet_ByExternalId_DeletesSnippet()
{


var responseType = await CreateSnippet();

var identifier = Reference.ByExternalId(responseType.ExternalId);
Expand All @@ -153,8 +139,6 @@ public async void DeleteSnippet_ByExternalId_DeletesSnippet()
//Todo create more elements
public async void CreateSnippet_CreatesSnippet()
{


var typeName = "HoorayType!";
var typeCodename = "hooray_codename_type";
var typeExternalId = "hooray_codename_external_id";
Expand Down Expand Up @@ -189,8 +173,6 @@ public async void CreateSnippet_CreatesSnippet()
public async void ModifySnippet_AddInto_ModifiesSnippet()
{
//Arrange


var responseType = await CreateSnippet();

var elementExternalId = "snippet_external_id2_patchaddinto";
Expand Down Expand Up @@ -219,7 +201,7 @@ public async void ModifySnippet_AddInto_ModifiesSnippet()


//assert
var addedElement = modifiedType.Elements.FirstOrDefault(x => x.ExternalId == elementExternalId).ToTextElement();
var addedElement = modifiedType.Elements.FirstOrDefault(x => x.ExternalId == elementExternalId).ToElement<TextElementMetadataModel>();
Assert.NotNull(addedElement);
Assert.Equal(addedElement.Name, textName);

Expand All @@ -233,9 +215,6 @@ public async void ModifySnippet_AddInto_ModifiesSnippet()
public async void ModifySnippet_Replace_ModifiesSnippet()
{
//arrange
//todo extract creation of type to method


var responseType = await CreateSnippet();

var expectedValue = "<h1>Here you can tell users how to fill in the element.</h1>";
Expand All @@ -253,7 +232,8 @@ public async void ModifySnippet_Replace_ModifiesSnippet()


//Assert
Assert.Equal(expectedValue, modifiedType.Elements.FirstOrDefault(x => x.Codename == responseType.Elements.First().Codename)?.ToGuidelines().Guidelines);
Assert.Equal(expectedValue,
modifiedType.Elements.FirstOrDefault(x => x.Codename == responseType.Elements.First().Codename)?.ToElement<GuidelinesElementMetadataModel>().Guidelines);


// Cleanup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public async void ModifyContentType_AddInto_ModifiesContentType()


//assert
var addedElement = modifiedType.Elements.FirstOrDefault(x => x.Codename == elementCodename).ToTextElement();
var addedElement = modifiedType.Elements.FirstOrDefault(x => x.Codename == elementCodename).ToElement<TextElementMetadataModel>();
Assert.NotNull(addedElement);
Assert.Equal(addedElement.Name, textName);

Expand Down Expand Up @@ -247,7 +247,8 @@ public async void ModifyContentType_Replace_ModifiesContentType()


//Assert
Assert.Equal(expectedValue, modifiedType.Elements.FirstOrDefault(x => x.Codename == responseType.Elements.First().Codename)?.ToGuidelines().Guidelines);
Assert.Equal(expectedValue,
modifiedType.Elements.FirstOrDefault(x => x.Codename == responseType.Elements.First().Codename)?.ToElement<GuidelinesElementMetadataModel>().Guidelines);


// Cleanup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ private ManagementClient CreateManagementClient(string testName)
id = typeof(TweetTestModel).GetProperty(nameof(TweetTestModel.Theme)).GetKontentElementId()
},
value = new[] {
// TODO - decide whether it is better to use ID instead of codename
NoExternalIdIdentifier.ByCodename(TWEET_THEME_ELEMENT_DARK_OPTION_CODENAME)
}
},
Expand All @@ -165,7 +164,6 @@ private ManagementClient CreateManagementClient(string testName)
id = typeof(TweetTestModel).GetProperty(nameof(TweetTestModel.DisplayOptions)).GetKontentElementId()
},
value = new[] {
// TODO - decide whether it is better to use ID instead of codename
NoExternalIdIdentifier.ByCodename(TWEET_DISPLAY_OPTIONS_HIDE_THREAD_OPTION_CODENAME)
}
}
Expand Down Expand Up @@ -193,7 +191,6 @@ private ManagementClient CreateManagementClient(string testName)
element = new {
id = typeof(ComplexTestModel).GetProperty(nameof(ComplexTestModel.Personas)).GetKontentElementId()
},
// TODO - decide whether it is better to use ID instead of codename
value = new[] { NoExternalIdIdentifier.ByCodename(EXISTING_TAXONOMY_TERM_CODENAME) } ,
codename = typeof(ComplexTestModel).GetProperty(nameof(ComplexTestModel.Personas)).GetCustomAttribute<JsonPropertyAttribute>()?.PropertyName
},
Expand All @@ -213,7 +210,6 @@ private ManagementClient CreateManagementClient(string testName)
},
value = new[]
{
// TODO - decide whether it is better to use ID instead of codename
NoExternalIdIdentifier.ByCodename(EXISTING_MULTIPLE_CHOICE_OPTION_CODENAME_PAID),
NoExternalIdIdentifier.ByCodename(EXISTING_MULTIPLE_CHOICE_OPTION_CODENAME_FEATURED)
},
Expand Down
112 changes: 99 additions & 13 deletions Kentico.Kontent.Management/Extenstions/ElementMetadataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,113 @@

namespace Kentico.Kontent.Management.Extenstions
{
/// <summary>
/// Extensions methods for the ElementMetadataBase class.
/// </summary>
public static class ElementMetadataExtension
{
//todo provide methods for all elementMetada types
public static TextElementMetadataModel ToTextElement(this ElementMetadataBase source)
/// <summary>
/// Transform the base class to the specific element.
/// </summary>
/// <param name="source">ElementMetadataBase class</param>
/// <returns></returns>
public static T ToElement<T>(this ElementMetadataBase source) where T : ElementMetadataBase
{
if(source.Type != ElementMetadataType.Text)
if(source == null)
{
throw new InvalidOperationException($"type {source.Type} cannot be converted to {nameof(TextElementMetadataModel)}");
return null;
}

return source as TextElementMetadataModel;
}

public static GuidelinesElementMetadataModel ToGuidelines(this ElementMetadataBase source)
{
if (source.Type != ElementMetadataType.Guidelines)
switch (source.Type)
{
throw new InvalidOperationException($"type {source.Type} cannot be converted to {nameof(GuidelinesElementMetadataModel)}");
}
case ElementMetadataType.Undefined:
throw new InvalidOperationException($"Type {source.Type} cannot be converted.");
case ElementMetadataType.Text:
if (source.Type != ElementMetadataType.Text)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(TextElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.RichText:
if (source.Type != ElementMetadataType.RichText)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(RichTextElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.Number:
if (source.Type != ElementMetadataType.Number)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(NumberElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.MultipleChoice:
if (source.Type != ElementMetadataType.MultipleChoice)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(MultipleChoiceElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.DateTime:
if (source.Type != ElementMetadataType.DateTime)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(DateTimeElementMetadataModel)}");
}

return source as GuidelinesElementMetadataModel;
return source as T;
case ElementMetadataType.Asset:
if (source.Type != ElementMetadataType.Asset)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(AssetElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.LinkedItems:
if (source.Type != ElementMetadataType.LinkedItems)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(LinkedItemsElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.Guidelines:
if (source.Type != ElementMetadataType.Guidelines)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(GuidelinesElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.Taxonomy:
if (source.Type != ElementMetadataType.Taxonomy)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(TaxonomyElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.UrlSlug:
if (source.Type != ElementMetadataType.UrlSlug)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(UrlSlugElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.Snippet:
if (source.Type != ElementMetadataType.Snippet)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(ContentTypeSnippetElementMetadataModel)}");
}

return source as T;
case ElementMetadataType.Custom:
if (source.Type != ElementMetadataType.Custom)
{
throw new InvalidOperationException($"Type {source.Type} cannot be converted to {nameof(CustomElementMetadataModel)}");
}

return source as T;
default: throw new InvalidOperationException($"Type {source.Type} cannot be converted to any known element");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class ComponentModel
/// <summary>
/// Gets or sets type of the component.
/// </summary>
// TODO validate if IEnumerable<object> would be more suitable
[JsonProperty("elements", Required = Required.Always)]
public IEnumerable<dynamic> Elements { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public sealed class ContentItemVariantModel
/// <summary>
/// Gets or sets workflow steps of the content item.
/// </summary>
//todo set?
[JsonProperty("workflow_step")]
public ObjectIdentifier WorkflowStep { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Kentico.Kontent.Management.Models.Assets;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Kentico.Kontent.Management.Models.LanguageVariants.Elements
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ public class CustomElement : BaseElement
[JsonProperty("value")]
public string Value { get; set; }

// TODO - add searchable value https://docs.kontent.ai/reference/management-api-v2#section/Custom-element
/// <summary>
/// Gets or sets searchable value of the custom element.
/// </summary>
[JsonProperty("searchable_value")]
public string SearchableValue { get; set; }

public CustomElement(dynamic data = null) : base((object)data)
{
if (data != null)
{
Value = data.value?.ToString();
//todo SearchableValue
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ public class LinkedItemsElement : BaseElement
/// Gets or sets value of linked items element.
/// </summary>
[JsonProperty("value")]
// TODO should be ContentItemIdentifier, or ContentItemVariantIdentifier
public IEnumerable<Reference> Value { get; set; }

public LinkedItemsElement(dynamic data = null) : base((object)data)
{
if (data != null)
{
// TODO - Verify if the internal type <ObjectIdentifier> is ok - maybe ContentItemIdentifier would fit in DynamicObjectJsonCoverter better
Value = (data.value as IEnumerable<dynamic>)?.Select<dynamic, Reference>(item => Reference.ById(Guid.Parse(item.id)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public MultipleChoiceElement(dynamic data = null) : base((object)data)
{
if (data != null)
{
// TODO - Verify if the internal type <ObjectIdentifier> is ok - maybe TaxonomyTermIdentifier would fit in DynamicObjectJsonCoverter better
Value = (data.value as IEnumerable<dynamic>).Select<dynamic, NoExternalIdIdentifier>(identifier => NoExternalIdIdentifier.ById(Guid.Parse(identifier.id)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public TaxonomyElement(dynamic data = null) : base((object)data)
{
if (data != null)
{
// TODO - Verify if the internal type <ObjectIdentifier> is ok - maybe TaxonomyTermIdentifier would fit in DynamicObjectJsonCoverter better
Value = (data.value as IEnumerable<dynamic>)?.Select<dynamic, NoExternalIdIdentifier>(identifier => NoExternalIdIdentifier.ById(Guid.Parse(identifier.id)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class LanguagePatchModel
[JsonProperty("property_name")]
public LanguangePropertyName PropertyName { get; set; }

//todo make it strongly typed
[JsonProperty("value")]
public dynamic Value { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class TaxonomyGroupReplacePatchModel : TaxonomyGroupOperationBaseModel
[JsonProperty("property_name")]
public PropertyName PropertyName { get; set; }

//todo make it strongly typed
[JsonProperty("value")]
public dynamic Value { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class SnippetPatchReplaceModel : SnippetOperationBaseModel
public override string Op => "replace";

[JsonProperty("value")]
//todo make it strongly typed
public dynamic Value { get; set; }

[JsonProperty("before")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class TextElementMetadataModel : ElementMetadataBase
[JsonProperty("maximum_text_length")]
public MaximumTextLengthModel MaximumTextLength { get; set; }

//todo possible validation in getter
public override ElementMetadataType Type => ElementMetadataType.Text;
}
}
Loading

0 comments on commit 709603e

Please sign in to comment.