Skip to content

Commit 8f7622a

Browse files
committed
Feat: Variants Utility
1 parent 1c4b133 commit 8f7622a

File tree

9 files changed

+458
-75
lines changed

9 files changed

+458
-75
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ gradle-app.setting
258258
### Gradle Patch ###
259259
**/build/
260260

261+
sample/
262+
261263
# End of https://www.toptal.com/developers/gitignore/api/macos,code-java,java-web,maven,gradle,intellij,visualstudiocode,eclipse
262264
.idea/compiler.xml
263265
.idea/encodings.xml

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@
115115
<artifactId>json-simple</artifactId>
116116
<version>${json.simple.version}</version>
117117
<scope>provided</scope>
118+
<exclusions>
119+
<exclusion>
120+
<groupId>junit</groupId>
121+
<artifactId>junit</artifactId>
122+
</exclusion>
123+
</exclusions>
118124
</dependency>
119125
<dependency>
120126
<groupId>org.springframework</groupId>
@@ -134,6 +140,14 @@
134140
<goals>
135141
<goal>prepare-agent</goal>
136142
</goals>
143+
<configuration>
144+
<!-- Exclude JDK/internal classes to avoid IllegalClassFormatException on Java 17+ -->
145+
<excludes>
146+
<exclude>sun/**</exclude>
147+
<exclude>com/sun/**</exclude>
148+
<exclude>jdk/**</exclude>
149+
</excludes>
150+
</configuration>
137151
</execution>
138152
<execution>
139153
<id>report</id>

src/main/java/com/contentstack/utils/Utils.java

Lines changed: 204 additions & 68 deletions
Large diffs are not rendered by default.

src/test/java/com/contentstack/utils/AssetLinkTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public void testRenderFunction() {
2323
String[] keys = new String[1];
2424
keys[0] = "assetlink";
2525
Utils.jsonToHTML(assetLink, keys, new DefaultOption());
26-
System.out.println(assetLink);
2726
Assert.assertEquals("<img display-type=\"display\" asset-name=\"11.jpg\" asset-type=\"image/jpeg\" asset-uid=\"asset_uid_1\" width=\"25.16914749661705\" className=\"dsd\" id=\"sdf\" type=\"asset\" content-type-uid=\"sys_assets\" class-name=\"embedded-asset\" src=\"https://image.url/11.jpg\" />", assetLink.opt("assetlink").toString());
2827
}
2928
}

src/test/java/com/contentstack/utils/ReadResource.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public class ReadResource {
1616

1717
public JSONObject readJson(String filename) throws IOException {
1818
File file = new File(filename);
19-
String absolutePath = file.getAbsolutePath();
20-
System.out.println(absolutePath);
2119
BufferedReader reader = new BufferedReader(new FileReader(file));
2220
StringBuilder stringBuilder = new StringBuilder();
2321
char[] buffer = new char[10];

src/test/java/com/contentstack/utils/TestRte.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ public void testAffectedEntry() throws IOException {
120120
final String rte = "src/test/resources/reports/wfs.json";
121121
JSONObject theRTE = new ReadResource().readJson(rte);
122122
String result = Utils.jsonToHTML(theRTE, new DefaultOption(), null);
123-
System.out.println(result);
124123
Assert.assertEquals(kWFSAffectedHtml, result);
125124
}
126125

@@ -130,7 +129,6 @@ public void testOne() throws IOException {
130129
final String rte = "src/test/resources/reports/one.json";
131130
JSONObject theRTE = new ReadResource().readJson(rte);
132131
String result = Utils.jsonToHTML(theRTE, new DefaultOption(), null);
133-
System.out.println(result);
134132
Assert.assertEquals(kONEHtml, result);
135133
}
136134

@@ -139,7 +137,6 @@ public void testOCT7Issue() throws IOException {
139137
final String rte = "src/test/resources/reports/oct_7.json";
140138
JSONObject theRTE = new ReadResource().readJson(rte);
141139
String result = Utils.jsonToHTML(theRTE, new DefaultOption(), null);
142-
System.out.println(result);
143140
// Assert.assertEquals(kONEHtml, result);
144141
}
145142

@@ -148,7 +145,6 @@ public void testIssueOct() throws IOException {
148145
final String rte = "src/test/resources/reports/issue_oct.json";
149146
JSONObject theRTE = new ReadResource().readJson(rte);
150147
String result = Utils.jsonToHTML(theRTE, new DefaultOption(), null);
151-
System.out.println(result);
152148
// Assert.assertEquals(kONEHtml, result);
153149
}
154150

src/test/java/com/contentstack/utils/UtilTests.java

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.contentstack.utils;
22

33
import com.contentstack.utils.render.DefaultOption;
4+
import org.json.JSONArray;
45
import org.json.JSONObject;
6+
import org.junit.Assert;
57
import org.junit.BeforeClass;
68
import org.junit.FixMethodOrder;
79
import org.junit.Test;
810
import org.junit.runners.MethodSorters;
911

1012
import java.io.IOException;
13+
import java.util.Arrays;
14+
import java.util.HashSet;
15+
import java.util.Set;
1116
import java.util.logging.Level;
1217
import java.util.logging.Logger;
1318

@@ -173,6 +178,129 @@ public void testUpdateAssetUrl() throws IOException{
173178
// System.out.println(localJsonObj1);
174179
}
175180

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+
176304
}
177305

178306

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"entries": [
3+
{
4+
"uid": "entry_uid_1",
5+
"_metadata": {},
6+
"locale": "en-us",
7+
"_version": 1,
8+
"title": "Sample Movie",
9+
"publish_details": {
10+
"time": "2025-12-11T07:56:17.574Z",
11+
"user": "test_user",
12+
"environment": "test_env",
13+
"locale": "en-us",
14+
"variants": {
15+
"cs_variant_0_0": {
16+
"alias": "cs_personalize_0_0",
17+
"environment": "test_env",
18+
"time": "2025-12-11T07:56:17.574Z",
19+
"locale": "en-us",
20+
"user": "test_user",
21+
"version": 1
22+
},
23+
"cs_variant_0_3": {
24+
"alias": "cs_personalize_0_3",
25+
"environment": "test_env",
26+
"time": "2025-12-11T07:56:17.582Z",
27+
"locale": "en-us",
28+
"user": "test_user",
29+
"version": 1
30+
}
31+
}
32+
}
33+
},
34+
{
35+
"uid": "entry_uid_2",
36+
"_metadata": {},
37+
"locale": "en-us",
38+
"_version": 2,
39+
"title": "Another Movie",
40+
"publish_details": {
41+
"time": "2025-12-11T07:10:19.964Z",
42+
"user": "test_user",
43+
"environment": "test_env",
44+
"locale": "en-us",
45+
"variants": {
46+
"cs_variant_0_0": {
47+
"alias": "cs_personalize_0_0",
48+
"environment": "test_env",
49+
"time": "2025-12-11T07:10:19.964Z",
50+
"locale": "en-us",
51+
"user": "test_user",
52+
"version": 2
53+
}
54+
}
55+
}
56+
},
57+
{
58+
"uid": "entry_uid_3",
59+
"_metadata": {},
60+
"locale": "en-us",
61+
"_version": 1,
62+
"title": "Movie No Variants",
63+
"publish_details": {
64+
"time": "2025-11-20T10:00:00.000Z",
65+
"user": "test_user",
66+
"environment": "test_env",
67+
"locale": "en-us"
68+
}
69+
}
70+
]
71+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"entry": {
3+
"uid": "entry_uid_single",
4+
"_metadata": {},
5+
"locale": "en-us",
6+
"_version": 1,
7+
"ACL": {},
8+
"_in_progress": false,
9+
"title": "Sample Movie",
10+
"created_at": "2025-11-20T10:00:00.000Z",
11+
"updated_at": "2025-12-11T07:56:17.574Z",
12+
"created_by": "test_user",
13+
"updated_by": "test_user",
14+
"publish_details": {
15+
"time": "2025-12-11T07:56:17.574Z",
16+
"user": "test_user",
17+
"environment": "test_env",
18+
"locale": "en-us",
19+
"variants": {
20+
"cs_variant_0_0": {
21+
"alias": "cs_personalize_0_0",
22+
"environment": "test_env",
23+
"time": "2025-12-11T07:56:17.574Z",
24+
"locale": "en-us",
25+
"user": "test_user",
26+
"version": 1
27+
},
28+
"cs_variant_0_3": {
29+
"alias": "cs_personalize_0_3",
30+
"environment": "test_env",
31+
"time": "2025-12-11T07:56:17.582Z",
32+
"locale": "en-us",
33+
"user": "test_user",
34+
"version": 1
35+
}
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)