From ab2390e6c0b23e64af1b740abf0cde0484b48f48 Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Sun, 17 Nov 2024 13:56:15 +0300 Subject: [PATCH 1/4] 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 74d6f6c1f8c4817df409d4837251cde18ec7c417 Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Sun, 17 Nov 2024 14:08:31 +0300 Subject: [PATCH 2/4] 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()); } } From 6e39de0c8bb6ed6c90d09a59807a1da8f66f614a Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Fri, 10 Jan 2025 00:41:09 +0300 Subject: [PATCH 3/4] fix tests --- .../org/openelisglobal/AppTestConfig.java | 29 +++++++++++++++ .../openelisglobal/note/NoteServiceTest.java | 36 +++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/openelisglobal/AppTestConfig.java b/src/test/java/org/openelisglobal/AppTestConfig.java index f0ad990808..307154fbc8 100644 --- a/src/test/java/org/openelisglobal/AppTestConfig.java +++ b/src/test/java/org/openelisglobal/AppTestConfig.java @@ -22,11 +22,16 @@ import org.openelisglobal.internationalization.MessageUtil; import org.openelisglobal.localization.dao.LocalizationDAO; import org.openelisglobal.localization.service.LocalizationServiceImpl; +import org.openelisglobal.note.dao.NoteDAO; import org.openelisglobal.note.service.NoteService; import org.openelisglobal.notification.service.AnalysisNotificationConfigService; import org.openelisglobal.notification.service.TestNotificationConfigService; import org.openelisglobal.observationhistory.service.ObservationHistoryService; import org.openelisglobal.observationhistorytype.service.ObservationHistoryTypeService; +import org.openelisglobal.organization.dao.OrganizationTypeDAO; +import org.openelisglobal.organization.daoimpl.OrganizationTypeDAOImpl; +import org.openelisglobal.organization.service.OrganizationService; +import org.openelisglobal.organization.service.OrganizationTypeService; import org.openelisglobal.panel.service.PanelService; import org.openelisglobal.panelitem.service.PanelItemService; import org.openelisglobal.program.service.ImmunohistochemistrySampleService; @@ -213,6 +218,12 @@ public RequesterTypeService requesterTypeService() { return mock(RequesterTypeService.class); } + @Bean() + @Profile("test") + public OrganizationService organizationService() { + return mock(OrganizationService.class); + } + @Bean() @Profile("test") public BasicAuthenticationDataService basicAuthenticationDataService() { @@ -404,6 +415,24 @@ public StatusOfSampleService statusOfSampleService() { return mock(StatusOfSampleService.class); } + @Bean + @Profile("test") + public OrganizationTypeService OrganizationTypeService() { + return mock(OrganizationTypeService.class); + } + + @Bean + @Profile("test") + public NoteDAO NoteDAO() { + return mock(NoteDAO.class); + } + + @Bean + @Profile("test") + public OrganizationTypeDAO organizationTypeDAO() { + return new OrganizationTypeDAOImpl(); + } + @Override public void configureMessageConverters(@NonNull List> converters) { WebMvcConfigurer.super.configureMessageConverters(converters); diff --git a/src/test/java/org/openelisglobal/note/NoteServiceTest.java b/src/test/java/org/openelisglobal/note/NoteServiceTest.java index 86e1e0816a..bd2dd8a08b 100644 --- a/src/test/java/org/openelisglobal/note/NoteServiceTest.java +++ b/src/test/java/org/openelisglobal/note/NoteServiceTest.java @@ -1,13 +1,15 @@ package org.openelisglobal.note; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import java.util.List; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openelisglobal.BaseWebContextSensitiveTest; +import org.openelisglobal.note.dao.NoteDAO; import org.openelisglobal.note.service.NoteService; import org.openelisglobal.note.valueholder.Note; import org.springframework.beans.factory.annotation.Autowired; @@ -17,9 +19,11 @@ public class NoteServiceTest extends BaseWebContextSensitiveTest { @Autowired NoteService noteService; + @Autowired + NoteDAO noteDAO; + @Before public void init() throws Exception { - assertNotNull(noteService); noteService.deleteAll(noteService.getAll()); } @@ -73,4 +77,32 @@ public void deleteAllNotes_shouldClearAllNotes() throws Exception { noteService.deleteAll(noteService.getAll()); assertEquals(0, noteService.getAll().size()); } + + @Test + public void getNotesOrderedByTypeAndLastUpdated_shouldReturnNotesInCorrectOrder() { + Note note1 = new Note(); + note1.setReferenceId("1"); + note1.setReferenceTableId("1"); + note1.setNoteType(Note.INTERNAL); + note1.setSubject("First Note"); + note1.setText("This is the first test note."); + note1.setSysUserId("testUser123"); + noteService.insert(note1); + + Note note2 = new Note(); + note2.setReferenceId("1"); + note2.setReferenceTableId("1"); + note2.setNoteType(Note.INTERNAL); + note2.setSubject("Second Note"); + note2.setText("This is the second test note."); + note2.setSysUserId("testUser456"); + noteService.insert(note2); + + List notes = noteService.getAllNotesByRefIdRefTable(note1); + assertFalse("Notes list should not be empty", notes.isEmpty()); + + assertEquals("Second note should be internal", Note.INTERNAL, notes.get(0).getNoteType()); + assertEquals("First note should be external", Note.INTERNAL, notes.get(1).getNoteType()); + } + } From be28aa5092a4fd2b6bd6975db7ed788018d0b9cb Mon Sep 17 00:00:00 2001 From: Agaba Derrick Date: Tue, 21 Jan 2025 21:14:35 +0300 Subject: [PATCH 4/4] fix --- .../org/openelisglobal/AppTestConfig.java | 14 ++++--- .../openelisglobal/note/NoteServiceTest.java | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/openelisglobal/AppTestConfig.java b/src/test/java/org/openelisglobal/AppTestConfig.java index 307154fbc8..0839dfdd2e 100644 --- a/src/test/java/org/openelisglobal/AppTestConfig.java +++ b/src/test/java/org/openelisglobal/AppTestConfig.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonInclude; import java.util.ArrayList; import java.util.List; +import javax.persistence.EntityManager; import lombok.NonNull; import org.apache.http.impl.client.CloseableHttpClient; import org.openelisglobal.audittrail.dao.AuditTrailService; @@ -28,8 +29,6 @@ import org.openelisglobal.notification.service.TestNotificationConfigService; import org.openelisglobal.observationhistory.service.ObservationHistoryService; import org.openelisglobal.observationhistorytype.service.ObservationHistoryTypeService; -import org.openelisglobal.organization.dao.OrganizationTypeDAO; -import org.openelisglobal.organization.daoimpl.OrganizationTypeDAOImpl; import org.openelisglobal.organization.service.OrganizationService; import org.openelisglobal.organization.service.OrganizationTypeService; import org.openelisglobal.panel.service.PanelService; @@ -46,6 +45,7 @@ import org.openelisglobal.sampleqaevent.service.SampleQaEventService; import org.openelisglobal.siteinformation.service.SiteInformationService; import org.openelisglobal.statusofsample.service.StatusOfSampleService; +import org.openelisglobal.systemuser.service.SystemUserService; import org.openelisglobal.systemusersection.service.SystemUserSectionService; import org.openelisglobal.test.dao.TestDAO; import org.openelisglobal.test.service.TestSectionService; @@ -428,9 +428,13 @@ public NoteDAO NoteDAO() { } @Bean - @Profile("test") - public OrganizationTypeDAO organizationTypeDAO() { - return new OrganizationTypeDAOImpl(); + public SystemUserService systemUserService() { + return mock(SystemUserService.class); + } + + @Bean + public EntityManager entityManager() { + return mock(EntityManager.class); } @Override diff --git a/src/test/java/org/openelisglobal/note/NoteServiceTest.java b/src/test/java/org/openelisglobal/note/NoteServiceTest.java index bd2dd8a08b..9d6d599738 100644 --- a/src/test/java/org/openelisglobal/note/NoteServiceTest.java +++ b/src/test/java/org/openelisglobal/note/NoteServiceTest.java @@ -1,5 +1,6 @@ package org.openelisglobal.note; +import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; @@ -12,6 +13,10 @@ import org.openelisglobal.note.dao.NoteDAO; import org.openelisglobal.note.service.NoteService; import org.openelisglobal.note.valueholder.Note; +import org.openelisglobal.referencetables.service.ReferenceTablesService; +import org.openelisglobal.sampleqaevent.service.SampleQaEventService; +import org.openelisglobal.systemuser.service.SystemUserService; +import org.openelisglobal.systemuser.valueholder.SystemUser; import org.springframework.beans.factory.annotation.Autowired; public class NoteServiceTest extends BaseWebContextSensitiveTest { @@ -22,8 +27,26 @@ public class NoteServiceTest extends BaseWebContextSensitiveTest { @Autowired NoteDAO noteDAO; + @Autowired + private ReferenceTablesService referenceTablesService; + + @Autowired + private SampleQaEventService sampleQaEventService; + + @Autowired + private SystemUserService systemUserService; + + private static final String TEST_USER_1 = "testUser123"; + private static final String TEST_USER_2 = "testUser456"; + private static final String REF_ID = "1"; + private static final String REF_TABLE_ID = "1"; + @Before public void init() throws Exception { + + createSystemUser(TEST_USER_1); + createSystemUser(TEST_USER_2); + noteService.deleteAll(noteService.getAll()); } @@ -31,6 +54,25 @@ public void init() throws Exception { public void tearDown() { noteService.deleteAll(noteService.getAll()); + deleteSystemUser(TEST_USER_1); + deleteSystemUser(TEST_USER_2); + } + + private void createSystemUser(String userId) { + SystemUser user = systemUserService.get(userId); + if (user == null) { + user = new SystemUser(); + user.setId(userId); + user.setLoginName(userId); // Set required fields + systemUserService.insert(user); + } + } + + private void deleteSystemUser(String userId) { + SystemUser user = systemUserService.get(userId); + if (user != null) { + systemUserService.delete(user); + } } @Test