View Javadoc

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