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.

ReleaseThuSEP 24
DueFriOCT 2 @ 11pm (EST)


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:

  1. Similar to the existing /authors end point, add a new http get endpoint on a new route /books where the user can see a list of all the books. You need to use Sql2oBookDao.listAll() to fetch and list all the books from the Books table. Return the list of books as a JSON array similar to what we did on authors route.

  2. Similar to the existing /addauthor route, define a new http post endpoint on a new route /addbook where the user can request to add a new book to the Books table.


    Do not worry about the case where the supplied authorId parameter in the post request does not exist in the Authors table. The database will reject the insertion and that is fine! Though, if authorId value exists in the Authors table, the book insertion should take place successfully.

  3. Add a new route named /delauthor. This should receive the post request to remove an author from the authors table. The post request may only contain one peice of data: author's name. You can use your Sql2oAuthorDao.delete from previous homework to delete an author.


    Note that an author's name must be unique, so author deletion will be performed based on name.

  4. Add a new route named /delbook. This should receive the post request to remove a book from the Books table. The post request may only contain one peice of data: book's isbn. You can use your Sql2oBookDao.delete from previous homework to delete a book.


    Note that a book's isbn must be unique, so book deletion will be performed based on isbn.


    Add all these new routes in Server.main function.

  5. Utilize Junit and OkHttp to write new unit test cases in RESTAPITest.java class under src/test/java to 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 /delbook endpoints.


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)!


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.


Ues method(s) with @BeforeClass/@Before annotations to set things up the way you want before tests are executed.


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.


Note that you need to work collaboratively to finish the homework, but you will make one submission as a group.