diff --git a/CHANGELOG.md b/CHANGELOG.md index 84206a93..01f7715d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/main/java/com/contentstack/sdk/Config.java b/src/main/java/com/contentstack/sdk/Config.java index e4567afc..54011b92 100644 --- a/src/main/java/com/contentstack/sdk/Config.java +++ b/src/main/java/com/contentstack/sdk/Config.java @@ -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; @@ -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. * diff --git a/src/main/java/com/contentstack/sdk/Stack.java b/src/main/java/com/contentstack/sdk/Stack.java index 2f27565d..20bca289 100644 --- a/src/main/java/com/contentstack/sdk/Stack.java +++ b/src/main/java/com/contentstack/sdk/Stack.java @@ -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; @@ -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/"); } } @@ -125,6 +137,7 @@ private void includeLivePreview() { * @throws IOException IO Exception */ public Stack livePreviewQuery(Map 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); @@ -137,7 +150,17 @@ public Stack livePreviewQuery(Map query) throws IOException { try { LinkedHashMap 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"); @@ -150,6 +173,9 @@ public Stack livePreviewQuery(Map query) throws IOException { config.setLivePreviewEntry(liveResponse.getJSONObject("entry")); } } + } else { + throw new IllegalStateException("Live Preview is not enabled in Config"); + } return this; } diff --git a/src/test/java/com/contentstack/sdk/TestAsset.java b/src/test/java/com/contentstack/sdk/TestAsset.java index 259efe83..d344d9f7 100644 --- a/src/test/java/com/contentstack/sdk/TestAsset.java +++ b/src/test/java/com/contentstack/sdk/TestAsset.java @@ -38,10 +38,10 @@ public void onCompletion(ResponseType responseType, List 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()); @@ -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()); diff --git a/src/test/java/com/contentstack/sdk/TestAssetLibrary.java b/src/test/java/com/contentstack/sdk/TestAssetLibrary.java index 48607e83..1238e981 100644 --- a/src/test/java/com/contentstack/sdk/TestAssetLibrary.java +++ b/src/test/java/com/contentstack/sdk/TestAssetLibrary.java @@ -24,10 +24,10 @@ void testNewAssetLibrary() { public void onCompletion(ResponseType responseType, List 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()); diff --git a/src/test/java/com/contentstack/sdk/TestEntry.java b/src/test/java/com/contentstack/sdk/TestEntry.java index 7df8c94c..657be712 100644 --- a/src/test/java/com/contentstack/sdk/TestEntry.java +++ b/src/test/java/com/contentstack/sdk/TestEntry.java @@ -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..."); } diff --git a/src/test/java/com/contentstack/sdk/TestLivePreview.java b/src/test/java/com/contentstack/sdk/TestLivePreview.java index 596d8d59..b5bec654 100644 --- a/src/test/java/com/contentstack/sdk/TestLivePreview.java +++ b/src/test/java/com/contentstack/sdk/TestLivePreview.java @@ -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; @@ -26,6 +30,7 @@ public static void setUp() { config = new Config(); } + /** * Test config test. */ @@ -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 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 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 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"); + } } diff --git a/src/test/java/com/contentstack/sdk/TestStack.java b/src/test/java/com/contentstack/sdk/TestStack.java index ccf89b55..edde1475 100644 --- a/src/test/java/com/contentstack/sdk/TestStack.java +++ b/src/test/java/com/contentstack/sdk/TestStack.java @@ -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()); } }); }