Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.openelisglobal.note;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.openelisglobal.BaseWebContextSensitiveTest;
- import org.openelisglobal.common.util.StringUtil;
- import org.openelisglobal.note.service.NoteService;
- import org.openelisglobal.note.service.NoteServiceImpl;
- import org.openelisglobal.note.valueholder.Note;
- import org.openelisglobal.sample.valueholder.Sample;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.sql.Timestamp;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import org.openelisglobal.sample.service.SampleService;
- import java.sql.Date;
- import java.time.format.DateTimeFormatter;
- import static org.junit.Assert.*;
- public class NoteServiceTest extends BaseWebContextSensitiveTest {
- @Autowired
- NoteService noteService;
- @Autowired
- private SampleService sampleService;
- @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());
- }
- @Test
- public void getNotesAsString_shouldReturnFormattedNotes() throws Exception {
- String receivedTimestamp = "13/06/2024";
- String accessionNumber = "12345";
- DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
- java.util.Date date = dateFormat.parse(receivedTimestamp);
- long time = date.getTime();
- Timestamp doc = new Timestamp(time);
- // Create and persist the Sample object
- Sample sampleObject = new Sample();
- sampleObject.setReceivedTimestamp(doc);
- sampleObject.setAccessionNumber(accessionNumber);
- sampleObject.setEnteredDate(Date.valueOf("2024-06-13"));
- sampleService.insert(sampleObject);
- Note note1 = new Note();
- note1.setText("First note");
- note1.setNoteType(Note.INTERNAL);
- note1.setReferenceId(accessionNumber);
- note1.setReferenceTableId("sample");
- Note note2 = new Note();
- note2.setText("Second note");
- note2.setNoteType(Note.NON_CONFORMITY);
- note2.setReferenceId(accessionNumber); /
- note2.setReferenceTableId("sample");
- Note note3 = new Note();
- note3.setText("Third note");
- note3.setNoteType(Note.EXTERNAL);
- note3.setReferenceId(accessionNumber);
- note3.setReferenceTableId("sample");
- noteService.insert(note1);
- noteService.insert(note2);
- noteService.insert(note3);
- Sample persistedSample = sampleService.getSampleByAccessionNumber(accessionNumber);
- String formattedNotes = noteService.getNotesAsString(
- persistedSample,
- true,
- true,
- "\n",
- new NoteServiceImpl.NoteType[]{
- NoteServiceImpl.NoteType.INTERNAL,
- NoteServiceImpl.NoteType.NON_CONFORMITY
- },
- false
- );
- // Assertions
- assertNotNull("Formatted notes should not be null", formattedNotes);
- assertTrue("Formatted notes should contain 'First note'", formattedNotes.contains("First note"));
- assertTrue("Formatted notes should contain 'Second note'", formattedNotes.contains("Second note"));
- assertFalse("Formatted notes should not contain 'Third note'", formattedNotes.contains("Third note"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment