5
5
import org .junit .jupiter .api .Disabled ;
6
6
import org .junit .jupiter .api .Test ;
7
7
8
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
9
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
10
+
11
+ import java .io .IOException ;
8
12
import java .util .HashMap ;
9
13
import java .util .logging .Level ;
10
14
import java .util .logging .Logger ;
@@ -26,6 +30,7 @@ public static void setUp() {
26
30
config = new Config ();
27
31
}
28
32
33
+
29
34
/**
30
35
* Test config test.
31
36
*/
@@ -158,5 +163,64 @@ void testCompleteLivePreviewInQuery() throws Exception {
158
163
Assertions .assertNotNull (entry );
159
164
}
160
165
166
+ @ Test
167
+ void testCompleteLivePreviewWithPreviewToken () throws IOException , IllegalAccessException {
168
+ Config livePreviewConfig = new Config ()
169
+ .enableLivePreview (true )
170
+ .setLivePreviewHost ("rest-preview.contentstack.com" )
171
+ .setPreviewToken ("preview_token" );
172
+
173
+ Stack stack = Contentstack .stack ("stackApiKey" , "deliveryToken" , "env1" , livePreviewConfig );
174
+
175
+ HashMap <String , String > hashMap = new HashMap <>();
176
+ hashMap .put ("live_preview" , "hash167673" );
177
+ hashMap .put ("content_type_uid" , "page" );
178
+
179
+ stack .livePreviewQuery (hashMap );
180
+ Entry entry = stack .contentType ("page" ).entry ("entry_uid" );
181
+ entry .fetch (null );
182
+ Assertions .assertNotNull (entry );
183
+
184
+ }
185
+
186
+ @ Test ()
187
+ void testLivePreviewWithoutPreviewToken () throws Exception {
188
+ Config livePreviewEnablerConfig = new Config ().enableLivePreview (true ).setLivePreviewHost ("rest-preview.contentstack.com" )
189
+ .setManagementToken ("fake@token" );
190
+ Stack stack = Contentstack .stack ("stackApiKey" , "deliveryToken" , "env1" , livePreviewEnablerConfig );
191
+ HashMap <String , String > hashMap = new HashMap <>();
192
+ hashMap .put ("live_preview" , "hash167673" );
193
+ hashMap .put ("content_type_uid" , "page" );
194
+
195
+ IllegalAccessError thrown = Assertions .assertThrows (IllegalAccessError .class , () -> {
196
+ stack .livePreviewQuery (hashMap );
197
+ }, "Expected livePreviewQuery to throw IllegalAccessError" );
198
+
199
+ Assertions .assertTrue (thrown .getMessage ().contains ("Provide the Preview Token for the host rest-preview.contentstack.com" ),
200
+ "Exception message should mention that Preview Token is required" );
201
+
202
+ logger .severe (thrown .getMessage ());
203
+ }
204
+
205
+ @ Test
206
+ void testLivePreviewDisabled () throws IllegalAccessException , IOException {
207
+ Config config = new Config ()
208
+ .enableLivePreview (false )
209
+ .setPreviewToken ("preview_token" );
210
+
211
+ Stack stack = Contentstack .stack ("stackApiKey" , "deliveryToken" , "env1" , config );
212
+
213
+ HashMap <String , String > hashMap = new HashMap <>();
214
+ hashMap .put ("live_preview" , "hash167673" );
215
+ hashMap .put ("content_type_uid" , "page" );
216
+
217
+ Exception exception = assertThrows (IllegalStateException .class , () -> {
218
+ stack .livePreviewQuery (hashMap );
219
+ });
220
+
221
+ // Optionally, you can check the message of the exception
222
+ assertEquals ("Live Preview is not enabled in Config" , exception .getMessage (),
223
+ "Expected exception message does not match" );
224
+ }
161
225
162
226
}
0 commit comments