View Javadoc

1   /**
2    * Copyright 2005-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.bo;
17  
18  
19  import org.junit.Test;
20  import org.kuali.rice.kim.api.identity.Person;
21  import org.kuali.rice.krad.UserSession;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.util.GlobalVariables;
24  import org.kuali.rice.krad.test.KRADTestCase;
25  
26  import java.io.BufferedReader;
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.FileWriter;
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  
33  import static org.junit.Assert.*;
34  
35  
36  /**
37   * AttachmentTest tests {@link Attachment} getters, setters and service methods
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class AttachmentTest extends KRADTestCase {
43  	
44  	Attachment dummyAttachment;
45  	
46  	@Override
47  	public void setUp() throws Exception {
48  		super.setUp();
49  		dummyAttachment = new Attachment();
50  		
51  	}
52  
53  	@Override
54  	public void tearDown() throws Exception {
55  		super.tearDown();
56  		dummyAttachment = null;
57  	}
58  
59  	@Test
60      /**
61       * tests {@link Attachment#getNoteIdentifier()} and {@link Attachment#setNoteIdentifier(Long)}
62       */
63  	public void testNoteIdentifier(){
64  		dummyAttachment.setNoteIdentifier((long)12345);
65  		assertTrue("Testing NoteIdentifier of Attachment in AttachmentTest",12345 == dummyAttachment.getNoteIdentifier());
66  	}
67  	
68  	@Test
69      /**
70       * tests {@link Attachment#getAttachmentMimeTypeCode()} and {@link Attachment#setAttachmentMimeTypeCode(String)}
71       */
72  	public void testAttachmentMimeTypeCode(){
73  		dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
74  		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","MIME_TYP", dummyAttachment.getAttachmentMimeTypeCode());
75  	}
76  	
77  	@Test
78      /**
79       * tests {@link Attachment#getAttachmentFileName()} and {@link Attachment#setAttachmentFileName(String)}
80       */
81  	public void testAttachmentFileName(){
82  		dummyAttachment.setAttachmentFileName("FILE_NM");
83  		assertEquals("Testing AttchmentFileName of Attachment in AttachmentTest","FILE_NM", dummyAttachment.getAttachmentFileName());
84  	}
85  	
86  	@Test
87      /**
88       * tests {@link Attachment#getAttachmentIdentifier()} and {@link Attachment#setAttachmentIdentifier(String)}
89       */
90  	public void testAttachmentIdentifier(){
91  		dummyAttachment.setAttachmentIdentifier("Att_ID");
92  		assertEquals("Testing Attachment in AttachmentTest","Att_ID", dummyAttachment.getAttachmentIdentifier());
93  	}
94  	
95  	@Test
96      /**
97       * tests {@link Attachment#getAttachmentFileSize()} and {@link Attachment#setAttachmentFileSize(Long)}
98       */
99  	public void testAttachmentFileSize(){
100 		dummyAttachment.setAttachmentFileSize((long)12345);
101 		assertTrue("Testing AttachmentFileSize of Attachment in AttachmentTest",12345 == dummyAttachment.getAttachmentFileSize());
102 	}
103 	
104 
105 	@Test
106     /**
107      * tests {@link Attachment#getAttachmentTypeCode()} and {@link Attachment#setAttachmentTypeCode(String)}
108      */
109 	public void testAttachmentTypeCode(){
110 		dummyAttachment.setAttachmentTypeCode("ATT_TYP_CD");
111 		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","ATT_TYP_CD", dummyAttachment.getAttachmentTypeCode());
112 	}
113 	
114 
115 	@Test
116     /**
117      * tests {@link Attachment#getNote()} and {@link Attachment#setNote(Note)}
118      */
119 	public void testNote(){
120 		Note dummyNote = new Note();
121 		dummyNote.setNoteText("Hello");
122 		dummyAttachment.setNote(dummyNote);
123 		assertEquals("Testing Note of Attachment in AttachmentTest","Hello", dummyAttachment.getNote().getNoteText());
124 	}
125 	
126 	@Test
127     /**
128      * tests {@link org.kuali.rice.krad.bo.Attachment#isComplete()}
129      */
130 	public void testComplete(){
131 	
132 		dummyAttachment.setAttachmentIdentifier("Att_ID");
133 		dummyAttachment.setAttachmentFileName("FILE_NM");
134 		dummyAttachment.setAttachmentFileSize(new Long(12345));
135 		dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
136 		assertTrue("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
137 		{
138 			dummyAttachment.setAttachmentFileName(null);
139 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
140 	
141 		}
142 		{
143 			dummyAttachment.setAttachmentFileSize((long)0);
144 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
145 		}
146 		{
147 			dummyAttachment.setAttachmentIdentifier(null);
148 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
149 		}
150 		{
151 			dummyAttachment.setAttachmentMimeTypeCode(null);
152 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
153 		}
154 		
155 	}
156 	
157 	@Test
158     /**
159      * tests {@link Attachment#getAttachmentContents()}
160      */
161 	public void testAttachmentContents() throws Exception {
162 		
163 		
164 		try{
165 			 
166 			FileWriter out = new FileWriter("dummy.txt");
167 			out.write("Hello testAttachmentContent");
168 			out.close();
169 						
170 			File dummyFile = new File("dummy.txt");  
171 			Note dummyNote = new Note();
172 			InputStream inStream = new FileInputStream("dummy.txt");
173 		
174 			GlobalVariables.setUserSession(new UserSession("quickstart"));
175 			
176 	        Person kualiUser = GlobalVariables.getUserSession().getPerson();
177 			PersistableBusinessObject parentNote = KRADServiceLocator.getNoteService().createNote(dummyNote, dummyAttachment, kualiUser.getPrincipalId());
178 			dummyAttachment = KRADServiceLocator.getAttachmentService().createAttachment( parentNote,
179 																					   	 "dummy.txt", 
180 																					     "MimeTypeCode",
181 																					     (int) (long) dummyFile.length(), 
182 																					     inStream,
183 																					     "AttachmentTypeCode");
184 			String result ="";
185             BufferedReader in =  new BufferedReader(new InputStreamReader(dummyAttachment.getAttachmentContents()));
186             String line;
187 			while ((line = in.readLine()) != null) {
188 				   result += line;
189 			}
190 			inStream.close();
191 			assertEquals("Testing attachmentContents in AttachmentTest","Hello testAttachmentContent",result );
192 		}
193 		finally{
194 			new File("dummy.txt").delete();
195 		}
196 	}
197 }