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  
13  import java.util.ArrayList;
14  import java.util.Date;
15  import java.util.HashMap;
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 org.apache.commons.io.IOUtils;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  /**
27   * <code>SendReceiveFunctionalTestCase</code> tests sending an receiving multiple
28   * small text files.
29   */
30  public class SftpSendReceiveFunctionalTestCase extends AbstractSftpTestCase
31  {
32  
33      private static final long TIMEOUT = 30000;
34  
35      private ArrayList<String> sendFiles;
36      private ArrayList<String> receiveFiles;
37  
38      private int nrOfFiles = 8;
39  
40      @Override
41      protected String getConfigResources()
42      {
43          return "mule-send-receive-test-config.xml";
44      }
45  
46      @Override
47      protected void doSetUp() throws Exception
48      {
49          super.doSetUp();
50  
51          initEndpointDirectory("inboundEndpoint");
52      }
53  
54      @Test
55      public void testSendAndReceiveSingleFile() throws Exception
56      {
57          sendFiles = new ArrayList<String>();
58  
59          sendFiles.add("file created on " + new Date().getTime());
60  
61          sendAndReceiveFiles();
62      }
63  
64      // Test Mule-1477 (an old VFS Connector issue, but test anyway).
65      @Test
66      public void testSendAndReceiveEmptyFile() throws Exception
67      {
68          sendFiles = new ArrayList<String>();
69  
70          sendFiles.add("");
71  
72          sendAndReceiveFiles();
73      }
74  
75      @Test
76      public void testSendAndReceiveMultipleFiles() throws Exception
77      {
78          sendFiles = new ArrayList<String>();
79  
80          for (int i = 1; i <= nrOfFiles; i++)
81          {
82              sendFiles.add("file" + i);
83          }
84  
85          sendAndReceiveFiles();
86      }
87  
88      protected void sendAndReceiveFiles() throws Exception
89      {
90          final CountDownLatch latch = new CountDownLatch(sendFiles.size());
91          final AtomicInteger loopCount = new AtomicInteger(0);
92  
93          MuleClient client = new MuleClient(muleContext);
94          assertTrue("muleContext is not started", muleContext.isStarted());
95          receiveFiles = new ArrayList<String>();
96  
97          EventCallback callback = new EventCallback()
98          {
99              public void eventReceived(MuleEventContext context, Object component) throws Exception
100             {
101                 logger.info("called " + loopCount.incrementAndGet() + " times");
102 
103                 SftpInputStream inputStream = (SftpInputStream) context.getMessage().getPayload();
104                 String o = IOUtils.toString(inputStream);
105                 if (sendFiles.contains(o))
106                 {
107                     receiveFiles.add(o);
108                 }
109                 else
110                 {
111                     fail("The received file was not sent. Received: '" + o + "'");
112                 }
113 
114                 latch.countDown();
115                 inputStream.close();
116             }
117         };
118 
119         getFunctionalTestComponent("receiving").setEventCallback(callback);
120 
121         for (String sendFile : sendFiles)
122         {
123             HashMap<String, String> props = new HashMap<String, String>(1);
124             props.put(SftpConnector.PROPERTY_FILENAME, sendFile + ".txt");
125             client.dispatch("vm://test.upload", sendFile, props);
126         }
127 
128         latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
129 
130         logger.debug("Number of files sent: " + sendFiles.size());
131         logger.debug("Number of files received: " + receiveFiles.size());
132 
133         // This makes sure we received the same number of files we sent, and that
134         // the content was a match (since only matched content gets on the
135         // receiveFiles ArrayList)
136         assertTrue("expected " + sendFiles.size() + " but got " + receiveFiles.size(),
137             sendFiles.size() == receiveFiles.size());
138     }
139 }