View Javadoc

1   /*
2    * $Id: SftpArchiveFunctionalTestCase.java 22475 2011-07-20 14:30:04Z justin.calleja $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
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   * Test the archive features.
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      // Size of the generated stream - 2 Mb
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          // this block moved from the constructor to allow this test to be skipped if
77          // the resource isn't found via
78          // AbstractMuleTestCase.isDisabledInThisEnvironment
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       * Test plain archive functionality with no extra features enabled
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          // Assert that the file now exists in the archive
97          assertFilesInLocalFilesystem(archive, FILE1_TXT);
98  
99          // And that the file is gone from the inbound endpoint
100         assertNoFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT1);
101     }
102 
103     /**
104      * Test archive functionality with full usage of temp-dir and creation of unique
105      * names of temp-files
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         // Assert that the file now exists in the archive
113         // (with some unknown timestamp in the filename)
114         // and that the tmp-archive-folders are empty
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         // Assert that the file is gone from the inbound endpoint (including its
120         // tmp-folders)
121         // Note that directories are not returned in this listing
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      * Test archive functionality with usage of temp-dir for inbound and outbound
130      * endpoints with creation of unique names of temp-files but not for the archive
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         // Assert that the file now exists in the archive
138         // (with some unknown timestamp in the filename)
139         assertFilesInLocalFilesystem(archive, FILE3_TMP_REGEXP);
140 
141         // Assert that the file is gone from the inbound endpoint (including its
142         // tmp-folders)
143         // Note that directories are not returned in this listing
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      * Test archive functionality with usage of temp-dir for archive but not for
152      * inbound and outbound endpoints
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         // Assert that the file now exists in the archive
160         // and that the tmp-archive-folders are empty
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         // Assert that the file is gone from the inbound endpoint
166         MuleClient mc = new MuleClient(muleContext);
167         assertNoFilesInEndpoint(mc, INBOUND_ENDPOINT4);
168     }
169 
170     /**
171      * Test error handling with plain archive functionality with no extra features
172      * enabled
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         // Assert that file still exists in the inbound endpoint after the failure
192         assertFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT1, FILE1_TXT);
193 
194         // Assert that no files exists in the archive after the error
195         assertNoFilesInLocalFilesystem(archive);
196     }
197 
198     /**
199      * Test error handling with archive functionality with full usage of temp-dir and
200      * creation of unique names of temp-files
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         // Assert that the file still exists in the inbound endpoint's tmp-folder
222         // after the failure
223         // (with some unknown timestamp in the filename)
224         // Note that directories are not returned in this listing
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         // Assert that no files exists in the archive after the error except from the
231         // temp-folders
232         assertFilesInLocalFilesystem(archive, new String[]{TMP_RECEIVING, TMP_SENDING});
233     }
234 
235     /**
236      * Test error handling with archive functionality with usage of temp-dir for
237      * inbound and outbound endpoints with creation of unique names of temp-files but
238      * not for the archive
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         // Assert that the file still exists in the inbound endpoint's tmp-folder
258         // after the failure
259         // (with some unknown timestamp in the filename)
260         // Note that directories are not returned in this listing
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         // Assert that no files exists in the archive after the error
267         assertNoFilesInLocalFilesystem(archive);
268     }
269 
270     /**
271      * Test error handling with archive functionality with usage of temp-dir for
272      * archive but not for inbound and outbound endpoints
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         // Assert that file still exists in the inbound endpoint after the failure
294         assertFilesInEndpoint(new MuleClient(muleContext), INBOUND_ENDPOINT4, FILE4_TXT);
295 
296         // Assert that no files exists in the archive after the error except from the
297         // temp-folders
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 }