Skip to content

Commit efa71dd

Browse files
committed
Add tests for nested global fields and API version header
- Implemented unit tests for creating, fetching, updating, and deleting nested global fields in GlobalFieldAPITest. - Added a test to verify that the API version header is correctly set in requests.
1 parent 56df5a4 commit efa71dd

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

src/test/java/com/contentstack/cms/stack/GlobalFieldAPITest.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
package com.contentstack.cms.stack;
22

3+
import java.io.IOException;
4+
import java.util.HashMap;
5+
36
import com.contentstack.cms.TestClient;
47
import com.contentstack.cms.Utils;
58
import com.contentstack.cms.core.Util;
9+
610
import okhttp3.Request;
11+
712
import org.json.simple.JSONObject;
813
import org.junit.jupiter.api.*;
914

15+
import com.contentstack.cms.Contentstack;
16+
import com.google.gson.JsonObject;
17+
18+
import okhttp3.ResponseBody;
19+
import retrofit2.Response;
20+
1021
@Tag("unit")
1122
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1223
class GlobalFieldAPITest {
1324

1425
public static GlobalField globalField;
1526
protected static String API_KEY = TestClient.API_KEY;
1627
protected static String MANAGEMENT_TOKEN = TestClient.MANAGEMENT_TOKEN;
28+
protected static String AUTHTOKEN = TestClient.AUTHTOKEN;
1729
protected static String globalFieldUid = "global_field_1";
1830
protected static String globalFieldUid2 = "nested_global_field";
1931
protected static Stack stack;
@@ -327,4 +339,103 @@ void testExportNestedGlobalField() {
327339
// Assertions.assertNull(request.url().encodedQuery());
328340
// }
329341

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+
330441
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"global_field": {
3+
"title": "Nested Global Field",
4+
"uid": "nested_global_field",
5+
"description": "",
6+
"schema": [
7+
{
8+
"data_type": "text",
9+
"display_name": "Single Line Textbox",
10+
"uid": "single_line",
11+
"field_metadata": {
12+
"description": "",
13+
"default_value": "",
14+
"version": 3
15+
},
16+
"format": "",
17+
"error_messages": {
18+
"format": ""
19+
},
20+
"mandatory": false,
21+
"multiple": false,
22+
"non_localizable": false,
23+
"unique": false,
24+
"indexed": false,
25+
"inbuilt_model": false
26+
},
27+
{
28+
"data_type": "global_field",
29+
"display_name": "SEO",
30+
"reference_to": "seo",
31+
"field_metadata": {
32+
"description": ""
33+
},
34+
"uid": "seo",
35+
"mandatory": false,
36+
"multiple": false,
37+
"non_localizable": false,
38+
"unique": false,
39+
"indexed": false,
40+
"inbuilt_model": false
41+
}
42+
]
43+
}
44+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"global_field": {
3+
"title": "Nested Global Field",
4+
"uid": "nested_global_field",
5+
"description": "",
6+
"schema": [
7+
{
8+
"data_type": "text",
9+
"display_name": "Single Line Textbox",
10+
"uid": "single_line",
11+
"field_metadata": {
12+
"description": "",
13+
"default_value": "",
14+
"version": 3
15+
},
16+
"format": "",
17+
"error_messages": {
18+
"format": ""
19+
},
20+
"mandatory": false,
21+
"multiple": false,
22+
"non_localizable": false,
23+
"unique": false,
24+
"indexed": false,
25+
"inbuilt_model": false
26+
},
27+
{
28+
"data_type": "global_field",
29+
"display_name": "SEO",
30+
"reference_to": "seo",
31+
"field_metadata": {
32+
"description": ""
33+
},
34+
"uid": "seo",
35+
"mandatory": false,
36+
"multiple": false,
37+
"non_localizable": false,
38+
"unique": false,
39+
"indexed": false,
40+
"inbuilt_model": false
41+
},
42+
{
43+
"data_type": "global_field",
44+
"display_name": "feature",
45+
"reference_to": "feature",
46+
"field_metadata": {
47+
"description": ""
48+
},
49+
"uid": "feature",
50+
"mandatory": false,
51+
"multiple": false,
52+
"non_localizable": false,
53+
"unique": false,
54+
"indexed": false,
55+
"inbuilt_model": false
56+
}
57+
]
58+
}
59+
}

0 commit comments

Comments
 (0)