View Javadoc

1   /*
2    * $Id: SftpCantCreateTempDirectoryTestCase.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.dataintegrity;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import java.io.IOException;
18  import java.util.Arrays;
19  import java.util.Collection;
20  
21  import org.junit.Test;
22  import org.junit.runners.Parameterized.Parameters;
23  import org.mule.api.transport.DispatchException;
24  import org.mule.module.client.MuleClient;
25  import org.mule.transport.sftp.SftpClient;
26  
27  /**
28   * Tests that files are not deleted if the temp directory can't be created
29   */
30  public class SftpCantCreateTempDirectoryTestCase extends AbstractSftpDataIntegrityTestCase
31  {
32      private static String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
33      private static String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
34  
35      public SftpCantCreateTempDirectoryTestCase(ConfigVariant variant, String configResources)
36      {
37          super(variant, configResources);
38      }
39      
40      @Parameters
41      public static Collection<Object[]> parameters()
42      {
43          return Arrays.asList(new Object[][]{
44              {ConfigVariant.SERVICE, "dataintegrity/sftp-dataintegrity-common-with-tempdir-config-service.xml"},
45              {ConfigVariant.FLOW, "dataintegrity/sftp-dataintegrity-common-with-tempdir-config-flow.xml"}
46          });
47      }
48  
49      @Override
50      protected void doSetUp() throws Exception
51      {
52          super.doSetUp();
53  
54          // Delete the in & outbound directories
55          initEndpointDirectory(INBOUND_ENDPOINT_NAME);
56          initEndpointDirectory(OUTBOUND_ENDPOINT_NAME);
57      }
58  
59      /**
60       * No write access on the outbound directory and thus the TEMP directory cant be
61       * created. The source file should still exist
62       *
63       * @throws Exception If an error occurred
64       */
65      @Test
66      public void testCantCreateTempDirectory() throws Exception
67      {
68          MuleClient muleClient = new MuleClient(muleContext);
69  
70          SftpClient sftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
71  
72          try
73          {
74              // change the chmod to "dr-x------" on the outbound-directory
75              // --> the temp directory should not be able to be created
76              remoteChmod(muleClient, sftpClient, OUTBOUND_ENDPOINT_NAME, 00500);
77  
78              // Send an file to the SFTP server, which the inbound-outboundEndpoint
79              // then can pick up
80              // Expect an error, permission denied
81              Exception exception = dispatchAndWaitForException(new DispatchParameters(INBOUND_ENDPOINT_NAME,
82                  OUTBOUND_ENDPOINT_NAME), "sftp", "service");
83              assertNotNull(exception);
84              assertTrue("expected DispatchException, but got : " + exception.getClass().toString(),
85                  exception instanceof DispatchException);
86              assertTrue("expected IOException, but got : " + exception.getCause().getClass().toString(),
87                  exception.getCause() instanceof IOException);
88              assertEquals("Could not create the directory 'uploading', caused by: Permission denied",
89                  exception.getCause().getMessage());
90  
91              verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME, true, false);
92          }
93          finally
94          {
95              sftpClient.disconnect();
96          }
97      }
98  
99      /**
100      * The same test as above, but with the difference that this time it should be
101      * okay to create the directory, and the file should be gone from the inbound
102      * directory.
103      */
104     // Works, but this is more or less the same test as SftpTempDirFunctionalTestCase
105     // so don't use this
106     // @Test
107     //public void testCanCreateTempDirectory() throws Exception
108     // {
109     // MuleClient muleClient = new MuleClient();
110     //
111     // dispatchAndWaitForDelivery(new DispatchParameters(INBOUND_ENDPOINT_NAME,
112     // OUTBOUND_ENDPOINT_NAME));
113     //
114     // verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME,
115     // false, true);
116     // }
117 
118 }