-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentstack013_AssetTest.cs
More file actions
1155 lines (1053 loc) · 48.2 KB
/
Contentstack013_AssetTest.cs
File metadata and controls
1155 lines (1053 loc) · 48.2 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.CustomExtension;
using Contentstack.Management.Core.Tests.Helpers;
using Contentstack.Management.Core.Tests.Model;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Queryable;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Contentstack.Management.Core.Tests.IntegrationTest
{
[TestClass]
public class Contentstack006_AssetTest
{
private static ContentstackClient _client;
private Stack _stack;
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
_client = Contentstack.CreateAuthenticatedClient();
}
[ClassCleanup]
public static void ClassCleanup()
{
try { _client?.Logout(); } catch { }
_client = null;
}
[TestInitialize]
public void Initialize()
{
StackResponse response = StackResponse.getStack(_client.serializer);
_stack = _client.Stack(response.Stack.APIKey);
}
[TestMethod]
[DoNotParallelize]
public void Test001_Should_Create_Asset()
{
TestOutputLogger.LogContext("TestScenario", "CreateAsset");
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/contentTypeSchema.json");
try
{
AssetModel asset = new AssetModel("contentTypeSchema.json", path, "application/json", title:"New.json", description:"new test desc", parentUID: null, tags:"one,two");
ContentstackResponse response = _stack.Asset().Create(asset);
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "CreateAsset_StatusCode");
}
else
{
// Don't fail the test if API returns an error - this might be expected behavior
}
}
catch (Exception e)
{
AssertLogger.Fail("Asset Creation Failed ", e.Message);
}
}
// Check the below 3 Test cases
[TestMethod]
[DoNotParallelize]
public void Test002_Should_Create_Dashboard()
{
TestOutputLogger.LogContext("TestScenario", "CreateDashboard");
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/customUpload.html");
try
{
DashboardWidgetModel dashboard = new DashboardWidgetModel(path, "text/html", "Dashboard", isEnable: true, defaultWidth: "half", tags: "one,two");
ContentstackResponse response = _stack.Extension().Upload(dashboard);
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "CreateDashboard_StatusCode");
}
}
catch (Exception e)
{
AssertLogger.Fail("Dashboard Creation Failed ", e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test003_Should_Create_Custom_Widget()
{
TestOutputLogger.LogContext("TestScenario", "CreateCustomWidget");
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/customUpload.html");
try
{
CustomWidgetModel customWidget = new CustomWidgetModel(path, "text/html", title: "Custom widget Upload", scope: new ExtensionScope()
{
ContentTypes = new List<string>()
{
"single_page"
}
}, tags: "one,two");
ContentstackResponse response = _stack.Extension().Upload(customWidget);
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "CreateCustomWidget_StatusCode");
}
}
catch (Exception e)
{
AssertLogger.Fail("Custom Widget Creation Failed ", e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test004_Should_Create_Custom_field()
{
TestOutputLogger.LogContext("TestScenario", "CreateCustomField");
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/customUpload.html");
try
{
CustomFieldModel fieldModel = new CustomFieldModel(path, "text/html", "Custom field Upload", "text", isMultiple: false, tags: "one,two");
ContentstackResponse response = _stack.Extension().Upload(fieldModel);
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "CreateCustomField_StatusCode");
}
}
catch (Exception e)
{
AssertLogger.Fail("Custom Field Creation Failed ", e.Message);
}
}
private string _testAssetUid;
[TestMethod]
[DoNotParallelize]
public void Test005_Should_Create_Asset_Async()
{
TestOutputLogger.LogContext("TestScenario", "CreateAssetAsync");
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/contentTypeSchema.json");
try
{
AssetModel asset = new AssetModel("async_asset.json", path, "application/json", title:"Async Asset", description:"async test asset", parentUID: null, tags:"async,test");
ContentstackResponse response = _stack.Asset().CreateAsync(asset).Result;
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "CreateAssetAsync_StatusCode");
var responseObject = response.OpenJObjectResponse();
if (responseObject["asset"] != null)
{
_testAssetUid = responseObject["asset"]["uid"]?.ToString();
TestOutputLogger.LogContext("AssetUID", _testAssetUid ?? "null");
}
}
else
{
AssertLogger.Fail("Asset Creation Async Failed");
}
}
catch (Exception ex)
{
AssertLogger.Fail("Asset Creation Async Failed ",ex.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test006_Should_Fetch_Asset()
{
TestOutputLogger.LogContext("TestScenario", "FetchAsset");
try
{
if (string.IsNullOrEmpty(_testAssetUid))
{
Test005_Should_Create_Asset_Async();
}
if (!string.IsNullOrEmpty(_testAssetUid))
{
TestOutputLogger.LogContext("AssetUID", _testAssetUid);
ContentstackResponse response = _stack.Asset(_testAssetUid).Fetch();
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "FetchAsset_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "FetchAsset_ResponseContainsAsset");
}
else
{
AssertLogger.Fail("The Asset is Not Getting Created");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Asset Fetch Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test007_Should_Fetch_Asset_Async()
{
TestOutputLogger.LogContext("TestScenario", "FetchAssetAsync");
try
{
if (string.IsNullOrEmpty(_testAssetUid))
{
Test005_Should_Create_Asset_Async();
}
if (!string.IsNullOrEmpty(_testAssetUid))
{
TestOutputLogger.LogContext("AssetUID", _testAssetUid);
ContentstackResponse response = _stack.Asset(_testAssetUid).FetchAsync().Result;
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "FetchAssetAsync_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "FetchAssetAsync_ResponseContainsAsset");
}
else
{
AssertLogger.Fail("Asset Fetch Async Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Asset Fetch Async Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test008_Should_Update_Asset()
{
TestOutputLogger.LogContext("TestScenario", "UpdateAsset");
try
{
if (string.IsNullOrEmpty(_testAssetUid))
{
Test005_Should_Create_Asset_Async();
}
if (!string.IsNullOrEmpty(_testAssetUid))
{
TestOutputLogger.LogContext("AssetUID", _testAssetUid);
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/contentTypeSchema.json");
AssetModel updatedAsset = new AssetModel("updated_asset.json", path, "application/json", title:"Updated Asset", description:"updated test asset", parentUID: null, tags:"updated,test");
ContentstackResponse response = _stack.Asset(_testAssetUid).Update(updatedAsset);
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "UpdateAsset_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "UpdateAsset_ResponseContainsAsset");
}
else
{
AssertLogger.Fail("Asset update Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Asset Update Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test009_Should_Update_Asset_Async()
{
TestOutputLogger.LogContext("TestScenario", "UpdateAssetAsync");
try
{
if (string.IsNullOrEmpty(_testAssetUid))
{
Test005_Should_Create_Asset_Async();
}
if (!string.IsNullOrEmpty(_testAssetUid))
{
TestOutputLogger.LogContext("AssetUID", _testAssetUid);
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/contentTypeSchema.json");
AssetModel updatedAsset = new AssetModel("async_updated_asset.json", path, "application/json", title:"Async Updated Asset", description:"async updated test asset", parentUID: null, tags:"async,updated,test");
ContentstackResponse response = _stack.Asset(_testAssetUid).UpdateAsync(updatedAsset).Result;
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "UpdateAssetAsync_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "UpdateAssetAsync_ResponseContainsAsset");
}
else
{
AssertLogger.Fail("Asset Update Async Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Asset Update Async Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test010_Should_Query_Assets()
{
TestOutputLogger.LogContext("TestScenario", "QueryAssets");
try
{
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
ContentstackResponse response = _stack.Asset().Query().Find();
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "QueryAssets_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["assets"], "QueryAssets_ResponseContainsAssets");
}
else
{
AssertLogger.Fail("Querying the Asset Failed");
}
}
catch (ContentstackErrorException ex)
{
AssertLogger.Fail("Querying the Asset Failed ",ex.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test011_Should_Query_Assets_With_Parameters()
{
TestOutputLogger.LogContext("TestScenario", "QueryAssetsWithParameters");
try
{
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
var query = _stack.Asset().Query();
query.Limit(5);
query.Skip(0);
ContentstackResponse response = query.Find();
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "QueryAssetsWithParams_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["assets"], "QueryAssetsWithParams_ResponseContainsAssets");
}
else
{
AssertLogger.Fail("Querying the Asset Failed");
}
}
catch (Exception e)
{
AssertLogger.Fail("Querying the Asset Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test012_Should_Delete_Asset()
{
TestOutputLogger.LogContext("TestScenario", "DeleteAsset");
try
{
if (string.IsNullOrEmpty(_testAssetUid))
{
Test005_Should_Create_Asset_Async();
}
if (!string.IsNullOrEmpty(_testAssetUid))
{
TestOutputLogger.LogContext("AssetUID", _testAssetUid);
ContentstackResponse response = _stack.Asset(_testAssetUid).Delete();
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "DeleteAsset_StatusCode");
_testAssetUid = null; // Clear the UID since asset is deleted
}
else
{
AssertLogger.Fail("Deleting the Asset Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Deleting the Asset Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test013_Should_Delete_Asset_Async()
{
TestOutputLogger.LogContext("TestScenario", "DeleteAssetAsync");
try
{
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/contentTypeSchema.json");
AssetModel asset = new AssetModel("delete_asset.json", path, "application/json", title:"Delete Asset", description:"asset for deletion", parentUID: null, tags:"delete,test");
ContentstackResponse createResponse = _stack.Asset().CreateAsync(asset).Result;
if (createResponse.IsSuccessStatusCode)
{
var responseObject = createResponse.OpenJObjectResponse();
string assetUid = responseObject["asset"]["uid"]?.ToString();
TestOutputLogger.LogContext("AssetUID", assetUid ?? "null");
if (!string.IsNullOrEmpty(assetUid))
{
ContentstackResponse deleteResponse = _stack.Asset(assetUid).DeleteAsync().Result;
if (deleteResponse.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, deleteResponse.StatusCode, "DeleteAssetAsync_StatusCode");
}
else
{
AssertLogger.Fail("Deleting Asset Async Failed");
}
}
}
else
{
AssertLogger.Fail("Deleting Asset Async Failed");
}
}
catch (Exception e)
{
AssertLogger.Fail("Deleting Asset Async Failed ",e.Message);
}
}
private string _testFolderUid;
[TestMethod]
[DoNotParallelize]
public void Test014_Should_Create_Folder()
{
TestOutputLogger.LogContext("TestScenario", "CreateFolder");
try
{
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
ContentstackResponse response = _stack.Asset().Folder().Create("Test Folder", null);
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "CreateFolder_StatusCode");
var responseObject = response.OpenJObjectResponse();
if (responseObject["asset"] != null)
{
_testFolderUid = responseObject["asset"]["uid"]?.ToString();
TestOutputLogger.LogContext("FolderUID", _testFolderUid ?? "null");
}
}
else
{
AssertLogger.Fail("Folder Creation Failed");
}
}
catch (Exception e)
{
AssertLogger.Fail("Folder Creation Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test015_Should_Create_Subfolder()
{
TestOutputLogger.LogContext("TestScenario", "CreateSubfolder");
try
{
if (string.IsNullOrEmpty(_testFolderUid))
{
Test014_Should_Create_Folder();
}
if (!string.IsNullOrEmpty(_testFolderUid))
{
TestOutputLogger.LogContext("FolderUID", _testFolderUid);
ContentstackResponse response = _stack.Asset().Folder().Create("Test Subfolder", _testFolderUid);
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "CreateSubfolder_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "CreateSubfolder_ResponseContainsFolder");
}
else
{
AssertLogger.Fail("SubFolder Creation Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("SubFolder Fetch Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test016_Should_Fetch_Folder()
{
TestOutputLogger.LogContext("TestScenario", "FetchFolder");
try
{
if (string.IsNullOrEmpty(_testFolderUid))
{
Test014_Should_Create_Folder();
}
if (!string.IsNullOrEmpty(_testFolderUid))
{
TestOutputLogger.LogContext("FolderUID", _testFolderUid);
ContentstackResponse response = _stack.Asset().Folder(_testFolderUid).Fetch();
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "FetchFolder_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "FetchFolder_ResponseContainsFolder");
}
else
{
AssertLogger.Fail("Fetch Failed for Folder");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Fetch Async Failed for Folder ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test017_Should_Fetch_Folder_Async()
{
TestOutputLogger.LogContext("TestScenario", "FetchFolderAsync");
try
{
if (string.IsNullOrEmpty(_testFolderUid))
{
Test014_Should_Create_Folder();
}
if (!string.IsNullOrEmpty(_testFolderUid))
{
TestOutputLogger.LogContext("FolderUID", _testFolderUid);
ContentstackResponse response = _stack.Asset().Folder(_testFolderUid).FetchAsync().Result;
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "FetchFolderAsync_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "FetchFolderAsync_ResponseContainsFolder");
}
else
{
AssertLogger.Fail("Fetch Async Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Fetch Async Failed for Folder ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test018_Should_Update_Folder()
{
TestOutputLogger.LogContext("TestScenario", "UpdateFolder");
try
{
if (string.IsNullOrEmpty(_testFolderUid))
{
Test014_Should_Create_Folder();
}
if (!string.IsNullOrEmpty(_testFolderUid))
{
TestOutputLogger.LogContext("FolderUID", _testFolderUid);
ContentstackResponse response = _stack.Asset().Folder(_testFolderUid).Update("Updated Test Folder", null);
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "UpdateFolder_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "UpdateFolder_ResponseContainsFolder");
}
else
{
AssertLogger.Fail("Folder update Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Folder Update Async Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test019_Should_Update_Folder_Async()
{
TestOutputLogger.LogContext("TestScenario", "UpdateFolderAsync");
try
{
// First create a folder if we don't have one
if (string.IsNullOrEmpty(_testFolderUid))
{
Test014_Should_Create_Folder();
}
if (!string.IsNullOrEmpty(_testFolderUid))
{
TestOutputLogger.LogContext("FolderUID", _testFolderUid);
ContentstackResponse response = _stack.Asset().Folder(_testFolderUid).UpdateAsync("Async Updated Test Folder", null).Result;
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.Created, response.StatusCode, "UpdateFolderAsync_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "UpdateFolderAsync_ResponseContainsFolder");
}
else
{
AssertLogger.Fail("Folder Update Async Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Folder Delete Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test022_Should_Delete_Folder()
{
TestOutputLogger.LogContext("TestScenario", "DeleteFolder");
try
{
// First create a folder if we don't have one
if (string.IsNullOrEmpty(_testFolderUid))
{
Test014_Should_Create_Folder();
}
if (!string.IsNullOrEmpty(_testFolderUid))
{
TestOutputLogger.LogContext("FolderUID", _testFolderUid);
ContentstackResponse response = _stack.Asset().Folder(_testFolderUid).Delete();
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "DeleteFolder_StatusCode");
_testFolderUid = null; // Clear the UID since folder is deleted
}
else
{
AssertLogger.Fail("Delete Folder Failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Delete Folder Async Failed ",e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test023_Should_Delete_Folder_Async()
{
TestOutputLogger.LogContext("TestScenario", "DeleteFolderAsync");
try
{
// Create a new folder for deletion
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
ContentstackResponse createResponse = _stack.Asset().Folder().Create("Delete Test Folder", null);
if (createResponse.IsSuccessStatusCode)
{
var responseObject = createResponse.OpenJObjectResponse();
string folderUid = responseObject["asset"]["uid"]?.ToString();
TestOutputLogger.LogContext("FolderUID", folderUid ?? "null");
if (!string.IsNullOrEmpty(folderUid))
{
ContentstackResponse deleteResponse = _stack.Asset().Folder(folderUid).DeleteAsync().Result;
if (deleteResponse.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, deleteResponse.StatusCode, "DeleteFolderAsync_StatusCode");
}
else
{
AssertLogger.Fail("The Delete Folder Async Failed");
}
}
}
else
{
AssertLogger.Fail("The Create Folder Call Failed");
}
}
catch (Exception e)
{
AssertLogger.Fail("Delete Folder Async Failed ",e.Message);
}
}
// Phase 4: Error Handling and Edge Case Tests
[TestMethod]
[DoNotParallelize]
public void Test024_Should_Handle_Invalid_Asset_Operations()
{
TestOutputLogger.LogContext("TestScenario", "HandleInvalidAssetOperations");
string invalidAssetUid = "invalid_asset_uid_12345";
TestOutputLogger.LogContext("InvalidAssetUID", invalidAssetUid);
// Test fetching non-existent asset - expect exception
try
{
_stack.Asset(invalidAssetUid).Fetch();
AssertLogger.Fail("Expected exception for invalid asset fetch, but operation succeeded");
}
catch (ContentstackErrorException ex)
{
// Expected exception for invalid asset operations
AssertLogger.IsTrue(ex.Message.Contains("not found") || ex.Message.Contains("invalid"),
$"Expected 'not found' or 'invalid' in exception message, got: {ex.Message}", "InvalidAssetFetch_ExceptionMessage");
}
// Test updating non-existent asset - expect exception
try
{
var path = Path.Combine(System.Environment.CurrentDirectory, "../../../Mock/contentTypeSchema.json");
AssetModel updateModel = new AssetModel("invalid_asset.json", path, "application/json", title:"Invalid Asset", description:"invalid test asset", parentUID: null, tags:"invalid,test");
_stack.Asset(invalidAssetUid).Update(updateModel);
AssertLogger.Fail("Expected exception for invalid asset update, but operation succeeded");
}
catch (ContentstackErrorException ex)
{
// Expected exception for invalid asset operations
AssertLogger.IsTrue(ex.Message.Contains("not found") || ex.Message.Contains("invalid"),
$"Expected 'not found' or 'invalid' in exception message, got: {ex.Message}", "InvalidAssetUpdate_ExceptionMessage");
}
// Test deleting non-existent asset - expect exception
try
{
_stack.Asset(invalidAssetUid).Delete();
AssertLogger.Fail("Expected exception for invalid asset delete, but operation succeeded");
}
catch (ContentstackErrorException ex)
{
// Expected exception for invalid asset operations
AssertLogger.IsTrue(ex.Message.Contains("not found") || ex.Message.Contains("invalid"),
$"Expected 'not found' or 'invalid' in exception message, got: {ex.Message}", "InvalidAssetDelete_ExceptionMessage");
}
}
[TestMethod]
[DoNotParallelize]
public void Test026_Should_Handle_Invalid_Folder_Operations()
{
TestOutputLogger.LogContext("TestScenario", "HandleInvalidFolderOperations");
string invalidFolderUid = "invalid_folder_uid_12345";
TestOutputLogger.LogContext("InvalidFolderUID", invalidFolderUid);
// Test fetching non-existent folder - expect ContentstackErrorException
bool fetchExceptionThrown = false;
try
{
_stack.Asset().Folder(invalidFolderUid).Fetch();
// If we get here, the API returned success for invalid folder
Console.WriteLine("Warning: Fetching invalid folder unexpectedly succeeded");
}
catch (ContentstackErrorException ex)
{
Console.WriteLine($"Expected ContentstackErrorException for invalid folder fetch: {ex.Message}");
fetchExceptionThrown = true;
}
AssertLogger.IsTrue(fetchExceptionThrown, "Expected ContentstackErrorException for invalid folder fetch", "InvalidFolderFetch_ExceptionThrown");
// Test updating non-existent folder - API may succeed or throw exception
try
{
ContentstackResponse updateResponse = _stack.Asset().Folder(invalidFolderUid).Update("Invalid Folder", null);
// If we get here, the API returned success for invalid folder update
Console.WriteLine("Warning: Updating invalid folder unexpectedly succeeded");
// This is unexpected but not necessarily wrong - API behavior may vary
}
catch (ContentstackErrorException ex)
{
// Expected behavior - API should throw ContentstackErrorException for invalid folder update
Console.WriteLine($"Expected ContentstackErrorException for invalid folder update: {ex.Message}");
}
// Don't assert on update behavior as API may handle this differently
// Test deleting non-existent folder - API may succeed or throw exception
try
{
ContentstackResponse deleteResponse = _stack.Asset().Folder(invalidFolderUid).Delete();
// If we get here, the API returned success for invalid folder delete
Console.WriteLine("Warning: Deleting invalid folder unexpectedly succeeded");
// This is unexpected but not necessarily wrong - API behavior may vary
}
catch (ContentstackErrorException ex)
{
// Expected behavior - API should throw ContentstackErrorException for invalid folder delete
Console.WriteLine($"Expected ContentstackErrorException for invalid folder delete: {ex.Message}");
}
// Don't assert on delete behavior as API may handle this differently
}
[TestMethod]
[DoNotParallelize]
public void Test027_Should_Handle_Asset_Creation_With_Invalid_File()
{
TestOutputLogger.LogContext("TestScenario", "HandleAssetCreationWithInvalidFile");
string invalidPath = Path.Combine(System.Environment.CurrentDirectory, "non_existent_file.json");
TestOutputLogger.LogContext("InvalidFilePath", invalidPath);
// Expect FileNotFoundException during AssetModel construction due to file not found
try
{
new AssetModel("invalid_file.json", invalidPath, "application/json", title:"Invalid File Asset", description:"asset with invalid file", parentUID: null, tags:"invalid,file");
AssertLogger.Fail("Expected FileNotFoundException during AssetModel construction, but it succeeded");
}
catch (FileNotFoundException ex)
{
// Expected exception for file not found during AssetModel construction
AssertLogger.IsTrue(ex.Message.Contains("non_existent_file.json") || ex.Message.Contains("Could not find file"),
$"Expected file not found exception, got: {ex.Message}", "InvalidFileAsset_ExceptionMessage");
}
}
[TestMethod]
[DoNotParallelize]
public void Test029_Should_Handle_Query_With_Invalid_Parameters()
{
TestOutputLogger.LogContext("TestScenario", "HandleQueryWithInvalidParameters");
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
// Test asset query with invalid parameters - expect exception to be raised directly
var assetQuery = _stack.Asset().Query();
assetQuery.Limit(-1); // Invalid limit
assetQuery.Skip(-1); // Invalid skip
try
{
assetQuery.Find();
AssertLogger.Fail("Expected exception for invalid query parameters, but operation succeeded");
}
catch (ArgumentException ex)
{
// Expected exception for invalid parameters
AssertLogger.IsTrue(ex.Message.Contains("limit") || ex.Message.Contains("skip") || ex.Message.Contains("invalid"),
$"Expected parameter validation error, got: {ex.Message}", "InvalidQuery_ArgumentException");
}
catch (ContentstackErrorException ex)
{
// Expected ContentstackErrorException for invalid parameters
AssertLogger.IsTrue(ex.Message.Contains("parameter") || ex.Message.Contains("invalid") || ex.Message.Contains("limit") || ex.Message.Contains("skip"),
$"Expected parameter validation error, got: {ex.Message}", "InvalidQuery_ContentstackErrorException");
}
}
[TestMethod]
[DoNotParallelize]
public void Test030_Should_Handle_Empty_Query_Results()
{
TestOutputLogger.LogContext("TestScenario", "HandleEmptyQueryResults");
try
{
TestOutputLogger.LogContext("StackAPIKey", _stack?.APIKey ?? "null");
// Test query with very high skip value to get empty results
var assetQuery = _stack.Asset().Query();
assetQuery.Skip(999999);
assetQuery.Limit(1);
ContentstackResponse response = assetQuery.Find();
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "EmptyQuery_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["assets"], "EmptyQuery_ResponseContainsAssets");
// Empty results are valid, so we don't assert on count
}
else
{
AssertLogger.Fail("Asset Querying with Empty Query Failed");
}
}
catch (Exception e)
{
AssertLogger.Fail(e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test031_Should_Fetch_Asset_With_Locale_Parameter()
{
TestOutputLogger.LogContext("TestScenario", "FetchAssetWithLocaleParameter");
try
{
if (string.IsNullOrEmpty(_testAssetUid))
{
Test005_Should_Create_Asset_Async();
}
if (!string.IsNullOrEmpty(_testAssetUid))
{
TestOutputLogger.LogContext("AssetUID", _testAssetUid);
var coll = new ParameterCollection();
coll.Add("locale", "en-us");
ContentstackResponse response = _stack.Asset(_testAssetUid).Fetch(coll);
if (response.IsSuccessStatusCode)
{
AssertLogger.AreEqual(System.Net.HttpStatusCode.OK, response.StatusCode, "FetchAssetWithLocale_StatusCode");
var responseObject = response.OpenJObjectResponse();
AssertLogger.IsNotNull(responseObject["asset"], "FetchAssetWithLocale_ResponseContainsAsset");
}
else
{
AssertLogger.Fail("Fetch asset with locale parameter failed");
}
}
}
catch (Exception e)
{
AssertLogger.Fail("Asset Fetch with locale parameter failed ", e.Message);
}
}
[TestMethod]
[DoNotParallelize]
public void Test032_Should_Fetch_Asset_Async_With_Locale_Parameter()
{
TestOutputLogger.LogContext("TestScenario", "FetchAssetAsyncWithLocaleParameter");
try
{
if (string.IsNullOrEmpty(_testAssetUid))
{
Test005_Should_Create_Asset_Async();
}
if (!string.IsNullOrEmpty(_testAssetUid))
{
TestOutputLogger.LogContext("AssetUID", _testAssetUid);
var coll = new ParameterCollection();
coll.Add("locale", "en-us");
ContentstackResponse response = _stack.Asset(_testAssetUid).FetchAsync(coll).Result;
if (response.IsSuccessStatusCode)
{