|
4 | 4 | import com.contentstack.cms.TestClient; |
5 | 5 | import com.contentstack.cms.Utils; |
6 | 6 | import com.contentstack.cms.core.Util; |
7 | | -import com.google.gson.JsonObject; |
8 | 7 |
|
9 | 8 | import okhttp3.Request; |
10 | 9 | import okhttp3.ResponseBody; |
| 10 | +import org.json.simple.JSONArray; |
11 | 11 | import org.json.simple.JSONObject; |
| 12 | +import org.json.simple.parser.JSONParser; |
| 13 | +import org.json.simple.parser.ParseException; |
12 | 14 | import org.junit.jupiter.api.*; |
13 | 15 |
|
14 | | -import retrofit2.Call; |
15 | 16 | import retrofit2.Response; |
16 | 17 |
|
17 | 18 | import java.io.IOException; |
18 | 19 | import java.util.HashMap; |
19 | 20 |
|
| 21 | +import static org.junit.jupiter.api.Assertions.*; |
| 22 | +import static org.junit.jupiter.api.Assumptions.*; |
| 23 | + |
| 24 | +/** |
| 25 | + * API tests for Role module. |
| 26 | + * A role is a collection of permissions that will be applicable to all users assigned this role. |
| 27 | + * |
| 28 | + * Reference: https://www.contentstack.com/docs/developers/sdks/content-management-sdk/java/reference#role |
| 29 | + */ |
20 | 30 | @Tag("api") |
| 31 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
21 | 32 | @TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
22 | | -class RoleAPITest { |
| 33 | +public class RoleAPITest { |
23 | 34 |
|
24 | 35 | protected static String AUTHTOKEN = TestClient.AUTHTOKEN; |
25 | 36 | protected static String API_KEY = TestClient.API_KEY; |
26 | | - protected static String _uid = TestClient.AUTHTOKEN; |
27 | 37 | protected static String MANAGEMENT_TOKEN = TestClient.MANAGEMENT_TOKEN; |
28 | | - protected static Roles roles; |
29 | | - protected static String Taxonomy_uid = "sample_two"; |
30 | | - protected static Taxonomy taxonomy; |
31 | | - protected static Terms term; |
32 | 38 | protected static Stack stack; |
33 | | - |
| 39 | + private JSONParser parser = new JSONParser(); |
| 40 | + private String existingRoleUid; |
34 | 41 |
|
35 | 42 | @BeforeAll |
36 | | - static void setup() { |
37 | | - final String AUTHTOKEN = TestClient.AUTHTOKEN; |
| 43 | + void setup() { |
38 | 44 | HashMap<String, Object> headers = new HashMap<>(); |
39 | 45 | headers.put(Util.API_KEY, API_KEY); |
40 | 46 | headers.put(Util.AUTHORIZATION, MANAGEMENT_TOKEN); |
41 | | - stack = new Contentstack.Builder().setAuthtoken(AUTHTOKEN).build().stack(headers); |
42 | | - roles = stack.roles(_uid); |
43 | | - taxonomy = stack.taxonomy(); |
44 | | - term = stack.taxonomy(Taxonomy_uid).terms(); |
| 47 | + stack = new Contentstack.Builder() |
| 48 | + .setAuthtoken(AUTHTOKEN) |
| 49 | + .setHost(TestClient.DEV_HOST) |
| 50 | + .build() |
| 51 | + .stack(headers); |
45 | 52 | } |
46 | 53 |
|
| 54 | + // ==================== READ OPERATIONS (API TESTS) ==================== |
| 55 | + |
47 | 56 | @Test |
48 | | - void allRoles() throws IOException { |
49 | | - roles = stack.roles(_uid); |
| 57 | + @Order(100) |
| 58 | + @DisplayName("Find all roles - should return roles array") |
| 59 | + void testFindAllRoles() throws IOException, ParseException { |
| 60 | + Roles roles = stack.roles(); |
50 | 61 | roles.addParam("include_rules", true); |
51 | 62 | roles.addParam("include_permissions", true); |
| 63 | + |
52 | 64 | Response<ResponseBody> response = roles.find().execute(); |
53 | | - if (response.isSuccessful()) { |
54 | | - Assertions.assertTrue(response.isSuccessful()); |
55 | | - } else { |
56 | | - Assertions.assertFalse(response.isSuccessful()); |
57 | | - } |
58 | 65 |
|
| 66 | + assertTrue(response.isSuccessful(), |
| 67 | + "Find roles failed: " + getErrorMessage(response)); |
| 68 | + |
| 69 | + String body = response.body().string(); |
| 70 | + JSONObject result = (JSONObject) parser.parse(body); |
| 71 | + |
| 72 | + assertTrue(result.containsKey("roles"), "Response should contain 'roles' key"); |
| 73 | + JSONArray rolesArray = (JSONArray) result.get("roles"); |
| 74 | + assertNotNull(rolesArray, "Roles array should not be null"); |
| 75 | + assertFalse(rolesArray.isEmpty(), "Stack should have at least one role"); |
| 76 | + |
| 77 | + // Validate role structure |
| 78 | + JSONObject firstRole = (JSONObject) rolesArray.get(0); |
| 79 | + assertTrue(firstRole.containsKey("uid"), "Role should have 'uid' field"); |
| 80 | + assertTrue(firstRole.containsKey("name"), "Role should have 'name' field"); |
| 81 | + |
| 82 | + // Store first role UID for fetch test |
| 83 | + existingRoleUid = (String) firstRole.get("uid"); |
| 84 | + |
| 85 | + System.out.println("[Test] Found " + rolesArray.size() + " role(s)"); |
59 | 86 | } |
60 | 87 |
|
61 | 88 | @Test |
62 | | - void singleRole() throws IOException { |
63 | | - roles = stack.roles(_uid); |
64 | | - Response<ResponseBody> response = roles.fetch().execute(); |
65 | | - if (response.isSuccessful()) { |
66 | | - Assertions.assertTrue(response.isSuccessful()); |
67 | | - } else { |
68 | | - Assertions.assertFalse(response.isSuccessful()); |
| 89 | + @Order(101) |
| 90 | + @DisplayName("Fetch single role - should return role details") |
| 91 | + void testFetchSingleRole() throws IOException, ParseException { |
| 92 | + assumeTrue(existingRoleUid != null, "Skipping: No role UID available from previous test"); |
| 93 | + |
| 94 | + Response<ResponseBody> response = stack.roles(existingRoleUid).fetch().execute(); |
| 95 | + |
| 96 | + assertTrue(response.isSuccessful(), |
| 97 | + "Fetch role failed: " + getErrorMessage(response)); |
| 98 | + |
| 99 | + String body = response.body().string(); |
| 100 | + JSONObject result = (JSONObject) parser.parse(body); |
| 101 | + |
| 102 | + assertTrue(result.containsKey("role"), "Response should contain 'role' key"); |
| 103 | + JSONObject role = (JSONObject) result.get("role"); |
| 104 | + assertEquals(existingRoleUid, role.get("uid"), "Role UID should match"); |
| 105 | + assertNotNull(role.get("name"), "Role should have name"); |
| 106 | + } |
| 107 | + |
| 108 | + private String getErrorMessage(Response<ResponseBody> response) { |
| 109 | + try { |
| 110 | + if (response.errorBody() != null) { |
| 111 | + return response.errorBody().string(); |
| 112 | + } |
| 113 | + } catch (IOException e) { |
| 114 | + // Ignore |
69 | 115 | } |
| 116 | + return "Unknown error (code: " + response.code() + ")"; |
70 | 117 | } |
71 | 118 |
|
| 119 | + // ==================== UNIT TESTS (Request Validation) ==================== |
| 120 | + |
72 | 121 | @Test |
73 | | - void createRole() { |
| 122 | + @Tag("unit") |
| 123 | + void testCreateRoleRequest() { |
| 124 | + String testRoleUid = "test_role_uid"; |
74 | 125 | JSONObject body = new JSONObject(); |
75 | 126 | body.put("abcd", "abcd"); |
76 | | - roles = stack.roles(_uid); |
| 127 | + Roles roles = stack.roles(testRoleUid); |
77 | 128 | roles.addHeader(Util.API_KEY, API_KEY); |
78 | 129 | roles.addHeader(Util.AUTHORIZATION, MANAGEMENT_TOKEN); |
79 | 130 | Request request = roles.create(body).request(); |
80 | | - Assertions.assertEquals(2, request.headers().names().size()); |
81 | 131 | Assertions.assertEquals("POST", request.method()); |
82 | | - Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals(2, request.url().pathSegments().size()); |
| 132 | + Assertions.assertTrue(request.url().isHttps()); |
| 133 | + Assertions.assertEquals(2, request.url().pathSegments().size()); |
83 | 134 | Assertions.assertEquals("v3", request.url().pathSegments().get(0)); |
84 | 135 | Assertions.assertEquals("roles", request.url().pathSegments().get(1)); |
85 | | - Assertions.assertNull(request.url().encodedQuery()); } |
| 136 | + } |
86 | 137 |
|
87 | 138 | @Test |
88 | | - void updateRole() { |
89 | | - |
90 | | - roles = stack.roles(_uid); |
| 139 | + @Tag("unit") |
| 140 | + void testUpdateRoleRequest() { |
| 141 | + String testRoleUid = "test_role_uid"; |
| 142 | + Roles roles = stack.roles(testRoleUid); |
91 | 143 | roles.addHeader("authtoken", AUTHTOKEN); |
92 | 144 | roles.addHeader("api_key", API_KEY); |
93 | 145 | JSONObject object = new JSONObject(); |
94 | 146 | object.put("key", "value"); |
95 | 147 |
|
96 | 148 | Request request = roles.update(object).request(); |
97 | | - Assertions.assertEquals(3, request.headers().names().size()); |
98 | 149 | Assertions.assertEquals("PUT", request.method()); |
99 | | - Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals(3, request.url().pathSegments().size()); |
| 150 | + Assertions.assertTrue(request.url().isHttps()); |
| 151 | + Assertions.assertEquals(3, request.url().pathSegments().size()); |
100 | 152 | Assertions.assertEquals("v3", request.url().pathSegments().get(0)); |
101 | 153 | Assertions.assertEquals("roles", request.url().pathSegments().get(1)); |
102 | | - Assertions.assertNull(request.url().encodedQuery()); } |
| 154 | + } |
103 | 155 |
|
104 | 156 | @Test |
105 | | - void deleteRole() { |
106 | | - roles = stack.roles(_uid); |
| 157 | + @Tag("unit") |
| 158 | + void testDeleteRoleRequest() { |
| 159 | + String testRoleUid = "test_role_uid"; |
| 160 | + Roles roles = stack.roles(testRoleUid); |
107 | 161 | roles.addHeader("authtoken", AUTHTOKEN); |
108 | 162 | roles.addHeader("api_key", API_KEY); |
109 | 163 | Request request = roles.delete().request(); |
110 | | - Assertions.assertEquals(3, request.headers().names().size()); |
111 | 164 | Assertions.assertEquals("DELETE", request.method()); |
112 | | - Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals(3, request.url().pathSegments().size()); |
| 165 | + Assertions.assertTrue(request.url().isHttps()); |
| 166 | + Assertions.assertEquals(3, request.url().pathSegments().size()); |
113 | 167 | Assertions.assertEquals("v3", request.url().pathSegments().get(0)); |
114 | 168 | Assertions.assertEquals("roles", request.url().pathSegments().get(1)); |
115 | | - Assertions.assertNull(request.url().encodedQuery()); } |
116 | | - @Test |
117 | | - void createRoleWithRule1() throws IOException{ |
118 | | - JSONObject requestTaxonomy = Utils.readJson("mocktaxonomy/create.json"); |
119 | | - taxonomy.addHeader(Util.API_KEY, API_KEY); |
120 | | - taxonomy.addHeader(Util.AUTHORIZATION, MANAGEMENT_TOKEN); |
121 | | - Request request = taxonomy.create(requestTaxonomy).request(); |
122 | | - Assertions.assertEquals(2, request.headers().names().size()); |
123 | | - Assertions.assertEquals("POST", request.method()); |
124 | | - Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals(2, request.url().pathSegments().size()); |
125 | | - Assertions.assertEquals("v3", request.url().pathSegments().get(0)); |
126 | | - Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); |
127 | | - Assertions.assertNull(request.url().encodedQuery()); } |
128 | | - @Test |
129 | | - void createRoleWithRule2() throws IOException{ |
130 | | - JSONObject requestTerm = Utils.readJson("mocktaxonomy/createTerm.json"); |
131 | | - Request request = term.create(requestTerm).request(); |
132 | | - Assertions.assertEquals(2, request.headers().names().size()); |
133 | | - Assertions.assertEquals("POST", request.method()); |
134 | | - Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals(4, request.url().pathSegments().size()); |
135 | | - Assertions.assertEquals("v3", request.url().pathSegments().get(0)); |
136 | | - Assertions.assertEquals("taxonomies", request.url().pathSegments().get(1)); |
137 | | - Assertions.assertNull(request.url().encodedQuery()); } |
| 169 | + } |
| 170 | + |
138 | 171 | @Test |
139 | | - void createRoleWithTaxonomy() throws IOException{ |
| 172 | + @Tag("unit") |
| 173 | + void testCreateRoleWithTaxonomyRequest() throws IOException { |
140 | 174 | JSONObject requestBody = Utils.readJson("mockrole/createRole.json"); |
| 175 | + Roles roles = stack.roles(); |
141 | 176 | roles.addHeader(Util.API_KEY, API_KEY); |
142 | 177 | roles.addHeader(Util.AUTHORIZATION, MANAGEMENT_TOKEN); |
143 | 178 | Request request = roles.create(requestBody).request(); |
144 | | - Assertions.assertEquals(3, request.headers().names().size()); |
145 | 179 | Assertions.assertEquals("POST", request.method()); |
146 | | - Assertions.assertTrue(request.url().isHttps()); Assertions.assertEquals(2, request.url().pathSegments().size()); |
| 180 | + Assertions.assertTrue(request.url().isHttps()); |
| 181 | + Assertions.assertEquals(2, request.url().pathSegments().size()); |
147 | 182 | Assertions.assertEquals("v3", request.url().pathSegments().get(0)); |
148 | 183 | Assertions.assertEquals("roles", request.url().pathSegments().get(1)); |
149 | | - Assertions.assertNull(request.url().encodedQuery()); } |
150 | | - |
| 184 | + } |
151 | 185 | } |
0 commit comments