View Javadoc

1   /*
2    * $Id: SftpCantWriteToFinalDestAfterTempDirectoryTestCase.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.assertFalse;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertTrue;
17  
18  import java.io.IOException;
19  import java.util.Arrays;
20  import java.util.Collection;
21  
22  import org.junit.Test;
23  import org.junit.runners.Parameterized.Parameters;
24  import org.mule.api.endpoint.ImmutableEndpoint;
25  import org.mule.api.transport.DispatchException;
26  import org.mule.module.client.MuleClient;
27  import org.mule.transport.sftp.SftpClient;
28  
29  /**
30   * Tests that files are not deleted if the final destination is not writable when
31   * using a temp directory.
32   */
33  public class SftpCantWriteToFinalDestAfterTempDirectoryTestCase extends AbstractSftpDataIntegrityTestCase
34  {
35  
36      private static String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
37      private static String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
38  
39      public SftpCantWriteToFinalDestAfterTempDirectoryTestCase(ConfigVariant variant, String configResources)
40      {
41          super(variant, configResources);
42      }
43  
44      @Parameters
45      public static Collection<Object[]> parameters()
46      {
47          return Arrays.asList(new Object[][]{
48              {ConfigVariant.SERVICE, "dataintegrity/sftp-dataintegrity-common-with-tempdir-config-service.xml"},
49              {ConfigVariant.FLOW, "dataintegrity/sftp-dataintegrity-common-with-tempdir-config-flow.xml"}});
50      }
51  
52      @Override
53      protected void doSetUp() throws Exception
54      {
55          super.doSetUp();
56  
57          // Delete the in & outbound directories
58          initEndpointDirectory(INBOUND_ENDPOINT_NAME);
59          initEndpointDirectory(OUTBOUND_ENDPOINT_NAME);
60      }
61  
62      /**
63       * No write access on the outbound directory but write access to the TEMP
64       * directory. The source file should still exist and no file should exist in the
65       * TEMP directory.
66       */
67      @Test
68      public void testCantWriteToFinalDestAfterTempDirectory() throws Exception
69      {
70          MuleClient muleClient = new MuleClient(muleContext);
71  
72          // Must create the temp directory before we change the access rights
73          createRemoteDirectory(muleClient, OUTBOUND_ENDPOINT_NAME, "uploading");
74  
75          SftpClient sftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
76  
77          try
78          {
79              // change the chmod to "dr-x------" on the outbound-directory
80              remoteChmod(muleClient, sftpClient, OUTBOUND_ENDPOINT_NAME, 00500);
81  
82              // Send an file to the SFTP server, which the inbound-outboundEndpoint
83              // then can pick up
84              // Expect an error, permission denied
85              Exception exception = dispatchAndWaitForException(new DispatchParameters(INBOUND_ENDPOINT_NAME,
86                  OUTBOUND_ENDPOINT_NAME), "sftp", "service");
87              assertNotNull(exception);
88              assertTrue("did not receive DispatchException, got : " + exception.getClass().toString(),
89                  exception instanceof DispatchException);
90              assertTrue("did not receive IOException, got : " + exception.getCause().getClass().toString(),
91                  exception.getCause() instanceof IOException);
92  
93              assertEquals("Permission denied", exception.getCause().getMessage());
94  
95              verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME, true, false);
96  
97              ImmutableEndpoint endpoint = (ImmutableEndpoint) muleClient.getProperty(OUTBOUND_ENDPOINT_NAME);
98              assertFalse("The inbound file should not be left in the TEMP-dir", super.verifyFileExists(
99                  sftpClient, endpoint.getEndpointURI().getPath() + "/" + TEMP_DIR, FILE_NAME));
100         }
101         finally
102         {
103             sftpClient.disconnect();
104         }
105     }
106 }