View Javadoc

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