-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstack012_NestedGlobalFieldTest.cs
More file actions
301 lines (263 loc) · 12.5 KB
/
Contentstack012_NestedGlobalFieldTest.cs
File metadata and controls
301 lines (263 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using AutoFixture;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Fields;
using Contentstack.Management.Core.Queryable;
using Contentstack.Management.Core.Tests.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Contentstack.Management.Core.Tests.IntegrationTest
{
[TestClass]
public class Contentstack008_NestedGlobalFieldTest
{
private Stack _stack;
[TestInitialize]
public void Initialize()
{
StackResponse response = StackResponse.getStack(Contentstack.Client.serializer);
_stack = Contentstack.Client.Stack(response.Stack.APIKey);
}
private ContentModelling CreateReferencedGlobalFieldModel()
{
return new ContentModelling
{
Title = "Referenced Global Field",
Uid = "referenced_global_field",
Description = "A global field that will be referenced by another global field",
Schema = new List<Field>
{
new TextboxField
{
DisplayName = "Title",
Uid = "title",
DataType = "text",
Mandatory = true,
Unique = true,
FieldMetadata = new FieldMetadata
{
Default = "true"
}
},
new TextboxField
{
DisplayName = "Description",
Uid = "description",
DataType = "text",
Mandatory = false,
FieldMetadata = new FieldMetadata
{
Description = "A description field"
}
}
}
};
}
private ContentModelling CreateNestedGlobalFieldModel()
{
return new ContentModelling
{
Title = "Nested Global Field Test",
Uid = "nested_global_field_test",
Description = "Test nested global field for .NET SDK",
Schema = new List<Field>
{
new TextboxField
{
DisplayName = "Single Line Textbox",
Uid = "single_line",
DataType = "text",
Mandatory = false,
Multiple = false,
Unique = false,
FieldMetadata = new FieldMetadata
{
Description = "",
DefaultValue = "",
Version = 3
}
},
new GlobalFieldReference
{
DisplayName = "Global Field Reference",
Uid = "global_field_reference",
DataType = "global_field",
ReferenceTo = "referenced_global_field",
Mandatory = false,
Multiple = false,
Unique = false,
NonLocalizable = false,
FieldMetadata = new FieldMetadata
{
Description = "Reference to another global field"
}
}
},
GlobalFieldRefs = new List<GlobalFieldRefs>
{
new GlobalFieldRefs
{
Uid = "referenced_global_field",
OccurrenceCount = 1,
IsChild = true,
Paths = new List<string> { "schema.1" }
}
}
};
}
[TestMethod]
[DoNotParallelize]
public void Test001_Should_Create_Referenced_Global_Field()
{
var referencedGlobalFieldModel = CreateReferencedGlobalFieldModel();
ContentstackResponse response = _stack.GlobalField().Create(referencedGlobalFieldModel);
GlobalFieldModel globalField = response.OpenTResponse<GlobalFieldModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modelling);
Assert.AreEqual(referencedGlobalFieldModel.Title, globalField.Modelling.Title);
Assert.AreEqual(referencedGlobalFieldModel.Uid, globalField.Modelling.Uid);
Assert.AreEqual(referencedGlobalFieldModel.Schema.Count, globalField.Modelling.Schema.Count);
}
[TestMethod]
[DoNotParallelize]
public void Test002_Should_Create_Nested_Global_Field()
{
var nestedGlobalFieldModel = CreateNestedGlobalFieldModel();
ContentstackResponse response = _stack.GlobalField().Create(nestedGlobalFieldModel);
GlobalFieldModel globalField = response.OpenTResponse<GlobalFieldModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modelling);
Assert.AreEqual(nestedGlobalFieldModel.Title, globalField.Modelling.Title);
Assert.AreEqual(nestedGlobalFieldModel.Uid, globalField.Modelling.Uid);
Assert.AreEqual(nestedGlobalFieldModel.Schema.Count, globalField.Modelling.Schema.Count);
}
[TestMethod]
[DoNotParallelize]
public void Test003_Should_Fetch_Nested_Global_Field()
{
ContentstackResponse response = _stack.GlobalField("nested_global_field_test").Fetch();
GlobalFieldModel globalField = response.OpenTResponse<GlobalFieldModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modelling);
Assert.AreEqual("nested_global_field_test", globalField.Modelling.Uid);
Assert.IsTrue(globalField.Modelling.Schema.Count >= 2);
}
[TestMethod]
[DoNotParallelize]
public async Task Test004_Should_Fetch_Async_Nested_Global_Field()
{
ContentstackResponse response = await _stack.GlobalField("nested_global_field_test").FetchAsync();
GlobalFieldModel globalField = response.OpenTResponse<GlobalFieldModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modelling);
Assert.AreEqual("nested_global_field_test", globalField.Modelling.Uid);
}
[TestMethod]
[DoNotParallelize]
public void Test005_Should_Update_Nested_Global_Field()
{
var updateModel = new ContentModelling
{
Title = "Updated Nested Global Field",
Uid = "nested_global_field_test",
Description = "Updated description for nested global field",
Schema = CreateNestedGlobalFieldModel().Schema,
GlobalFieldRefs = CreateNestedGlobalFieldModel().GlobalFieldRefs
};
ContentstackResponse response = _stack.GlobalField("nested_global_field_test").Update(updateModel);
GlobalFieldModel globalField = response.OpenTResponse<GlobalFieldModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modelling);
Assert.AreEqual(updateModel.Title, globalField.Modelling.Title);
Assert.AreEqual("nested_global_field_test", globalField.Modelling.Uid);
}
[TestMethod]
[DoNotParallelize]
public async Task Test006_Should_Update_Async_Nested_Global_Field()
{
var updateModel = new ContentModelling
{
Title = "Updated Async Nested Global Field",
Uid = "nested_global_field_test",
Description = "Updated async description for nested global field",
Schema = CreateNestedGlobalFieldModel().Schema,
GlobalFieldRefs = CreateNestedGlobalFieldModel().GlobalFieldRefs
};
ContentstackResponse response = await _stack.GlobalField("nested_global_field_test").UpdateAsync(updateModel);
GlobalFieldModel globalField = response.OpenTResponse<GlobalFieldModel>();
Assert.IsNotNull(response);
Assert.IsNotNull(globalField);
Assert.IsNotNull(globalField.Modelling);
Assert.AreEqual(updateModel.Title, globalField.Modelling.Title);
Assert.AreEqual("nested_global_field_test", globalField.Modelling.Uid);
}
[TestMethod]
[DoNotParallelize]
public void Test007_Should_Query_Nested_Global_Fields()
{
ContentstackResponse response = _stack.GlobalField().Query().Find();
GlobalFieldsModel globalFields = response.OpenTResponse<GlobalFieldsModel>();
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);
}
[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()
{
// This has been used to avoid tthe confirmation prompt during deletion in case the global field is referenced
var parameters = new ParameterCollection();
parameters.Add("force", "true");
ContentstackResponse response = _stack.GlobalField("referenced_global_field").Delete(parameters);
Assert.IsNotNull(response);
}
[TestMethod]
[DoNotParallelize]
public void Test008_Should_Delete_Nested_Global_Field()
{
ContentstackResponse response = _stack.GlobalField("nested_global_field_test").Delete();
Assert.IsNotNull(response);
}
}
}