View Javadoc

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