View Javadoc

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