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.MuleEventContext;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.functional.EventCallback;
12  import org.mule.tck.functional.FunctionalTestComponent;
13  
14  import java.util.HashMap;
15  import java.util.Map;
16  
17  import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
18  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
19  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
20  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicReference;
21  import org.apache.commons.io.IOUtils;
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  /**
31   * <code>LargeFileReceiveFunctionalTestCase</code> tests receiving a large file
32   * message from an sftp service.
33   * 
34   * @author Lennart H��ggkvist
35   */
36  public class SftpIdentityFileFunctionalTestCase extends AbstractSftpTestCase
37  {
38      private static final Log logger = LogFactory.getLog(SftpIdentityFileFunctionalTestCase.class);
39  
40      private static final int DEFAULT_TIMEOUT = 10000;
41  
42      // Increase this to be a little larger than expected download time
43      private static final String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
44  
45      @Override
46      protected String getConfigResources()
47      {
48          return "mule-sftp-identity-file-config.xml";
49      }
50  
51      @Override
52      protected void doSetUp() throws Exception
53      {
54          super.doSetUp();
55  
56          initEndpointDirectory(INBOUND_ENDPOINT_NAME);
57      }
58  
59      // Downloads large file in the remote directory specified in config
60      @Test
61      public void testIdentityFile() throws Exception
62      {
63          final CountDownLatch latch = new CountDownLatch(1);
64          final AtomicReference message = new AtomicReference();
65          final AtomicInteger loopCount = new AtomicInteger(0);
66  
67          EventCallback callback = new EventCallback()
68          {
69              public synchronized void eventReceived(MuleEventContext context, Object component)
70              {
71                  try
72                  {
73                      logger.info("called " + loopCount.incrementAndGet() + " times");
74                      // without this we may have problems with the many repeats
75                      if (1 == latch.getCount())
76                      {
77                          String o = IOUtils.toString((SftpInputStream) context.getMessage().getPayload());
78                          message.set(o);
79                          latch.countDown();
80                      }
81                  }
82                  catch (Exception e)
83                  {
84                      logger.error(e.getMessage(), e);
85                  }
86              }
87          };
88  
89          MuleClient client = new MuleClient(muleContext);
90  
91          // Ensure that no other files exists
92          // cleanupRemoteFtpDirectory(client, INBOUND_ENDPOINT_NAME);
93  
94          Map properties = new HashMap();
95          // properties.put("filename", "foo.bar");
96  
97          Object component = getComponent("testComponent");
98          assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
99          FunctionalTestComponent ftc = (FunctionalTestComponent) component;
100         assertNotNull(ftc);
101 
102         ftc.setEventCallback(callback);
103 
104         logger.debug("before dispatch");
105         // Send an file to the SFTP server, which the inbound-endpoint then can pick
106         // up
107         client.dispatch(getAddressByEndpoint(client, INBOUND_ENDPOINT_NAME), TEST_MESSAGE, properties);
108         logger.debug("before retrieve");
109 
110         latch.await(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
111 
112         assertEquals(TEST_MESSAGE, message.get());
113     }
114 
115 }