1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.shared.controllers;
16
17 import org.apache.commons.io.IOUtils;
18 import org.apache.log4j.Logger;
19 import org.junit.Before;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.kuali.mobility.file.entity.File;
24 import org.kuali.mobility.file.service.FileService;
25 import org.mockito.Mock;
26 import org.springframework.mock.web.*;
27 import org.springframework.ui.ExtendedModelMap;
28 import org.springframework.ui.Model;
29
30 import javax.servlet.http.HttpServletResponse;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.util.ArrayList;
34 import java.util.Properties;
35
36 import static org.junit.Assert.assertTrue;
37 import static org.junit.Assert.fail;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40
41
42
43
44 @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
45 public class FileControllerTest {
46 private static final Logger LOG = Logger.getLogger(FileControllerTest.class);
47
48 private static final String CONTENT_TYPE = "text/plain";
49 private static final String CONTENT_ENCODING = "UTF-8";
50
51 private static final String INDEX = "files";
52 private static final String FILE_NAME = "file.test.properties";
53 private static final String FORM_FILE_NAME = "file";
54 private static final String FORM_UPLOAD_RESPONSE = "{\"name\":\"file.test.properties\",\"fileid\":\"null\",\"size\":\"1295\"}";
55
56 private static MockServletContext servletContext;
57 private FileController controller;
58 private Properties kmeProperties;
59
60 @Mock
61 private FileService fileService;
62
63 @BeforeClass
64 public static void setUpClass() throws Exception {
65 servletContext = new MockServletContext();
66 }
67
68 @Before
69 public void preTest() {
70 this.setController(new FileController());
71 getController().setFileService(getFileService());
72 }
73
74 @Test
75 public void testInjectionSucceeded() {
76 assertTrue("Failed to find the file service", getController().getFileService() == getFileService());
77 }
78
79 @Test
80 public void testIndex() {
81 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
82 Model uiModel = new ExtendedModelMap();
83
84 when(getFileService().findAllFiles()).thenReturn(new ArrayList<File>());
85
86 String viewName;
87 try {
88 viewName = getController().index(request, uiModel);
89 } catch( Exception e ) {
90 LOG.error(e.getLocalizedMessage(),e);
91 viewName = null;
92 }
93 assertTrue("View not what was expected.", INDEX.equals(viewName));
94 }
95
96 @Test
97 public void testRemoveFile() {
98 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
99 Model uiModel = new ExtendedModelMap();
100
101 Long fileHash = Long.getLong("42");
102 File file = new File();
103 file.setId(fileHash);
104
105 when(getFileService().findFileById(fileHash)).thenReturn(file);
106 when(getFileService().removeFile(file)).thenReturn(true);
107
108 String viewName;
109 try {
110 viewName = getController().removeFile(uiModel, request, fileHash);
111 } catch( Exception e ) {
112 LOG.error(e.getLocalizedMessage(),e);
113 viewName = null;
114 }
115 assertTrue("Failed to remove file.", INDEX.equals(viewName));
116 }
117
118 @Test
119 public void testRemoveFileNullFile() {
120 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
121 Model uiModel = new ExtendedModelMap();
122
123 Long fileHash = Long.getLong("42");
124
125 when(getFileService().findFileById(fileHash)).thenReturn(null);
126
127 String viewName;
128 try {
129 viewName = getController().removeFile(uiModel, request, fileHash);
130 } catch( Exception e ) {
131 LOG.error(e.getLocalizedMessage(),e);
132 viewName = null;
133 }
134 assertTrue("Failed to remove file.", INDEX.equals(viewName));
135 }
136
137 @Test
138 public void testRemoveFileFailOnService() {
139 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
140 Model uiModel = new ExtendedModelMap();
141
142 Long fileHash = Long.getLong("42");
143 File file = new File();
144 file.setId(fileHash);
145
146 when(getFileService().findFileById(fileHash)).thenReturn(file);
147 when(getFileService().removeFile(file)).thenReturn(false);
148
149 String viewName;
150 try {
151 viewName = getController().removeFile(uiModel, request, fileHash);
152 } catch( Exception e ) {
153 LOG.error(e.getLocalizedMessage(),e);
154 viewName = null;
155 }
156 assertTrue("Failed to remove file.", INDEX.equals(viewName));
157 }
158
159 @Test
160 public void testHandleFormUpload() {
161 MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
162
163 String viewName;
164 try {
165 InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);
166
167 MockMultipartFile mockFile = new MockMultipartFile(FORM_FILE_NAME,FILE_NAME,CONTENT_TYPE,in);
168
169 request.addFile(mockFile);
170
171 File file = new File(mockFile);
172
173 when(getFileService().saveFile(file)).thenReturn(Long.valueOf(42));
174
175 viewName = getController().handleFormUpload(request);
176 } catch(IOException ioe) {
177 LOG.error(ioe.getLocalizedMessage(),ioe);
178 viewName = null;
179 } catch( Exception e ) {
180 LOG.error(e.getLocalizedMessage(),e);
181 viewName = null;
182 }
183 assertTrue("Failed to handle form upload.", FORM_UPLOAD_RESPONSE.equals(viewName));
184 }
185
186 @Test
187 public void testGetFile() {
188 String viewName;
189 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
190 MockHttpServletResponse response = new MockHttpServletResponse();
191 try {
192 InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);
193
194 MockMultipartFile mockFile = new MockMultipartFile(FORM_FILE_NAME,FILE_NAME,CONTENT_TYPE,in);
195
196 File file = new File(mockFile);
197
198 when(getFileService().findFileById(file.getId())).thenReturn(file);
199
200 getController().getFile(file.getId(),request,response);
201
202 assertTrue("Content type of response is not text/plain.",CONTENT_TYPE.equals(response.getContentType()));
203 assertTrue("Response content length does not match file length.",file.getFileSize()==response.getContentLength());
204 String responseText = IOUtils.toString(response.getContentAsByteArray(),CONTENT_ENCODING);
205
206 String fileContent = IOUtils.toString(file.getBytes(),CONTENT_ENCODING);
207 assertTrue("Response content does not match file content.",fileContent.equals(responseText));
208
209 } catch(IOException ioe) {
210 LOG.error(ioe.getLocalizedMessage(),ioe);
211 fail("Could not get file because of an IOException.");
212 } catch( Exception e ) {
213 LOG.error(e.getLocalizedMessage(),e);
214 fail("Could not get file because of an Exception.");
215 }
216 }
217
218 @Test
219 public void testGetFileThrowIOException() {
220 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
221 HttpServletResponse response = mock(HttpServletResponse.class);
222 try {
223 InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_NAME);
224
225 MockMultipartFile mockFile = new MockMultipartFile(FORM_FILE_NAME,FILE_NAME,CONTENT_TYPE,in);
226
227 File file = new File(mockFile);
228
229 when(getFileService().findFileById(file.getId())).thenReturn(file);
230 when(response.getOutputStream()).thenThrow(new IOException());
231
232 getController().getFile(file.getId(),request,response);
233 } catch(IOException ioe) {
234 LOG.error(ioe.getLocalizedMessage(),ioe);
235 fail("Could not get file because of an IOException.");
236 } catch( Exception e ) {
237 LOG.error(e.getLocalizedMessage(),e);
238 fail("Could not get file because of an Exception.");
239 }
240 }
241
242 public FileController getController() {
243 return controller;
244 }
245
246 public void setController(FileController controller) {
247 this.controller = controller;
248 }
249
250 public Properties getKmeProperties() {
251 return kmeProperties;
252 }
253
254 public void setKmeProperties(Properties kmeProperties) {
255 this.kmeProperties = kmeProperties;
256 }
257
258 public FileService getFileService() {
259 return fileService;
260 }
261
262 public void setFileService(FileService fileService) {
263 this.fileService = fileService;
264 }
265 }