View Javadoc

1   /*
2    * $Id: SftpFilterTestCase.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.util.Arrays;
17  import java.util.Collection;
18  import java.util.HashMap;
19  
20  import org.junit.Test;
21  import org.junit.runners.Parameterized.Parameters;
22  import org.mule.api.endpoint.ImmutableEndpoint;
23  import org.mule.module.client.MuleClient;
24  import org.mule.transport.sftp.dataintegrity.AbstractSftpDataIntegrityTestCase;
25  
26  /**
27   * Simple test to verify that the filter configuration works. Note that the transport
28   * uses an "early" filter to improves the performance.
29   */
30  public class SftpFilterTestCase extends AbstractSftpDataIntegrityTestCase
31  {
32      private static String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
33      private static String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
34  
35      public SftpFilterTestCase(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-filter-config-service.xml"},
45              {ConfigVariant.FLOW, "mule-sftp-filter-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      }
57  
58      @Test
59      public void testFilter() throws Exception
60      {
61          MuleClient muleClient = new MuleClient(muleContext);
62  
63          // Send .txt file using muleclient.dipatch directly (since the file won't be
64          // delivered to the endpoint (due to filter settings) we can't wait for a
65          // delivery notification....
66          HashMap<String, String> txtProps = new HashMap<String, String>(1);
67          txtProps.put(SftpConnector.PROPERTY_FILENAME, FILE_NAME);
68          muleClient.dispatch(getAddressByEndpoint(muleClient, INBOUND_ENDPOINT_NAME), TEST_MESSAGE, txtProps);
69  
70          // Send .xml file
71          DispatchParameters dp = new DispatchParameters(INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME);
72          dp.setFilename("file.xml");
73          dispatchAndWaitForDelivery(dp);
74  
75          SftpClient outboundSftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
76          ImmutableEndpoint outboundEndpoint = (ImmutableEndpoint) muleClient.getProperty(OUTBOUND_ENDPOINT_NAME);
77  
78          SftpClient inboundSftpClient = getSftpClient(muleClient, INBOUND_ENDPOINT_NAME);
79          ImmutableEndpoint inboundEndpoint = (ImmutableEndpoint) muleClient.getProperty(INBOUND_ENDPOINT_NAME);
80  
81          assertFalse("The xml file should not be left in the inbound directory", verifyFileExists(
82              inboundSftpClient, inboundEndpoint.getEndpointURI().getPath(), "file.xml"));
83          assertTrue("The xml file should be in the outbound directory", verifyFileExists(outboundSftpClient,
84              outboundEndpoint.getEndpointURI().getPath(), "file.xml"));
85  
86          assertTrue("The txt file should be left in the inbound directory", verifyFileExists(
87              inboundSftpClient, inboundEndpoint.getEndpointURI().getPath(), FILE_NAME));
88          assertFalse("The txt file should not be in the outbound directory", verifyFileExists(
89              outboundSftpClient, outboundEndpoint.getEndpointURI().getPath(), FILE_NAME));
90      }
91  }