@Test public void testGetNotesWithQuery() { // test the first three (known) notes with a query final Response responseMsg = target .path("notes") .queryParam("query", TEST_BODY) .request(MediaType.APPLICATION_JSON) .get(); final List<Note> testNotesList = responseMsg.readEntity(new GenericType<List<Note>>() {}); Assert.assertEquals("Unexpected number of notes returned.", 3, testNotesList.size()); for (final Note testNote : testNotesList) { if (testNote.getId() == 1) { Assert.assertEquals("Note has unexpected id.", 1, testNote.getId()); Assert.assertEquals("Note has unexpected body.", TEST_BODY + 1, testNote.getBody()); } if (testNote.getId() == 2) { Assert.assertEquals("Note has unexpected id.", 2, testNote.getId()); Assert.assertEquals("Note has unexpected body.", TEST_BODY + 2, testNote.getBody()); } if (testNote.getId() == 3) { Assert.assertEquals("Note has unexpected id.", 3, testNote.getId()); Assert.assertEquals("Note has unexpected body.", TEST_BODY + 3, testNote.getBody()); } } }
@BeforeClass public static void setUp() throws Exception { // the first three notes are known final NotesDAO notesDAO = NotesDAO.getInstance(); notesDAO.clearNotes(); Note note = new Note(); note.setBody(TEST_BODY + '1'); notesDAO.addNote(note); note = new Note(); note.setBody(TEST_BODY + '2'); notesDAO.addNote(note); note = new Note(); note.setBody(TEST_BODY + '3'); notesDAO.addNote(note); // start the server server = Main.startServer(); // create the client Client c = ClientBuilder.newClient(); target = c.target(Main.BASE_URI); }
@Test public void testUpdateNoteBadRequest() { final Note newNote = new Note(); newNote.setBody("updateme"); Response responseMsg = target .path("notes") .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(newNote, MediaType.APPLICATION_JSON_TYPE)); Note testNote = responseMsg.readEntity(Note.class); Assert.assertTrue("Note should have id greater than 0.", testNote.getId() > 0); final int testId = 0; final String testBody = "updated"; testNote.setBody(testBody); responseMsg = target .path("notes/" + testId) .request(MediaType.APPLICATION_JSON_TYPE) .put(Entity.entity(testNote, MediaType.APPLICATION_JSON_TYPE)); Assert.assertEquals("Expected 400 Bad Request.", 400, responseMsg.getStatus()); }
@Test public void testGetNoteById() { final int testId = 1; final Response responseMsg = target.path("notes/" + testId).request(MediaType.APPLICATION_JSON).get(); final Note testNote = responseMsg.readEntity(Note.class); Assert.assertEquals("Note has unexpected id.", testId, testNote.getId()); Assert.assertEquals("Note has unexpected body.", TEST_BODY + testId, testNote.getBody()); }
@Test public void testUpdateNoteNotFound() { final Note newNote = new Note(); newNote.setBody("updateme"); final Response responseMsg = target .path("notes/" + newNote.getId()) .request(MediaType.APPLICATION_JSON_TYPE) .put(Entity.entity(newNote, MediaType.APPLICATION_JSON_TYPE)); Assert.assertEquals("Expected 404 Not Found.", 404, responseMsg.getStatus()); }
@Test public void testAddNote() { final String testBody = "This is a note."; final Note newNote = new Note(); newNote.setBody(testBody); final Response responseMsg = target .path("notes") .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.entity(newNote, MediaType.APPLICATION_JSON_TYPE)); final Note testNote = responseMsg.readEntity(Note.class); Assert.assertNotSame("Note has unexpected id.", 0, testNote.getId()); Assert.assertEquals("Note has unexpected body.", testBody, testNote.getBody()); }