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.xmpp;
8   
9   import org.mule.api.MuleMessage;
10  import org.mule.module.client.MuleClient;
11  import org.mule.tck.functional.FunctionalTestComponent;
12  import org.mule.transport.NullPayload;
13  import org.mule.transport.xmpp.JabberSender.Callback;
14  import org.mule.util.concurrent.Latch;
15  
16  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
17  import org.jivesoftware.smack.packet.Message;
18  import org.jivesoftware.smack.packet.Packet;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  public class XmppMessageSyncTestCase extends AbstractXmppTestCase
26  {
27      
28      protected static final long JABBER_SEND_THREAD_SLEEP_TIME = 1000;
29      private static final String RECEIVE_SERVICE_NAME = "receiveFromJabber";
30          
31      @Override
32      protected String getXmppConfigResources()
33      {
34          return "xmpp-message-sync-config.xml";
35      }
36  
37      @Test
38      public void testSendSync() throws Exception
39      {   
40          MuleClient client = new MuleClient(muleContext);
41          MuleMessage reply = client.send("vm://in", TEST_MESSAGE, null);
42          assertNotNull(reply);
43          assertEquals(NullPayload.getInstance(), reply.getPayload());
44          
45          Packet packet = jabberClient.receive(RECEIVE_TIMEOUT);
46          assertReceivedPacketEqualsMessageSent(packet);
47      }
48      
49      @Test
50      public void testReceiveSync() throws Exception
51      {
52          startService(RECEIVE_SERVICE_NAME);
53          
54          Latch receiveLatch = new Latch();
55          setupTestServiceComponent(receiveLatch);
56          
57          sendJabberMessageFromNewThread();
58          assertTrue(receiveLatch.await(RECEIVE_TIMEOUT, TimeUnit.MILLISECONDS));
59      }
60  
61      private void setupTestServiceComponent(Latch receiveLatch) throws Exception
62      {   
63          Object testComponent = getComponent(RECEIVE_SERVICE_NAME);
64          assertTrue(testComponent instanceof FunctionalTestComponent);
65          FunctionalTestComponent component = (FunctionalTestComponent) testComponent;
66          
67          XmppCallback callback = new XmppCallback(receiveLatch, expectedXmppMessageType());
68          component.setEventCallback(callback);
69      }
70  
71      @Test
72      public void testRequestSync() throws Exception
73      {
74          doTestRequest("xmpp://MESSAGE/mule2@localhost?exchangePattern=request-response");
75      }
76      
77      protected void doTestRequest(String url) throws Exception
78      {
79          sendJabberMessageFromNewThread();
80  
81          MuleClient client = new MuleClient(muleContext);
82          MuleMessage muleMessage = client.request(url, RECEIVE_TIMEOUT);
83          assertNotNull(muleMessage);
84  
85          Message xmppMessage = (Message) muleMessage.getPayload();
86          assertEquals(expectedXmppMessageType(), xmppMessage.getType());
87          assertEquals(TEST_MESSAGE, xmppMessage.getBody());
88      }
89  
90      protected Message.Type expectedXmppMessageType()
91      {
92          return Message.Type.normal;
93      }
94  
95      protected void sendJabberMessageFromNewThread()
96      {
97          JabberSender sender = new JabberSender(new Callback()
98          {
99              public void doit() throws Exception
100             {
101                 Thread.sleep(JABBER_SEND_THREAD_SLEEP_TIME);
102                 jabberClient.sendMessage(muleJabberUserId, TEST_MESSAGE);
103             }
104         });
105         startSendThread(sender);
106     }
107 }