Skip to content

Commit eaeb237

Browse files
committed
test: testcases for live preview
1 parent 564ad8f commit eaeb237

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/test/java/com/contentstack/sdk/TestLivePreview.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import org.junit.jupiter.api.Disabled;
66
import org.junit.jupiter.api.Test;
77

8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
10+
11+
import java.io.IOException;
812
import java.util.HashMap;
913
import java.util.logging.Level;
1014
import java.util.logging.Logger;
@@ -26,6 +30,7 @@ public static void setUp() {
2630
config = new Config();
2731
}
2832

33+
2934
/**
3035
* Test config test.
3136
*/
@@ -110,8 +115,10 @@ void testExceptionWhenAllRequiredParamsNotProvided() {
110115
.setLivePreviewHost("live-preview.contentstack.io");
111116
try {
112117
Contentstack.stack("liveAPIKey", "liveAccessToken", "liveEnv", livePreviewEnablerConfig);
118+
System.out.println("no it works");
113119
} catch (Exception e) {
114120
Assertions.assertEquals("managementToken is required", e.getLocalizedMessage());
121+
System.out.println(e.getLocalizedMessage());
115122
logger.severe(e.getLocalizedMessage());
116123
}
117124
}
@@ -158,5 +165,64 @@ void testCompleteLivePreviewInQuery() throws Exception {
158165
Assertions.assertNotNull(entry);
159166
}
160167

168+
@Test
169+
void testCompleteLivePreviewWithPreviewToken() throws IOException, IllegalAccessException {
170+
Config livePreviewConfig = new Config()
171+
.enableLivePreview(true)
172+
.setLivePreviewHost("rest-preview.contentstack.com")
173+
.setPreviewToken("preview_token");
174+
175+
Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", livePreviewConfig);
176+
177+
HashMap<String, String> hashMap = new HashMap<>();
178+
hashMap.put("live_preview", "hash167673");
179+
hashMap.put("content_type_uid", "page");
180+
181+
stack.livePreviewQuery(hashMap);
182+
Entry entry = stack.contentType("page").entry("entry_uid");
183+
entry.fetch(null);
184+
Assertions.assertNotNull(entry);
185+
186+
}
187+
188+
@Test()
189+
void testLivePreviewWithoutPreviewToken() throws Exception {
190+
Config livePreviewEnablerConfig = new Config().enableLivePreview(true).setLivePreviewHost("rest-preview.contentstack.com")
191+
.setManagementToken("fake@token");
192+
Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", livePreviewEnablerConfig);
193+
HashMap<String, String> hashMap = new HashMap<>();
194+
hashMap.put("live_preview", "hash167673");
195+
hashMap.put("content_type_uid", "page");
196+
197+
IllegalAccessError thrown = Assertions.assertThrows(IllegalAccessError.class, () -> {
198+
stack.livePreviewQuery(hashMap);
199+
}, "Expected livePreviewQuery to throw IllegalAccessError");
200+
201+
Assertions.assertTrue(thrown.getMessage().contains("Provide the Preview Token for the host rest-preview.contentstack.com"),
202+
"Exception message should mention that Preview Token is required");
203+
204+
logger.severe(thrown.getMessage());
205+
}
206+
207+
@Test
208+
void testLivePreviewDisabled() throws IllegalAccessException, IOException {
209+
Config config = new Config()
210+
.enableLivePreview(false)
211+
.setPreviewToken("preview_token");
212+
213+
Stack stack = Contentstack.stack("stackApiKey", "deliveryToken", "env1", config);
214+
215+
HashMap<String, String> hashMap = new HashMap<>();
216+
hashMap.put("live_preview", "hash167673");
217+
hashMap.put("content_type_uid", "page");
218+
219+
Exception exception = assertThrows(IllegalStateException.class, () -> {
220+
stack.livePreviewQuery(hashMap);
221+
});
222+
223+
// Optionally, you can check the message of the exception
224+
assertEquals("Live Preview is not enabled in Config", exception.getMessage(),
225+
"Expected exception message does not match");
226+
}
161227

162228
}

0 commit comments

Comments
 (0)