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>SftpFileAgeFunctionalTestCase</code> tests the fileAge functionality.
32   * 
33   * @author Lennart H��ggkvist
34   */
35  
36  public class SftpFileAgeFunctionalTestCase extends AbstractSftpTestCase
37  {
38      
39      private static final Log logger = LogFactory.getLog(SftpFileAgeFunctionalTestCase.class);
40  
41      private static final String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
42  
43      protected static final long TIMEOUT = 10000 * 6;
44  
45      @Override
46      protected String getConfigResources()
47      {
48          return "mule-sftp-file-age-config.xml";
49      }
50  
51      @Override
52      protected void doSetUp() throws Exception
53      {
54          super.doSetUp();
55  
56          initEndpointDirectory("inboundEndpoint");
57      }
58  
59      @Test
60      public void testFileAge() throws Exception
61      {
62          final CountDownLatch latch = new CountDownLatch(1);
63          final AtomicReference message = new AtomicReference();
64          final AtomicInteger loopCount = new AtomicInteger(0);
65  
66          EventCallback callback = new EventCallback()
67          {
68              public synchronized void eventReceived(MuleEventContext context, Object component)
69              {
70                  try
71                  {
72                      logger.info("called " + loopCount.incrementAndGet() + " times");
73                      // without this we may have problems with the many repeats
74                      if (1 == latch.getCount())
75                      {
76                          String o = IOUtils.toString((SftpInputStream) context.getMessage().getPayload());
77                          message.set(o);
78                          latch.countDown();
79                      }
80                  }
81                  catch (Exception e)
82                  {
83                      logger.error(e.getMessage(), e);
84                  }
85              }
86          };
87  
88          MuleClient client = new MuleClient(muleContext);
89  
90          // Ensure that no other files exists
91          // cleanupRemoteFtpDirectory(client, INBOUND_ENDPOINT_NAME);
92  
93          Object component = getComponent("testComponent");
94          assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
95          FunctionalTestComponent ftc = (FunctionalTestComponent) component;
96          assertNotNull(ftc);
97  
98          ftc.setEventCallback(callback);
99  
100         // Use one specific filename so that the file is overwritten if necessarily
101         Map properties = new HashMap();
102         // properties.put("filename", "fileage-test.tmp");
103 
104         long startTime = System.currentTimeMillis();
105 
106         logger.debug("before dispatch");
107         // Send an file to the SFTP server, which the inbound-endpoint then can pick
108         // up
109         client.dispatch(getAddressByEndpoint(client, INBOUND_ENDPOINT_NAME), TEST_MESSAGE, properties);
110         logger.debug("before retrieve");
111 
112         latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
113 
114         // We assume that the total time never should be less than fileAge. That
115         // means that the fileAge value
116         // in this test must be rather high
117         long time = System.currentTimeMillis() - startTime;
118 
119         int maxTimeDiff = 1000; // Max time diff between localhost and the server.
120                                 // Ie. the time can differ up to this and the test
121         // will be okay. This is used because localhost/developer machine is not
122         // always synchronized with the server(s)
123         int expectedMinTime = 2000 - maxTimeDiff;
124         assertTrue("The total time should never be less the 'fileAge' ms (was " + time + ", expected "
125                    + expectedMinTime + ")", time > expectedMinTime);
126 
127         assertEquals(TEST_MESSAGE, message.get());
128     }
129 
130 }