Skip to content
Closed
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
40 changes: 35 additions & 5 deletions Contentstack.Core.Tests/Helpers/TestDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,49 @@ static TestDataHelper()
#endregion

#region Taxonomy

/// <summary>
/// Gets the taxonomy term for USA state (e.g., "california")
/// </summary>
public static string TaxUsaState =>
public static string TaxUsaState =>
GetRequiredConfig("TAX_USA_STATE");

/// <summary>
/// Gets the taxonomy term for India state (e.g., "maharashtra")
/// </summary>
public static string TaxIndiaState =>
public static string TaxIndiaState =>
GetRequiredConfig("TAX_INDIA_STATE");


/// <summary>
/// API key for the taxonomy-publish test stack (gadgets).
/// </summary>
public static string TaxPublishApiKey =>
GetRequiredConfig("TAX_PUBLISH_API_KEY");

/// <summary>
/// Delivery token for the taxonomy-publish test stack.
/// </summary>
public static string TaxPublishDeliveryToken =>
GetRequiredConfig("TAX_PUBLISH_DELIVERY_TOKEN");

/// <summary>
/// Environment for the taxonomy-publish test stack.
/// </summary>
public static string TaxPublishEnvironment =>
GetRequiredConfig("TAX_PUBLISH_ENVIRONMENT");

/// <summary>
/// UID of the published taxonomy to use in localization tests (e.g. "gadgets").
/// </summary>
public static string TaxPublishTaxonomyUid =>
GetRequiredConfig("TAX_PUBLISH_TAXONOMY_UID");

/// <summary>
/// Locale code used for localized taxonomy/term delivery tests (e.g. "hi-in").
/// </summary>
public static string TaxPublishLocale =>
GetRequiredConfig("TAX_PUBLISH_LOCALE");

#endregion

#region Live Preview
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Contentstack.Core.Configuration;
using Contentstack.Core.Models;
using Contentstack.Core.Tests.Helpers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Contentstack.Core.Tests.Integration.Taxonomy
{
/// <summary>
/// End-to-end runner for all taxonomy localisation CDA calls.
/// Mirrors the TypeScript smoke-test script — runs every endpoint in sequence,
/// prints the SDK call, real HTTP request (URL + headers), and full JSON response.
/// Run with:
/// dotnet test --filter "FullyQualifiedName~TaxonomyLocalisationRunner" --logger "console;verbosity=detailed"
/// </summary>
[Trait("Category", "TaxonomyLocalisationRunner")]
public class TaxonomyLocalisationRunner
{
private readonly ITestOutputHelper _out;

private const string ApiKey = "blt168147f34138ebac";
private const string DeliveryToken = "cs91c6d782d3c9ca953c0a2685";
private const string Environment = "dev";
private const string TaxonomyUid = "gadgets";
private const string Locale = "hi-in";

public TaxonomyLocalisationRunner(ITestOutputHelper output)
{
_out = output;
}

private ContentstackClient CreateClient()
{
var options = new ContentstackOptions
{
ApiKey = ApiKey,
DeliveryToken = DeliveryToken,
Environment = Environment
};
return new ContentstackClient(options);
}

private void Section(int num, string title)
{
var line = new string('─', 60);
_out.WriteLine("");
_out.WriteLine(line);
_out.WriteLine($" {num}. {title}");
_out.WriteLine(line);
}

private void PrintResponse(string label, object data)
{
_out.WriteLine($"✅ {label}");
_out.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));
}

private void PrintError(string label, Exception ex)
{
_out.WriteLine($"❌ {label}");
_out.WriteLine($" {ex.Message}");
}

[Fact(DisplayName = "TaxonomyLocalisationRunner - Full end-to-end run of all CDA localisation calls")]
public async Task Run_All_TaxonomyLocalisation_Calls()
{
var client = CreateClient();
string firstTermUid = null;

// ── 1. GET /v3/taxonomies/gadgets?environment=dev ─────────────────
Section(1, $"Fetch taxonomy uid={TaxonomyUid} (master locale)");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Fetch<JObject>()");
try
{
var result = await client.Taxonomies(TaxonomyUid).Fetch<JObject>();
PrintResponse($"GET /taxonomies/{TaxonomyUid}", result);
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}", ex); }

// ── 2. GET /v3/taxonomies/gadgets?environment=dev&locale=hi-in ────
Section(2, $"Fetch taxonomy uid={TaxonomyUid} locale={Locale}");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Fetch<JObject>(\"{Locale}\")");
try
{
var result = await client.Taxonomies(TaxonomyUid).Fetch<JObject>(Locale);
PrintResponse($"GET /taxonomies/{TaxonomyUid}?locale={Locale}", result);
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}?locale={Locale}", ex); }

// ── 3. GET /v3/taxonomies/gadgets/terms?environment=dev&locale=hi-in
Section(3, $"Find terms taxonomy={TaxonomyUid} locale={Locale}");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Terms().SetLocale(\"{Locale}\").Find<JObject>()");
try
{
var result = await client.Taxonomies(TaxonomyUid).Terms()
.SetLocale(Locale)
.Find<JObject>();
PrintResponse($"GET /taxonomies/{TaxonomyUid}/terms?locale={Locale}", result);
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}/terms?locale={Locale}", ex); }

// ── 4. GET /v3/taxonomies/gadgets/terms?locale=hi-in&include_fallback=true
Section(4, $"Find terms taxonomy={TaxonomyUid} locale={Locale} include_fallback=true");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Terms().SetLocale(\"{Locale}\").IncludeFallback().Find<JObject>()");
try
{
var result = await client.Taxonomies(TaxonomyUid).Terms()
.SetLocale(Locale)
.IncludeFallback()
.Find<JObject>();
PrintResponse($"GET /taxonomies/{TaxonomyUid}/terms?locale={Locale}&include_fallback=true", result);

// Grab first term uid for subsequent calls
var terms = result?.Items;
foreach (var t in terms ?? System.Array.Empty<JObject>())
{
firstTermUid = t?["uid"]?.ToString();
if (!string.IsNullOrEmpty(firstTermUid)) break;
}
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}/terms with fallback", ex); }

if (string.IsNullOrEmpty(firstTermUid))
{
_out.WriteLine("");
_out.WriteLine("⚠️ No term UID found — skipping single-term calls.");
return;
}

_out.WriteLine($"\n📌 Using term uid = \"{firstTermUid}\" for single-term calls.");

// ── 5. GET /v3/taxonomies/gadgets/terms/:uid?locale=hi-in ─────────
Section(5, $"Fetch single term uid={firstTermUid} locale={Locale}");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Term(\"{firstTermUid}\").Fetch<JObject>(\"{Locale}\")");
try
{
var result = await client.Taxonomies(TaxonomyUid).Term(firstTermUid).Fetch<JObject>(Locale);
PrintResponse($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}?locale={Locale}", result);
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}?locale={Locale}", ex); }

// ── 6. GET /v3/taxonomies/gadgets/terms/:uid/locales ──────────────
Section(6, $"Term locales uid={firstTermUid}");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Term(\"{firstTermUid}\").Locales<JToken>()");
try
{
var result = await client.Taxonomies(TaxonomyUid).Term(firstTermUid).Locales<JToken>();
PrintResponse($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}/locales", result);
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}/locales", ex); }

// ── 7. GET /v3/taxonomies/gadgets/terms/:uid/ancestors ────────────
Section(7, $"Term ancestors uid={firstTermUid}");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Term(\"{firstTermUid}\").Ancestors<JToken>()");
try
{
var result = await client.Taxonomies(TaxonomyUid).Term(firstTermUid).Ancestors<JToken>();
PrintResponse($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}/ancestors", result);
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}/ancestors", ex); }

// ── 8. GET /v3/taxonomies/gadgets/terms/:uid/descendants ──────────
Section(8, $"Term descendants uid={firstTermUid}");
_out.WriteLine($"SDK : client.Taxonomies(\"{TaxonomyUid}\").Term(\"{firstTermUid}\").Descendants<JToken>()");
try
{
var result = await client.Taxonomies(TaxonomyUid).Term(firstTermUid).Descendants<JToken>();
PrintResponse($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}/descendants", result);
}
catch (Exception ex) { PrintError($"GET /taxonomies/{TaxonomyUid}/terms/{firstTermUid}/descendants", ex); }
}
}
}
Loading
Loading