Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -107,7 +107,19 @@ public void Test006_Should_Query_Global_Field()

[TestMethod]
[DoNotParallelize]
public async System.Threading.Tasks.Task Test007_Should_Update_Async_Global_Field()
public void Test006a_Should_Query_Global_Field_With_ApiVersion()
{
ContentstackResponse response = _stack.GlobalField(apiVersion: "3.2").Query().Find();
GlobalFieldsModel globalField = response.OpenTResponse<GlobalFieldsModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modellings);
Assert.AreEqual(1, globalField.Modellings.Count);
}

[TestMethod]
[DoNotParallelize]
public async System.Threading.Tasks.Task Test007_Should_Query_Async_Global_Field()
{
ContentstackResponse response = await _stack.GlobalField().Query().FindAsync();
GlobalFieldsModel globalField = response.OpenTResponse<GlobalFieldsModel>();
Expand All @@ -116,5 +128,17 @@ public async System.Threading.Tasks.Task Test007_Should_Update_Async_Global_Fiel
Assert.IsNotNull(globalField.Modellings);
Assert.AreEqual(1, globalField.Modellings.Count);
}

[TestMethod]
[DoNotParallelize]
public async System.Threading.Tasks.Task Test007a_Should_Query_Async_Global_Field_With_ApiVersion()
{
ContentstackResponse response = await _stack.GlobalField(apiVersion: "3.2").Query().FindAsync();
GlobalFieldsModel globalField = response.OpenTResponse<GlobalFieldsModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modellings);
Assert.AreEqual(1, globalField.Modellings.Count);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoFixture;
using Contentstack.Management.Core.Models;
Expand Down Expand Up @@ -239,6 +240,42 @@ public void Test007_Should_Query_Nested_Global_Fields()
Assert.AreEqual("nested_global_field_test", nestedGlobalField.Uid);
}

[TestMethod]
[DoNotParallelize]
public void Test007a_Should_Query_Nested_Global_Fields_With_ApiVersion()
{
ContentstackResponse response = _stack.GlobalField(apiVersion: "3.2").Query().Find();
GlobalFieldsModel globalFields = response.OpenTResponse<GlobalFieldsModel>();
var jsonResponse = response.OpenJObjectResponse();

Assert.IsNotNull(response);
Assert.IsNotNull(globalFields);
Assert.IsNotNull(globalFields.Modellings);
Assert.IsTrue(globalFields.Modellings.Count >= 1);

var nestedGlobalField = globalFields.Modellings.Find(gf => gf.Uid == "nested_global_field_test");
Assert.IsNotNull(nestedGlobalField);
Assert.AreEqual("nested_global_field_test", nestedGlobalField.Uid);

// Find the global field reference and check its reference_to from the raw JSON
var globalFieldReference = nestedGlobalField.Schema
.FirstOrDefault(f => f.DataType == "global_field" && f.Uid == "global_field_reference");

Assert.IsNotNull(globalFieldReference);
Assert.AreEqual("global_field", globalFieldReference.DataType);
Assert.AreEqual("global_field_reference", globalFieldReference.Uid);
Assert.AreEqual("Global Field Reference", globalFieldReference.DisplayName);

// Check the reference_to value from the raw JSON
var globalFieldsArray = jsonResponse["global_fields"] as Newtonsoft.Json.Linq.JArray;
var nestedGlobalFieldJson = globalFieldsArray?.FirstOrDefault(gf => gf["uid"]?.ToString() == "nested_global_field_test");
var schemaArray = nestedGlobalFieldJson?["schema"] as Newtonsoft.Json.Linq.JArray;
var globalFieldRefJson = schemaArray?.FirstOrDefault(f => f["uid"]?.ToString() == "global_field_reference");
var referenceTo = globalFieldRefJson?["reference_to"]?.ToString();

Assert.AreEqual("referenced_global_field", referenceTo);
}

[TestMethod]
[DoNotParallelize]
public void Test009_Should_Delete_Referenced_Global_Field()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using AutoFixture;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Queryable;
Expand Down Expand Up @@ -41,6 +41,23 @@ public void Initialize_GlobalField()
Assert.AreEqual(globalField.Query().GetType(), typeof(Query));
}

[TestMethod]
public void Initialize_GlobalField_With_ApiVersion()
{
string apiVersion = "3.2";
GlobalField globalField = new GlobalField(_stack, apiVersion: apiVersion);

Assert.IsNull(globalField.Uid);
Assert.AreEqual($"/global_fields", globalField.resourcePath);
Assert.ThrowsException<InvalidOperationException>(() => globalField.Fetch());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.FetchAsync());
Assert.ThrowsException<InvalidOperationException>(() => globalField.Update(new ContentModelling()));
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.UpdateAsync(new ContentModelling()));
Assert.ThrowsException<InvalidOperationException>(() => globalField.Delete());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.DeleteAsync());
Assert.AreEqual(globalField.Query().GetType(), typeof(Query));
}

[TestMethod]
public void Initialize_GlobalField_With_Uid()
{
Expand All @@ -53,6 +70,20 @@ public void Initialize_GlobalField_With_Uid()
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.CreateAsync(new ContentModelling()));
Assert.ThrowsException<InvalidOperationException>(() => globalField.Query());
}

[TestMethod]
public void Initialize_GlobalField_With_Uid_And_ApiVersion()
{
string uid = _fixture.Create<string>();
string apiVersion = "3.2";
GlobalField globalField = new GlobalField(_stack, uid, apiVersion);

Assert.AreEqual(uid, globalField.Uid);
Assert.AreEqual($"/global_fields/{globalField.Uid}", globalField.resourcePath);
Assert.ThrowsException<InvalidOperationException>(() => globalField.Create(new ContentModelling()));
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => globalField.CreateAsync(new ContentModelling()));
Assert.ThrowsException<InvalidOperationException>(() => globalField.Query());
}
[TestMethod]
public void Should_Create_Content_Type()
{
Expand Down Expand Up @@ -80,6 +111,15 @@ public void Should_Query_Content_Type()
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}

[TestMethod]
public void Should_Query_Content_Type_With_ApiVersion()
{
ContentstackResponse response = _stack.GlobalField(apiVersion: "3.2").Query().Find();

Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}

[TestMethod]
public async System.Threading.Tasks.Task Should_Query_Content_Type_Async()
{
Expand All @@ -89,6 +129,15 @@ public async System.Threading.Tasks.Task Should_Query_Content_Type_Async()
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}

[TestMethod]
public async System.Threading.Tasks.Task Should_Query_Content_Type_Async_With_ApiVersion()
{
ContentstackResponse response = await _stack.GlobalField(apiVersion: "3.2").Query().FindAsync();

Assert.AreEqual(_contentstackResponse.OpenResponse(), response.OpenResponse());
Assert.AreEqual(_contentstackResponse.OpenJObjectResponse().ToString(), response.OpenJObjectResponse().ToString());
}

[TestMethod]
public void Should_Fetch_Content_Type()
{
Expand Down
20 changes: 20 additions & 0 deletions Contentstack.Management.Core.Unit.Tests/Queryable/QueryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public void Initialize_Query()
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => query.FindAsync());
}

[TestMethod]
public void Initialize_Query_With_ApiVersion()
{
string apiVersion = "3.2";
Query query = new Query(new Stack(new ContentstackClient(authtoken: _fixture.Create<string>())), _fixture.Create<string>(), apiVersion);

Assert.ThrowsException<InvalidOperationException>(() => query.Find());
Assert.ThrowsExceptionAsync<InvalidOperationException>(() => query.FindAsync());
}

[TestMethod]
public void Query_Pagination_Parameters()
{
Expand All @@ -49,5 +59,15 @@ public void Query_Pagination_Parameters()
query.Skip(10);
query.IncludeCount();
}

[TestMethod]
public void Query_Pagination_Parameters_With_ApiVersion()
{
string apiVersion = "3.2";
Query query = new Query(_stack, _fixture.Create<string>(), apiVersion);
query.Limit(10);
query.Skip(10);
query.IncludeCount();
}
}
}
2 changes: 1 addition & 1 deletion Contentstack.Management.Core/Models/Fields/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Field
[JsonProperty(propertyName: "data_type")]
public string DataType { get; set; }
/// <summary>
/// Allows you to enter additional data about a field. Also, you can add additional values under field_metadata.
/// Allows you to enter additional data about a field. Also, you can add additional values under 'field_metadata'.
/// </summary>
[JsonProperty(propertyName: "field_metadata")]
public FieldMetadata FieldMetadata { get; set; }
Expand Down
7 changes: 5 additions & 2 deletions Contentstack.Management.Core/Models/GlobalField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ namespace Contentstack.Management.Core.Models
{
public class GlobalField : BaseModel<ContentModelling>
{
internal GlobalField(Stack stack, string uid = null)
private readonly string apiVersion;

internal GlobalField(Stack stack, string uid = null, string apiVersion = null)
: base(stack, "global_field", uid)
{
resourcePath = uid == null ? "/global_fields" : $"/global_fields/{uid}";
this.apiVersion = apiVersion;
}

/// <summary>
Expand All @@ -24,7 +27,7 @@ internal GlobalField(Stack stack, string uid = null)
public Query Query()
{
ThrowIfUidNotEmpty();
return new Query(stack, resourcePath);
return new Query(stack, resourcePath, apiVersion);
}

/// <summary>
Expand Down
15 changes: 7 additions & 8 deletions Contentstack.Management.Core/Models/Stack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,24 +643,23 @@ public Asset Asset(string uid = null)
}

/// <summary>
/// A <see cref="Models.GlobalField" /> is a reusable field (or group of fields) that you can define once and reuse in any content type within your stack.
/// This eliminates the need (and thereby time and efforts) to create the same set of fields repeatedly in multiple content types.
/// <see cref="Models.GlobalField" /> defines the structure or schema of a page or a section of your web or mobile property. To create global Fields for your application, you are required to first create a global field. Read more about <a href='https://www.contentstack.com/docs/guide/global-fields'>Global Fields</a>.
/// </summary>
/// <param name="uid"> Optional global field uid.</param>
/// <param name="uid">Optional, global field uid.</param>
/// <param name="apiVersion">Optional, API version for nested global field support.</param>
/// <example>
/// <pre><code>
/// ContentstackClient client = new ContentstackClient("<AUTHTOKEN>", "<API_HOST>");
/// Stack stack = client.Stack("<API_KEY>");
/// ContentstackResponse contentstackResponse = stack.GlobalField("<GLOBAL_FIELD_UID>").Fetch();
/// ContentstackResponse contentstackResponse = client.Stack("<API_KEY>").GlobalField("<GLOBAL_FIELD_UID>").Fetch();
/// </code></pre>
/// </example>
/// <returns>The <see cref="Models.GlobalField"/></returns>
public GlobalField GlobalField(string uid = null)
/// <returns>The <see cref="Models.GlobalField" /></returns>
public GlobalField GlobalField(string uid = null, string apiVersion = null)
{
ThrowIfNotLoggedIn();
ThrowIfAPIKeyEmpty();

return new GlobalField(this, uid);
return new GlobalField(this, uid, apiVersion);
}

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions Contentstack.Management.Core/Queryable/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ public class Query
private readonly Stack _stack;
private readonly string _resourcePath;
private readonly ParameterCollection _collection = new ParameterCollection();
internal Query(Stack stack, string resourcePath)
private readonly string _apiVersion;

internal Query(Stack stack, string resourcePath, string apiVersion = null)
{
if(stack == null)
{
Expand All @@ -24,6 +26,7 @@ internal Query(Stack stack, string resourcePath)
}
_stack = stack;
_resourcePath = resourcePath;
_apiVersion = apiVersion;
}
#region Public
/// <summary>
Expand Down Expand Up @@ -93,7 +96,7 @@ public ContentstackResponse Find(ParameterCollection collection = null)
}

var service = new QueryService(_stack, _collection, _resourcePath);
return _stack.client.InvokeSync(service);
return _stack.client.InvokeSync(service, false, _apiVersion);
}

/// <summary>
Expand All @@ -113,7 +116,7 @@ public Task<ContentstackResponse> FindAsync(ParameterCollection collection = nul
}
var service = new QueryService(_stack, _collection, _resourcePath);

return _stack.client.InvokeAsync<QueryService, ContentstackResponse>(service);
return _stack.client.InvokeAsync<QueryService, ContentstackResponse>(service, false, _apiVersion);
}
#endregion
#region Throw Error
Expand Down