1+ // Contentstack.Core/ContentstackClientTest.cs
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Reflection ;
5+ using System . Threading . Tasks ;
6+ using Xunit ;
7+ using Contentstack . Core . Configuration ;
8+
9+ namespace Contentstack . Core . Tests
10+ {
11+ public class ContentstackClientTest
12+ {
13+ private ContentstackClient CreateClient ( )
14+ {
15+ var options = new ContentstackOptions ( )
16+ {
17+ ApiKey = "api_key" ,
18+ DeliveryToken = "delivery_token" ,
19+ Environment = "environment" ,
20+ Version = "1.2.3" ,
21+ LivePreview = new LivePreviewConfig ( )
22+ {
23+ Enable = true ,
24+ Host = "https://preview.contentstack.com" ,
25+ ReleaseId = "rid" ,
26+ PreviewTimestamp = "ts" ,
27+ PreviewToken = "pt"
28+
29+ }
30+ } ;
31+ return new ContentstackClient ( options ) ;
32+ }
33+
34+ private string GetPrivateField ( ContentstackClient client , string fieldName )
35+ {
36+ var field = typeof ( ContentstackClient ) . GetField ( fieldName , BindingFlags . NonPublic | BindingFlags . Instance ) ;
37+ return ( string ) field . GetValue ( client ) ;
38+ }
39+
40+ [ Fact ]
41+ public void SetEntryUid_SetsValue_WhenNonEmpty ( )
42+ {
43+ var client = CreateClient ( ) ;
44+ client . SetEntryUid ( "entry123" ) ;
45+ Assert . Equal ( "entry123" , GetPrivateField ( client , "currentEntryUid" ) ) ;
46+ }
47+
48+ [ Fact ]
49+ public void SetEntryUid_DoesNotSet_WhenEmpty ( )
50+ {
51+ var client = CreateClient ( ) ;
52+ client . SetEntryUid ( "entry123" ) ;
53+ client . SetEntryUid ( "" ) ;
54+ Assert . Equal ( "entry123" , GetPrivateField ( client , "currentEntryUid" ) ) ;
55+ }
56+
57+ [ Fact ]
58+ public void SetEntryUid_DoesNotSet_WhenNull ( )
59+ {
60+ var client = CreateClient ( ) ;
61+ client . SetEntryUid ( "entry123" ) ;
62+ client . SetEntryUid ( null ) ;
63+ Assert . Equal ( "entry123" , GetPrivateField ( client , "currentEntryUid" ) ) ;
64+ }
65+
66+ [ Fact ]
67+ public void ContentType_SetscurrentContenttypeUid_WhenNonEmpty ( )
68+ {
69+ var client = CreateClient ( ) ;
70+ client . ContentType ( "blog" ) ;
71+ Assert . Equal ( "blog" , GetPrivateField ( client , "currentContenttypeUid" ) ) ;
72+ }
73+
74+ [ Fact ]
75+ public void ContentType_DoesNotSet_WhenEmpty ( )
76+ {
77+ var client = CreateClient ( ) ;
78+ client . ContentType ( "blog" ) ;
79+ client . ContentType ( "" ) ;
80+ Assert . Equal ( "blog" , GetPrivateField ( client , "currentContenttypeUid" ) ) ;
81+ }
82+
83+ [ Fact ]
84+ public void ContentType_DoesNotSet_WhenNull ( )
85+ {
86+ var client = CreateClient ( ) ;
87+ client . ContentType ( "blog" ) ;
88+ client . ContentType ( null ) ;
89+ Assert . Equal ( "blog" , GetPrivateField ( client , "currentContenttypeUid" ) ) ;
90+ }
91+
92+ [ Fact ]
93+ public void ContentType_ReturnsContentTypeInstance ( )
94+ {
95+ var client = CreateClient ( ) ;
96+ var contentType = client . ContentType ( "blog" ) ;
97+ Assert . NotNull ( contentType ) ;
98+ Assert . Equal ( "blog" , contentType . ContentTypeId ) ;
99+ }
100+
101+ [ Fact ]
102+ public void GlobalField_ReturnsGlobalFieldInstance ( )
103+ {
104+ var client = CreateClient ( ) ;
105+ var globalField = client . GlobalField ( "author" ) ;
106+ Assert . NotNull ( globalField ) ;
107+ Assert . Equal ( "author" , globalField . GlobalFieldId ) ;
108+ }
109+
110+ [ Fact ]
111+ public void Asset_ReturnsAssetInstance ( )
112+ {
113+ var client = CreateClient ( ) ;
114+ var asset = client . Asset ( "asset_uid" ) ;
115+ Assert . NotNull ( asset ) ;
116+ Assert . Equal ( "asset_uid" , asset . Uid ) ;
117+ }
118+
119+ [ Fact ]
120+ public void AssetLibrary_ReturnsAssetLibraryInstance ( )
121+ {
122+ var client = CreateClient ( ) ;
123+ var assetLibrary = client . AssetLibrary ( ) ;
124+ Assert . NotNull ( assetLibrary ) ;
125+ }
126+
127+ [ Fact ]
128+ public void Taxonomies_ReturnsTaxonomyInstance ( )
129+ {
130+ var client = CreateClient ( ) ;
131+ var taxonomy = client . Taxonomies ( ) ;
132+ Assert . NotNull ( taxonomy ) ;
133+ }
134+
135+ [ Fact ]
136+ public void GetVersion_ReturnsVersion ( )
137+ {
138+ var client = CreateClient ( ) ;
139+ var t = client . GetVersion ( ) ;
140+ Assert . Equal ( "1.2.3" , client . GetVersion ( ) ) ;
141+ }
142+
143+ [ Fact ]
144+ public void GetApplicationKey_ReturnsApiKey ( )
145+ {
146+ var client = CreateClient ( ) ;
147+ Assert . Equal ( "api_key" , client . GetApplicationKey ( ) ) ;
148+ }
149+
150+ [ Fact ]
151+ public void GetAccessToken_ReturnsDeliveryToken ( )
152+ {
153+ var client = CreateClient ( ) ;
154+ Assert . Equal ( "delivery_token" , client . GetAccessToken ( ) ) ;
155+ }
156+
157+ [ Fact ]
158+ public void GetLivePreviewConfig_ReturnsConfig ( )
159+ {
160+ var client = CreateClient ( ) ;
161+ Assert . NotNull ( client . GetLivePreviewConfig ( ) ) ;
162+ }
163+
164+ [ Fact ]
165+ public void RemoveHeader_RemovesHeader ( )
166+ {
167+ var client = CreateClient ( ) ;
168+ client . SetHeader ( "custom" , "value" ) ;
169+ client . RemoveHeader ( "custom" ) ;
170+ var localHeaders = typeof ( ContentstackClient )
171+ . GetField ( "_LocalHeaders" , BindingFlags . NonPublic | BindingFlags . Instance )
172+ . GetValue ( client ) as Dictionary < string , object > ;
173+ Assert . False ( localHeaders . ContainsKey ( "custom" ) ) ;
174+ }
175+
176+ [ Fact ]
177+ public void SetHeader_AddsHeader ( )
178+ {
179+ var client = CreateClient ( ) ;
180+ client . SetHeader ( "custom" , "value" ) ;
181+ var localHeaders = typeof ( ContentstackClient )
182+ . GetField ( "_LocalHeaders" , BindingFlags . NonPublic | BindingFlags . Instance )
183+ . GetValue ( client ) as Dictionary < string , object > ;
184+ Assert . True ( localHeaders . ContainsKey ( "custom" ) ) ;
185+ Assert . Equal ( "value" , localHeaders [ "custom" ] ) ;
186+ }
187+
188+ [ Fact ]
189+ public async Task LivePreviewQueryAsync_SetsLivePreviewConfigFields ( )
190+ {
191+ var client = CreateClient ( ) ;
192+ client . ContentType ( "ctuid" ) ;
193+ client . ContentType ( "ctuid" ) . Entry ( "euid" ) ;
194+ // Mock GetLivePreviewData to avoid actual HTTP call
195+ var method = typeof ( ContentstackClient ) . GetMethod ( "GetLivePreviewData" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
196+ method . Invoke ( client , null ) ; // Just to ensure method exists
197+
198+ var query = new Dictionary < string , string >
199+ {
200+ { "live_preview" , "hash" } ,
201+ { "release_id" , "rid" } ,
202+ { "preview_timestamp" , "ts" }
203+ } ;
204+
205+ // Patch GetLivePreviewData to return a dummy JObject
206+
207+ // Since GetLivePreviewData is private and does a real HTTP call,
208+ // we only test the config fields are set correctly before the call
209+ await client . LivePreviewQueryAsync ( query ) ;
210+ var v = client . GetLivePreviewConfig ( ) ;
211+ Assert . Equal ( "ctuid" , GetPrivateField ( client , "currentContenttypeUid" ) ) ;
212+ Assert . Equal ( "euid" , GetPrivateField ( client , "currentEntryUid" ) ) ;
213+ Assert . Equal ( true , v . Enable ) ;
214+ Assert . Equal ( "rid" , v . ReleaseId ) ;
215+ Assert . Equal ( "ts" , v . PreviewTimestamp ) ;
216+ Assert . Equal ( "pt" , v . PreviewToken ) ;
217+ }
218+
219+ // For SyncRecursive, SyncPaginationToken, SyncToken, you should mock HTTP calls.
220+ // Here we just check that the methods exist and can be called (will throw if not configured).
221+ [ Fact ]
222+ public async Task SyncRecursive_ThrowsOrReturns ( )
223+ {
224+ var client = CreateClient ( ) ;
225+ await Assert . ThrowsAnyAsync < Exception > ( ( ) => client . SyncRecursive ( ) ) ;
226+ }
227+
228+ [ Fact ]
229+ public async Task SyncPaginationToken_ThrowsOrReturns ( )
230+ {
231+ var client = CreateClient ( ) ;
232+ await Assert . ThrowsAnyAsync < Exception > ( ( ) => client . SyncPaginationToken ( "pagetoken" ) ) ;
233+ }
234+
235+ [ Fact ]
236+ public async Task SyncToken_ThrowsOrReturns ( )
237+ {
238+ var client = CreateClient ( ) ;
239+ await Assert . ThrowsAnyAsync < Exception > ( ( ) => client . SyncToken ( "synctoken" ) ) ;
240+ }
241+ }
242+ }
0 commit comments