Skip to content

Commit 5e99b72

Browse files
Added tests
1 parent 94b249a commit 5e99b72

File tree

1 file changed

+172
-8
lines changed

1 file changed

+172
-8
lines changed

test/CodeSharingPlatformTest.java

Lines changed: 172 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import com.google.gson.JsonObject;
33
import org.hyperskill.hstest.dynamic.input.DynamicTesting;
44
import org.hyperskill.hstest.dynamic.input.DynamicTestingMethod;
5+
import org.hyperskill.hstest.exception.outcomes.PresentationError;
56
import org.hyperskill.hstest.exception.outcomes.WrongAnswer;
67
import org.hyperskill.hstest.mocks.web.response.HttpResponse;
78
import org.hyperskill.hstest.stage.SpringTest;
@@ -15,8 +16,10 @@
1516
import java.util.HashMap;
1617
import java.util.Map;
1718

19+
import static org.hyperskill.hstest.common.Utils.sleep;
1820
import static org.hyperskill.hstest.testing.expect.Expectation.expect;
1921
import static org.hyperskill.hstest.testing.expect.json.JsonChecker.isArray;
22+
import static org.hyperskill.hstest.testing.expect.json.JsonChecker.isInteger;
2023
import static org.hyperskill.hstest.testing.expect.json.JsonChecker.isObject;
2124
import static org.hyperskill.hstest.testing.expect.json.JsonChecker.isString;
2225

@@ -49,10 +52,28 @@ public CodeSharingPlatformTest() {
4952
"Snippet #12",
5053
"Snippet #13",
5154
"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",
5264
};
5365

5466
final Map<Integer, String> ids = new HashMap<>();
5567
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;
5677

5778
static String th(int val) {
5879
if (val == 1) {
@@ -117,6 +138,14 @@ static Element getById(Element doc, String url, String id, String tag) {
117138
return elem;
118139
}
119140

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+
120149
@DynamicTestingMethod
121150
public DynamicTesting[] dt = new DynamicTesting[] {
122151
// test 1
@@ -222,11 +251,71 @@ static Element getById(Element doc, String url, String id, String tag) {
222251
// test 85
223252
() -> checkApiLatest(13, 12, 11, 10, 9, 8, 7, 6, 5, 4),
224253
() -> 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),
225312
};
226313

227314
private CheckResult checkApiCode(int id) {
228315
String codeId = ids.get(id);
229316
String snippet = SNIPPETS[id];
317+
int time = secs.get(id);
318+
int views = this.views.get(id);
230319

231320
HttpResponse resp = get(API_CODE + codeId).send();
232321
checkStatusCode(resp, 200);
@@ -241,15 +330,40 @@ private CheckResult checkApiCode(int id) {
241330
dates.put(id, s);
242331
return true;
243332
}))
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+
244350
);
245351

246352
return CheckResult.correct();
247353
}
248354

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+
249361
private CheckResult checkWebCode(int id) {
250362
String codeId = ids.get(id);
251363
String apiSnippet = SNIPPETS[id];
252364
String apiDate = dates.get(id);
365+
int time = secs.get(id);
366+
int views = this.views.get(id);
253367

254368
String req = WEB_CODE + codeId;
255369
HttpResponse resp = get(req).send();
@@ -276,15 +390,56 @@ private CheckResult checkWebCode(int id) {
276390
"and api snippet date are different");
277391
}
278392

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");
283432
}
284433

285434
return CheckResult.correct();
286435
}
287436

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+
288443
private CheckResult checkWebCodeNew() {
289444
HttpResponse resp = get(WEB_CODE_NEW).send();
290445
checkStatusCode(resp, 200);
@@ -301,17 +456,24 @@ private CheckResult checkWebCodeNew() {
301456
}
302457

303458
private CheckResult postSnippet(int id) {
459+
return postSnippet(id, 0, 0);
460+
}
461+
462+
private CheckResult postSnippet(int id, int secs, int views) {
304463
String snippet = SNIPPETS[id];
464+
this.secs.put(id, secs);
465+
this.views.put(id, views);
305466

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();
307471
checkStatusCode(resp, 200);
308472

309473
expect(resp.getContent()).asJson().check(
310474
isObject()
311475
.value("id", isString(i -> {
312-
try {
313-
Integer.parseInt(i);
314-
} catch (NumberFormatException ex) {
476+
if (i.length() != 36) {
315477
return false;
316478
}
317479
ids.put(id, "" + i);
@@ -331,6 +493,8 @@ private CheckResult checkApiLatest(int... ids) {
331493
isArray(ids.length, isObject()
332494
.value("code", isString())
333495
.value("date", isString())
496+
.value("time", 0)
497+
.value("views", 0)
334498
)
335499
);
336500

0 commit comments

Comments
 (0)