|
1 | 1 | package com.contentstack.utils; |
2 | 2 |
|
3 | 3 | import com.contentstack.utils.render.DefaultOption; |
| 4 | +import org.json.JSONArray; |
4 | 5 | import org.json.JSONObject; |
| 6 | +import org.junit.Assert; |
5 | 7 | import org.junit.BeforeClass; |
6 | 8 | import org.junit.FixMethodOrder; |
7 | 9 | import org.junit.Test; |
8 | 10 | import org.junit.runners.MethodSorters; |
9 | 11 |
|
10 | 12 | import java.io.IOException; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.Set; |
11 | 16 | import java.util.logging.Level; |
12 | 17 | import java.util.logging.Logger; |
13 | 18 |
|
@@ -173,6 +178,129 @@ public void testUpdateAssetUrl() throws IOException{ |
173 | 178 | // System.out.println(localJsonObj1); |
174 | 179 | } |
175 | 180 |
|
| 181 | + private static Set<String> jsonArrayToStringSet(JSONArray arr) { |
| 182 | + Set<String> set = new HashSet<>(); |
| 183 | + for (int i = 0; i < arr.length(); i++) { |
| 184 | + set.add(arr.getString(i)); |
| 185 | + } |
| 186 | + return set; |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testGetVariantAliasesSingleEntry() throws IOException { |
| 191 | + final String json = "src/test/resources/variant_entry_single.json"; |
| 192 | + JSONObject full = new ReadResource().readJson(json); |
| 193 | + JSONObject entry = full.getJSONObject("entry"); |
| 194 | + String contentTypeUid = "movie"; |
| 195 | + |
| 196 | + JSONObject result = Utils.getVariantAliases(entry, contentTypeUid); |
| 197 | + |
| 198 | + Assert.assertTrue(result.has("entry_uid") && !result.getString("entry_uid").isEmpty()); |
| 199 | + Assert.assertEquals(contentTypeUid, result.getString("contenttype_uid")); |
| 200 | + JSONArray variants = result.getJSONArray("variants"); |
| 201 | + Assert.assertNotNull(variants); |
| 202 | + Set<String> aliasSet = jsonArrayToStringSet(variants); |
| 203 | + Assert.assertEquals( |
| 204 | + new HashSet<>(Arrays.asList("cs_personalize_0_0", "cs_personalize_0_3")), |
| 205 | + aliasSet); |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + public void testGetDataCsvariantsAttributeSingleEntry() throws IOException { |
| 210 | + final String json = "src/test/resources/variant_entry_single.json"; |
| 211 | + JSONObject full = new ReadResource().readJson(json); |
| 212 | + JSONObject entry = full.getJSONObject("entry"); |
| 213 | + String contentTypeUid = "movie"; |
| 214 | + |
| 215 | + JSONObject result = Utils.getDataCsvariantsAttribute(entry, contentTypeUid); |
| 216 | + |
| 217 | + Assert.assertTrue(result.has("data-csvariants")); |
| 218 | + String dataCsvariantsStr = result.getString("data-csvariants"); |
| 219 | + JSONArray arr = new JSONArray(dataCsvariantsStr); |
| 220 | + Assert.assertEquals(1, arr.length()); |
| 221 | + JSONObject first = arr.getJSONObject(0); |
| 222 | + Assert.assertTrue(first.has("entry_uid") && !first.getString("entry_uid").isEmpty()); |
| 223 | + Assert.assertEquals(contentTypeUid, first.getString("contenttype_uid")); |
| 224 | + Set<String> aliasSet = jsonArrayToStringSet(first.getJSONArray("variants")); |
| 225 | + Assert.assertEquals( |
| 226 | + new HashSet<>(Arrays.asList("cs_personalize_0_0", "cs_personalize_0_3")), |
| 227 | + aliasSet); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void testGetVariantAliasesMultipleEntries() throws IOException { |
| 232 | + final String json = "src/test/resources/variant_entries.json"; |
| 233 | + JSONObject full = new ReadResource().readJson(json); |
| 234 | + JSONArray entries = full.getJSONArray("entries"); |
| 235 | + String contentTypeUid = "movie"; |
| 236 | + |
| 237 | + JSONArray result = Utils.getVariantAliases(entries, contentTypeUid); |
| 238 | + |
| 239 | + Assert.assertNotNull(result); |
| 240 | + Assert.assertEquals(3, result.length()); |
| 241 | + JSONObject first = result.getJSONObject(0); |
| 242 | + Assert.assertTrue(first.has("entry_uid") && !first.getString("entry_uid").isEmpty()); |
| 243 | + Assert.assertEquals(contentTypeUid, first.getString("contenttype_uid")); |
| 244 | + Set<String> firstSet = jsonArrayToStringSet(first.getJSONArray("variants")); |
| 245 | + Assert.assertEquals(new HashSet<>(Arrays.asList("cs_personalize_0_0", "cs_personalize_0_3")), firstSet); |
| 246 | + JSONObject second = result.getJSONObject(1); |
| 247 | + Assert.assertTrue(second.has("entry_uid") && !second.getString("entry_uid").isEmpty()); |
| 248 | + Assert.assertEquals(1, second.getJSONArray("variants").length()); |
| 249 | + Assert.assertEquals("cs_personalize_0_0", second.getJSONArray("variants").getString(0)); |
| 250 | + JSONObject third = result.getJSONObject(2); |
| 251 | + Assert.assertTrue(third.has("entry_uid") && !third.getString("entry_uid").isEmpty()); |
| 252 | + Assert.assertEquals(0, third.getJSONArray("variants").length()); |
| 253 | + } |
| 254 | + |
| 255 | + @Test |
| 256 | + public void testGetDataCsvariantsAttributeMultipleEntries() throws IOException { |
| 257 | + final String json = "src/test/resources/variant_entries.json"; |
| 258 | + JSONObject full = new ReadResource().readJson(json); |
| 259 | + JSONArray entries = full.getJSONArray("entries"); |
| 260 | + String contentTypeUid = "movie"; |
| 261 | + |
| 262 | + JSONObject result = Utils.getDataCsvariantsAttribute(entries, contentTypeUid); |
| 263 | + |
| 264 | + Assert.assertTrue(result.has("data-csvariants")); |
| 265 | + String dataCsvariantsStr = result.getString("data-csvariants"); |
| 266 | + JSONArray arr = new JSONArray(dataCsvariantsStr); |
| 267 | + Assert.assertEquals(3, arr.length()); |
| 268 | + Assert.assertTrue(arr.getJSONObject(0).has("entry_uid") && !arr.getJSONObject(0).getString("entry_uid").isEmpty()); |
| 269 | + Assert.assertEquals(2, arr.getJSONObject(0).getJSONArray("variants").length()); |
| 270 | + Assert.assertTrue(arr.getJSONObject(1).has("entry_uid") && !arr.getJSONObject(1).getString("entry_uid").isEmpty()); |
| 271 | + Assert.assertEquals(1, arr.getJSONObject(1).getJSONArray("variants").length()); |
| 272 | + Assert.assertTrue(arr.getJSONObject(2).has("entry_uid") && !arr.getJSONObject(2).getString("entry_uid").isEmpty()); |
| 273 | + Assert.assertEquals(0, arr.getJSONObject(2).getJSONArray("variants").length()); |
| 274 | + } |
| 275 | + |
| 276 | + @Test(expected = IllegalArgumentException.class) |
| 277 | + public void testGetVariantAliasesThrowsWhenEntryNull() { |
| 278 | + Utils.getVariantAliases((JSONObject) null, "landing_page"); |
| 279 | + } |
| 280 | + |
| 281 | + @Test(expected = IllegalArgumentException.class) |
| 282 | + public void testGetVariantAliasesThrowsWhenContentTypeUidNull() throws IOException { |
| 283 | + final String json = "src/test/resources/variant_entry_single.json"; |
| 284 | + JSONObject full = new ReadResource().readJson(json); |
| 285 | + JSONObject entry = full.getJSONObject("entry"); |
| 286 | + Utils.getVariantAliases(entry, null); |
| 287 | + } |
| 288 | + |
| 289 | + @Test(expected = IllegalArgumentException.class) |
| 290 | + public void testGetVariantAliasesThrowsWhenContentTypeUidEmpty() throws IOException { |
| 291 | + final String json = "src/test/resources/variant_entry_single.json"; |
| 292 | + JSONObject full = new ReadResource().readJson(json); |
| 293 | + JSONObject entry = full.getJSONObject("entry"); |
| 294 | + Utils.getVariantAliases(entry, ""); |
| 295 | + } |
| 296 | + |
| 297 | + @Test |
| 298 | + public void testGetDataCsvariantsAttributeWhenEntryNull() { |
| 299 | + JSONObject result = Utils.getDataCsvariantsAttribute((JSONObject) null, "landing_page"); |
| 300 | + Assert.assertTrue(result.has("data-csvariants")); |
| 301 | + Assert.assertEquals("[]", result.getString("data-csvariants")); |
| 302 | + } |
| 303 | + |
176 | 304 | } |
177 | 305 |
|
178 | 306 |
|
|
0 commit comments