1
1
package com .contentstack .cms .stack ;
2
2
3
+ import java .io .IOException ;
4
+ import java .util .HashMap ;
5
+
3
6
import com .contentstack .cms .TestClient ;
4
7
import com .contentstack .cms .Utils ;
5
8
import com .contentstack .cms .core .Util ;
9
+
6
10
import okhttp3 .Request ;
11
+
7
12
import org .json .simple .JSONObject ;
8
13
import org .junit .jupiter .api .*;
9
14
15
+ import com .contentstack .cms .Contentstack ;
16
+ import com .google .gson .JsonObject ;
17
+
18
+ import okhttp3 .ResponseBody ;
19
+ import retrofit2 .Response ;
20
+
10
21
@ Tag ("unit" )
11
22
@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
12
23
class GlobalFieldAPITest {
13
24
14
25
public static GlobalField globalField ;
15
26
protected static String API_KEY = TestClient .API_KEY ;
16
27
protected static String MANAGEMENT_TOKEN = TestClient .MANAGEMENT_TOKEN ;
28
+ protected static String AUTHTOKEN = TestClient .AUTHTOKEN ;
17
29
protected static String globalFieldUid = "global_field_1" ;
18
30
protected static String globalFieldUid2 = "nested_global_field" ;
19
31
protected static Stack stack ;
@@ -327,4 +339,103 @@ void testExportNestedGlobalField() {
327
339
// Assertions.assertNull(request.url().encodedQuery());
328
340
// }
329
341
342
+
343
+ @ Test
344
+ void testApiVersionHeaderIsSet () {
345
+ HashMap <String , Object > headers = new HashMap <>();
346
+ headers .put (Util .API_KEY , API_KEY );
347
+ headers .put (Util .AUTHORIZATION , MANAGEMENT_TOKEN );
348
+ String apiVersion = "3.2" ;
349
+ Stack stack = new Contentstack .Builder ().setAuthtoken (AUTHTOKEN ).build ().stack (headers );
350
+ GlobalField gfWithApiVersion = new GlobalField (stack .client , stack .headers , "feature" );
351
+ gfWithApiVersion .addHeader ("api_version" , apiVersion );
352
+ Request request = gfWithApiVersion .fetch ().request ();
353
+ Assertions .assertEquals (apiVersion , request .header ("api_version" ));
354
+ }
355
+
356
+ // --- Nested Global Field Test Suite ---
357
+ @ Nested
358
+ @ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
359
+ class NestedGlobalFieldTests {
360
+ GlobalField nestedGlobalField ;
361
+ String nestedUid = "nested_global_field" ;
362
+ String apiVersion = "3.2" ;
363
+
364
+ @ BeforeEach
365
+ void setupNested () {
366
+ HashMap <String , Object > headers = new HashMap <>();
367
+ headers .put (Util .API_KEY , API_KEY );
368
+ headers .put (Util .AUTHORIZATION , MANAGEMENT_TOKEN );
369
+ Stack stack = new Contentstack .Builder ().setAuthtoken (AUTHTOKEN ).build ().stack (headers );
370
+ nestedGlobalField = new GlobalField (stack .client , stack .headers , nestedUid );
371
+ nestedGlobalField .addHeader ("api_version" , apiVersion );
372
+ }
373
+
374
+ @ Test
375
+ @ Order (1 )
376
+ void testCreateNestedGlobalField () throws IOException {
377
+ JSONObject requestBody = Utils .readJson ("globalfield/nested_global_field.json" );
378
+ Request request = nestedGlobalField .create (requestBody ).request ();
379
+ Assertions .assertEquals ("https://api.contentstack.io/v3/global_fields" , request .url ().toString ());
380
+ Assertions .assertEquals ("/v3/global_fields" , request .url ().encodedPath ());
381
+ Assertions .assertEquals ("https" , request .url ().scheme ());
382
+ Assertions .assertEquals ("POST" , request .method ());
383
+ Assertions .assertEquals (apiVersion , request .header ("api_version" ));
384
+ Response <ResponseBody > response = nestedGlobalField .create (requestBody ).execute ();
385
+ Assertions .assertEquals (201 , response .code ());
386
+ }
387
+
388
+ @ Test
389
+ @ Order (2 )
390
+ void testGetNestedGlobalField () throws IOException {
391
+ nestedGlobalField .addParam ("include_global_fields" , true );
392
+ nestedGlobalField .addParam ("include_validation_keys" , true );
393
+ Request request = nestedGlobalField .fetch ().request ();
394
+ Assertions .assertEquals ("https://api.contentstack.io/v3/global_fields/" + nestedUid + "?include_global_fields=true&include_validation_keys=true" , request .url ().toString ());
395
+ Assertions .assertEquals ("https" , request .url ().scheme ());
396
+ Assertions .assertEquals ("GET" , request .method ());
397
+ Assertions .assertEquals (apiVersion , request .header ("api_version" ));
398
+ Response <ResponseBody > response = nestedGlobalField .fetch ().execute ();
399
+ Assertions .assertEquals (200 , response .code ());
400
+ JsonObject responseBody = Utils .toJson (response ).getAsJsonObject ();
401
+ JsonObject globalField = responseBody .getAsJsonObject ("global_field" );
402
+ Assertions .assertEquals ("Nested Global Field" , globalField .get ("title" ).getAsString ());
403
+ Assertions .assertTrue (globalField .has ("referred_global_fields" ));
404
+ Assertions .assertTrue (globalField .has ("validation_keys" ));
405
+ }
406
+
407
+ @ Test
408
+ @ Order (3 )
409
+ void testUpdateNestedGlobalField () throws IOException {
410
+ JSONObject requestBody = Utils .readJson ("globalfield/nested_global_field_update1.json" );
411
+ Request request = nestedGlobalField .update (requestBody ).request ();
412
+ Assertions .assertEquals ("https://api.contentstack.io/v3/global_fields/" + nestedUid , request .url ().toString ());
413
+ Assertions .assertEquals ("/v3/global_fields/" + nestedUid , request .url ().encodedPath ());
414
+ Assertions .assertEquals ("https" , request .url ().scheme ());
415
+ Assertions .assertEquals ("PUT" , request .method ());
416
+ Assertions .assertEquals (apiVersion , request .header ("api_version" ));
417
+ Response <ResponseBody > response = nestedGlobalField .update (requestBody ).execute ();
418
+ Assertions .assertEquals (200 , response .code ());
419
+ JsonObject responseBody = Utils .toJson (response ).getAsJsonObject ();
420
+ JsonObject globalField = responseBody .getAsJsonObject ("global_field" );
421
+ Assertions .assertEquals ("Nested Global Field" , globalField .get ("title" ).getAsString ());
422
+
423
+ }
424
+
425
+ @ Test
426
+ @ Order (4 )
427
+ void testDeleteNestedGlobalField () throws IOException {
428
+ Request request = nestedGlobalField .delete ().request ();
429
+ Assertions .assertEquals ("https://api.contentstack.io/v3/global_fields/" + nestedUid + "?force=true" , request .url ().toString ());
430
+ Assertions .assertEquals ("https" , request .url ().scheme ());
431
+ Assertions .assertEquals ("DELETE" , request .method ());
432
+ Assertions .assertEquals (apiVersion , request .header ("api_version" ));
433
+ Response <ResponseBody > response = nestedGlobalField .delete ().execute ();
434
+ Assertions .assertEquals (200 , response .code ());
435
+ JsonObject responseBody = Utils .toJson (response ).getAsJsonObject ();
436
+ Assertions .assertEquals ("Global Field deleted successfully." , responseBody .get ("notice" ).getAsString ());
437
+ }
438
+ }
439
+
440
+
330
441
}
0 commit comments