1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.sftp;
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.util.Arrays;
18 import java.util.Collection;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.concurrent.CountDownLatch;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.atomic.AtomicInteger;
24 import java.util.concurrent.atomic.AtomicReference;
25
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Test;
28 import org.junit.runners.Parameterized.Parameters;
29 import org.mule.api.MuleEventContext;
30 import org.mule.module.client.MuleClient;
31 import org.mule.tck.functional.EventCallback;
32 import org.mule.tck.functional.FunctionalTestComponent;
33
34
35
36
37
38
39
40 public class SftpFileAgeFunctionalTestCase extends AbstractSftpTestCase
41 {
42 private static final String INBOUND_ENDPOINT_NAME = "inboundEndpoint";
43
44 protected static final long TIMEOUT = 10000 * 6;
45
46 public SftpFileAgeFunctionalTestCase(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-sftp-file-age-config-service.xml"},
55 {ConfigVariant.FLOW, "mule-sftp-file-age-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 testFileAge() throws Exception
68 {
69 final CountDownLatch latch = new CountDownLatch(1);
70 final AtomicReference<String> message = new AtomicReference<String>();
71 final AtomicInteger loopCount = new AtomicInteger(0);
72
73 EventCallback callback = new EventCallback()
74 {
75 @Override
76 public synchronized void eventReceived(MuleEventContext context, Object component)
77 {
78 try
79 {
80 logger.info("called " + loopCount.incrementAndGet() + " times");
81
82 if (1 == latch.getCount())
83 {
84 String o = IOUtils.toString((SftpInputStream) context.getMessage().getPayload());
85 message.set(o);
86 latch.countDown();
87 }
88 }
89 catch (Exception e)
90 {
91 logger.error(e.getMessage(), e);
92 }
93 }
94 };
95
96 MuleClient client = new MuleClient(muleContext);
97
98
99
100
101 Object component = getComponent("testComponent");
102 assertTrue("FunctionalTestComponent expected", component instanceof FunctionalTestComponent);
103 FunctionalTestComponent ftc = (FunctionalTestComponent) component;
104 assertNotNull(ftc);
105
106 ftc.setEventCallback(callback);
107
108
109 Map<?, ?> properties = new HashMap<Object, Object>();
110
111
112 long startTime = System.currentTimeMillis();
113
114 logger.debug("before dispatch");
115
116
117 client.dispatch(getAddressByEndpoint(client, INBOUND_ENDPOINT_NAME), TEST_MESSAGE, properties);
118 logger.debug("before retrieve");
119
120 latch.await(TIMEOUT, TimeUnit.MILLISECONDS);
121
122
123
124
125 long time = System.currentTimeMillis() - startTime;
126
127 int maxTimeDiff = 1000;
128
129
130
131 int expectedMinTime = 2000 - maxTimeDiff;
132 assertTrue("The total time should never be less the 'fileAge' ms (was " + time + ", expected "
133 + expectedMinTime + ")", time > expectedMinTime);
134
135 assertEquals(TEST_MESSAGE, message.get());
136 }
137 }