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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [v1.2.0](https://github.com/contentstack/contentstack-management-dotnet/tree/v1.2.0)(2026-11-12)

- **New**
- **Branch create: optional environment list**: `BranchModel.PublishConfig` (`BranchPublishConfigModel`, with `Environments` as a list of environment names, not UIDs) lets `Branch.Create` / `CreateAsync` clone only the listed environments. `[]` clones none; leaving `PublishConfig` unset clones all environments, as before. The list is sent unchanged; an unknown name or a mix rejects with the API's 422 error (`ContentstackErrorException`, message, error code and errors unchanged) and no branch is created.
- **Branch fetch**: a fetched `BranchModel` carries `PublishConfig.Environments` as the API returns it; it is `null` when the API omits it.
- **Test**
- Unit tests for the branch create body with two names, `[]` and unset (through `Create` and `CreateAsync`), a mocked 422 rejection, and fetched branch deserialization.
- Integration tests for create with two names, `[]`, an unknown name and a mix. The test class now creates two uniquely named environments on the stack and deletes them afterwards, because the `development` and `staging` names it used before do not exist on the stack the suite creates, so the two-name case failed with a 422. Run live against the NA prod API, the four environment-list cases pass.

## [v1.1.1](https://github.com/contentstack/contentstack-management-dotnet/tree/v1.1.1)(2026-07-30)

- **Fix**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public static void Fail(string message)
public static void Fail(string message, params object[] parameters)
{
TestOutputLogger.LogAssertion("Fail", "N/A", message ?? "", false);
Assert.Fail(message, parameters);
// MSTest 4 removed Assert.Fail(string, params object[]); it formatted the message
// with the parameters, so format here to preserve that behaviour.
Assert.Fail(parameters is { Length: > 0 } ? string.Format(message, parameters) : message);
}

public static void Inconclusive(string message)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ public void Test031_Should_Throw_When_Clone_With_Null_Name()
try
{
releaseUid = CreateTestRelease();
Assert.ThrowsException<ArgumentNullException>(() => _stack.Release(releaseUid).Clone(null, _testReleaseDescription));
Assert.ThrowsExactly<ArgumentNullException>(() => _stack.Release(releaseUid).Clone(null, _testReleaseDescription));
}
finally
{
Expand All @@ -1311,7 +1311,7 @@ public async Task Test032_Should_Throw_When_Clone_With_Null_Name_Async()
try
{
releaseUid = await CreateTestReleaseAsync();
await Assert.ThrowsExceptionAsync<ArgumentNullException>(() => _stack.Release(releaseUid).CloneAsync(null, _testReleaseDescription));
await Assert.ThrowsExactlyAsync<ArgumentNullException>(() => _stack.Release(releaseUid).CloneAsync(null, _testReleaseDescription));
}
finally
{
Expand Down
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 System.Text.Json.Nodes;
using System.Threading.Tasks;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
Expand All @@ -24,28 +27,65 @@ public class Contentstack005_BranchTest
private const string TestBranchUidAsync = "dotnet_int_br_a"; // max 15 chars, alphanumeric+underscore
private const string NonExistentBranchUid = "dotnet_no_br";

// publish_config.environments: names (not UIDs) that must exist on the test stack.
// Set these to two real environment names on the stack before running the live suite.
private static readonly string EnvironmentOne = "dotnet_brenv_" + Guid.NewGuid().ToString("N").Substring(0,6);
private static string _envOneUid;
private static readonly string EnvironmentTwo = "dotnet_brenv_" + Guid.NewGuid().ToString("N").Substring(0,6);
private static string _envTwoUid;
private const string UnknownEnvironment = "dotnet_no_env_99999";
private const string EnvTwoBranchUid = "dotnet_env_two"; // max 15 chars, alphanumeric+underscore
private const string EnvNoneBranchUid = "dotnet_env_none"; // max 15 chars, alphanumeric+underscore
private const string EnvUnknownBranchUid = "dotnet_env_unk"; // max 15 chars, alphanumeric+underscore
private const string EnvMixBranchUid = "dotnet_env_mix"; // max 15 chars, alphanumeric+underscore

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
_client = Contentstack.CreateAuthenticatedClient();
StackResponse response = StackResponse.getStack(_client.serializer);
_stack = _client.Stack(response.Stack.APIKey);

_envOneUid = CreateTestEnvironment(EnvironmentOne);
_envTwoUid = CreateTestEnvironment(EnvironmentTwo);

// Best-effort cleanup of any branches left over from a previous test run
TryDeleteBranch(TestBranchUid);
TryDeleteBranch(TestBranchUidAsync);
TryDeleteBranch(EnvTwoBranchUid);
TryDeleteBranch(EnvNoneBranchUid);
TryDeleteBranch(EnvUnknownBranchUid);
TryDeleteBranch(EnvMixBranchUid);
}

[ClassCleanup]
public static void ClassCleanup()
{
TryDeleteBranch(_createdBranchUid);
TryDeleteBranch(_createdBranchUidAsync);
TryDeleteBranch(EnvTwoBranchUid);
TryDeleteBranch(EnvNoneBranchUid);
TryDeleteBranch(EnvUnknownBranchUid);
TryDeleteBranch(EnvMixBranchUid);

try { if (_envOneUid != null) _stack.Environment(_envOneUid).Delete(); } catch { }
try { if (_envTwoUid != null) _stack.Environment(_envTwoUid).Delete(); } catch { }

try { _client?.Logout(); } catch { }
_client = null;
}

private static string CreateTestEnvironment(string name)
{
var model = new EnvironmentModel
{
Name = name,
Urls = new List<LocalesUrl> { new LocalesUrl { Locale = "en-us", Url = "https://example.com" } }
};
ContentstackResponse r = _stack.Environment().Create(model);
return r.IsSuccessStatusCode ? r.OpenJsonObjectResponse()["environment"]?["uid"]?.ToString() : null;
}

private static void TryDeleteBranch(string uid)
{
if (string.IsNullOrEmpty(uid)) return;
Expand Down Expand Up @@ -320,7 +360,7 @@ private static async Task<ContentstackResponse> RetryOnBranchProvisioningAsync(F
public void Test012_Should_Throw_When_Query_Called_With_Uid()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Query_WithUid_Throws");
Assert.ThrowsException<InvalidOperationException>(() =>
Assert.ThrowsExactly<InvalidOperationException>(() =>
_stack.Branch("some-uid").Query());
}

Expand All @@ -329,7 +369,7 @@ public void Test012_Should_Throw_When_Query_Called_With_Uid()
public void Test013_Should_Throw_When_Fetch_Called_Without_Uid()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Fetch_NoUid_Throws_Sync");
Assert.ThrowsException<ArgumentException>(() =>
Assert.ThrowsExactly<ArgumentException>(() =>
_stack.Branch().Fetch());
}

Expand All @@ -338,7 +378,7 @@ public void Test013_Should_Throw_When_Fetch_Called_Without_Uid()
public async Task Test014_Should_Throw_When_FetchAsync_Called_Without_Uid()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Fetch_NoUid_Throws_Async");
await Assert.ThrowsExceptionAsync<ArgumentException>(() =>
await Assert.ThrowsExactlyAsync<ArgumentException>(() =>
_stack.Branch().FetchAsync());
}

Expand All @@ -347,7 +387,7 @@ await Assert.ThrowsExceptionAsync<ArgumentException>(() =>
public void Test015_Should_Throw_When_Delete_Called_Without_Uid()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Delete_NoUid_Throws_Sync");
Assert.ThrowsException<ArgumentException>(() =>
Assert.ThrowsExactly<ArgumentException>(() =>
_stack.Branch().Delete());
}

Expand All @@ -356,7 +396,7 @@ public void Test015_Should_Throw_When_Delete_Called_Without_Uid()
public async Task Test016_Should_Throw_When_DeleteAsync_Called_Without_Uid()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Delete_NoUid_Throws_Async");
await Assert.ThrowsExceptionAsync<ArgumentException>(() =>
await Assert.ThrowsExactlyAsync<ArgumentException>(() =>
_stack.Branch().DeleteAsync());
}

Expand All @@ -365,7 +405,7 @@ await Assert.ThrowsExceptionAsync<ArgumentException>(() =>
public void Test017_Should_Throw_When_Create_Called_With_Uid()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Create_WithUid_Throws_Sync");
Assert.ThrowsException<InvalidOperationException>(() =>
Assert.ThrowsExactly<InvalidOperationException>(() =>
_stack.Branch("some-uid").Create(new BranchModel()));
}

Expand All @@ -374,7 +414,7 @@ public void Test017_Should_Throw_When_Create_Called_With_Uid()
public async Task Test018_Should_Throw_When_CreateAsync_Called_With_Uid()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Create_WithUid_Throws_Async");
await Assert.ThrowsExceptionAsync<InvalidOperationException>(() =>
await Assert.ThrowsExactlyAsync<InvalidOperationException>(() =>
_stack.Branch("some-uid").CreateAsync(new BranchModel()));
}

Expand Down Expand Up @@ -504,8 +544,132 @@ public void Test025_Should_Fail_Create_Duplicate_Branch()
}
}

// ---- Create with publish_config.environments ---------------------------

[TestMethod]
[DoNotParallelize]
public void Test026_Should_Create_Branch_With_Two_Environment_Names()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Create_TwoEnvironments");
try
{
var model = NewEnvironmentBranch(EnvTwoBranchUid, EnvironmentOne, EnvironmentTwo);
ContentstackResponse response = _stack.Branch().Create(model);
AssertLogger.IsNotNull(response.OpenJsonObjectResponse(), "response");
AssertEnvironmentsEqual(response, "Branch_Create_TwoEnvironments", EnvironmentOne, EnvironmentTwo);
}
catch (Exception e)
{
AssertLogger.Fail(e.Message);
}
}

[TestMethod]
[DoNotParallelize]
public void Test027_Should_Create_Branch_With_Empty_Environment_List()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Create_EmptyEnvironments");
try
{
var model = NewEnvironmentBranch(EnvNoneBranchUid);
ContentstackResponse response = _stack.Branch().Create(model);
AssertLogger.IsNotNull(response.OpenJsonObjectResponse(), "response");
// [] must read back as an empty list (present, not null), so assert it is not null first
AssertEnvironmentsEqual(response, "Branch_Create_EmptyEnvironments");
}
catch (Exception e)
{
AssertLogger.Fail(e.Message);
}
}

[TestMethod]
[DoNotParallelize]
public void Test028_Should_Reject_Create_With_Unknown_Environment_Name()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Create_UnknownEnvironment");
var model = NewEnvironmentBranch(EnvUnknownBranchUid, UnknownEnvironment);
try
{
_stack.Branch().Create(model);
AssertLogger.Fail("Expected API error for an unknown environment name");
}
catch (ContentstackErrorException ex)
{
AssertEnvironmentRejected(ex, EnvUnknownBranchUid, "Branch_Create_UnknownEnvironment");
}
}

[TestMethod]
[DoNotParallelize]
public void Test029_Should_Reject_Create_With_Mix_Of_Valid_And_Unknown_Environment_Names()
{
TestOutputLogger.LogContext("TestScenario", "Branch_Create_MixedEnvironments");
var model = NewEnvironmentBranch(EnvMixBranchUid, EnvironmentOne, UnknownEnvironment);
try
{
_stack.Branch().Create(model);
AssertLogger.Fail("Expected API error for a mix of valid and unknown environment names");
}
catch (ContentstackErrorException ex)
{
AssertEnvironmentRejected(ex, EnvMixBranchUid, "Branch_Create_MixedEnvironments");
}
}

// ---- Helper methods ---------------------------------------------------

private static BranchModel NewEnvironmentBranch(string uid, params string[] environments)
{
return new BranchModel
{
Uid = uid,
Source = SourceBranch,
PublishConfig = new BranchPublishConfigModel { Environments = new List<string>(environments) }
};
}

private static List<string> EnvironmentsOf(ContentstackResponse response)
{
JsonNode node = response.OpenJsonObjectResponse()["branch"]?["publish_config"]?["environments"];
return node?.AsArray().Select(n => n!.GetValue<string>()).ToList();
}

private static void AssertEnvironmentsEqual(ContentstackResponse response, string assertionName, params string[] expected)
{
List<string> names = EnvironmentsOf(response);
AssertLogger.IsNotNull(names, "publish_config.environments");
AssertLogger.IsTrue(
names.SequenceEqual(expected),
$"Expected environments [{string.Join(", ", expected)}] but got [{string.Join(", ", names)}]",
assertionName);
}

private static void AssertEnvironmentRejected(ContentstackErrorException ex, string uid, string assertionName)
{
AssertLogger.IsTrue(
ex.StatusCode == (HttpStatusCode)422,
$"Expected 422, got {(int)ex.StatusCode} ({ex.StatusCode})",
assertionName);
AssertLogger.IsTrue(
ex.Errors != null && ex.Errors.ContainsKey("publish_config.environments"),
"Expected the API error to be keyed publish_config.environments",
assertionName);

// No branch may be created when the request is rejected
bool exists;
try
{
_stack.Branch(uid).Fetch();
exists = true;
}
catch (ContentstackErrorException)
{
exists = false;
}
AssertLogger.IsTrue(!exists, "A rejected create must not leave a branch behind", assertionName);
}

private static void AssertBranchApiError(ContentstackErrorException ex, string assertionName)
{
AssertLogger.IsTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ public void Test028_Should_Fail_Fetch_With_Empty_UID()
{
TestOutputLogger.LogContext("TestScenario", "FetchNestedGlobalField_EmptyUID");

Assert.ThrowsException<ArgumentException>(
Assert.ThrowsExactly<ArgumentException>(
() => _stack.GlobalField("").Fetch(),
"Expected ArgumentException for empty UID");
}
Expand All @@ -1329,7 +1329,7 @@ public void Test029_Should_Fail_Update_With_Empty_UID()
TestOutputLogger.LogContext("TestScenario", "UpdateNestedGlobalField_EmptyUID");
var updateModel = CreateNestedGlobalFieldModel();

Assert.ThrowsException<ArgumentException>(
Assert.ThrowsExactly<ArgumentException>(
() => _stack.GlobalField("").Update(updateModel),
"Expected ArgumentException for empty UID");
}
Expand All @@ -1340,7 +1340,7 @@ public void Test030_Should_Fail_Delete_With_Empty_UID()
{
TestOutputLogger.LogContext("TestScenario", "DeleteNestedGlobalField_EmptyUID");

Assert.ThrowsException<ArgumentException>(
Assert.ThrowsExactly<ArgumentException>(
() => _stack.GlobalField("").Delete(),
"Expected ArgumentException for empty UID");
}
Expand All @@ -1352,7 +1352,7 @@ public void Test031_Should_Fail_Create_With_UID_Set()
TestOutputLogger.LogContext("TestScenario", "CreateNestedGlobalField_UIDSet");
var model = CreateNestedGlobalFieldModel();

Assert.ThrowsException<InvalidOperationException>(
Assert.ThrowsExactly<InvalidOperationException>(
() => _stack.GlobalField("some_uid").Create(model),
"Expected InvalidOperationException when UID is set for create operation");
}
Expand All @@ -1363,7 +1363,7 @@ public void Test032_Should_Fail_Query_With_UID_Set()
{
TestOutputLogger.LogContext("TestScenario", "QueryNestedGlobalField_UIDSet");

Assert.ThrowsException<InvalidOperationException>(
Assert.ThrowsExactly<InvalidOperationException>(
() => _stack.GlobalField("some_uid").Query().Find(),
"Expected InvalidOperationException when UID is set for query operation");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void ClassInitialize(TestContext _)
_client = Contentstack.CreateAuthenticatedClient();
}

[ClassCleanup(ClassCleanupBehavior.EndOfClass)]
[ClassCleanup]
public static void ClassCleanup()
{
try { _client?.Logout(); } catch { }
Expand Down
Loading
Loading