Skip to content

Commit

Permalink
Merge pull request #132 from contentstack/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
cs-raj authored Sep 24, 2024
2 parents 7c245e6 + a4040c4 commit 0910f21
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

-Feature: Variants method introduced

## v1.15.0

### Date: 04-Sep-2024

- Live Preview 2.0 support added

## v1.14.2

### Date: 15-July-2024
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/contentstack/sdk/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Config {
protected JSONObject livePreviewEntry = null;
protected ContentstackRegion region = ContentstackRegion.US;
protected String managementToken;
protected String previewToken;
protected String branch;
protected Proxy proxy = null;
protected String[] earlyAccess = null;
Expand Down Expand Up @@ -181,6 +182,17 @@ protected Config setLivePreviewEntry(@NotNull JSONObject livePreviewEntry) {
return this;
}

/**
* Sets preview token.
*
* @param previewToken the preview token
* @return the preview token
*/
public Config setPreviewToken(@NotNull String previewToken){
this.previewToken = previewToken;
return this;
}

/**
* Sets management token.
*
Expand Down
28 changes: 27 additions & 1 deletion src/main/java/com/contentstack/sdk/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import com.contentstack.sdk.Constants.REQUEST_CONTROLLER;

import retrofit2.Response;
import retrofit2.Retrofit;

Expand Down Expand Up @@ -98,6 +101,15 @@ private void client(String endpoint) {

private void includeLivePreview() {
if (config.enableLivePreview) {
String urlLivePreview = config.livePreviewHost;
if(config.region != null && !config.region.name().isEmpty()){
if(config.region.name() == "US" ){
config.livePreviewHost = urlLivePreview;
}else{
String regionPrefix = config.region.name().toLowerCase();
config.livePreviewHost = regionPrefix + "-" + urlLivePreview;
}
}
this.livePreviewEndpoint = "https://".concat(config.livePreviewHost).concat("/v3/content_types/");
}
}
Expand Down Expand Up @@ -125,6 +137,7 @@ private void includeLivePreview() {
* @throws IOException IO Exception
*/
public Stack livePreviewQuery(Map<String, String> query) throws IOException {
if(config.enableLivePreview){
config.livePreviewHash = query.get(LIVE_PREVIEW);
config.livePreviewEntryUid = query.get(ENTRY_UID);
config.livePreviewContentType = query.get(CONTENT_TYPE_UID);
Expand All @@ -137,7 +150,17 @@ public Stack livePreviewQuery(Map<String, String> query) throws IOException {
try {
LinkedHashMap<String, Object> liveHeader = new LinkedHashMap<>();
liveHeader.put("api_key", this.headers.get("api_key"));
liveHeader.put("authorization", config.managementToken);

if(config.livePreviewHost.equals("rest-preview.contentstack.com"))
{
if(config.previewToken != null) {
liveHeader.put("preview_token", config.previewToken);
} else{
throw new IllegalAccessError("Provide the Preview Token for the host rest-preview.contentstack.com");
}
} else {
liveHeader.put("authorization", config.managementToken);
}
response = this.service.getRequest(livePreviewUrl, liveHeader).execute();
} catch (IOException e) {
throw new IllegalStateException("IO Exception while executing the Live Preview url");
Expand All @@ -150,6 +173,9 @@ public Stack livePreviewQuery(Map<String, String> query) throws IOException {
config.setLivePreviewEntry(liveResponse.getJSONObject("entry"));
}
}
} else {
throw new IllegalStateException("Live Preview is not enabled in Config");
}
return this;
}

Expand Down
16 changes: 8 additions & 8 deletions src/test/java/com/contentstack/sdk/TestAsset.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public void onCompletion(ResponseType responseType, List<Asset> assets, Error er
Asset model = assets.get(0);
assetUid = model.getAssetUid();
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
Assertions.assertEquals("image/jpeg", model.getFileType());
Assertions.assertEquals("1775299", model.getFileSize());
Assertions.assertEquals("phoenix2.jpg", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("phoenix2.jpg"));
Assertions.assertEquals("image/png", model.getFileType());
Assertions.assertEquals("13006", model.getFileSize());
Assertions.assertEquals("iot-icon.png", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
Assertions.assertTrue(model.toJSON().has("created_at"));
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
Assertions.assertEquals("gregory", model.getUpdateAt().getCalendarType());
Expand All @@ -60,10 +60,10 @@ void testNewAssetZOnlyForOrderByUid() {
@Override
public void onCompletion(ResponseType responseType, Error error) {
Assertions.assertTrue(asset.getAssetUid().startsWith("blt"));
Assertions.assertEquals("image/jpeg", asset.getFileType());
Assertions.assertEquals("1775299", asset.getFileSize());
Assertions.assertEquals("phoenix2.jpg", asset.getFileName());
Assertions.assertTrue(asset.getUrl().endsWith("phoenix2.jpg"));
Assertions.assertEquals("image/png", asset.getFileType());
Assertions.assertEquals("13006", asset.getFileSize());
Assertions.assertEquals("iot-icon.png", asset.getFileName());
Assertions.assertTrue(asset.getUrl().endsWith("iot-icon.png"));
Assertions.assertTrue(asset.toJSON().has("created_at"));
Assertions.assertTrue(asset.getCreatedBy().startsWith("blt"));
Assertions.assertEquals("gregory", asset.getUpdateAt().getCalendarType());
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/contentstack/sdk/TestAssetLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ void testNewAssetLibrary() {
public void onCompletion(ResponseType responseType, List<Asset> assets, Error error) {
Asset model = assets.get(0);
Assertions.assertTrue(model.getAssetUid().startsWith("blt"));
assertEquals("image/jpeg", model.getFileType());
assertEquals("1775299", model.getFileSize());
assertEquals("phoenix2.jpg", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("phoenix2.jpg"));
assertEquals("image/png", model.getFileType());
assertEquals("13006", model.getFileSize());
assertEquals("iot-icon.png", model.getFileName());
Assertions.assertTrue(model.getUrl().endsWith("iot-icon.png"));
Assertions.assertTrue(model.toJSON().has("created_at"));
Assertions.assertTrue(model.getCreatedBy().startsWith("blt"));
assertEquals("gregory", model.getUpdateAt().getCalendarType());
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/contentstack/sdk/TestEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void onCompletion(ResponseType responseType, Error error) {
@Test
@Order(4)
void entryCalling() {
Assertions.assertEquals(6, entry.headers.size());
Assertions.assertEquals(7, entry.headers.size());
logger.info("passed...");
}

Expand Down
64 changes: 64 additions & 0 deletions src/test/java/com/contentstack/sdk/TestLivePreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -26,6 +30,7 @@ public static void setUp() {
config = new Config();
}


/**
* Test config test.
*/
Expand Down Expand Up @@ -158,5 +163,64 @@ void testCompleteLivePreviewInQuery() throws Exception {
Assertions.assertNotNull(entry);
}

@Test
void testCompleteLivePreviewWithPreviewToken() throws IOException, IllegalAccessException {
Config livePreviewConfig = new Config()
.enableLivePreview(true)
.setLivePreviewHost("rest-preview.contentstack.com")
.setPreviewToken("preview_token");

Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", livePreviewConfig);

HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("live_preview", "hash167673");
hashMap.put("content_type_uid", "page");

stack.livePreviewQuery(hashMap);
Entry entry = stack.contentType("page").entry("entry_uid");
entry.fetch(null);
Assertions.assertNotNull(entry);

}

@Test()
void testLivePreviewWithoutPreviewToken() throws Exception {
Config livePreviewEnablerConfig = new Config().enableLivePreview(true).setLivePreviewHost("rest-preview.contentstack.com")
.setManagementToken("fake@token");
Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", livePreviewEnablerConfig);
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("live_preview", "hash167673");
hashMap.put("content_type_uid", "page");

IllegalAccessError thrown = Assertions.assertThrows(IllegalAccessError.class, () -> {
stack.livePreviewQuery(hashMap);
}, "Expected livePreviewQuery to throw IllegalAccessError");

Assertions.assertTrue(thrown.getMessage().contains("Provide the Preview Token for the host rest-preview.contentstack.com"),
"Exception message should mention that Preview Token is required");

logger.severe(thrown.getMessage());
}

@Test
void testLivePreviewDisabled() throws IllegalAccessException, IOException {
Config config = new Config()
.enableLivePreview(false)
.setPreviewToken("preview_token");

Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", config);

HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("live_preview", "hash167673");
hashMap.put("content_type_uid", "page");

Exception exception = assertThrows(IllegalStateException.class, () -> {
stack.livePreviewQuery(hashMap);
});

// Optionally, you can check the message of the exception
assertEquals("Live Preview is not enabled in Config", exception.getMessage(),
"Expected exception message does not match");
}

}
2 changes: 1 addition & 1 deletion src/test/java/com/contentstack/sdk/TestStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void testGetAllContentTypes() {
@Override
public void onCompletion(ContentTypesModel contentTypesModel, Error error) {
assertTrue(contentTypesModel.getResponse() instanceof JSONArray);
assertEquals(5, ((JSONArray) contentTypesModel.getResponse()).length());
assertEquals(8, ((JSONArray) contentTypesModel.getResponse()).length());
}
});
}
Expand Down

0 comments on commit 0910f21

Please sign in to comment.