Skip to content

Commit

Permalink
187 respect custom namespace setting for typed models
Browse files Browse the repository at this point in the history
  • Loading branch information
Sevitas committed Jun 4, 2023
1 parent 6a65eb4 commit 1adf7dc
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal IEnumerable<ClassCodeGenerator> GetClassCodeGenerators(ContentTypeModel

return new List<ClassCodeGenerator>
{
new TypedExtendedDeliveryClassCodeGenerator(typedClassDefinition, GetFileClassName(classDefinition.ClassName + TypedSuffixFileName)),
new TypedExtendedDeliveryClassCodeGenerator(typedClassDefinition, GetFileClassName(classDefinition.ClassName + TypedSuffixFileName), Options.Namespace),
ClassCodeGeneratorFactory.CreateClassCodeGenerator(Options, classDefinition, GetFileClassName(classDefinition.ClassName), Logger)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Kontent.Ai.ModelGenerator.Core.Common;
using Kontent.Ai.ModelGenerator.Core.Configuration;
using Kontent.Ai.ModelGenerator.Core.Contract;
using Kontent.Ai.ModelGenerator.Core.Generators.Class;
using Kontent.Ai.ModelGenerator.Core.Services;
using Microsoft.Extensions.Options;
using Moq;
Expand Down Expand Up @@ -103,6 +104,47 @@ public void GetClassCodeGenerator_Returns(StructuredModelFlags structuredModel)
result.ClassFilename.Should().Be($"{contentTypeCodename}.Generated");
}

[Theory]
[InlineData("CustomNamespace", "CustomNamespace")]
[InlineData(null, ClassCodeGenerator.DefaultNamespace)]
public void GetClassCodeGenerator_CustomNamespace_Returns(string customNamespace, string expectedNamespace)
{
var mockOptions = new Mock<IOptions<CodeGeneratorOptions>>();
mockOptions.SetupGet(option => option.Value).Returns(new CodeGeneratorOptions
{
ManagementApi = false,
Namespace = customNamespace
});

var elementCodename = "element_codename";
var elementType = "text";
var contentElement = new Mock<IContentElement>();
contentElement.SetupGet(element => element.Type).Returns(elementType);
contentElement.SetupGet(element => element.Codename).Returns(elementCodename);

var contentType = new Mock<IContentType>();
var contentTypeCodename = "Contenttype";
contentType.SetupGet(type => type.System.Codename).Returns(contentTypeCodename);
contentType.SetupGet(type => type.Elements).Returns(new Dictionary<string, IContentElement> { { elementCodename, contentElement.Object } });

_deliveryElementService.Setup(x => x.GetElementType(elementType)).Returns(elementType);

var codeGenerator = new DeliveryCodeGenerator(
mockOptions.Object,
_outputProviderMock.Object,
_deliveryClientMock.Object,
ClassCodeGeneratorFactory,
ClassDefinitionFactory,
_deliveryElementService.Object,
Logger.Object);

var result = codeGenerator.GetClassCodeGenerator(contentType.Object);

Logger.VerifyNoOtherCalls();
result.ClassFilename.Should().Be($"{contentTypeCodename}.Generated");
result.Namespace.Should().Be(expectedNamespace);
}

[Fact]
public void GetClassCodeGenerator_DuplicateSystemProperty_MessageIsLogged()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,133 @@ public void GetClassCodeGenerators_ExtendedDeliverPreviewModelsIsFalse_Returns(S
result.Should().BeEquivalentTo(expected);
}

[Theory]
[InlineData("CustomNamespace", "CustomNamespace")]
[InlineData(null, ClassCodeGenerator.DefaultNamespace)]
public void GetClassCodeGenerators_CustomNamespace_Returns(string customNamespace, string expectedNamespace)
{
var structuredModel = StructuredModelFlags.NotSet;
var mockOptions = new Mock<IOptions<CodeGeneratorOptions>>();
mockOptions.SetupGet(option => option.Value).Returns(new CodeGeneratorOptions
{
ManagementApi = false,
ExtendedDeliveryModels = true,
Namespace = customNamespace
});

var contentType = new ContentTypeModel
{
Codename = "content_type",
Elements = new List<ElementMetadataBase>
{
LinkedItemsContentTypeData.SingleAllowedTypeMultiItems,
LinkedItemsContentTypeData.SingleAllowedTypeExactlySingleItem,
LinkedItemsContentTypeData.SingleAllowedTypeAtMostSingleItem,
LinkedItemsContentTypeData.MultiAllowedTypesExactlySingleItem,
LinkedItemsContentTypeData.MultiAllowedTypesAtMostSingleItem,
LinkedItemsContentTypeData.MultiAllowedTypesMultiItems,
SubpagesContentTypeData.SingleAllowedTypeMultiItems,
SubpagesContentTypeData.SingleAllowedTypeExactlySingleItem,
SubpagesContentTypeData.SingleAllowedTypeAtMostSingleItem,
SubpagesContentTypeData.MultiAllowedTypesExactlySingleItem,
SubpagesContentTypeData.MultiAllowedTypesAtMostSingleItem,
SubpagesContentTypeData.MultiAllowedTypesMultiItems,
}
};

var contentTypes = new List<ContentTypeModel>
{
contentType,
LinkedItemsContentTypeData.ArticleContentType,
LinkedItemsContentTypeData.HeroContentType,
SubpagesContentTypeData.HeroContentType,
SubpagesContentTypeData.ArticleContentType
};

foreach (var elementMetadataBase in contentType.Elements)
{
_deliveryElementService.Setup(x => x.GetElementType(elementMetadataBase.Type.ToString())).Returns(elementMetadataBase.Type.ToString());
}

var codeGenerator = new ExtendedDeliveryCodeGenerator(
mockOptions.Object,
_outputProvider,
_managementClient,
ClassCodeGeneratorFactory,
ClassDefinitionFactory,
_deliveryElementService.Object,
Logger.Object);

var result = codeGenerator.GetClassCodeGenerators(contentType, new List<ContentTypeSnippetModel>(), contentTypes).ToList();

var expectedTypedExtendedDeliveryClassDefinition = new ClassDefinition(contentType.Codename);
expectedTypedExtendedDeliveryClassDefinition.Properties.AddRange(new List<Property>
{
Property.FromContentTypeElement(
LinkedItemsContentTypeData.SingleAllowedTypeMultiItems,
$"IEnumerable<{LinkedItemsContentTypeData.HeroContentType.Name}>",
"Modular_Content_Heroes_Hero"),
Property.FromContentTypeElement(LinkedItemsContentTypeData.SingleAllowedTypeExactlySingleItem, LinkedItemsContentTypeData.ArticleContentType.Name),
Property.FromContentTypeElement(LinkedItemsContentTypeData.SingleAllowedTypeAtMostSingleItem, LinkedItemsContentTypeData.HeroContentType.Name),
Property.FromContentTypeElement(
SubpagesContentTypeData.SingleAllowedTypeMultiItems,
$"IEnumerable<{SubpagesContentTypeData.HeroContentType.Name}>",
"Subpages_Heroes_Hero"),
Property.FromContentTypeElement(SubpagesContentTypeData.SingleAllowedTypeExactlySingleItem, SubpagesContentTypeData.ArticleContentType.Name),
Property.FromContentTypeElement(SubpagesContentTypeData.SingleAllowedTypeAtMostSingleItem, SubpagesContentTypeData.HeroContentType.Name)
});
expectedTypedExtendedDeliveryClassDefinition.PropertyCodenameConstants.AddRange(new List<string>
{
"Modular_Content_Heroes_Hero",
"modular_content_article",
"modular_content_hero",
"Subpages_Heroes_Hero",
"subpages_article",
"subpages_hero"
});

var expectedExtendedDeliveryClassDefinition = new ClassDefinition(contentType.Codename);
expectedExtendedDeliveryClassDefinition.Properties.AddRange(new List<Property>
{
Property.FromContentTypeElement(LinkedItemsContentTypeData.SingleAllowedTypeMultiItems, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(LinkedItemsContentTypeData.SingleAllowedTypeExactlySingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(LinkedItemsContentTypeData.SingleAllowedTypeAtMostSingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(LinkedItemsContentTypeData.MultiAllowedTypesExactlySingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(LinkedItemsContentTypeData.MultiAllowedTypesAtMostSingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(LinkedItemsContentTypeData.MultiAllowedTypesMultiItems, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(SubpagesContentTypeData.SingleAllowedTypeMultiItems, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(SubpagesContentTypeData.SingleAllowedTypeExactlySingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(SubpagesContentTypeData.SingleAllowedTypeAtMostSingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(SubpagesContentTypeData.MultiAllowedTypesExactlySingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(SubpagesContentTypeData.MultiAllowedTypesAtMostSingleItem, DefaultLinkedItemsType(structuredModel)),
Property.FromContentTypeElement(SubpagesContentTypeData.MultiAllowedTypesMultiItems, DefaultLinkedItemsType(structuredModel))
});
expectedExtendedDeliveryClassDefinition.PropertyCodenameConstants.AddRange(new List<string>
{
"modular_content_heroes",
"modular_content_article",
"modular_content_hero",
"modular_content_blog",
"modular_content_coffee",
"modular_content_coffees",
"subpages_heroes",
"subpages_article",
"subpages_hero",
"subpages_blog",
"subpages_coffee",
"subpages_coffees"
});

var expected = new List<ClassCodeGenerator>
{
new TypedExtendedDeliveryClassCodeGenerator(expectedTypedExtendedDeliveryClassDefinition, "ContentType.Typed.Generated", expectedNamespace),
new ExtendedDeliveryClassCodeGenerator(expectedExtendedDeliveryClassDefinition, "ContentType.Generated", false, Logger.Object, expectedNamespace)
};

Logger.VerifyNoOtherCalls();
result.Should().BeEquivalentTo(expected);
}

[Fact]
public void GetClassCodeGenerators_InvalidIdentifier_MessageIsLogged()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Kontent.Ai.ModelGenerator.Core.Common;
using Kontent.Ai.ModelGenerator.Core.Configuration;
using Kontent.Ai.ModelGenerator.Core.Contract;
using Kontent.Ai.ModelGenerator.Core.Generators.Class;
using Kontent.Ai.ModelGenerator.Core.Tests.TestHelpers;
using Microsoft.Extensions.Options;
using Moq;
Expand Down Expand Up @@ -161,13 +162,16 @@ public async Task RunAsync_ContentTypeHasInvalidIdentifier_MessageIsLogged()
result.Should().Be(0);
}

[Fact]
public void GetClassCodeGenerator_Returns()
[Theory]
[InlineData("CustomNamespace", "CustomNamespace")]
[InlineData(null, ClassCodeGenerator.DefaultNamespace)]
public void GetClassCodeGenerator_Returns(string customNamespace, string expectedNamespace)
{
var mockOptions = new Mock<IOptions<CodeGeneratorOptions>>();
mockOptions.SetupGet(option => option.Value).Returns(new CodeGeneratorOptions
{
ManagementApi = true
ManagementApi = true,
Namespace = customNamespace
});

var managementClient = new Mock<IManagementClient>();
Expand Down Expand Up @@ -195,6 +199,7 @@ public void GetClassCodeGenerator_Returns()

Logger.VerifyNoOtherCalls();
result.ClassFilename.Should().Be($"{contentTypeCodename}.Generated");
result.Namespace.Should().Be(expectedNamespace);
}

[Fact]
Expand Down

0 comments on commit 1adf7dc

Please sign in to comment.