1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.xmpp;
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.concurrent.TimeUnit;
20
21 import org.jivesoftware.smack.packet.Message;
22 import org.jivesoftware.smack.packet.Packet;
23 import org.junit.Test;
24 import org.junit.runners.Parameterized.Parameters;
25 import org.mule.api.MuleMessage;
26 import org.mule.module.client.MuleClient;
27 import org.mule.tck.functional.FunctionalTestComponent;
28 import org.mule.transport.NullPayload;
29 import org.mule.transport.xmpp.JabberSender.Callback;
30 import org.mule.util.concurrent.Latch;
31
32 public class XmppMessageSyncTestCase extends AbstractXmppTestCase
33 {
34
35 protected static final long JABBER_SEND_THREAD_SLEEP_TIME = 1000;
36 private static final String RECEIVE_SERVICE_NAME = "receiveFromJabber";
37
38 public XmppMessageSyncTestCase(ConfigVariant variant, String configResources)
39 {
40 super(variant, configResources);
41 }
42
43 @Parameters
44 public static Collection<Object[]> parameters()
45 {
46 return Arrays.asList(new Object[][]{
47 {ConfigVariant.SERVICE, AbstractXmppTestCase.COMMON_CONFIG + "," + "xmpp-message-sync-config-service.xml"},
48 {ConfigVariant.FLOW, AbstractXmppTestCase.COMMON_CONFIG + "," + "xmpp-message-sync-config-flow.xml"}
49 });
50 }
51
52 @Test
53 public void testSendSync() throws Exception
54 {
55 MuleClient client = new MuleClient(muleContext);
56 MuleMessage reply = client.send("vm://in", TEST_MESSAGE, null);
57 assertNotNull(reply);
58 assertEquals(NullPayload.getInstance(), reply.getPayload());
59
60 Packet packet = jabberClient.receive(RECEIVE_TIMEOUT);
61 assertReceivedPacketEqualsMessageSent(packet);
62 }
63
64 @Test
65 public void testReceiveSync() throws Exception
66 {
67 startService(RECEIVE_SERVICE_NAME);
68
69 Latch receiveLatch = new Latch();
70 setupTestServiceComponent(receiveLatch);
71
72 sendJabberMessageFromNewThread();
73 assertTrue(receiveLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
74 }
75
76 private void setupTestServiceComponent(Latch receiveLatch) throws Exception
77 {
78 Object testComponent = getComponent(RECEIVE_SERVICE_NAME);
79 assertTrue(testComponent instanceof FunctionalTestComponent);
80 FunctionalTestComponent component = (FunctionalTestComponent) testComponent;
81
82 XmppCallback callback = new XmppCallback(receiveLatch, expectedXmppMessageType());
83 component.setEventCallback(callback);
84 }
85
86 @Test
87 public void testRequestSync() throws Exception
88 {
89 doTestRequest("xmpp://MESSAGE/mule2@localhost?exchangePattern=request-response");
90 }
91
92 protected void doTestRequest(String url) throws Exception
93 {
94 sendJabberMessageFromNewThread();
95
96 MuleClient client = new MuleClient(muleContext);
97 MuleMessage muleMessage = client.request(url, RECEIVE_TIMEOUT);
98 assertNotNull(muleMessage);
99
100 Message xmppMessage = (Message) muleMessage.getPayload();
101 assertEquals(expectedXmppMessageType(), xmppMessage.getType());
102 assertEquals(TEST_MESSAGE, xmppMessage.getBody());
103 }
104
105 protected Message.Type expectedXmppMessageType()
106 {
107 return Message.Type.normal;
108 }
109
110 protected void sendJabberMessageFromNewThread()
111 {
112 JabberSender sender = new JabberSender(new Callback()
113 {
114 public void doit() throws Exception
115 {
116 Thread.sleep(JABBER_SEND_THREAD_SLEEP_TIME);
117 jabberClient.sendMessage(muleJabberUserId, TEST_MESSAGE);
118 }
119 });
120 startSendThread(sender);
121 }
122 }