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