Skip to content

Feat/dx 2898 pojo impl testcases #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 14, 2025
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v2.2.0

### Date: 25-Aug-2025

- POJO implementation added

## v2.1.3

### Date: 06-Jun-2025
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.contentstack.sdk</groupId>
<artifactId>java</artifactId>
<version>2.1.3</version>
<version>2.2.0</version>
<packaging>jar</packaging>
<name>contentstack-java</name>
<description>Java SDK for Contentstack Content Delivery API</description>
Expand All @@ -22,7 +22,7 @@
<dotenv-source.version>3.0.0</dotenv-source.version>
<rxjava-source.version>3.1.10</rxjava-source.version>
<retrofit-source.version>2.11.0</retrofit-source.version>
<loggin.version>4.12.0</loggin.version>
<loggin.version>5.1.0</loggin.version>
<jococo-plugin.version>0.8.5</jococo-plugin.version>
<lombok-source.version>1.18.36</lombok-source.version>
<junit-jupiter.version>5.11.4</junit-jupiter.version>
Expand Down Expand Up @@ -184,7 +184,7 @@
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>bolt</artifactId>
<version>1.44.0</version>
<version>1.45.3</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
Expand All @@ -194,12 +194,12 @@
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<version>2.0.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.cdimascio/java-dotenv -->
<dependency>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/contentstack/sdk/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/contentstack/sdk/AssetModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/contentstack/sdk/AssetsModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> objects = new ArrayList<>();
Expand Down
42 changes: 40 additions & 2 deletions src/main/java/com/contentstack/sdk/ContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,13 +22,23 @@
* @version 1.0.0
* @since 01-11-2017
*/

@Getter
@Setter
public class ContentType {

protected static final Logger logger = Logger.getLogger(ContentType.class.getSimpleName());
protected String contentTypeUid;
protected Stack stackInstance = null;
protected LinkedHashMap<String, Object> 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");
}
Expand Down Expand Up @@ -156,7 +168,17 @@ private void fetchContentTypes(String urlString, JSONObject params, HashMap<Stri
if (callback != null) {
HashMap<String, Object> 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);
}
});
}
}

Expand All @@ -173,4 +195,20 @@ private HashMap<String, Object> 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;
}
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/contentstack/sdk/ContentTypesModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/contentstack/sdk/EntryModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/com/contentstack/sdk/TestAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}
});
}

}
30 changes: 30 additions & 0 deletions src/test/java/com/contentstack/sdk/TestContentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
}


}
48 changes: 45 additions & 3 deletions src/test/java/com/contentstack/sdk/TestEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

}
}
Loading