diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cf8920..381cf62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # CHANGELOG +## v2.2.0 + +### Date: 25-Aug-2025 + +- POJO implementation added + ## v2.1.3 ### Date: 06-Jun-2025 diff --git a/pom.xml b/pom.xml index dcf1628..1917c51 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.contentstack.sdk java - 2.1.3 + 2.2.0 jar contentstack-java Java SDK for Contentstack Content Delivery API @@ -22,7 +22,7 @@ 3.0.0 3.1.10 2.11.0 - 4.12.0 + 5.1.0 0.8.5 1.18.36 5.11.4 @@ -184,7 +184,7 @@ com.slack.api bolt - 1.44.0 + 1.45.3 org.jetbrains @@ -194,12 +194,12 @@ com.squareup.okhttp3 okhttp - 4.12.0 + 5.1.0 org.slf4j slf4j-simple - 1.7.36 + 2.0.17 diff --git a/src/main/java/com/contentstack/sdk/Asset.java b/src/main/java/com/contentstack/sdk/Asset.java index a03f63a..e281f53 100644 --- a/src/main/java/com/contentstack/sdk/Asset.java +++ b/src/main/java/com/contentstack/sdk/Asset.java @@ -3,6 +3,8 @@ import org.jetbrains.annotations.NotNull; import org.json.JSONObject; import retrofit2.Retrofit; +import lombok.Getter; +import lombok.Setter; import java.util.Calendar; import java.util.HashMap; @@ -29,6 +31,8 @@ * @version 1.0.0 * @since 01-11-2017 */ +@Getter +@Setter public class Asset { protected static final Logger logger = Logger.getLogger(Asset.class.getSimpleName()); diff --git a/src/main/java/com/contentstack/sdk/AssetModel.java b/src/main/java/com/contentstack/sdk/AssetModel.java index 7be3db6..3aa4a12 100644 --- a/src/main/java/com/contentstack/sdk/AssetModel.java +++ b/src/main/java/com/contentstack/sdk/AssetModel.java @@ -3,11 +3,17 @@ import java.util.LinkedHashMap; import org.json.JSONArray; import org.json.JSONObject; +import lombok.Getter; +import lombok.Setter; +import lombok.NoArgsConstructor; /** * The type Asset model. */ +@Getter +@Setter +@NoArgsConstructor class AssetModel { String uploadedUid; diff --git a/src/main/java/com/contentstack/sdk/AssetsModel.java b/src/main/java/com/contentstack/sdk/AssetsModel.java index 7102bc7..f587995 100644 --- a/src/main/java/com/contentstack/sdk/AssetsModel.java +++ b/src/main/java/com/contentstack/sdk/AssetsModel.java @@ -5,10 +5,16 @@ import org.json.JSONArray; import org.json.JSONObject; +import lombok.Getter; +import lombok.Setter; +import lombok.NoArgsConstructor; /** * The type Assets model. */ +@Getter +@Setter +@NoArgsConstructor class AssetsModel { List objects = new ArrayList<>(); diff --git a/src/main/java/com/contentstack/sdk/ContentType.java b/src/main/java/com/contentstack/sdk/ContentType.java index ae2d2ee..da2b8ba 100644 --- a/src/main/java/com/contentstack/sdk/ContentType.java +++ b/src/main/java/com/contentstack/sdk/ContentType.java @@ -2,7 +2,9 @@ import org.jetbrains.annotations.NotNull; import org.json.JSONObject; - +import org.json.JSONArray; +import lombok.Getter; +import lombok.Setter; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; @@ -20,6 +22,9 @@ * @version 1.0.0 * @since 01-11-2017 */ + +@Getter +@Setter public class ContentType { protected static final Logger logger = Logger.getLogger(ContentType.class.getSimpleName()); @@ -27,6 +32,13 @@ public class ContentType { protected Stack stackInstance = null; protected LinkedHashMap headers = null; + // NEW: Content type data fields for POJO access (public for Lombok-generated getters) + public String title; + public String description; + public String uid; + public JSONArray schema; + public JSONObject contentTypeData; + protected ContentType() throws IllegalAccessException { throw new IllegalAccessException("Can Not Access Private Modifier"); } @@ -156,7 +168,17 @@ private void fetchContentTypes(String urlString, JSONObject params, HashMap urlParams = getUrlParams(params); new CSBackgroundTask(this, stackInstance, Constants.FETCHCONTENTTYPES, urlString, headers, urlParams, - Constants.REQUEST_CONTROLLER.CONTENTTYPES.toString(), callback); + // Constants.REQUEST_CONTROLLER.CONTENTTYPES.toString(), callback); + Constants.REQUEST_CONTROLLER.CONTENTTYPES.toString(), new ContentTypesCallback() { + @Override + public void onCompletion(ContentTypesModel model, Error error) { + if (error == null) { + // NEW: Store content type data in this instance for POJO access + model.setContentTypeData(ContentType.this); + } + callback.onCompletion(model, error); + } + }); } } @@ -173,4 +195,20 @@ private HashMap getUrlParams(JSONObject urlQueriesJSON) { return hashMap; } + /** + * Set content type data from JSON response. + * This method is called internally by ContentTypesModel. + * + * @param ctData the content type data JSONObject + */ + protected void setContentTypeData(JSONObject ctData) { + if (ctData != null) { + this.title = ctData.optString("title"); + this.description = ctData.optString("description"); + this.uid = ctData.optString("uid"); + this.schema = ctData.optJSONArray("schema"); + this.contentTypeData = ctData; + } + } + } diff --git a/src/main/java/com/contentstack/sdk/ContentTypesModel.java b/src/main/java/com/contentstack/sdk/ContentTypesModel.java index 2fadcde..05332b8 100644 --- a/src/main/java/com/contentstack/sdk/ContentTypesModel.java +++ b/src/main/java/com/contentstack/sdk/ContentTypesModel.java @@ -5,12 +5,18 @@ import java.util.List; import org.json.JSONArray; import org.json.JSONObject; +import lombok.Getter; +import lombok.Setter; +import lombok.NoArgsConstructor; /** * The ContentTypesModel that contains content type response */ +@Getter +@Setter +@NoArgsConstructor public class ContentTypesModel { private Object response; @@ -58,4 +64,17 @@ public Object getResponse() { public JSONArray getResultArray() { return responseJSONArray; } + + /** + * Set content type data in the ContentType instance for POJO access. + * This method is called internally after fetching content type data. + * + * @param contentType the ContentType instance to set data in + */ + public void setContentTypeData(ContentType contentType) { + if (response instanceof JSONObject) { + JSONObject ctData = (JSONObject) response; + contentType.setContentTypeData(ctData); + } + } } diff --git a/src/main/java/com/contentstack/sdk/EntryModel.java b/src/main/java/com/contentstack/sdk/EntryModel.java index cbfddc6..eb0b543 100644 --- a/src/main/java/com/contentstack/sdk/EntryModel.java +++ b/src/main/java/com/contentstack/sdk/EntryModel.java @@ -4,8 +4,12 @@ import java.util.Map; import org.json.JSONArray; import org.json.JSONObject; +import lombok.Getter; +import lombok.Setter; +@Getter +@Setter class EntryModel { private static final String PUBLISH_DETAIL_KEY = "publish_details"; diff --git a/src/test/java/com/contentstack/sdk/TestAsset.java b/src/test/java/com/contentstack/sdk/TestAsset.java index 742f2bb..e27c9ac 100644 --- a/src/test/java/com/contentstack/sdk/TestAsset.java +++ b/src/test/java/com/contentstack/sdk/TestAsset.java @@ -189,4 +189,42 @@ void testAssetIncludeOwner() { Assertions.assertTrue(asset.urlQueries.has("include_metadata")); } + @Test + void testAssetAsPOJO() { + Asset asset = stack.asset(assetUid); + asset.fetch(new FetchResultCallback() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + if (error == null) { + Assertions.assertNotNull(asset.getAssetUid()); + Assertions.assertNotNull(asset.getFileType()); + Assertions.assertNotNull(asset.getFileSize()); + Assertions.assertNotNull(asset.getFileName()); + Assertions.assertNotNull(asset.getUrl()); + Assertions.assertNotNull(asset.getTags()); + Assertions.assertNotNull(asset.toJSON()); + } + } + }); + } + + @Test + void testAssetTypeSafety() { + Asset asset = stack.asset(assetUid); + asset.fetch(new FetchResultCallback() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + if (error == null) { + Assertions.assertNotNull(asset.getAssetUid()); + Assertions.assertNotNull(asset.getFileType()); + Assertions.assertNotNull(asset.getFileSize()); + Assertions.assertNotNull(asset.getFileName()); + Assertions.assertNotNull(asset.getUrl()); + Assertions.assertNotNull(asset.getTags()); + + } + } + }); + } + } diff --git a/src/test/java/com/contentstack/sdk/TestContentType.java b/src/test/java/com/contentstack/sdk/TestContentType.java index c79eaad..2948f9b 100644 --- a/src/test/java/com/contentstack/sdk/TestContentType.java +++ b/src/test/java/com/contentstack/sdk/TestContentType.java @@ -97,5 +97,35 @@ public void onCompletion(ContentTypesModel model, Error error) { }); } + @Test + void testContentTypeAsPOJO() { + ContentType contentType = stack.contentType("product"); + Assertions.assertNotNull(contentType.contentTypeUid); + Assertions.assertNotNull(contentType); + + Entry entry = contentType.entry("test-entry-uid"); + Query query = contentType.query(); + Assertions.assertNotNull(entry); + Assertions.assertNotNull(query); + Assertions.assertEquals("product", entry.getContentType()); + Assertions.assertEquals("product", query.getContentType()); + } + + @Test + void testContentTypePOJODataAccess() throws IllegalAccessException { + ContentType contentType = stack.contentType("product"); + JSONObject paramObj = new JSONObject(); + paramObj.put("include_schema", "true"); + contentType.fetch(paramObj, new ContentTypesCallback() { + @Override + public void onCompletion(ContentTypesModel model, Error error) { + if (error == null) { + Assertions.assertNotNull(contentType.contentTypeUid); + Assertions.assertNotNull(contentType); + } + } + }); + } + } diff --git a/src/test/java/com/contentstack/sdk/TestEntry.java b/src/test/java/com/contentstack/sdk/TestEntry.java index 3dfde0f..044ab5e 100644 --- a/src/test/java/com/contentstack/sdk/TestEntry.java +++ b/src/test/java/com/contentstack/sdk/TestEntry.java @@ -6,9 +6,7 @@ import java.util.List; import java.util.logging.Logger; import org.junit.jupiter.api.*; - -import static org.junit.Assert.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; @TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @@ -551,4 +549,48 @@ public void onCompletion(ResponseType responseType, Error error) { logger.info("passed..."); } + @Test + @Order(60) + void testEntryAsPOJO() { + Entry entry1 = stack.contentType("product").entry(entryUid); + + entry1.fetch(new EntryResultCallBack() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + if (error == null) { + System.out.println("entry fetched successfully"); + } + } + }); + + Assertions.assertNotNull(entry1.getTitle()); + Assertions.assertNotNull(entry1.getUid()); + Assertions.assertNotNull(entry1.getContentType()); + Assertions.assertNotNull(entry1.getLocale()); + } + + @Test + @Order(61) + void testEntryTypeSafety() { + Entry entry = stack.contentType(CONTENT_TYPE).entry(entryUid); + entry.fetch(new EntryResultCallBack() { + @Override + public void onCompletion(ResponseType responseType, Error error) { + if (error == null) { + Assertions.assertEquals(entryUid, entry.getUid()); + } + } + }); + + String title = entry.getTitle(); + String uid = entry.getUid(); + String contentType = entry.getContentType(); + String locale = entry.getLocale(); + + Assertions.assertTrue(title instanceof String); + Assertions.assertTrue(uid instanceof String); + Assertions.assertTrue(contentType instanceof String); + Assertions.assertTrue(locale instanceof String); + + } }