View Javadoc

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