@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 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()); }