View Javadoc
1   /**
2    * Copyright 2005-2014 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  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.BufferedReader;
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.FileWriter;
26  import java.io.InputStream;
27  import java.io.InputStreamReader;
28  
29  import org.junit.Test;
30  import org.kuali.rice.kim.api.identity.Person;
31  import org.kuali.rice.krad.UserSession;
32  import org.kuali.rice.krad.service.KRADServiceLocator;
33  import org.kuali.rice.krad.test.KRADTestCase;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  
36  
37  /**
38   * AttachmentTest tests {@link Attachment} getters, setters and service methods
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  public class AttachmentTest extends KRADTestCase {
44  
45  	Attachment dummyAttachment;
46  	
47  	@Override
48  	public void setUp() throws Exception {
49  		super.setUp();
50  		dummyAttachment = new Attachment();
51  		
52  	}
53  
54  	@Override
55  	public void tearDown() throws Exception {
56  		super.tearDown();
57  		dummyAttachment = null;
58  	}
59  
60  	@Test
61      /**
62       * tests {@link Attachment#getNoteIdentifier()} and {@link Attachment#setNoteIdentifier(Long)}
63       */
64  	public void testNoteIdentifier(){
65          dummyAttachment.setNoteIdentifier((long)12345);
66  		assertTrue("Testing NoteIdentifier of Attachment in AttachmentTest",12345 == dummyAttachment.getNoteIdentifier());
67  	}
68  	
69  	@Test
70      /**
71       * tests {@link Attachment#getAttachmentMimeTypeCode()} and {@link Attachment#setAttachmentMimeTypeCode(String)}
72       */
73  	public void testAttachmentMimeTypeCode(){
74          dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
75  		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","MIME_TYP", dummyAttachment.getAttachmentMimeTypeCode());
76  	}
77  	
78  	@Test
79      /**
80       * tests {@link Attachment#getAttachmentFileName()} and {@link Attachment#setAttachmentFileName(String)}
81       */
82  	public void testAttachmentFileName(){
83          dummyAttachment.setAttachmentFileName("FILE_NM");
84  		assertEquals("Testing AttchmentFileName of Attachment in AttachmentTest","FILE_NM", dummyAttachment.getAttachmentFileName());
85  	}
86  	
87  	@Test
88      /**
89       * tests {@link Attachment#getAttachmentIdentifier()} and {@link Attachment#setAttachmentIdentifier(String)}
90       */
91  	public void testAttachmentIdentifier(){
92          dummyAttachment.setAttachmentIdentifier("Att_ID");
93  		assertEquals("Testing Attachment in AttachmentTest","Att_ID", dummyAttachment.getAttachmentIdentifier());
94  	}
95  	
96  	@Test
97      /**
98       * tests {@link Attachment#getAttachmentFileSize()} and {@link Attachment#setAttachmentFileSize(Long)}
99       */
100 	public void testAttachmentFileSize(){
101         dummyAttachment.setAttachmentFileSize((long)12345);
102 		assertTrue("Testing AttachmentFileSize of Attachment in AttachmentTest",12345 == dummyAttachment.getAttachmentFileSize());
103 	}
104 	
105 
106 	@Test
107     /**
108      * tests {@link Attachment#getAttachmentTypeCode()} and {@link Attachment#setAttachmentTypeCode(String)}
109      */
110 	public void testAttachmentTypeCode(){
111         dummyAttachment.setAttachmentTypeCode("ATT_TYP_CD");
112 		assertEquals("Testing AttachmentmimeTypeCode of Attachment in AttachmentTest","ATT_TYP_CD", dummyAttachment.getAttachmentTypeCode());
113 	}
114 	
115 
116 	@Test
117     /**
118      * tests {@link Attachment#getNote()} and {@link Attachment#setNote(Note)}
119      */
120 	public void testNote(){
121 		Note dummyNote = new Note();
122 		dummyNote.setNoteText("Hello");
123         dummyAttachment.setNote(dummyNote);
124 		assertEquals("Testing Note of Attachment in AttachmentTest","Hello", dummyAttachment.getNote().getNoteText());
125 	}
126 	
127 	@Test
128     /**
129      * tests {@link org.kuali.rice.krad.bo.Attachment#isComplete()}
130      */
131 	public void testComplete(){
132 
133         Attachment dummyAttachment = ((Attachment)this.dummyAttachment);
134 
135 		dummyAttachment.setAttachmentIdentifier("Att_ID");
136 		dummyAttachment.setAttachmentFileName("FILE_NM");
137 		dummyAttachment.setAttachmentFileSize(new Long(12345));
138 		dummyAttachment.setAttachmentMimeTypeCode("MIME_TYP");
139 		assertTrue("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
140 		{
141 			dummyAttachment.setAttachmentFileName(null);
142 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
143 	
144 		}
145 		{
146 			dummyAttachment.setAttachmentFileSize((long)0);
147 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
148 		}
149 		{
150 			dummyAttachment.setAttachmentIdentifier(null);
151 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
152 		}
153 		{
154 			dummyAttachment.setAttachmentMimeTypeCode(null);
155 			assertFalse("Testing Complete of Attachment in AttachmentTest",dummyAttachment.isComplete());
156 		}
157 		
158 	}
159 	
160 	@Test
161     /**
162      * tests {@link Attachment#getAttachmentContents()}
163      */
164 	public void testAttachmentContents() throws Exception {
165 		
166 		
167 		try{
168 			 
169 			FileWriter out = new FileWriter("dummy.txt");
170 			out.write("Hello testAttachmentContent");
171 			out.close();
172 						
173 			File dummyFile = new File("dummy.txt");  
174 			Note dummyNote = new Note();
175 			InputStream inStream = new FileInputStream("dummy.txt");
176 		
177 			GlobalVariables.setUserSession(new UserSession("quickstart"));
178 			
179 	        Person kualiUser = GlobalVariables.getUserSession().getPerson();
180 			Note parentNote = KRADServiceLocator.getNoteService().createNote(dummyNote, dummyAttachment, kualiUser.getPrincipalId());
181 			dummyAttachment = KRADServiceLocator.getAttachmentService().createAttachment( parentNote,
182 																					   	 "dummy.txt", 
183 																					     "MimeTypeCode",
184 																					     (int) (long) dummyFile.length(), 
185 																					     inStream,
186 																					     "AttachmentTypeCode");
187 			String result ="";
188             BufferedReader in =  new BufferedReader(new InputStreamReader(dummyAttachment.getAttachmentContents()));
189             String line;
190 			while ((line = in.readLine()) != null) {
191 				   result += line;
192 			}
193 			inStream.close();
194 			assertEquals("Testing attachmentContents in AttachmentTest","Hello testAttachmentContent",result );
195 		}
196 		finally{
197 			new File("dummy.txt").delete();
198 		}
199 	}
200 }