Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<ReleaseVersion>$(Version)</ReleaseVersion>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../CSManagementSDK.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.8.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.8.2" />
<PackageReference Include="coverlet.collector" Version="6.0.4"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.2" />
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Helpers\" />
<Folder Include="IntegrationTest\" />
<Folder Include="Model\" />
<Folder Include="Mock\" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Mock\*.json" />
</ItemGroup>
<ItemGroup>
<Content Include="appSettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Remove="Microsoft.AspNetCore.Http" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Contentstack.Management.Core\contentstack.management.core.csproj" />
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>

<IsPackable>false</IsPackable>
<ReleaseVersion>$(Version)</ReleaseVersion>

<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../CSManagementSDK.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.8.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.8.2" />
<PackageReference Include="coverlet.collector" Version="6.0.4"><IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="9.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.2" />
<PackageReference Include="AutoFixture" Version="4.18.1" />
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.3.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="Helpers\" />
<Folder Include="IntegrationTest\" />
<Folder Include="Model\" />
<Folder Include="Mock\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Mock\*.json" />
</ItemGroup>

<ItemGroup>
<Content Include="appSettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<None Remove="Microsoft.AspNetCore.Http" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Contentstack.Management.Core\contentstack.management.core.csproj" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions Contentstack.Management.Core.Tests/Helpers/AssertLogger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Contentstack.Management.Core.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Contentstack.Management.Core.Tests.Helpers
Expand Down Expand Up @@ -110,5 +114,50 @@ public static void Inconclusive(string message)
TestOutputLogger.LogAssertion("Inconclusive", "N/A", message ?? "", false);
Assert.Inconclusive(message);
}

/// <summary>
/// Asserts a Contentstack API error with an HTTP status in the allowed set.
/// </summary>
public static ContentstackErrorException ThrowsContentstackError(Action action, string name, params HttpStatusCode[] acceptableStatuses)
{
var ex = ThrowsException<ContentstackErrorException>(action, name);
IsTrue(
acceptableStatuses.Contains(ex.StatusCode),
$"Expected one of [{string.Join(", ", acceptableStatuses)}] but was {ex.StatusCode}",
"statusCode");
return ex;
}

/// <summary>
/// Async variant: runs the task and expects <see cref="ContentstackErrorException"/> with an allowed status.
/// </summary>
public static async Task<ContentstackErrorException> ThrowsContentstackErrorAsync(Func<Task> action, string name, params HttpStatusCode[] acceptableStatuses)
{
try
{
await action();
TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", "ContentstackErrorException", "NoException", false);
throw new AssertFailedException($"Expected exception ContentstackErrorException was not thrown.");
}
catch (ContentstackErrorException ex)
{
IsTrue(
acceptableStatuses.Contains(ex.StatusCode),
$"Expected one of [{string.Join(", ", acceptableStatuses)}] but was {ex.StatusCode}",
"statusCode");
TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", nameof(ContentstackErrorException), ex.StatusCode.ToString(), true);
return ex;
}
catch (AssertFailedException)
{
throw;
}
catch (Exception ex)
{
TestOutputLogger.LogAssertion($"ThrowsContentstackErrorAsync({name})", nameof(ContentstackErrorException), ex.GetType().Name, false);
throw new AssertFailedException(
$"Expected exception ContentstackErrorException but got {ex.GetType().Name}: {ex.Message}", ex);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Contentstack.Management.Core.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Contentstack.Management.Core.Tests.Helpers
{
/// <summary>
/// Loads embedded content-type JSON and assigns unique UIDs/titles for disposable integration tests.
/// </summary>
public static class ContentTypeFixtureLoader
{
public static ContentModelling LoadFromMock(JsonSerializer serializer, string embeddedFileName, string uidSuffix)
{
var text = Contentstack.GetResourceText(embeddedFileName);
var jo = JObject.Parse(text);
var baseUid = jo["uid"]?.Value<string>() ?? "ct";
jo["uid"] = $"{baseUid}_{uidSuffix}";
var title = jo["title"]?.Value<string>() ?? "CT";
jo["title"] = $"{title} {uidSuffix}";
return jo.ToObject<ContentModelling>(serializer);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Tests.Helpers;
using Contentstack.Management.Core.Tests.Model;
Expand All @@ -8,7 +11,7 @@
namespace Contentstack.Management.Core.Tests.IntegrationTest
{
[TestClass]
public class Contentstack005_ContentTypeTest
public class Contentstack012_ContentTypeTest
{
private static ContentstackClient _client;
private Stack _stack;
Expand Down Expand Up @@ -39,34 +42,30 @@ public void Initialize ()

[TestMethod]
[DoNotParallelize]
public void Test001_Should_Create_Content_Type()
public void Test001_Should_Create_SinglePage_Content_Type()
{
TestOutputLogger.LogContext("TestScenario", "CreateContentType_SinglePage");
ContentstackResponse response = _stack.ContentType().Create(_singlePage);
ContentTypeModel ContentType = response.OpenTResponse<ContentTypeModel>();
TestOutputLogger.LogContext("ContentType", _singlePage.Uid);
AssertLogger.IsNotNull(response, "response");
ContentTypeModel ContentType = TryCreateOrFetchContentType(_singlePage);
AssertLogger.IsNotNull(ContentType, "ContentType");
AssertLogger.IsNotNull(ContentType.Modelling, "ContentType.Modelling");
AssertLogger.AreEqual(_singlePage.Title, ContentType.Modelling.Title, "Title");
AssertLogger.AreEqual(_singlePage.Uid, ContentType.Modelling.Uid, "Uid");
AssertLogger.AreEqual(_singlePage.Schema.Count, ContentType.Modelling.Schema.Count, "SchemaCount");
AssertLogger.IsTrue(ContentType.Modelling.Schema.Count >= _singlePage.Schema.Count, "SchemaCount");
}

[TestMethod]
[DoNotParallelize]
public void Test002_Should_Create_Content_Type()
public void Test002_Should_Create_MultiPage_Content_Type()
{
TestOutputLogger.LogContext("TestScenario", "CreateContentType_MultiPage");
ContentstackResponse response = _stack.ContentType().Create(_multiPage);
ContentTypeModel ContentType = response.OpenTResponse<ContentTypeModel>();
TestOutputLogger.LogContext("ContentType", _multiPage.Uid);
AssertLogger.IsNotNull(response, "response");
ContentTypeModel ContentType = TryCreateOrFetchContentType(_multiPage);
AssertLogger.IsNotNull(ContentType, "ContentType");
AssertLogger.IsNotNull(ContentType.Modelling, "ContentType.Modelling");
AssertLogger.AreEqual(_multiPage.Title, ContentType.Modelling.Title, "Title");
AssertLogger.AreEqual(_multiPage.Uid, ContentType.Modelling.Uid, "Uid");
AssertLogger.AreEqual(_multiPage.Schema.Count, ContentType.Modelling.Schema.Count, "SchemaCount");
AssertLogger.IsTrue(ContentType.Modelling.Schema.Count >= _multiPage.Schema.Count, "SchemaCount");
}

[TestMethod]
Expand All @@ -82,7 +81,7 @@ public void Test003_Should_Fetch_Content_Type()
AssertLogger.IsNotNull(ContentType.Modelling, "ContentType.Modelling");
AssertLogger.AreEqual(_multiPage.Title, ContentType.Modelling.Title, "Title");
AssertLogger.AreEqual(_multiPage.Uid, ContentType.Modelling.Uid, "Uid");
AssertLogger.AreEqual(_multiPage.Schema.Count, ContentType.Modelling.Schema.Count, "SchemaCount");
AssertLogger.IsTrue(ContentType.Modelling.Schema.Count >= _multiPage.Schema.Count, "SchemaCount");
}

[TestMethod]
Expand All @@ -98,7 +97,7 @@ public async System.Threading.Tasks.Task Test004_Should_Fetch_Async_Content_Type
AssertLogger.IsNotNull(ContentType.Modelling, "ContentType.Modelling");
AssertLogger.AreEqual(_singlePage.Title, ContentType.Modelling.Title, "Title");
AssertLogger.AreEqual(_singlePage.Uid, ContentType.Modelling.Uid, "Uid");
AssertLogger.AreEqual(_singlePage.Schema.Count, ContentType.Modelling.Schema.Count, "SchemaCount");
AssertLogger.IsTrue(ContentType.Modelling.Schema.Count >= _singlePage.Schema.Count, "SchemaCount");
}

[TestMethod]
Expand All @@ -115,7 +114,7 @@ public void Test005_Should_Update_Content_Type()
AssertLogger.IsNotNull(ContentType.Modelling, "ContentType.Modelling");
AssertLogger.AreEqual(_multiPage.Title, ContentType.Modelling.Title, "Title");
AssertLogger.AreEqual(_multiPage.Uid, ContentType.Modelling.Uid, "Uid");
AssertLogger.AreEqual(_multiPage.Schema.Count, ContentType.Modelling.Schema.Count, "SchemaCount");
AssertLogger.IsTrue(ContentType.Modelling.Schema.Count >= _multiPage.Schema.Count, "SchemaCount");
}

[TestMethod]
Expand Down Expand Up @@ -152,7 +151,7 @@ public async System.Threading.Tasks.Task Test006_Should_Update_Async_Content_Typ
AssertLogger.IsNotNull(ContentType, "ContentType");
AssertLogger.IsNotNull(ContentType.Modelling, "ContentType.Modelling");
AssertLogger.AreEqual(_multiPage.Uid, ContentType.Modelling.Uid, "Uid");
AssertLogger.AreEqual(_multiPage.Schema.Count, ContentType.Modelling.Schema.Count, "SchemaCount");
AssertLogger.IsTrue(ContentType.Modelling.Schema.Count >= _multiPage.Schema.Count, "SchemaCount");
Console.WriteLine($"Successfully updated content type with {ContentType.Modelling.Schema.Count} fields");
}
else
Expand All @@ -176,7 +175,9 @@ public void Test007_Should_Query_Content_Type()
AssertLogger.IsNotNull(response, "response");
AssertLogger.IsNotNull(ContentType, "ContentType");
AssertLogger.IsNotNull(ContentType.Modellings, "ContentType.Modellings");
AssertLogger.AreEqual(2, ContentType.Modellings.Count, "ModellingsCount");
AssertLogger.IsTrue(ContentType.Modellings.Count >= 2, "At least legacy single_page and multi_page exist");
AssertLogger.IsTrue(ContentType.Modellings.Any(m => m.Uid == _singlePage.Uid), "single_page in query result");
AssertLogger.IsTrue(ContentType.Modellings.Any(m => m.Uid == _multiPage.Uid), "multi_page in query result");
}

[TestMethod]
Expand All @@ -189,7 +190,29 @@ public async System.Threading.Tasks.Task Test008_Should_Query_Async_Content_Type
AssertLogger.IsNotNull(response, "response");
AssertLogger.IsNotNull(ContentType, "ContentType");
AssertLogger.IsNotNull(ContentType.Modellings, "ContentType.Modellings");
AssertLogger.AreEqual(2, ContentType.Modellings.Count, "ModellingsCount");
AssertLogger.IsTrue(ContentType.Modellings.Count >= 2, "At least legacy single_page and multi_page exist");
AssertLogger.IsTrue(ContentType.Modellings.Any(m => m.Uid == _singlePage.Uid), "single_page in query result");
AssertLogger.IsTrue(ContentType.Modellings.Any(m => m.Uid == _multiPage.Uid), "multi_page in query result");
}

/// <summary>
/// Creates the content type when missing; otherwise fetches it (stack may already have legacy types).
/// </summary>
private ContentTypeModel TryCreateOrFetchContentType(ContentModelling modelling)
{
try
{
var response = _stack.ContentType().Create(modelling);
return response.OpenTResponse<ContentTypeModel>();
}
catch (ContentstackErrorException ex) when (
ex.StatusCode == HttpStatusCode.UnprocessableEntity
|| ex.StatusCode == HttpStatusCode.Conflict
|| ex.StatusCode == (HttpStatusCode)422)
{
var response = _stack.ContentType(modelling.Uid).Fetch();
return response.OpenTResponse<ContentTypeModel>();
}
}
}
}
Loading
Loading