diff --git a/.gitignore b/.gitignore index 8b68604..9dfd37d 100644 --- a/.gitignore +++ b/.gitignore @@ -25,8 +25,8 @@ packages/ */appSettings.json api_referece/* .sonarqube/ -*.html *.cobertura.xml integration-test-report_*.html *.zip -.trx \ No newline at end of file +.trx +*.html \ No newline at end of file diff --git a/Contentstack.Management.Core.Tests/IntegrationTest/Contentstack005_BranchTest.cs b/Contentstack.Management.Core.Tests/IntegrationTest/Contentstack005_BranchTest.cs index 6f0722e..430dbe5 100644 --- a/Contentstack.Management.Core.Tests/IntegrationTest/Contentstack005_BranchTest.cs +++ b/Contentstack.Management.Core.Tests/IntegrationTest/Contentstack005_BranchTest.cs @@ -193,7 +193,7 @@ public void Test008_Should_Fetch_Created_Branch() Assert.Inconclusive("Test004 did not create branch — skipping."); try { - ContentstackResponse response = _stack.Branch(_createdBranchUid).Fetch(); + ContentstackResponse response = RetryOnBranchProvisioning(() => _stack.Branch(_createdBranchUid).Fetch()); var json = response.OpenJsonObjectResponse(); AssertLogger.IsNotNull(json, "response"); } @@ -212,7 +212,7 @@ public async Task Test009_Should_Fetch_Created_Branch_Async() Assert.Inconclusive("Test005 did not create branch — skipping."); try { - ContentstackResponse response = await _stack.Branch(_createdBranchUidAsync).FetchAsync(); + ContentstackResponse response = await RetryOnBranchProvisioningAsync(() => _stack.Branch(_createdBranchUidAsync).FetchAsync()); var json = response.OpenJsonObjectResponse(); AssertLogger.IsNotNull(json, "response"); } @@ -235,7 +235,7 @@ public void Test010_Should_Delete_Branch() { var force = new ParameterCollection(); force.Add("force", true); - ContentstackResponse response = _stack.Branch(_createdBranchUid).Delete(force); + ContentstackResponse response = RetryOnBranchProvisioning(() => _stack.Branch(_createdBranchUid).Delete(force)); var json = response.OpenJsonObjectResponse(); AssertLogger.IsNotNull(json, "response"); _createdBranchUid = null; @@ -257,7 +257,7 @@ public async Task Test011_Should_Delete_Branch_Async() { var force = new ParameterCollection(); force.Add("force", true); - ContentstackResponse response = await _stack.Branch(_createdBranchUidAsync).DeleteAsync(force); + ContentstackResponse response = await RetryOnBranchProvisioningAsync(() => _stack.Branch(_createdBranchUidAsync).DeleteAsync(force)); var json = response.OpenJsonObjectResponse(); AssertLogger.IsNotNull(json, "response"); _createdBranchUidAsync = null; @@ -268,6 +268,51 @@ public async Task Test011_Should_Delete_Branch_Async() } } + // ---- Branch provisioning retry helpers --------------------------------- + // Branch creation is async server-side: POST /branches returns 201 immediately + // with `"deleted_at": "in-progress"` while the branch is still being provisioned. + // Fetch/Delete against a branch in that state returns a 422 "branch is not valid" + // error until the provisioning job completes, so retry with backoff instead of + // failing on the first attempt. + + private static bool IsBranchProvisioningError(ContentstackErrorException ex) + { + return ex.StatusCode == (HttpStatusCode)422 && ex.Message != null && + (ex.Message.Contains("Failed to fetch Branch") || ex.Message.Contains("Branch delete failed") || ex.Message.Contains("is not valid")); + } + + private static ContentstackResponse RetryOnBranchProvisioning(Func action, int maxAttempts = 6, int delayMs = 1000) + { + for (int attempt = 1; attempt < maxAttempts; attempt++) + { + try + { + return action(); + } + catch (ContentstackErrorException ex) when (IsBranchProvisioningError(ex)) + { + Task.Delay(delayMs).Wait(); + } + } + return action(); + } + + private static async Task RetryOnBranchProvisioningAsync(Func> action, int maxAttempts = 6, int delayMs = 1000) + { + for (int attempt = 1; attempt < maxAttempts; attempt++) + { + try + { + return await action(); + } + catch (ContentstackErrorException ex) when (IsBranchProvisioningError(ex)) + { + await Task.Delay(delayMs); + } + } + return await action(); + } + // ---- SDK validation tests (no API call) -------------------------------- [TestMethod] diff --git a/Contentstack.Management.Core.Tests/Mock/extension.html b/Contentstack.Management.Core.Tests/Mock/extension.html new file mode 100644 index 0000000..777e673 --- /dev/null +++ b/Contentstack.Management.Core.Tests/Mock/extension.html @@ -0,0 +1,27 @@ + + + + + + + +
+ + +