Guest User

Untitled

a guest
Nov 19th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. package org.openelisglobal.note;
  2.  
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. import org.openelisglobal.BaseWebContextSensitiveTest;
  7. import org.openelisglobal.common.util.StringUtil;
  8. import org.openelisglobal.note.service.NoteService;
  9. import org.openelisglobal.note.service.NoteServiceImpl;
  10. import org.openelisglobal.note.valueholder.Note;
  11. import org.openelisglobal.sample.valueholder.Sample;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import java.sql.Timestamp;
  14. import java.text.DateFormat;
  15. import java.text.SimpleDateFormat;
  16. import org.openelisglobal.sample.service.SampleService;
  17. import java.sql.Date;
  18. import java.time.format.DateTimeFormatter;
  19.  
  20. import static org.junit.Assert.*;
  21.  
  22. public class NoteServiceTest extends BaseWebContextSensitiveTest {
  23.  
  24. @Autowired
  25. NoteService noteService;
  26.  
  27. @Autowired
  28. private SampleService sampleService;
  29.  
  30.  
  31. @Before
  32. public void init() throws Exception {
  33. assertNotNull(noteService);
  34. noteService.deleteAll(noteService.getAll());
  35. }
  36.  
  37. @After
  38. public void tearDown() {
  39. noteService.deleteAll(noteService.getAll());
  40.  
  41. }
  42.  
  43. @Test
  44. public void deleteNote_shouldDeleteNote() throws Exception {
  45. String subject = "Test Note Subject";
  46. String text = "This is a test note text.";
  47.  
  48. Note note = new Note();
  49. note.setSubject(subject);
  50. note.setText(text);
  51.  
  52. String noteId = noteService.insert(note);
  53.  
  54. Note savedNote = noteService.get(noteId);
  55. noteService.delete(savedNote);
  56. assertEquals(0, noteService.getAll().size());
  57. }
  58.  
  59. @Test
  60. public void getNote_shouldReturnNullForNonExistentNote() throws Exception {
  61. String nonExistentNoteId = "nonExistentNoteId";
  62.  
  63. Note note = noteService.get(nonExistentNoteId);
  64. assertNull(note);
  65. }
  66.  
  67. @Test
  68. public void deleteAllNotes_shouldClearAllNotes() throws Exception {
  69. String subject1 = "Note 1";
  70. String text1 = "First test note.";
  71.  
  72. Note note1 = new Note();
  73. note1.setSubject(subject1);
  74. note1.setText(text1);
  75. noteService.insert(note1);
  76.  
  77. String subject2 = "Note 2";
  78. String text2 = "Second test note.";
  79.  
  80. Note note2 = new Note();
  81. note2.setSubject(subject2);
  82. note2.setText(text2);
  83. noteService.insert(note2);
  84. noteService.deleteAll(noteService.getAll());
  85. assertEquals(0, noteService.getAll().size());
  86. }
  87.  
  88. @Test
  89. public void getNotesAsString_shouldReturnFormattedNotes() throws Exception {
  90. String receivedTimestamp = "13/06/2024";
  91. String accessionNumber = "12345";
  92.  
  93. DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
  94. java.util.Date date = dateFormat.parse(receivedTimestamp);
  95. long time = date.getTime();
  96. Timestamp doc = new Timestamp(time);
  97.  
  98. // Create and persist the Sample object
  99. Sample sampleObject = new Sample();
  100. sampleObject.setReceivedTimestamp(doc);
  101. sampleObject.setAccessionNumber(accessionNumber);
  102. sampleObject.setEnteredDate(Date.valueOf("2024-06-13"));
  103. sampleService.insert(sampleObject);
  104.  
  105. Note note1 = new Note();
  106. note1.setText("First note");
  107. note1.setNoteType(Note.INTERNAL);
  108. note1.setReferenceId(accessionNumber);
  109. note1.setReferenceTableId("sample");
  110.  
  111. Note note2 = new Note();
  112. note2.setText("Second note");
  113. note2.setNoteType(Note.NON_CONFORMITY);
  114. note2.setReferenceId(accessionNumber); /
  115. note2.setReferenceTableId("sample");
  116.  
  117. Note note3 = new Note();
  118. note3.setText("Third note");
  119. note3.setNoteType(Note.EXTERNAL);
  120. note3.setReferenceId(accessionNumber);
  121. note3.setReferenceTableId("sample");
  122.  
  123. noteService.insert(note1);
  124. noteService.insert(note2);
  125. noteService.insert(note3);
  126.  
  127. Sample persistedSample = sampleService.getSampleByAccessionNumber(accessionNumber);
  128.  
  129. String formattedNotes = noteService.getNotesAsString(
  130. persistedSample,
  131. true,
  132. true,
  133. "\n",
  134. new NoteServiceImpl.NoteType[]{
  135. NoteServiceImpl.NoteType.INTERNAL,
  136. NoteServiceImpl.NoteType.NON_CONFORMITY
  137. },
  138. false
  139. );
  140.  
  141. // Assertions
  142. assertNotNull("Formatted notes should not be null", formattedNotes);
  143. assertTrue("Formatted notes should contain 'First note'", formattedNotes.contains("First note"));
  144. assertTrue("Formatted notes should contain 'Second note'", formattedNotes.contains("Second note"));
  145. assertFalse("Formatted notes should not contain 'Third note'", formattedNotes.contains("Third note"));
  146. }
  147.  
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment