View Javadoc

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