Skip to content
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

Feat/dx 1239 live preview impl tests #131

Merged
merged 6 commits into from
Sep 4, 2024
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

## v1.15.0

### Date: 04-Sep-2024

- Live Preview 2.0 support added

## v1.14.2

### Date: 15-July-2024
Expand Down
2 changes: 1 addition & 1 deletion 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>1.14.2</version>
<version>1.15.0</version>
<packaging>jar</packaging>
<name>contentstack-java</name>
<description>Java SDK for Contentstack Content Delivery API</description>
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 @@ -66,7 +66,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
Loading