View Javadoc

1   /*
2    * $Id: SftpDuplicateHandlingFunctionalTestCase.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.assertTrue;
15  import static org.junit.Assert.fail;
16  
17  import java.util.Arrays;
18  import java.util.Collection;
19  
20  import org.apache.commons.lang.NotImplementedException;
21  import org.junit.Test;
22  import org.junit.runners.Parameterized.Parameters;
23  import org.mule.api.endpoint.ImmutableEndpoint;
24  import org.mule.api.transport.DispatchException;
25  import org.mule.module.client.MuleClient;
26  
27  /**
28   * Test the archive features.
29   */
30  public class SftpDuplicateHandlingFunctionalTestCase extends AbstractSftpTestCase
31  {
32      
33      private static final long TIMEOUT = 10000;
34  
35      // Size of the generated stream - 2 Mb
36      final static int SEND_SIZE = 1024 * 1024 * 2;
37  
38      public SftpDuplicateHandlingFunctionalTestCase(ConfigVariant variant, String configResources)
39      {
40          super(variant, configResources);
41          
42          // Only start mule once for all tests below, save a lot of time..., if test3
43          // starts failing, comment this out
44          setDisposeContextPerClass(true);
45  
46          // Increase the timeout of the test to 300 s
47          logger.info("Timeout was set to: " + System.getProperty(TEST_TIMEOUT_SYSTEM_PROPERTY, "-1"));
48          System.setProperty(TEST_TIMEOUT_SYSTEM_PROPERTY, "300000");
49          logger.info("Timeout is now set to: " + System.getProperty(TEST_TIMEOUT_SYSTEM_PROPERTY, "-1"));
50      }
51      
52      @Parameters
53      public static Collection<Object[]> parameters()
54      {
55          return Arrays.asList(new Object[][]{
56              {ConfigVariant.SERVICE, "mule-sftp-duplicateHandling-test-config-service.xml"},
57              {ConfigVariant.FLOW, "mule-sftp-duplicateHandling-test-config-flow.xml"}
58          });
59      }
60  
61      @Override
62      protected void doSetUp() throws Exception
63      {
64          super.doSetUp();
65  
66          initEndpointDirectory("inboundEndpoint1");
67          initEndpointDirectory("inboundEndpoint2");
68          initEndpointDirectory("inboundEndpoint3");
69          initEndpointDirectory("inboundEndpoint4");
70          // FIXME: disabled due to missing config
71          // initEndpointDirectory("outboundEndpoint5");
72  
73          muleContext.setExceptionListener(new org.mule.transport.sftp.notification.ExceptionListener());
74      }
75  
76      /**
77       * Test 1 - test duplicate handling by throwing an exception
78       */
79      @Test
80      public void testDuplicateHandlingThrowException() throws Exception
81      {
82          // TODO. Add some tests specific to this test, i.e. not only rely on the
83          // tests performed by executeTest().
84  
85          executeBaseTest("inboundEndpoint1", "vm://test.upload1", "file1.txt", SEND_SIZE, "receiving1",
86              TIMEOUT, "sending1");
87      }
88  
89      /**
90       * Test 2 - test duplicate handling by overwriting the existing file Not yet
91       * implemented, so currently we check for a valid exception...
92       */
93      @Test
94      public void testDuplicateHandlingOverwrite() throws Exception
95      {
96          // TODO. Add some tests specific to this test, i.e. not only rely on the
97          // tests performed by executeTest().
98  
99          try
100         {
101             executeBaseTest("inboundEndpoint2", "vm://test.upload2", "file2.txt", SEND_SIZE, "receiving2",
102                 TIMEOUT, "sftp", "sending2");
103             fail("Should have received an Exception");
104         }
105         catch (Exception e)
106         {
107             assertTrue("did not receive DispatchException, got : " + e.getClass().toString(),
108                 e instanceof DispatchException);
109             assertTrue(
110                 "did not receive NotImplementedException, got : " + e.getCause().getClass().toString(),
111                 e.getCause() instanceof NotImplementedException);
112         }
113     }
114 
115     /**
116      * Test 3 - test duplicate handling by adding a sequence number to the new file
117      */
118     @Test
119     public void testDuplicateHandlingAddSeqNo() throws Exception
120     {
121         // TODO. Add some tests specific to this test, i.e. not only rely on the
122         // tests performed by executeTest().
123 
124         executeBaseTest("inboundEndpoint3", "vm://test.upload3", "file3.txt", SEND_SIZE, "receiving3",
125             TIMEOUT, "receiving3");
126     }
127 
128     /**
129      * Test 4 - test duplicate handling by adding a sequence number to the new file
130      * using default value on connector
131      */
132     @Test
133     public void testDuplicateHandlingAddSeqNoUsingConnector() throws Exception
134     {
135         // TODO. Add some tests specific to this test, i.e. not only rely on the
136         // tests performed by executeTest().
137 
138         executeBaseTest("inboundEndpoint4", "vm://test.upload4", "file4.txt", SEND_SIZE, "receiving4",
139             TIMEOUT, "receiving4");
140 
141         MuleClient muleClient = new MuleClient(muleContext);
142         ImmutableEndpoint endpoint = getImmutableEndpoint(muleClient, "send4outbound");
143         SftpUtil util = new SftpUtil(endpoint);
144 
145         assertEquals("The value on the connector should be used", "addSeqNo", util.getDuplicateHandling());
146     }
147 
148     /**
149      * Test 5 - test duplicate handling by adding a sequence number to the new file
150      * without file extension
151      */
152     /*
153      * @Test
154     public void testDuplicateHandlingAddSeqNoWithNoFileExtension() throws
155      * Exception { MuleClient muleClient = new MuleClient(); HashMap<String, String>
156      * txtProps = new HashMap<String, String>(1);
157      * txtProps.put(SftpConnector.PROPERTY_FILENAME, "file5");
158      * muleClient.dispatch("vm://test.upload5", TEST_MESSAGE, txtProps); // TODO:
159      * make a executeBaseTest that doesn't require a FunctionalTestComponent
160      * Thread.sleep(5000); // File #2 muleClient.dispatch("vm://test.upload5",
161      * TEST_MESSAGE, txtProps); Thread.sleep(5000); SftpClient sftpClient = null; try
162      * { sftpClient = getSftpClient(muleClient, "outboundEndpoint5");
163      * ImmutableEndpoint endpoint = (ImmutableEndpoint)
164      * muleClient.getProperty("outboundEndpoint5");
165      * assertTrue("The file should exist in the directory",
166      * verifyFileExists(sftpClient, endpoint.getEndpointURI(), "file5"));
167      * assertTrue("The file should exist in the directory",
168      * verifyFileExists(sftpClient, endpoint.getEndpointURI(), "file5_1")); } finally
169      * { if (sftpClient != null) { sftpClient.disconnect(); } } }
170      */
171 }