From fe81a854136a462d79392d2d8fbe5b22e8379552 Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Sun, 17 Nov 2024 13:56:15 +0300 Subject: [PATCH 1/2] Add-noteservice-Tests --- .../openelisglobal/note/NoteServiceTest.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/test/java/org/openelisglobal/note/NoteServiceTest.java diff --git a/src/test/java/org/openelisglobal/note/NoteServiceTest.java b/src/test/java/org/openelisglobal/note/NoteServiceTest.java new file mode 100644 index 0000000000..be8f5ef0bc --- /dev/null +++ b/src/test/java/org/openelisglobal/note/NoteServiceTest.java @@ -0,0 +1,77 @@ +package org.openelisglobal.note; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.openelisglobal.BaseWebContextSensitiveTest; +import org.openelisglobal.note.service.NoteService; +import org.openelisglobal.note.valueholder.Note; +import org.springframework.beans.factory.annotation.Autowired; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertEquals; + +public class NoteServiceTest extends BaseWebContextSensitiveTest { + + @Autowired + NoteService noteService; + + @Before + public void init() throws Exception { + assertNotNull(noteService); + noteService.deleteAll(noteService.getAll()); + } + + @After + public void tearDown() { + noteService.deleteAll(noteService.getAll()); + + } + + @Test + public void deleteNote_shouldDeleteNote() throws Exception { + String subject = "Test Note Subject"; + String text = "This is a test note text."; + + Note note = new Note(); + note.setSubject(subject); + note.setText(text); + + String noteId = noteService.insert(note); + + Note savedNote = noteService.get(noteId); + noteService.delete(savedNote); + assertEquals(0, noteService.getAll().size()); + } + + @Test + public void getNote_shouldReturnNullForNonExistentNote() throws Exception { + String nonExistentNoteId = "nonExistentNoteId"; + + Note note = noteService.get(nonExistentNoteId); + assertNull(note); + } + + @Test + public void deleteAllNotes_shouldClearAllNotes() throws Exception { + String subject1 = "Note 1"; + String text1 = "First test note."; + + Note note1 = new Note(); + note1.setSubject(subject1); + note1.setText(text1); + noteService.insert(note1); + + String subject2 = "Note 2"; + String text2 = "Second test note."; + + Note note2 = new Note(); + note2.setSubject(subject2); + note2.setText(text2); + noteService.insert(note2); + + noteService.deleteAll(noteService.getAll()); + + assertEquals(0, noteService.getAll().size()); + } +} From 86a4458d6cdd28f74864e5ac91d7d97b6797c92d Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Sun, 17 Nov 2024 14:08:31 +0300 Subject: [PATCH 2/2] fix --- .../java/org/openelisglobal/note/NoteServiceTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/openelisglobal/note/NoteServiceTest.java b/src/test/java/org/openelisglobal/note/NoteServiceTest.java index be8f5ef0bc..86e1e0816a 100644 --- a/src/test/java/org/openelisglobal/note/NoteServiceTest.java +++ b/src/test/java/org/openelisglobal/note/NoteServiceTest.java @@ -1,5 +1,9 @@ package org.openelisglobal.note; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -7,9 +11,6 @@ import org.openelisglobal.note.service.NoteService; import org.openelisglobal.note.valueholder.Note; import org.springframework.beans.factory.annotation.Autowired; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertEquals; public class NoteServiceTest extends BaseWebContextSensitiveTest { @@ -69,9 +70,7 @@ public void deleteAllNotes_shouldClearAllNotes() throws Exception { note2.setSubject(subject2); note2.setText(text2); noteService.insert(note2); - noteService.deleteAll(noteService.getAll()); - assertEquals(0, noteService.getAll().size()); } }