1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kew.notes;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20
21 import java.io.FileReader;
22 import java.io.StringWriter;
23 import java.sql.Timestamp;
24 import java.util.Date;
25
26 import org.junit.Test;
27 import org.kuali.rice.kew.notes.service.NoteService;
28 import org.kuali.rice.kew.service.KEWServiceLocator;
29 import org.kuali.rice.kew.test.KEWTestCase;
30 import org.kuali.rice.kew.test.TestUtilities;
31 import org.kuali.rice.krad.service.KRADServiceLocator;
32
33 public class NoteServiceTest extends KEWTestCase {
34
35 @Test public void testAttachmentSave() throws Exception {
36 Note note = new Note();
37 note.setNoteAuthorWorkflowId("fakeyUser");
38 note.setDocumentId("2");
39 note.setNoteCreateDate(new Timestamp(new Date().getTime()));
40 note.setNoteText("i like notes");
41
42 Attachment attachment = new Attachment();
43 attachment.setNote(note);
44 attachment.setMimeType("mimeType");
45 attachment.setFileName("attachedFile.txt");
46 attachment.setAttachedObject(TestUtilities.loadResource(this.getClass(), "attachedFile.txt"));
47
48 note.getAttachments().add(attachment);
49
50 NoteService noteService = KEWServiceLocator.getNoteService();
51 note = noteService.saveNote(note);
52 KRADServiceLocator.getDataObjectService().flush(Note.class);
53 attachment = note.getAttachments().get(0);
54 assertNotNull("Note should have a id", note.getNoteId());
55 assertNotNull("Note should have a version number", note.getLockVerNbr());
56
57 assertNotNull("Attachment should have a id", attachment.getAttachmentId());
58 assertNotNull("Attachment should have version number", attachment.getLockVerNbr());
59 assertNotNull("Attachment file loc should reflect file system location", attachment.getFileLoc());
60
61 FileReader fileReader = new FileReader(noteService.findAttachmentFile(attachment));
62 StringWriter stringWriter = new StringWriter();
63 int c;
64 while ((c = fileReader.read()) != -1) {
65 stringWriter.write(c);
66 }
67
68 assertEquals("Attached file content should equal source file content", "I'm an attached file", stringWriter.getBuffer().toString());
69 }
70
71 }