Homework 3
You will be adding new API endpoints (to deliver new functionalities) in the MyBooksApp as well as writing some unit tests to make sure the endpoints work as expected.
| Day | Date | |
|---|---|---|
| Release | Thu | SEP 24 |
| Due | Fri | OCT 2 @ 11pm (EST) |
Task
Grab a copy of homework 3 starter code, under hw3_starter folder, from the MyBookApp repo on jhu-oose github organization. The current implemetation has a file named Server.java with a main function in it. You should be able to run Server.main to start SparkJava server. Currently, there are three functioning routes namely /, /authors and /addauthor. Feel free to test them and see how they work to get warmed up! Your task is to:
Similar to the existing
/authorsend point, add a new http get endpoint on a new route/bookswhere the user can see a list of all the books. You need to useSql2oBookDao.listAll()to fetch and list all the books from theBookstable. Return the list of books as a JSON array similar to what we did onauthorsroute.Similar to the existing
/addauthorroute, define a new http post endpoint on a new route/addbookwhere the user can request to add a new book to theBookstable.info
Do not worry about the case where the supplied
authorIdparameter in the post request does not exist in theAuthorstable. The database will reject the insertion and that is fine! Though, ifauthorIdvalue exists in theAuthorstable, the book insertion should take place successfully.Add a new route named
/delauthor. This should receive the post request to remove an author from theauthorstable. The post request may only contain one peice of data: author's name. You can use yourSql2oAuthorDao.deletefrom previous homework to delete an author.info
Note that an author's
namemust be unique, so author deletion will be performed based onname.Add a new route named
/delbook. This should receive the post request to remove a book from theBookstable. The post request may only contain one peice of data: book's isbn. You can use yourSql2oBookDao.deletefrom previous homework to delete a book.info
Note that a book's
isbnmust be unique, so book deletion will be performed based onisbn.info
Add all these new routes in
Server.mainfunction.Utilize Junit and OkHttp to write new unit test cases in
RESTAPITest.javaclass undersrc/test/javato make sure the newly added endpoints function properly. At the very least, you should write one test case for each of/books,/addbook,/delauthor, and/delbookendpoints.
caution
For /delauthor and /delbook, you may not only rely on the returned http status code. Go a step further and make sure the query was executed successfully and had the expected effect. For instance, make sure the author was really deleted (if deleting an author)!
tip
Feel free to write more unit tests and/or assertions in your existing test cases. For example, it would be very sensible to write multiple tests for deletion of authors or books, and then also make sure that ON CASCADE DELETE really works; that is deleting an author from authors table will remove all the books written by that author from the books table.
tip
Ues method(s) with @BeforeClass/@Before annotations to set things up the way you want before tests are executed.
Submission
In your group repo, create a new folder named homeworks. Under homeworks, create a subfolder named hw3. Push all your files under homeworks/hw3 folder. Create a README.md in homeworks/hw3 folder and include in it all the assumptions made, work done, etc. When finished, zip the hw3 folder and submit it as a single file to Gradescope.
info
Note that you need to work collaboratively to finish the homework, but you will make one submission as a group.