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;
8   
9   import org.mule.api.endpoint.EndpointURI;
10  import org.mule.api.endpoint.ImmutableEndpoint;
11  import org.mule.module.client.MuleClient;
12  
13  import java.io.IOException;
14  
15  import org.junit.Test;
16  
17  import static org.junit.Assert.assertFalse;
18  import static org.junit.Assert.assertTrue;
19  
20  public class SftpTempDirFunctionalTestCase extends AbstractSftpTestCase
21  {
22  
23      private static final String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
24      private static final String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
25      private static final String INBOUND_ENDPOINT_NAME2 = "inboundEndpoint2";
26      private static final String OUTBOUND_ENDPOINT_NAME2 = "outboundEndpoint2";
27      private static final String TEMP_DIR = "uploading";
28  
29      @Override
30      protected String getConfigResources()
31      {
32          return "mule-sftp-temp-dir-config.xml";
33      }
34  
35      @Override
36      protected void doSetUp() throws Exception
37      {
38          super.doSetUp();
39  
40          initEndpointDirectory(INBOUND_ENDPOINT_NAME);
41          initEndpointDirectory(OUTBOUND_ENDPOINT_NAME);
42          initEndpointDirectory(INBOUND_ENDPOINT_NAME2);
43          initEndpointDirectory(OUTBOUND_ENDPOINT_NAME2);
44      }
45  
46      @Test
47      public void testTempDirInbound() throws Exception
48      {
49          MuleClient muleClient = new MuleClient(muleContext);
50  
51          DispatchParameters p = new DispatchParameters(INBOUND_ENDPOINT_NAME2, OUTBOUND_ENDPOINT_NAME2);
52          p.setSftpConnector("sftpCustomConnectorTempDirInbound");
53          dispatchAndWaitForDelivery(p);
54  
55          // Verify inbound
56          SftpClient sftpClientInbound = getSftpClient(muleClient, INBOUND_ENDPOINT_NAME2);
57          ImmutableEndpoint endpointInbound = (ImmutableEndpoint) muleClient.getProperty(INBOUND_ENDPOINT_NAME2);
58          try
59          {
60              assertTrue("The temp directory should have been created", tempDirectoryExists(sftpClientInbound,
61                  muleClient, INBOUND_ENDPOINT_NAME2));
62              assertFalse("No file should exist in the temp directory", super.verifyFileExists(
63                  sftpClientInbound, endpointInbound.getEndpointURI().getPath() + "/uploading", FILE_NAME));
64              assertFalse("The file should not exist in the source directory", super.verifyFileExists(
65                  sftpClientInbound, endpointInbound.getEndpointURI(), FILE_NAME));
66          }
67          finally
68          {
69              sftpClientInbound.disconnect();
70          }
71  
72          // Verify outbound
73          SftpClient sftpClientOutbound = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME2);
74          ImmutableEndpoint endpointOutbound = (ImmutableEndpoint) muleClient.getProperty(OUTBOUND_ENDPOINT_NAME2);
75          try
76          {
77              assertFalse("The temp directory should not have been created", tempDirectoryExists(
78                  sftpClientOutbound, muleClient, OUTBOUND_ENDPOINT_NAME2));
79              assertTrue("The file should exist in the final destination : " + FILE_NAME,
80                  super.verifyFileExists(sftpClientOutbound, endpointOutbound.getEndpointURI(), FILE_NAME));
81          }
82          finally
83          {
84              sftpClientOutbound.disconnect();
85          }
86  
87      }
88  
89      @Test
90      public void testTempDirOutbound() throws Exception
91      {
92          MuleClient muleClient = new MuleClient(muleContext);
93  
94          DispatchParameters p = new DispatchParameters(INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME);
95          p.setSftpConnector("sftpCustomConnector");
96          dispatchAndWaitForDelivery(p);
97  
98          // Verify inbound
99          SftpClient sftpClientInbound = getSftpClient(muleClient, INBOUND_ENDPOINT_NAME);
100         ImmutableEndpoint endpointInbound = (ImmutableEndpoint) muleClient.getProperty(INBOUND_ENDPOINT_NAME);
101         try
102         {
103             assertFalse("The temp directory should not have been created", tempDirectoryExists(
104                 sftpClientInbound, muleClient, INBOUND_ENDPOINT_NAME));
105             assertFalse("The file should not exist in the source directory", super.verifyFileExists(
106                 sftpClientInbound, endpointInbound.getEndpointURI(), FILE_NAME));
107         }
108         finally
109         {
110             sftpClientInbound.disconnect();
111         }
112 
113         // Verify outbound
114         SftpClient sftpClientOutbound = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
115         ImmutableEndpoint endpointOutbound = (ImmutableEndpoint) muleClient.getProperty(OUTBOUND_ENDPOINT_NAME);
116         try
117         {
118             assertTrue("The temp directory should have been created", tempDirectoryExists(sftpClientOutbound,
119                 muleClient, OUTBOUND_ENDPOINT_NAME));
120             assertTrue("The file should exist in the final destination", super.verifyFileExists(
121                 sftpClientOutbound, endpointOutbound.getEndpointURI(), FILE_NAME));
122         }
123         finally
124         {
125             sftpClientOutbound.disconnect();
126         }
127     }
128 
129     private boolean tempDirectoryExists(SftpClient sftpClient, MuleClient muleClient, String endpointName)
130         throws IOException
131     {
132         try
133         {
134             EndpointURI endpointURI = getUriByEndpointName(muleClient, endpointName);
135 
136             sftpClient.changeWorkingDirectory(endpointURI.getPath() + "/" + TEMP_DIR);
137             return true;
138         }
139         catch (IOException f)
140         {
141             return false;
142         }
143     }
144 }