Skip to content

Next #132

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 29 commits into from
Sep 24, 2024
Merged

Next #132

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
564ad8f
feat : Live preview 2.0 implementation
reeshika-h Sep 3, 2024
eaeb237
test: testcases for live preview
reeshika-h Sep 3, 2024
cbe7a17
chore: updated testcases assertions
reeshika-h Sep 3, 2024
2f8ed21
chore: version bump
reeshika-h Sep 3, 2024
22bdaad
chore: removed print statements
reeshika-h Sep 4, 2024
e6ba359
chore: removed print statements
reeshika-h Sep 4, 2024
caea007
Merge pull request #131 from contentstack/feat/DX-1239-Live-Preview-i…
reeshika-h Sep 4, 2024
e554300
feat : variants implementation
reeshika-h Jun 25, 2024
926a8af
Chore : indentation and dummy ids added
reeshika-h Jun 27, 2024
e3d8c1e
Chore : Indentation done
reeshika-h Jun 27, 2024
b215356
feat: added where method in AssetLibrary class
reeshika-h Jul 17, 2024
05d17c6
change: removed unused libraries
reeshika-h Jul 17, 2024
cdf225e
codeql-analysis.yml
aravindbuilt May 17, 2024
71204d0
chore: Version bump
reeshika-h Aug 20, 2024
5989d7c
feat : Live preview 2.0 implementation
reeshika-h Sep 3, 2024
6ad0725
test: testcases for live preview
reeshika-h Sep 3, 2024
b66df0d
chore: updated testcases assertions
reeshika-h Sep 3, 2024
ab30eb3
chore: version bump
reeshika-h Sep 3, 2024
ec99677
chore: removed print statements
reeshika-h Sep 4, 2024
da9072b
chore: removed print statements
reeshika-h Sep 4, 2024
7681f4d
feat : variants implementation
reeshika-h Jun 25, 2024
733be8f
Chore : indentation and dummy ids added
reeshika-h Jun 27, 2024
517ba43
Chore : Indentation done
reeshika-h Jun 27, 2024
7f7dea4
feat: added where method in AssetLibrary class
reeshika-h Jul 17, 2024
ee38778
change: removed unused libraries
reeshika-h Jul 17, 2024
12ea59a
codeql-analysis.yml
aravindbuilt May 17, 2024
3c98e72
Merge pull request #135 from contentstack/fix/conflicts
harshithad0703 Sep 13, 2024
b3d9d34
Merge branch 'next' into merge-next-into-master
reeshika-h Sep 20, 2024
a4040c4
Merge pull request #136 from contentstack/merge-next-into-master
reeshika-h Sep 23, 2024
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
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
Loading