Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ gradle-app.setting
### Gradle Patch ###
**/build/

sample/

# End of https://www.toptal.com/developers/gitignore/api/macos,code-java,java-web,maven,gradle,intellij,visualstudiocode,eclipse
.idea/compiler.xml
.idea/encodings.xml
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2012 - 2025 Contentstack
Copyright (c) 2012 - 2026 Contentstack

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@
<artifactId>json-simple</artifactId>
<version>${json.simple.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand All @@ -134,6 +140,14 @@
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!-- Exclude JDK/internal classes to avoid IllegalClassFormatException on Java 17+ -->
<excludes>
<exclude>sun/**</exclude>
<exclude>com/sun/**</exclude>
<exclude>jdk/**</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>report</id>
Expand Down
272 changes: 204 additions & 68 deletions src/main/java/com/contentstack/utils/Utils.java

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion src/test/java/com/contentstack/utils/AssetLinkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public void testRenderFunction() {
String[] keys = new String[1];
keys[0] = "assetlink";
Utils.jsonToHTML(assetLink, keys, new DefaultOption());
System.out.println(assetLink);
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());
}
}
2 changes: 0 additions & 2 deletions src/test/java/com/contentstack/utils/ReadResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public class ReadResource {

public JSONObject readJson(String filename) throws IOException {
File file = new File(filename);
String absolutePath = file.getAbsolutePath();
System.out.println(absolutePath);
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder stringBuilder = new StringBuilder();
char[] buffer = new char[10];
Expand Down
4 changes: 0 additions & 4 deletions src/test/java/com/contentstack/utils/TestRte.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public void testAffectedEntry() throws IOException {
final String rte = "src/test/resources/reports/wfs.json";
JSONObject theRTE = new ReadResource().readJson(rte);
String result = Utils.jsonToHTML(theRTE, new DefaultOption(), null);
System.out.println(result);
Assert.assertEquals(kWFSAffectedHtml, result);
}

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

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

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

Expand Down
128 changes: 128 additions & 0 deletions src/test/java/com/contentstack/utils/UtilTests.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.contentstack.utils;

import com.contentstack.utils.render.DefaultOption;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

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

private static Set<String> jsonArrayToStringSet(JSONArray arr) {
Set<String> set = new HashSet<>();
for (int i = 0; i < arr.length(); i++) {
set.add(arr.getString(i));
}
return set;
}

@Test
public void testGetVariantAliasesSingleEntry() throws IOException {
final String json = "src/test/resources/variant_entry_single.json";
JSONObject full = new ReadResource().readJson(json);
JSONObject entry = full.getJSONObject("entry");
String contentTypeUid = "movie";

JSONObject result = Utils.getVariantAliases(entry, contentTypeUid);

Assert.assertTrue(result.has("entry_uid") && !result.getString("entry_uid").isEmpty());
Assert.assertEquals(contentTypeUid, result.getString("contenttype_uid"));
JSONArray variants = result.getJSONArray("variants");
Assert.assertNotNull(variants);
Set<String> aliasSet = jsonArrayToStringSet(variants);
Assert.assertEquals(
new HashSet<>(Arrays.asList("cs_personalize_0_0", "cs_personalize_0_3")),
aliasSet);
}

@Test
public void testGetDataCsvariantsAttributeSingleEntry() throws IOException {
final String json = "src/test/resources/variant_entry_single.json";
JSONObject full = new ReadResource().readJson(json);
JSONObject entry = full.getJSONObject("entry");
String contentTypeUid = "movie";

JSONObject result = Utils.getDataCsvariantsAttribute(entry, contentTypeUid);

Assert.assertTrue(result.has("data-csvariants"));
String dataCsvariantsStr = result.getString("data-csvariants");
JSONArray arr = new JSONArray(dataCsvariantsStr);
Assert.assertEquals(1, arr.length());
JSONObject first = arr.getJSONObject(0);
Assert.assertTrue(first.has("entry_uid") && !first.getString("entry_uid").isEmpty());
Assert.assertEquals(contentTypeUid, first.getString("contenttype_uid"));
Set<String> aliasSet = jsonArrayToStringSet(first.getJSONArray("variants"));
Assert.assertEquals(
new HashSet<>(Arrays.asList("cs_personalize_0_0", "cs_personalize_0_3")),
aliasSet);
}

@Test
public void testGetVariantAliasesMultipleEntries() throws IOException {
final String json = "src/test/resources/variant_entries.json";
JSONObject full = new ReadResource().readJson(json);
JSONArray entries = full.getJSONArray("entries");
String contentTypeUid = "movie";

JSONArray result = Utils.getVariantAliases(entries, contentTypeUid);

Assert.assertNotNull(result);
Assert.assertEquals(3, result.length());
JSONObject first = result.getJSONObject(0);
Assert.assertTrue(first.has("entry_uid") && !first.getString("entry_uid").isEmpty());
Assert.assertEquals(contentTypeUid, first.getString("contenttype_uid"));
Set<String> firstSet = jsonArrayToStringSet(first.getJSONArray("variants"));
Assert.assertEquals(new HashSet<>(Arrays.asList("cs_personalize_0_0", "cs_personalize_0_3")), firstSet);
JSONObject second = result.getJSONObject(1);
Assert.assertTrue(second.has("entry_uid") && !second.getString("entry_uid").isEmpty());
Assert.assertEquals(1, second.getJSONArray("variants").length());
Assert.assertEquals("cs_personalize_0_0", second.getJSONArray("variants").getString(0));
JSONObject third = result.getJSONObject(2);
Assert.assertTrue(third.has("entry_uid") && !third.getString("entry_uid").isEmpty());
Assert.assertEquals(0, third.getJSONArray("variants").length());
}

@Test
public void testGetDataCsvariantsAttributeMultipleEntries() throws IOException {
final String json = "src/test/resources/variant_entries.json";
JSONObject full = new ReadResource().readJson(json);
JSONArray entries = full.getJSONArray("entries");
String contentTypeUid = "movie";

JSONObject result = Utils.getDataCsvariantsAttribute(entries, contentTypeUid);

Assert.assertTrue(result.has("data-csvariants"));
String dataCsvariantsStr = result.getString("data-csvariants");
JSONArray arr = new JSONArray(dataCsvariantsStr);
Assert.assertEquals(3, arr.length());
Assert.assertTrue(arr.getJSONObject(0).has("entry_uid") && !arr.getJSONObject(0).getString("entry_uid").isEmpty());
Assert.assertEquals(2, arr.getJSONObject(0).getJSONArray("variants").length());
Assert.assertTrue(arr.getJSONObject(1).has("entry_uid") && !arr.getJSONObject(1).getString("entry_uid").isEmpty());
Assert.assertEquals(1, arr.getJSONObject(1).getJSONArray("variants").length());
Assert.assertTrue(arr.getJSONObject(2).has("entry_uid") && !arr.getJSONObject(2).getString("entry_uid").isEmpty());
Assert.assertEquals(0, arr.getJSONObject(2).getJSONArray("variants").length());
}

@Test(expected = IllegalArgumentException.class)
public void testGetVariantAliasesThrowsWhenEntryNull() {
Utils.getVariantAliases((JSONObject) null, "landing_page");
}

@Test(expected = IllegalArgumentException.class)
public void testGetVariantAliasesThrowsWhenContentTypeUidNull() throws IOException {
final String json = "src/test/resources/variant_entry_single.json";
JSONObject full = new ReadResource().readJson(json);
JSONObject entry = full.getJSONObject("entry");
Utils.getVariantAliases(entry, null);
}

@Test(expected = IllegalArgumentException.class)
public void testGetVariantAliasesThrowsWhenContentTypeUidEmpty() throws IOException {
final String json = "src/test/resources/variant_entry_single.json";
JSONObject full = new ReadResource().readJson(json);
JSONObject entry = full.getJSONObject("entry");
Utils.getVariantAliases(entry, "");
}

@Test
public void testGetDataCsvariantsAttributeWhenEntryNull() {
JSONObject result = Utils.getDataCsvariantsAttribute((JSONObject) null, "landing_page");
Assert.assertTrue(result.has("data-csvariants"));
Assert.assertEquals("[]", result.getString("data-csvariants"));
}

}


Expand Down
71 changes: 71 additions & 0 deletions src/test/resources/variant_entries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"entries": [
{
"uid": "entry_uid_1",
"_metadata": {},
"locale": "en-us",
"_version": 1,
"title": "Sample Movie",
"publish_details": {
"time": "2025-12-11T07:56:17.574Z",
"user": "test_user",
"environment": "test_env",
"locale": "en-us",
"variants": {
"cs_variant_0_0": {
"alias": "cs_personalize_0_0",
"environment": "test_env",
"time": "2025-12-11T07:56:17.574Z",
"locale": "en-us",
"user": "test_user",
"version": 1
},
"cs_variant_0_3": {
"alias": "cs_personalize_0_3",
"environment": "test_env",
"time": "2025-12-11T07:56:17.582Z",
"locale": "en-us",
"user": "test_user",
"version": 1
}
}
}
},
{
"uid": "entry_uid_2",
"_metadata": {},
"locale": "en-us",
"_version": 2,
"title": "Another Movie",
"publish_details": {
"time": "2025-12-11T07:10:19.964Z",
"user": "test_user",
"environment": "test_env",
"locale": "en-us",
"variants": {
"cs_variant_0_0": {
"alias": "cs_personalize_0_0",
"environment": "test_env",
"time": "2025-12-11T07:10:19.964Z",
"locale": "en-us",
"user": "test_user",
"version": 2
}
}
}
},
{
"uid": "entry_uid_3",
"_metadata": {},
"locale": "en-us",
"_version": 1,
"title": "Movie No Variants",
"publish_details": {
"time": "2025-11-20T10:00:00.000Z",
"user": "test_user",
"environment": "test_env",
"locale": "en-us"
}
}
]
}
39 changes: 39 additions & 0 deletions src/test/resources/variant_entry_single.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"entry": {
"uid": "entry_uid_single",
"_metadata": {},
"locale": "en-us",
"_version": 1,
"ACL": {},
"_in_progress": false,
"title": "Sample Movie",
"created_at": "2025-11-20T10:00:00.000Z",
"updated_at": "2025-12-11T07:56:17.574Z",
"created_by": "test_user",
"updated_by": "test_user",
"publish_details": {
"time": "2025-12-11T07:56:17.574Z",
"user": "test_user",
"environment": "test_env",
"locale": "en-us",
"variants": {
"cs_variant_0_0": {
"alias": "cs_personalize_0_0",
"environment": "test_env",
"time": "2025-12-11T07:56:17.574Z",
"locale": "en-us",
"user": "test_user",
"version": 1
},
"cs_variant_0_3": {
"alias": "cs_personalize_0_3",
"environment": "test_env",
"time": "2025-12-11T07:56:17.582Z",
"locale": "en-us",
"user": "test_user",
"version": 1
}
}
}
}
}
Loading