1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.sftp;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.ResourceBundle;
23
24 import org.junit.Test;
25 import org.junit.runners.Parameterized.Parameters;
26 import org.mule.module.client.MuleClient;
27
28
29
30
31 public class SftpArchiveFunctionalTestCase extends AbstractSftpTestCase
32 {
33
34 private static final long TIMEOUT = 15000;
35
36 private static final String FILE1_TXT = "file1.txt";
37 private static final String FILE2_TXT = "file2.txt";
38 private static final String FILE3_TXT = "file3.txt";
39 private static final String FILE4_TXT = "file4.txt";
40
41 private static final String FILE2_TMP_REGEXP = "file2_.+";
42 private static final String FILE3_TMP_REGEXP = "file3_.+";
43
44 private static final String TMP_SENDING = "tmp_sending";
45 private static final String TMP_RECEIVING = "tmp_receiving";
46
47 private static final String INBOUND_ENDPOINT1 = "inboundEndpoint1";
48 private static final String INBOUND_ENDPOINT2 = "inboundEndpoint2";
49 private static final String INBOUND_ENDPOINT3 = "inboundEndpoint3";
50 private static final String INBOUND_ENDPOINT4 = "inboundEndpoint4";
51
52
53 final static int SEND_SIZE = 1024 * 1024 * 2;
54
55 private String archive = null;
56 private String archiveCanonicalPath = null;
57
58 public SftpArchiveFunctionalTestCase(ConfigVariant variant, String configResources)
59 {
60 super(variant, configResources);
61 }
62
63 @Parameters
64 public static Collection<Object[]> parameters()
65 {
66 return Arrays.asList(new Object[][]{
67 {ConfigVariant.SERVICE, "mule-sftp-archive-test-config-service.xml"},
68 {ConfigVariant.FLOW, "mule-sftp-archive-test-config-flow.xml"}});
69 }
70
71 @Override
72 protected void doSetUp() throws Exception
73 {
74 super.doSetUp();
75
76
77
78
79 ResourceBundle rb = ResourceBundle.getBundle("sftp-settings");
80 archive = rb.getString("ARCHIVE");
81 archiveCanonicalPath = new File(archive).getCanonicalPath();
82
83 recursiveDeleteInLocalFilesystem(new File(archive));
84 initEndpointDirectories(new String[]{"receiving1", "receiving2", "receiving3", "receiving4"},
85 new String[]{INBOUND_ENDPOINT1, INBOUND_ENDPOINT2, INBOUND_ENDPOINT3, INBOUND_ENDPOINT4});
86 }
87
88
89
90
91 @Test
92 public void testArchive1() throws Exception
93 {
94 executeBaseTest(INBOUND_ENDPOINT1, "vm://test.upload1", FILE1_TXT, SEND_SIZE, "receiving1", TIMEOUT);
95
96
97 assertFilesInLocalFilesystem(archive, FILE1_TXT);
98
99
100 assertNoFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT1);
101 }
102
103
104
105
106
107 @Test
108 public void testArchive2() throws Exception
109 {
110 executeBaseTest(INBOUND_ENDPOINT2, "vm://test.upload2", FILE2_TXT, SEND_SIZE, "receiving2", TIMEOUT);
111
112
113
114
115 assertFilesInLocalFilesystem(archive, new String[]{TMP_RECEIVING, TMP_SENDING, FILE2_TMP_REGEXP});
116 assertNoFilesInLocalFilesystem(archive + File.separatorChar + TMP_RECEIVING);
117 assertNoFilesInLocalFilesystem(archive + File.separatorChar + TMP_SENDING);
118
119
120
121
122 MuleClient mc = new MuleClient(muleContext);
123 assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT2);
124 assertNoFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT2, TMP_RECEIVING);
125 assertNoFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT2, TMP_SENDING);
126 }
127
128
129
130
131
132 @Test
133 public void testArchive3() throws Exception
134 {
135 executeBaseTest(INBOUND_ENDPOINT3, "vm://test.upload3", FILE3_TXT, SEND_SIZE, "receiving3", TIMEOUT);
136
137
138
139 assertFilesInLocalFilesystem(archive, FILE3_TMP_REGEXP);
140
141
142
143
144 MuleClient mc = new MuleClient(muleContext);
145 assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT3);
146 assertNoFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT3, TMP_RECEIVING);
147 assertNoFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT3, TMP_SENDING);
148 }
149
150
151
152
153
154 @Test
155 public void testArchive4() throws Exception
156 {
157 executeBaseTest(INBOUND_ENDPOINT4, "vm://test.upload4", FILE4_TXT, SEND_SIZE, "receiving4", TIMEOUT);
158
159
160
161 assertFilesInLocalFilesystem(archive, new String[]{TMP_RECEIVING, TMP_SENDING, FILE4_TXT});
162 assertNoFilesInLocalFilesystem(archive + File.separatorChar + TMP_RECEIVING);
163 assertNoFilesInLocalFilesystem(archive + File.separatorChar + TMP_SENDING);
164
165
166 MuleClient mc = new MuleClient(muleContext);
167 assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT4);
168 }
169
170
171
172
173
174 @Test
175 public void testCantWriteToArchive1() throws Exception
176 {
177 makeArchiveReadOnly();
178 try
179 {
180 executeBaseTest(INBOUND_ENDPOINT1, "vm://test.upload1", FILE1_TXT, SEND_SIZE, "receiving1",
181 TIMEOUT, "sftp");
182 fail("Expected error");
183 }
184 catch (Exception e)
185 {
186 assertNotNull(e);
187 assertTrue(e instanceof IOException);
188 assertEquals("Destination folder is not writeable: " + archiveCanonicalPath, e.getMessage());
189 }
190
191
192 assertFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT1, FILE1_TXT);
193
194
195 assertNoFilesInLocalFilesystem(archive);
196 }
197
198
199
200
201
202 @Test
203 public void testCantWriteToArchive2() throws Exception
204 {
205 makeArchiveTmpFolderReadOnly();
206 makeArchiveReadOnly();
207 try
208 {
209 executeBaseTest(INBOUND_ENDPOINT2, "vm://test.upload2", FILE2_TXT, SEND_SIZE, "receiving2",
210 TIMEOUT, "sftp");
211 fail("Expected error");
212 }
213 catch (Exception e)
214 {
215 assertNotNull(e);
216 assertTrue(e instanceof IOException);
217 assertEquals("Destination folder is not writeable: " + archiveCanonicalPath + File.separatorChar
218 + TMP_RECEIVING, e.getMessage());
219 }
220
221
222
223
224
225 MuleClient mc = new MuleClient(muleContext);
226 assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT2);
227 assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT2, TMP_RECEIVING);
228 assertFilesInEndpoint(mc, INBOUND_ENDPOINT2, TMP_SENDING, FILE2_TMP_REGEXP);
229
230
231
232 assertFilesInLocalFilesystem(archive, new String[]{TMP_RECEIVING, TMP_SENDING});
233 }
234
235
236
237
238
239
240 @Test
241 public void testCantWriteToArchive3() throws Exception
242 {
243 makeArchiveReadOnly();
244 try
245 {
246 executeBaseTest(INBOUND_ENDPOINT3, "vm://test.upload3", FILE3_TXT, SEND_SIZE, "receiving3",
247 TIMEOUT, "sftp");
248 fail("Expected error");
249 }
250 catch (Exception e)
251 {
252 assertNotNull(e);
253 assertTrue(e instanceof IOException);
254 assertEquals("Destination folder is not writeable: " + archiveCanonicalPath, e.getMessage());
255 }
256
257
258
259
260
261 MuleClient mc = new MuleClient(muleContext);
262 assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT3);
263 assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT3, TMP_RECEIVING);
264 assertFilesInEndpoint(mc, INBOUND_ENDPOINT3, TMP_SENDING, FILE3_TMP_REGEXP);
265
266
267 assertNoFilesInLocalFilesystem(archive);
268 }
269
270
271
272
273
274 @Test
275 public void testCantWriteToArchive4() throws Exception
276 {
277 makeArchiveTmpFolderReadOnly();
278 makeArchiveReadOnly();
279 try
280 {
281 executeBaseTest(INBOUND_ENDPOINT4, "vm://test.upload4", FILE4_TXT, SEND_SIZE, "receiving4",
282 TIMEOUT, "sftp");
283 fail("Expected error");
284 }
285 catch (Exception e)
286 {
287 assertNotNull(e);
288 assertTrue(e instanceof IOException);
289 assertEquals("Destination folder is not writeable: " + archiveCanonicalPath + File.separatorChar
290 + TMP_RECEIVING, e.getMessage());
291 }
292
293
294 assertFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT4, FILE4_TXT);
295
296
297
298 assertFilesInLocalFilesystem(archive, new String[]{TMP_RECEIVING, TMP_SENDING});
299 }
300
301 private void makeArchiveReadOnly() throws IOException
302 {
303 makeFolderReadOnly(archive);
304 }
305
306 private void makeArchiveTmpFolderReadOnly() throws IOException
307 {
308 makeFolderReadOnly(archive + File.separator + TMP_SENDING);
309 makeFolderReadOnly(archive + File.separator + TMP_RECEIVING);
310 }
311
312 private void makeFolderReadOnly(String folderName) throws IOException
313 {
314 File folder = new File(folderName);
315 if (!folder.exists())
316 {
317 if (!folder.mkdirs())
318 {
319 throw new IOException("Failed to create folder: " + folderName);
320 }
321 }
322 if (!folder.setReadOnly())
323 {
324 throw new IOException("Failed to make folder readonly: " + folderName);
325 }
326 }
327
328 }