View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.sftp.dataintegrity;
8   
9   import org.mule.api.endpoint.ImmutableEndpoint;
10  import org.mule.api.transport.DispatchException;
11  import org.mule.module.client.MuleClient;
12  import org.mule.transport.sftp.SftpClient;
13  
14  import java.io.IOException;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertNotNull;
19  import static org.junit.Assert.assertTrue;
20  
21  /**
22   * Verify that the original file is not lost if the outbound directory doesn't exist
23   */
24  public class SftpNoOutboundDirectoryTestCase extends AbstractSftpDataIntegrityTestCase
25  {
26  
27      private static final String ENDPOINT_NAME = "inboundEndpoint";
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "dataintegrity/sftp-no-outbound-directory-config.xml";
33      }
34  
35      @Override
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39  
40          // Delete the in & outbound directories
41          initEndpointDirectory(ENDPOINT_NAME);
42      }
43  
44      /**
45       * The outbound directory doesn't exist. The source file should still exist
46       */
47      @Test
48      public void testNoOutboundDirectory() throws Exception
49      {
50          MuleClient muleClient = new MuleClient(muleContext);
51  
52          // Send an file to the SFTP server, which the inbound-outboundEndpoint then
53          // can pick up
54          Exception exception = dispatchAndWaitForException(new DispatchParameters(ENDPOINT_NAME, null),
55              "sftp", "service");
56          assertNotNull(exception);
57  
58          assertTrue("expected DispatchException, but got " + exception.getClass().toString(),
59              exception instanceof DispatchException);
60          assertTrue("expected IOException, but got " + exception.getCause().getClass().toString(),
61              exception.getCause() instanceof IOException);
62          assertTrue("wrong starting message : " + exception.getCause().getMessage(), exception.getCause()
63              .getMessage()
64              .startsWith("Error 'No such file' occurred when trying to CDW to '"));
65          assertTrue("wrong ending message : " + exception.getCause().getMessage(), exception.getCause()
66              .getMessage()
67              .endsWith("/DIRECTORY-MISSING'."));
68  
69          SftpClient sftpClient = getSftpClient(muleClient, ENDPOINT_NAME);
70          try
71          {
72              ImmutableEndpoint endpoint = (ImmutableEndpoint) muleClient.getProperty(ENDPOINT_NAME);
73              assertTrue("The inbound file should still exist", super.verifyFileExists(sftpClient,
74                  endpoint.getEndpointURI(), FILE_NAME));
75          }
76          finally
77          {
78              sftpClient.disconnect();
79          }
80      }
81  
82  }