Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
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