-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainTest.java
486 lines (467 loc) · 20.6 KB
/
MainTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package com.teamtreehouse.blog;
import com.teamtreehouse.blog.dao.Sql2oBlogDao;
import com.teamtreehouse.blog.dao.Sql2oEntryDao;
import com.teamtreehouse.blog.model.BlogEntry;
import com.teamtreehouse.blog.model.Comment;
import com.teamtreehouse.blog.testing.ApiClient;
import com.teamtreehouse.blog.testing.ApiResponse;
import org.junit.*;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.sql2o.Connection;
import org.sql2o.Sql2o;
import spark.ModelAndView;
import spark.Spark;
import spark.template.handlebars.HandlebarsTemplateEngine;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.*;
public class MainTest {
// test port
private static final String PORT = "4568";
// test data source
private static final String TEST_DATASOURCE = "jdbc:h2:mem:testing";
// database connection
private Connection mConnection;
// our main testing class, generating requests, and returning responses
private ApiClient mApiClient;
// used to check cookie with password
private static final String mCookieWithPassword = "password=admin";
// right password
private final String mRightPassword = "admin";
// used for checking error page
private static final String NOT_FOUND_MESSAGE = "No such entry found";
// model of error page to be put to handlebars template engine
private HashMap<String, Object> mErrorPageModel;
// DAOs
private Sql2oBlogDao mSql2oBlogDao;
private Sql2oEntryDao mSql2oEntryDao;
// just to print test names before tests
@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
System.out.printf("%n -------- Starting test: %s %n",description.getMethodName());
}
};
@BeforeClass
public static void startServer() {
// setting up our Api with port and test data source
String[] args = {PORT, TEST_DATASOURCE};
Main.main(args);
// wait until server is up and running
Spark.awaitInitialization();
}
@AfterClass
public static void stopServer() {
Spark.stop();
}
@Before
public void setUp() throws Exception {
// get our DAOs connected with database
String connectionString = TEST_DATASOURCE +
";INIT=RUNSCRIPT from 'classpath:db/init.sql'";
Sql2o sql2o = new Sql2o(connectionString, "", "");
mConnection = sql2o.open();
mSql2oBlogDao = Main.getSql2oBlogDao();
mSql2oEntryDao = Main.getSql2oEntryDao();
// fill our test database with three test entries
// set up our ApiClient
mApiClient = new ApiClient("http://localhost:" + PORT);
// generate error 404 page model
mErrorPageModel = new HashMap<>();
mErrorPageModel.put("status", 404);
mErrorPageModel.put("errorMessage", NOT_FOUND_MESSAGE);
// make first request to index page to fill our database
mApiClient.request("GET", "/");
}
@After
public void tearDown() throws Exception {
mConnection.close();
}
// methods to get html page as a string giving a .hbs file name and model
// to put, null or filled
// @return String - rendered html with Handlebars template engine
private String getHtmlOfPageWithHbsWithNullModel(String hbsFileName) {
HandlebarsTemplateEngine handlebarsTemplateEngine =
new HandlebarsTemplateEngine();
return handlebarsTemplateEngine
.render(new ModelAndView(null, hbsFileName));
}
private String getHtmlOfPageWithHbsWithModel(String hbsFileName,
Map<?,?> model) {
HandlebarsTemplateEngine handlebarsTemplateEngine =
new HandlebarsTemplateEngine();
return handlebarsTemplateEngine
.render(new ModelAndView(model, hbsFileName));
}
// methods to get response body of GET request to given URI with or without
// password cookie
// @return String - body of response
private String
getResponseBodyOfGetRequestWithoutPasswordCookie(String pageUri) {
return mApiClient
.request("GET", pageUri)
.getBody();
}
private String
getResponseBodyOfGetRequestWithRightPasswordCookie(String pageUri) {
return mApiClient
.request("GET", pageUri, null, mCookieWithPassword)
.getBody();
}
// methods to get response body of POST request to given URI with or without
// password cookie
// @return String - body of response
private String
getResponseBodyOfPostRequestWithoutPasswordCookie(String pageUri,
String requestBody) {
return mApiClient
.request("POST", pageUri, requestBody)
.getBody();
}
private String
getResponseBodyOfPostRequestWithRightPasswordCookie(
String pageUri, String requestBody) {
return mApiClient
.request("POST", pageUri, requestBody, mCookieWithPassword)
.getBody();
}
// actual tests
@Test
public void unauthorisedGetRequestOnNewEntryPageRedirectsToPasswordPage()
throws Exception {
// Given no cookies with password
// When get to new entries page
// password html page should come as a response
assertEquals(
getHtmlOfPageWithHbsWithNullModel("password.hbs"),
getResponseBodyOfGetRequestWithoutPasswordCookie("/entries/new"));
}
@Test
public void unauthorisedGetRequestOnEditEntryPageRedirectsToPasswordPage()
throws Exception {
// Given no cookies with password
// When get request to edit entries page is made
// Then password html page should come as a response, even if there are
// no such entry
assertEquals(
getHtmlOfPageWithHbsWithNullModel("password.hbs"),
getResponseBodyOfGetRequestWithoutPasswordCookie("/entries/edit/somePage"));
}
@Test
public void unauthorisedPostRequestOnSaveEntryPageRedirectsToPasswordPage()
throws Exception {
// Given no cookies with password
// When get request to edit entries page is made
// Then password html page should come as a response, even if there are
// no such entry
assertEquals(
getHtmlOfPageWithHbsWithNullModel("password.hbs"),
getResponseBodyOfPostRequestWithoutPasswordCookie(
"/entries/save/somePage","title=title&body=body"));
}
@Test
public void unauthorisedGetRequestOnRemoveEntryPageRedirectsToPasswordPage()
throws Exception {
// Given no cookies with password
// When get request to edit entries page is made
// Then password html page should come as a response, even if there are
// no such entry
assertEquals(
getHtmlOfPageWithHbsWithNullModel("password.hbs"),
getResponseBodyOfGetRequestWithoutPasswordCookie(
"/entries/remove/somePage")
);
}
@Test
public void authorizedGetRequestOnNewEntryPageShowsNewPage() throws Exception {
// Given cookie with password
// When get request to new entry page is made
// Then new entry page is returned
assertEquals(
getHtmlOfPageWithHbsWithNullModel("new.hbs"),
getResponseBodyOfGetRequestWithRightPasswordCookie("/entries/new"));
}
@Test
public void authorizedRequestOnNonExistingDetailEntryPageShowsNotFoundPage()
throws Exception {
// Given cookie with password
// When get request to non-existing edit entry page is made
// Then not-found error page is returned
assertEquals(
getHtmlOfPageWithHbsWithModel("not-found.hbs", mErrorPageModel),
getResponseBodyOfGetRequestWithRightPasswordCookie(
"/entries/detail/1234543/title"));
}
@Test
public void authorizedRequestOnEditEntryPageWithNoEntriesShowsNotFoundPage()
throws Exception {
// Given cookie with password
// model of error page:
// When get request to non-existing edit entry page is made
// Then not-found error page is returned
assertEquals(
getHtmlOfPageWithHbsWithModel("not-found.hbs", mErrorPageModel),
getResponseBodyOfGetRequestWithRightPasswordCookie(
"/entries/edit/1234543/title"));
}
//
// // Not quite right test, but I will still leave it here
// @Test
// public void addingNewEntryWithPostRequestReturnsRightResponseHomePageWithNewEntry()
// throws Exception {
// // Given initial DAO with 3 test entries, model of html created by
// // handlebars template engine, with all entries from POST request
//
// // When POST request to new page is made, and new DAO is used to
// // create index page
// ApiResponse apiResponse =
// mApiClient.request("POST",
// "/entries/new",
// "title=title&body=body&tags=tag1",
// mCookieWithPassword);
// Map<String, Object> model = new HashMap<>();
// model.put("entries", Main.mSimpleBlogDao.findAllEntries());
// // then body of response should be same as of index page with simple
// // blog entry dao given
// assertEquals(
// getHtmlOfPageWithHbsWithModel("index.hbs", model),
// apiResponse.getBody()
// );
// }
//
@Test
public void givingWrongPasswordRedirectsBackToHomePageWhenSessionIsNew()
throws Exception {
// Given no cookies with password, and new empty session
// When user tries type wrong password, ( same will happen when
// password is right )
// Then home page is returned back
Map<String, Object> model = new HashMap<>();
model.put("entries", mSql2oBlogDao.findAllEntries());
assertEquals(
getHtmlOfPageWithHbsWithModel("index.hbs", model),
getResponseBodyOfPostRequestWithoutPasswordCookie(
"/password","password=password"));
}
//
// // test does not work because session is not saved in filter method,
// // however, line with redirect to protected page in post("/password/page")
// // is there
// @Test
// public void givingRightPasswordRedirectsBackToPageWhereHeAskedFor()
// throws Exception {
// // Given no cookies with password, and session with new page
// // set after get request sent to page with new entries
// mApiClient.request("GET", "/entries/new");
// ApiResponse apiResponse = mApiClient.request("POST",
// "/password",
// "password=" + mRightPassword,
// "password=" + mRightPassword + "; JSESSIONID=" + Main.getSessionId());
// // When user tries type right password, making POST request at
// // password page
// // Then home page is returned back
// assertEquals(
// getHtmlOfPageWithHbsWithNullModel("password.hbs"),
// apiResponse.getBody()
// );
// }
//
//
@Test
public void newEntryIncreasesSizeOfDatabase() throws Exception {
// Given cookie with password,
// When user posts new entry
getResponseBodyOfPostRequestWithRightPasswordCookie(
"/entries/new","title=title&body=body");
// Then size of DAO is increased
assertEquals(4, mSql2oBlogDao.findAllEntries().size());
}
@Test
public void detailPageReturnedRightAsWeExpected() throws Exception {
// Given no cookies with password, no sessions, and first blog entry
// with its comments
BlogEntry firstBlogEntry =
mSql2oBlogDao.findEntryById(1);
HashMap<String, Object> model = new HashMap<>();
model.put("entry", firstBlogEntry);
model.put("comments", mSql2oEntryDao.findByEntryId(1));
// When we make GET request to detail page of first test Entry
String requestBodyOfGetRequestToDetailPage =
getResponseBodyOfGetRequestWithRightPasswordCookie(
"/entries/detail/"
+ firstBlogEntry.getId() + "/"
+ firstBlogEntry.getSlugFromTitle()
);
// Then body of detail page modeled offline using handlebars
// should be equal to response body of actual detail page
assertEquals(
getHtmlOfPageWithHbsWithModel("detail.hbs", model),
requestBodyOfGetRequestToDetailPage
);
}
@Test
public void postToEntriesDetailsPageThatDoesNotExistsReturnsNotFoundPage()
throws Exception {
// Given no cookies with password, no sessions, database with three
// test entries
// When we make POST request to page with address that doesn't exist
ApiResponse apiResponse =
mApiClient.request("POST",
"/entries/detail/12442341/title",
"title=title&body=body");
// Then body of response of this request should be equal to 404
// modeled offline with handlebars page
assertEquals(
getHtmlOfPageWithHbsWithModel("not-found.hbs", mErrorPageModel),
apiResponse.getBody()
);
}
@Test
public void postToEntriesDetailsExistingPageTryingToCreateAnonymousCommentReturnsDetailPageWithNewComment()
throws Exception {
// Given no cookies with password, no sessions, dao with three
// test entries
// When we make POST request to detail page, to create comment
// with empty author name and non-empty body
BlogEntry firstBlogEntry =
mSql2oBlogDao.findEntryById(1);
ApiResponse apiResponse =
mApiClient.request("POST",
"/entries/detail/"
+ firstBlogEntry.getId() +
"/" + firstBlogEntry.getSlugFromTitle(),
"name=&body=body");
// Then author name of last added comment should be "Anonymous"
// NOTE: here i know that in test database each test entry has one
// comment, that's why I use get(1). But it is not cheating,
// because it is test database
Comment lastComment = mSql2oEntryDao
.findByEntryId(firstBlogEntry.getId()).get(1);
assertEquals("Anonymous", lastComment.getAuthor());
}
@Test
public void postToEntriesDetailsExistingPageTryingToCreateCommentReturnsDetailPageWithNewComment()
throws Exception {
// Given no cookies with password, no sessions, dao with three
// test entries
// When we make POST request to page with address that doesn't exist
BlogEntry firstBlogEntry =
mSql2oBlogDao.findEntryById(1);
ApiResponse apiResponseOfPostCommentToEntryDetailPage =
mApiClient.request("POST",
"/entries/detail/"
+ firstBlogEntry.getId() +
"/" + firstBlogEntry.getSlugFromTitle(),
"name=name&body=body");
HashMap<String, Object> model = new HashMap<>();
model.put("entry", firstBlogEntry);
model.put("comments", mSql2oEntryDao.findByEntryId(1));
// Then body of response of this request should be equal to
// modeled offline with handlebars page of detail entry with
// new comment
assertEquals(
getHtmlOfPageWithHbsWithModel("detail.hbs", model),
apiResponseOfPostCommentToEntryDetailPage.getBody()
);
}
//
@Test
public void editEntryPageIsReturnedCorrectly() throws Exception {
// Given cookie with password, no sessions, dao with three
// test entries, and offline page generated by us using handlebars
// and first blog entry
BlogEntry firstBlogEntry =
mSql2oBlogDao.findEntryById(1);
HashMap<String, Object> model = new HashMap<>();
model.put("entry", firstBlogEntry);
String htmlStringOfEditEntryPageRenderedOfflineWithModel =
getHtmlOfPageWithHbsWithModel("edit.hbs", model);
// When GET request with right password is made to edit entry page
String bodyOfGetRequestWithRightPassword =
getResponseBodyOfGetRequestWithRightPasswordCookie(
"/entries/edit/"
+ firstBlogEntry.getId() + "/"
+ firstBlogEntry.getSlugFromTitle());
// Then response body of request should be equal to our
// offline generated model
assertEquals(
htmlStringOfEditEntryPageRenderedOfflineWithModel,
bodyOfGetRequestWithRightPassword
);
}
@Test
public void postRequestToSaveEntryReturnsCorrectHomePage() throws Exception {
// Given cookie with password, dao with three test entries, no
// sessions, and first entry edit page
BlogEntry firstBlogEntry =
mSql2oBlogDao.findEntryById(1);
// When user edits entry, changing title and body
String bodyOfResponseToPostRequestMadeToEditPage =
getResponseBodyOfPostRequestWithRightPasswordCookie(
"/entries/save/" + firstBlogEntry.getId() + "/"
+ firstBlogEntry.getSlugFromTitle(),
"title=title&body=body"
);
Map<String, Object> model = new HashMap<>();
model.put("entries", mSql2oBlogDao.findAllEntries());
String htmlStringOfModeledIndexPageWithNewEntry =
getHtmlOfPageWithHbsWithModel("index.hbs", model);
// Then body of response should be equal to new home page with changed
// home page
assertEquals(bodyOfResponseToPostRequestMadeToEditPage,
htmlStringOfModeledIndexPageWithNewEntry);
}
@Test
public void savingEntryWithNewTitleOrBodySavesComments() throws Exception {
// Given cookie with password, dao with three test entries, no
// sessions, and first entry edit page
int numberOfCommentsBeforeEdit = mSql2oEntryDao.findAll().size();
BlogEntry firstBlogEntry =
mSql2oBlogDao.findEntryById(1);
// When user edits entry, changing title and body
getResponseBodyOfPostRequestWithRightPasswordCookie(
"/entries/save/" + firstBlogEntry.getId() + "/"
+ firstBlogEntry.getSlugFromTitle(),
"title=title&body=body"
);
// old comments should be save for new entry
assertEquals(
numberOfCommentsBeforeEdit, mSql2oEntryDao.findAll().size());
}
@Test
public void removingFirstEntryReturnsChangesSizeOfDaoToTwo()
throws Exception {
// Given cookie with password, no session, dao with three test entries,
// and detail page of first entry
BlogEntry firstBlogEntry =
mSql2oBlogDao.findEntryById(1);
// When remove button is pressed, and get request to /entries/remove/...
// is made
getResponseBodyOfGetRequestWithRightPasswordCookie(
"/entries/remove/" + firstBlogEntry.getId() + "/"
+ firstBlogEntry.getSlugFromTitle()
);
// Then size of blog dao should be equal to two. That right page will
// be generated we know from other tests
assertEquals(2, mSql2oBlogDao.findAllEntries().size());
}
@Test
public void makingGetRequestToNonExistingEntryReturnsErrorPageAndNotServerError()
throws Exception {
// Given cookie with password, no session, dao with three test entries,
// When remove button is pressed, and get request to /entries/remove/...
// is made
String responseBodyOfGetRequestMadeWhenRemoveIsPressed =
getResponseBodyOfGetRequestWithRightPasswordCookie(
"/entries/remove/123456/title");
String htmlStringOfErrorPageGeneratedByUs =
getHtmlOfPageWithHbsWithModel("not-found.hbs", mErrorPageModel);
// Then user should be returned to home page, and body of the page
// generated from changed DAO by us is equal to get response body
assertEquals(
htmlStringOfErrorPageGeneratedByUs,
responseBodyOfGetRequestMadeWhenRemoveIsPressed);
}
}