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.dataintegrity;
8   
9   import org.mule.api.endpoint.ImmutableEndpoint;
10  import org.mule.api.transport.DispatchException;
11  import org.mule.module.client.MuleClient;
12  import org.mule.transport.sftp.SftpClient;
13  
14  import java.io.IOException;
15  
16  import org.junit.Test;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  
22  /**
23   * Test the three different types of handling when duplicate files (i.e. file names)
24   * are being transferred by SftpTransport. Available duplicate handling types are: -
25   * SftpConnector.PROPERTY_DUPLICATE_HANDLING_THROW_EXCEPTION = "throwException" -
26   * SftpConnectorPROPERTY_DUPLICATE_HANDLING_OVERWRITE = "overwrite" (currently not
27   * implemented) - SftpConnector.PROPERTY_DUPLICATE_HANDLING_ASS_SEQ_NO = "addSeqNo"
28   */
29  public class SftpCheckDuplicateFileHandlingTestCase extends AbstractSftpDataIntegrityTestCase
30  {
31  
32      private static String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
33      private static String OUTBOUND_ENDPOINT_NAME = "outboundEndpoint";
34  
35      private static String INBOUND_ENDPOINT_NAME2 = "inboundEndpoint2";
36      private static String OUTBOUND_ENDPOINT_NAME2 = "outboundEndpoint2";
37  
38      @Override
39      protected String getConfigResources()
40      {
41          return "dataintegrity/sftp-dataintegrity-duplicate-handling.xml";
42      }
43  
44      @Override
45      protected void doSetUp() throws Exception
46      {
47          initEndpointDirectories(new String[]{"serviceDuplicateHandlingRename",
48              "serviceDuplicateHandlingThrowException"}, new String[]{INBOUND_ENDPOINT_NAME,
49              INBOUND_ENDPOINT_NAME2, OUTBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME2});
50  
51          muleContext.setExceptionListener(new org.mule.transport.sftp.notification.ExceptionListener());
52      }
53  
54      /**
55       * Try to transfer two files with the same name. The second file will be given a
56       * new name.
57       */
58      @Test
59      public void testDuplicateChangeNameHandling() throws Exception
60      {
61  
62          MuleClient muleClient = new MuleClient(muleContext);
63          SftpClient sftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME);
64  
65          try
66          {
67  
68              // Send a file to the SFTP server, which the inbound-outboundEndpoint
69              // then can pick up
70              dispatchAndWaitForDelivery(new DispatchParameters(INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME));
71  
72              // Make sure the file exists only in the outbound endpoint
73              verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME, false, true);
74  
75              // Transfer the second file
76              dispatchAndWaitForDelivery(new DispatchParameters(INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME));
77  
78              // Make sure a file still exists only in the outbound endpoint
79              verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME, OUTBOUND_ENDPOINT_NAME, false, true);
80  
81              // Make sure a new file with name according to the notation is created
82              ImmutableEndpoint endpoint = (ImmutableEndpoint) muleClient.getProperty(OUTBOUND_ENDPOINT_NAME);
83              assertTrue("A new file in the outbound endpoint should exist", verifyFileExists(sftpClient,
84                  endpoint.getEndpointURI().getPath(), "file_1.txt"));
85  
86          }
87          finally
88          {
89              sftpClient.disconnect();
90          }
91      }
92  
93      /**
94       * Test transferring a duplicate file. The default handling of duplicates is to
95       * throw an exception.
96       */
97      @Test
98      public void testDuplicateDefaultExceptionHandling() throws Exception
99      {
100 
101         MuleClient muleClient = new MuleClient(muleContext);
102         SftpClient sftpClient = getSftpClient(muleClient, OUTBOUND_ENDPOINT_NAME2);
103 
104         try
105         {
106 
107             // Send an file to the SFTP server, which the inbound-outboundEndpoint
108             // then can pick up
109             dispatchAndWaitForDelivery(new DispatchParameters(INBOUND_ENDPOINT_NAME2, OUTBOUND_ENDPOINT_NAME2));
110 
111             verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME2, OUTBOUND_ENDPOINT_NAME2, false, true);
112 
113             Exception exception = dispatchAndWaitForException(new DispatchParameters(INBOUND_ENDPOINT_NAME2,
114                 OUTBOUND_ENDPOINT_NAME2), "sftp", "serviceDuplicateHandlingThrowException");
115             assertNotNull(exception);
116             assertTrue(exception instanceof DispatchException);
117             assertTrue(exception.getCause() instanceof IOException);
118             assertEquals("Failure", exception.getCause().getMessage());
119 
120             verifyInAndOutFiles(muleClient, INBOUND_ENDPOINT_NAME2, OUTBOUND_ENDPOINT_NAME2, true, true);
121 
122         }
123         finally
124         {
125             sftpClient.disconnect();
126         }
127     }
128 }