001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.notes;
017
018import static org.junit.Assert.assertEquals;
019import static org.junit.Assert.assertNotNull;
020
021import java.io.FileReader;
022import java.io.StringWriter;
023import java.sql.Timestamp;
024import java.util.Date;
025
026import org.junit.Test;
027import org.kuali.rice.kew.notes.service.NoteService;
028import org.kuali.rice.kew.service.KEWServiceLocator;
029import org.kuali.rice.kew.test.KEWTestCase;
030import org.kuali.rice.kew.test.TestUtilities;
031import org.kuali.rice.krad.service.KRADServiceLocator;
032
033public class NoteServiceTest extends KEWTestCase {
034                
035    @Test public void testAttachmentSave() throws Exception {
036                Note note = new Note();
037                note.setNoteAuthorWorkflowId("fakeyUser");
038                note.setDocumentId("2");
039                note.setNoteCreateDate(new Timestamp(new Date().getTime()));
040                note.setNoteText("i like notes");
041                
042                Attachment attachment = new Attachment();
043                attachment.setNote(note);
044                attachment.setMimeType("mimeType");
045                attachment.setFileName("attachedFile.txt");
046                attachment.setAttachedObject(TestUtilities.loadResource(this.getClass(), "attachedFile.txt"));
047                
048                note.getAttachments().add(attachment);
049                
050                NoteService noteService = KEWServiceLocator.getNoteService();
051                note = noteService.saveNote(note);
052        KRADServiceLocator.getDataObjectService().flush(Note.class);
053        attachment = note.getAttachments().get(0);
054                assertNotNull("Note should have a id", note.getNoteId());
055                assertNotNull("Note should have a version number", note.getLockVerNbr());
056                
057                assertNotNull("Attachment should have a id", attachment.getAttachmentId());
058                assertNotNull("Attachment should have version number", attachment.getLockVerNbr());
059                assertNotNull("Attachment file loc should reflect file system location", attachment.getFileLoc());
060                
061                FileReader fileReader = new FileReader(noteService.findAttachmentFile(attachment));
062                StringWriter stringWriter = new StringWriter();
063                int c;
064        while ((c = fileReader.read()) != -1) {
065                stringWriter.write(c);
066        }
067        //i'm being lazy and knowing what's in the source file
068        assertEquals("Attached file content should equal source file content", "I'm an attached file", stringWriter.getBuffer().toString());
069        }
070
071}