2
2
import com .google .gson .JsonObject ;
3
3
import org .hyperskill .hstest .dynamic .input .DynamicTesting ;
4
4
import org .hyperskill .hstest .dynamic .input .DynamicTestingMethod ;
5
+ import org .hyperskill .hstest .exception .outcomes .PresentationError ;
5
6
import org .hyperskill .hstest .exception .outcomes .WrongAnswer ;
6
7
import org .hyperskill .hstest .mocks .web .response .HttpResponse ;
7
8
import org .hyperskill .hstest .stage .SpringTest ;
15
16
import java .util .HashMap ;
16
17
import java .util .Map ;
17
18
19
+ import static org .hyperskill .hstest .common .Utils .sleep ;
18
20
import static org .hyperskill .hstest .testing .expect .Expectation .expect ;
19
21
import static org .hyperskill .hstest .testing .expect .json .JsonChecker .isArray ;
22
+ import static org .hyperskill .hstest .testing .expect .json .JsonChecker .isInteger ;
20
23
import static org .hyperskill .hstest .testing .expect .json .JsonChecker .isObject ;
21
24
import static org .hyperskill .hstest .testing .expect .json .JsonChecker .isString ;
22
25
@@ -49,10 +52,28 @@ public CodeSharingPlatformTest() {
49
52
"Snippet #12" ,
50
53
"Snippet #13" ,
51
54
"Snippet #14" ,
55
+
56
+ "Snippet #15" ,
57
+ "Snippet #16" ,
58
+ "Snippet #17" ,
59
+ "Snippet #18" ,
60
+ "Snippet #19" ,
61
+ "Snippet #20" ,
62
+ "Snippet #21" ,
63
+ "Snippet #22" ,
52
64
};
53
65
54
66
final Map <Integer , String > ids = new HashMap <>();
55
67
final Map <Integer , String > dates = new HashMap <>();
68
+ final Map <Integer , Integer > secs = new HashMap <>();
69
+ final Map <Integer , Integer > views = new HashMap <>();
70
+
71
+ boolean checkSecret = false ;
72
+ long freezeTime = 0 ;
73
+ long awaitTime = 0 ;
74
+ long sleepDurationSec = 0 ;
75
+ long sleepLowerBound = 0 ;
76
+ long sleepUpperBound = 0 ;
56
77
57
78
static String th (int val ) {
58
79
if (val == 1 ) {
@@ -117,6 +138,14 @@ static Element getById(Element doc, String url, String id, String tag) {
117
138
return elem ;
118
139
}
119
140
141
+ static void checkMissingId (Element doc , String url , String id ) {
142
+ Element elem = doc .getElementById (id );
143
+ if (elem != null ) {
144
+ throw new WrongAnswer ("GET " + url +
145
+ " shouldn't contain an element with id \" " + id + "\" , but one was found" );
146
+ }
147
+ }
148
+
120
149
@ DynamicTestingMethod
121
150
public DynamicTesting [] dt = new DynamicTesting [] {
122
151
// test 1
@@ -222,11 +251,71 @@ static Element getById(Element doc, String url, String id, String tag) {
222
251
// test 85
223
252
() -> checkApiLatest (13 , 12 , 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 ),
224
253
() -> checkWebLatest (13 , 12 , 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 ),
254
+
255
+ // test 87
256
+ () -> postSnippet (14 ),
257
+ () -> postSnippet (15 , 100 , 20 ),
258
+ () -> postSnippet (16 ),
259
+ () -> postSnippet (17 , 0 , 5 ),
260
+ () -> postSnippet (18 ),
261
+ () -> postSnippet (19 , 3 , 0 ),
262
+ () -> postSnippet (20 ),
263
+ () -> postSnippet (21 , 30 , 0 ),
264
+
265
+ // test 95
266
+ () -> checkApiCode (14 ),
267
+ () -> checkWebCode (14 ),
268
+ () -> checkApiCode (16 ),
269
+ () -> checkWebCode (16 ),
270
+ () -> checkApiCode (18 ),
271
+ () -> checkWebCode (18 ),
272
+ () -> checkApiCode (20 ),
273
+ () -> checkWebCode (20 ),
274
+
275
+ // test 103
276
+ () -> {
277
+ freezeTime = System .currentTimeMillis ();
278
+ sleep (5000 );
279
+ return reloadServer ();
280
+ },
281
+
282
+ // test 104
283
+ () -> {
284
+ awaitTime = System .currentTimeMillis ();
285
+ sleepDurationSec = (awaitTime - freezeTime ) / 1000 ;
286
+ sleepLowerBound = sleepDurationSec ;
287
+ sleepUpperBound = sleepLowerBound + 10 ;
288
+ checkSecret = true ;
289
+ return CheckResult .correct ();
290
+ },
291
+
292
+ // test 105
293
+ () -> checkApiCode (15 ),
294
+ () -> checkWebCode (15 ),
295
+ () -> checkApiCode (17 ),
296
+ () -> checkWebCode (17 ),
297
+ () -> checkApiCode404 (19 ),
298
+ () -> checkWebCode404 (19 ),
299
+ () -> checkApiCode (21 ),
300
+ () -> checkWebCode (21 ),
301
+
302
+ // test 113
303
+ () -> checkApiLatest (20 , 18 , 16 , 14 , 13 , 12 , 11 , 10 , 9 , 8 ),
304
+ () -> checkWebLatest (20 , 18 , 16 , 14 , 13 , 12 , 11 , 10 , 9 , 8 ),
305
+
306
+ // test 115
307
+ () -> checkApiCode (17 ),
308
+ () -> checkApiCode (17 ),
309
+ () -> checkWebCode (17 ),
310
+ () -> checkApiCode404 (17 ),
311
+ () -> checkWebCode404 (17 ),
225
312
};
226
313
227
314
private CheckResult checkApiCode (int id ) {
228
315
String codeId = ids .get (id );
229
316
String snippet = SNIPPETS [id ];
317
+ int time = secs .get (id );
318
+ int views = this .views .get (id );
230
319
231
320
HttpResponse resp = get (API_CODE + codeId ).send ();
232
321
checkStatusCode (resp , 200 );
@@ -241,15 +330,40 @@ private CheckResult checkApiCode(int id) {
241
330
dates .put (id , s );
242
331
return true ;
243
332
}))
333
+ .value ("time" , isInteger (i -> {
334
+ if (!checkSecret || time == 0 ) {
335
+ return i == 0 ;
336
+ }
337
+ int upperBound = (int ) (time - sleepLowerBound );
338
+ int lowerBound = (int ) (time - sleepUpperBound );
339
+ return i >= lowerBound && i <= upperBound ;
340
+ }))
341
+ .value ("views" , isInteger (i -> {
342
+ if (!checkSecret || views == 0 ) {
343
+ return i == 0 ;
344
+ }
345
+ boolean result = i == views - 1 ;
346
+ this .views .put (id , views - 1 );
347
+ return result ;
348
+ }))
349
+
244
350
);
245
351
246
352
return CheckResult .correct ();
247
353
}
248
354
355
+ private CheckResult checkApiCode404 (int id ) {
356
+ HttpResponse resp = get (API_CODE + ids .get (id )).send ();
357
+ checkStatusCode (resp , 404 );
358
+ return CheckResult .correct ();
359
+ }
360
+
249
361
private CheckResult checkWebCode (int id ) {
250
362
String codeId = ids .get (id );
251
363
String apiSnippet = SNIPPETS [id ];
252
364
String apiDate = dates .get (id );
365
+ int time = secs .get (id );
366
+ int views = this .views .get (id );
253
367
254
368
String req = WEB_CODE + codeId ;
255
369
HttpResponse resp = get (req ).send ();
@@ -276,15 +390,56 @@ private CheckResult checkWebCode(int id) {
276
390
"and api snippet date are different" );
277
391
}
278
392
279
- if (!html .contains ("hljs.initHighlightingOnLoad()" )) {
280
- return CheckResult .wrong (
281
- "Can't determine if code highlighting works or not.\n " +
282
- "Use \" hljs.initHighlightingOnLoad()\" inside the script tags in the HTML page." );
393
+ if (time != 0 ) {
394
+ Element timeSpan = getById (doc , req , "time_restriction" , "span" );
395
+ String timeText = timeSpan .text ();
396
+ int timeOnPage ;
397
+ try {
398
+ timeOnPage = expect (timeText ).toContain (1 ).integers ().get (0 );
399
+ } catch (PresentationError ex ) {
400
+ return CheckResult .wrong (
401
+ "GET " + req + " cannot find number of seconds inside \" time_restriction\" span element.\n " +
402
+ "Full text:\n " + timeSpan
403
+ );
404
+ }
405
+ int upperBound = (int ) (time - sleepLowerBound );
406
+ int lowerBound = (int ) (time - sleepUpperBound );
407
+
408
+ if (!(timeOnPage >= lowerBound && timeOnPage <= upperBound )) {
409
+ return CheckResult .wrong ("GET " + req + " should " +
410
+ "contain time restriction between " + lowerBound
411
+ + " and " + upperBound + ", found: " + timeOnPage + "\n " +
412
+ "Full text:\n " + timeSpan );
413
+ }
414
+ } else {
415
+ checkMissingId (doc , req , "time_restriction" );
416
+ }
417
+
418
+ if (views != 0 ) {
419
+ Element viewsSpan = getById (doc , req , "views_restriction" , "span" );
420
+ String viewsText = viewsSpan .text ();
421
+ int viewsOnPage = expect (viewsText ).toContain (1 ).integers ().get (0 );
422
+
423
+ if (viewsOnPage != views - 1 ) {
424
+ return CheckResult .wrong ("GET " + req + " should " +
425
+ "contain views restriction equal to " + (views - 1 )
426
+ + ", found: " + viewsOnPage + "\n " +
427
+ "Full text:\n " + viewsSpan );
428
+ }
429
+ this .views .put (id , views - 1 );
430
+ } else {
431
+ checkMissingId (doc , req , "views_restriction" );
283
432
}
284
433
285
434
return CheckResult .correct ();
286
435
}
287
436
437
+ private CheckResult checkWebCode404 (int id ) {
438
+ HttpResponse resp = get (WEB_CODE + ids .get (id )).send ();
439
+ checkStatusCode (resp , 404 );
440
+ return CheckResult .correct ();
441
+ }
442
+
288
443
private CheckResult checkWebCodeNew () {
289
444
HttpResponse resp = get (WEB_CODE_NEW ).send ();
290
445
checkStatusCode (resp , 200 );
@@ -301,17 +456,24 @@ private CheckResult checkWebCodeNew() {
301
456
}
302
457
303
458
private CheckResult postSnippet (int id ) {
459
+ return postSnippet (id , 0 , 0 );
460
+ }
461
+
462
+ private CheckResult postSnippet (int id , int secs , int views ) {
304
463
String snippet = SNIPPETS [id ];
464
+ this .secs .put (id , secs );
465
+ this .views .put (id , views );
305
466
306
- HttpResponse resp = post (API_CODE_NEW , "{\" code\" :\" " + snippet + "\" }" ).send ();
467
+ HttpResponse resp = post (API_CODE_NEW ,
468
+ "{\" code\" :\" " + snippet + "\" , " +
469
+ "\" time\" : " + secs +", " +
470
+ "\" views\" : " + views + "}" ).send ();
307
471
checkStatusCode (resp , 200 );
308
472
309
473
expect (resp .getContent ()).asJson ().check (
310
474
isObject ()
311
475
.value ("id" , isString (i -> {
312
- try {
313
- Integer .parseInt (i );
314
- } catch (NumberFormatException ex ) {
476
+ if (i .length () != 36 ) {
315
477
return false ;
316
478
}
317
479
ids .put (id , "" + i );
@@ -331,6 +493,8 @@ private CheckResult checkApiLatest(int... ids) {
331
493
isArray (ids .length , isObject ()
332
494
.value ("code" , isString ())
333
495
.value ("date" , isString ())
496
+ .value ("time" , 0 )
497
+ .value ("views" , 0 )
334
498
)
335
499
);
336
500
0 commit comments