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.MuleEventContext;
10  import org.mule.api.MuleMessage;
11  import org.mule.module.client.MuleClient;
12  import org.mule.tck.functional.EventCallback;
13  import org.mule.tck.functional.FunctionalTestComponent;
14  import org.mule.transport.NullPayload;
15  import org.mule.transport.xmpp.JabberSender.Callback;
16  import org.mule.util.UUID;
17  import org.mule.util.concurrent.Latch;
18  
19  import java.util.Properties;
20  
21  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
22  import org.jivesoftware.smack.packet.Message;
23  import org.jivesoftware.smack.packet.Packet;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertNotNull;
28  import static org.junit.Assert.assertTrue;
29  
30  public class XmppMucSyncTestCase extends AbstractXmppTestCase
31  {
32      
33      protected static final long JABBER_SEND_THREAD_SLEEP_TIME = 1000;
34      private static final String RECEIVE_SERVICE_NAME = "receiveFromJabber";
35      private static final long SHORT_RETRIEVE_TIMEOUT = 100;
36      
37      private final String testMessage = UUID.getUUID().toString();
38      
39      @Override
40      protected void doSetUp() throws Exception
41      {
42          super.doSetUp();
43  
44          Properties properties = (Properties) muleContext.getRegistry().lookupObject("properties");
45          String chatroom = properties.getProperty("chatroom");
46          assertNotNull(chatroom);
47          
48          jabberClient.joinGroupchat(chatroom);
49      }
50  
51      @Override
52      protected String getXmppConfigResources()
53      {
54          return "xmpp-muc-sync-config.xml";
55      }
56  
57      @Test
58      public void testSendSync() throws Exception
59      {           
60          MuleClient client = new MuleClient(muleContext);
61          MuleMessage reply = client.send("vm://in", testMessage, null);
62          assertNotNull(reply);
63          
64          assertEquals(NullPayload.getInstance(), reply.getPayload());
65          
66          Packet packet = jabberClient.receive(RECEIVE_TIMEOUT);
67          // The groupchat may have a backlog of messages whis is sent before our input is transmitted.
68          // Poll the entire groupchat history
69          boolean inputSeen = false;
70          packet = jabberClient.receive(SHORT_RETRIEVE_TIMEOUT);
71          while (packet != null)
72          {
73              String payload = ((Message) packet).getBody();
74              if (payload.equals(testMessage))
75              {
76                  inputSeen = true;
77                  break;
78              }
79  
80              packet = jabberClient.receive(SHORT_RETRIEVE_TIMEOUT);
81          }
82          assertTrue(inputSeen);
83      }
84      
85      @Test
86      public void testReceiveSync() throws Exception
87      {
88          startService(RECEIVE_SERVICE_NAME);
89  
90          Latch receiveLatch = new Latch();
91          setupTestServiceComponent(receiveLatch);
92          
93          sendJabberMessageFromNewThread();
94          assertTrue(receiveLatch.await(60, TimeUnit.SECONDS));
95      }
96      
97      private void setupTestServiceComponent(Latch receiveLatch) throws Exception
98      {   
99          Object testComponent = getComponent(RECEIVE_SERVICE_NAME);
100         assertTrue(testComponent instanceof FunctionalTestComponent);
101         FunctionalTestComponent component = (FunctionalTestComponent) testComponent;
102         
103         XmppGroupchatCallback callback = new XmppGroupchatCallback(receiveLatch);
104         component.setEventCallback(callback);
105     }
106     
107     protected Message.Type expectedXmppMessageType()
108     {
109         return Message.Type.groupchat;
110     }
111     
112     protected void sendJabberMessageFromNewThread()
113     {
114         JabberSender sender = new JabberSender(new Callback()
115         {
116             public void doit() throws Exception
117             {
118                 Thread.sleep(JABBER_SEND_THREAD_SLEEP_TIME);
119                 jabberClient.sendGroupchatMessage(testMessage);
120             }
121         });
122         startSendThread(sender);
123     }
124     
125     private class XmppGroupchatCallback implements EventCallback
126     {
127         private Latch latch;
128         
129         public XmppGroupchatCallback(Latch latch)
130         {
131             super();
132             this.latch = latch;
133         }
134         
135         public void eventReceived(MuleEventContext context, Object component) throws Exception
136         {
137             MuleMessage muleMessage = context.getMessage();
138             Object payload = muleMessage.getPayload();
139             assertTrue(payload instanceof Message);
140             
141             Message jabberMessage = (Message) payload;
142             if (jabberMessage.getBody().equals(testMessage))
143             {
144                 latch.countDown();
145             }
146         }
147     }
148 }