PageRenderTime 60ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/sip-servlets/sip-servlets-test-suite/testsuite/src/test/java/org/mobicents/servlet/sip/testsuite/targeting/SessionKeyTargetingSipServletTest.java

http://mobicents.googlecode.com/
Java | 292 lines | 190 code | 38 blank | 64 comment | 10 complexity | 1629e2b08f942481b6ed4c9853d6cef6 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.servlet.sip.testsuite.targeting;
  23. import java.text.ParseException;
  24. import java.util.Iterator;
  25. import javax.sip.InvalidArgumentException;
  26. import javax.sip.SipException;
  27. import javax.sip.SipProvider;
  28. import javax.sip.address.SipURI;
  29. import org.apache.log4j.Logger;
  30. import org.mobicents.servlet.sip.SipServletTestCase;
  31. import org.mobicents.servlet.sip.testsuite.ProtocolObjects;
  32. import org.mobicents.servlet.sip.testsuite.TestSipListener;
  33. /**
  34. * This test is aimed to test the @SipApplicationKey annotation and check that this is compliant with JSR 289
  35. *
  36. * @author <A HREF="mailto:jean.deruelle@gmail.com">Jean Deruelle</A>
  37. *
  38. */
  39. public class SessionKeyTargetingSipServletTest extends SipServletTestCase {
  40. private static transient Logger logger = Logger.getLogger(SessionKeyTargetingSipServletTest.class);
  41. private static final String TRANSPORT = "udp";
  42. private static final boolean AUTODIALOG = true;
  43. private static final int TIMEOUT = 10000;
  44. private static final int TIMEOUT_TIMER = 6000;
  45. private static final int CONTAINER_PORT = 5070;
  46. // private static final int TIMEOUT = 100000000;
  47. TestSipListener sender1Chat1;
  48. TestSipListener sender2Chat1;
  49. TestSipListener sender3Chat1;
  50. TestSipListener sender1Chat2;
  51. ProtocolObjects sender1Chat1ProtocolObjects;
  52. ProtocolObjects sender2Chat1ProtocolObjects;
  53. ProtocolObjects sender3Chat1ProtocolObjects;
  54. ProtocolObjects sender1Chat2ProtocolObjects;
  55. public SessionKeyTargetingSipServletTest(String name) {
  56. super(name);
  57. // startTomcatOnStartup = false;
  58. // autoDeployOnStartup = false;
  59. // initTomcatOnStartup = false;
  60. }
  61. @Override
  62. public void deployApplication() {
  63. assertTrue(tomcat.deployContext(
  64. projectHome + "/sip-servlets-test-suite/applications/chatroom-sip-servlet/src/main/sipapp",
  65. "sip-test-context", "sip-test"));
  66. }
  67. @Override
  68. protected String getDarConfigurationFile() {
  69. return "file:///" + projectHome + "/sip-servlets-test-suite/testsuite/src/test/resources/" +
  70. "org/mobicents/servlet/sip/testsuite/targeting/chatroom-sip-servlet-dar.properties";
  71. }
  72. @Override
  73. protected void setUp() throws Exception {
  74. super.setUp();
  75. sender1Chat1ProtocolObjects =new ProtocolObjects(
  76. "sender1Chat1", "gov.nist", TRANSPORT, AUTODIALOG, null, null, null);
  77. sender2Chat1ProtocolObjects =new ProtocolObjects(
  78. "sender2Chat1", "gov.nist", TRANSPORT, AUTODIALOG, null, null, null);
  79. sender3Chat1ProtocolObjects =new ProtocolObjects(
  80. "sender3Chat1", "gov.nist", TRANSPORT, AUTODIALOG, null, null, null);
  81. sender1Chat2ProtocolObjects =new ProtocolObjects(
  82. "sender1Chat2", "gov.nist", TRANSPORT, AUTODIALOG, null, null, null);
  83. sender1Chat1 = new TestSipListener(5071, CONTAINER_PORT, sender1Chat1ProtocolObjects, false);
  84. SipProvider senderProvider = sender1Chat1.createProvider();
  85. senderProvider.addSipListener(sender1Chat1);
  86. sender1Chat1ProtocolObjects.start();
  87. sender2Chat1 = new TestSipListener(5072, CONTAINER_PORT, sender2Chat1ProtocolObjects, false);
  88. SipProvider sender2Provider = sender2Chat1.createProvider();
  89. sender2Provider.addSipListener(sender2Chat1);
  90. sender2Chat1ProtocolObjects.start();
  91. sender3Chat1 = new TestSipListener(5073, CONTAINER_PORT, sender3Chat1ProtocolObjects, false);
  92. SipProvider sender3Provider = sender3Chat1.createProvider();
  93. sender3Provider.addSipListener(sender3Chat1);
  94. sender3Chat1ProtocolObjects.start();
  95. sender1Chat2 = new TestSipListener(5074, CONTAINER_PORT, sender1Chat2ProtocolObjects, false);
  96. SipProvider sender4Provider = sender1Chat2.createProvider();
  97. sender4Provider.addSipListener(sender1Chat2);
  98. sender1Chat2ProtocolObjects.start();
  99. }
  100. /**
  101. * This test check to see if all requests with the same request URI
  102. * "sip:mychatroom1@example.com" will be handled by the same application
  103. * session. (The sip servlet will return the same key for the same request
  104. * URI)
  105. *
  106. * So it starts 4 sip clients. 3 of them will connect to the same request
  107. * Uri and the last one to another request uri. The sip servlet application
  108. * will send back a MESSAGE containing the number of clients connected to
  109. * the same application session upon ACK reception.
  110. *
  111. * The 3 first clients should receive respectively 1, 2 and 3. And the 4th
  112. * client should receive 1.
  113. *
  114. * @throws InterruptedException
  115. * @throws SipException
  116. * @throws ParseException
  117. * @throws InvalidArgumentException
  118. */
  119. public void testSipApplicationSessionTargeting() throws InterruptedException, SipException, ParseException, InvalidArgumentException {
  120. String fromName = "sender1Chat1";
  121. String fromSipAddress = "sip-servlets.com";
  122. SipURI fromAddress = sender1Chat1ProtocolObjects.addressFactory.createSipURI(
  123. fromName, fromSipAddress);
  124. String toUser = "chatroom1";
  125. String toSipAddress = "sip-servlets.com";
  126. SipURI toAddress = sender1Chat1ProtocolObjects.addressFactory.createSipURI(
  127. toUser, toSipAddress);
  128. sender1Chat1.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
  129. Thread.sleep(TIMEOUT);
  130. assertTrue(sender1Chat1.isAckSent());
  131. fromName = "sender2Chat1";
  132. fromAddress = sender2Chat1ProtocolObjects.addressFactory.createSipURI(
  133. fromName, fromSipAddress);
  134. sender2Chat1.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
  135. Thread.sleep(TIMEOUT);
  136. assertTrue(sender2Chat1.isAckSent());
  137. fromName = "sender3Chat1";
  138. fromAddress = sender3Chat1ProtocolObjects.addressFactory.createSipURI(
  139. fromName, fromSipAddress);
  140. sender3Chat1.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
  141. Thread.sleep(TIMEOUT);
  142. assertTrue(sender3Chat1.isAckSent());
  143. toUser = "chatroom2";
  144. toAddress = sender1Chat2ProtocolObjects.addressFactory.createSipURI(
  145. toUser, toSipAddress);
  146. fromName = "sender1Chat2";
  147. fromAddress = sender1Chat2ProtocolObjects.addressFactory.createSipURI(
  148. fromName, fromSipAddress);
  149. sender1Chat2.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
  150. Thread.sleep(TIMEOUT);
  151. assertTrue(sender1Chat2.isAckSent());
  152. Thread.sleep(TIMEOUT);
  153. sender1Chat1.sendBye();
  154. sender2Chat1.sendBye();
  155. sender3Chat1.sendBye();
  156. sender1Chat2.sendBye();
  157. Thread.sleep(TIMEOUT);
  158. assertTrue(sender1Chat1.getOkToByeReceived());
  159. assertTrue(sender2Chat1.getOkToByeReceived());
  160. assertTrue(sender3Chat1.getOkToByeReceived());
  161. assertTrue(sender1Chat2.getOkToByeReceived());
  162. Thread.sleep(TIMEOUT_TIMER);
  163. Iterator<String> allMessagesIterator = sender1Chat1.getAllMessagesContent().iterator();
  164. logger.info("all messages received from sender1Chat1 : ");
  165. while (allMessagesIterator.hasNext()) {
  166. String message = (String) allMessagesIterator.next();
  167. logger.info(message);
  168. }
  169. assertEquals(1, sender1Chat1.getAllMessagesContent().size());
  170. assertTrue(sender1Chat1.getAllMessagesContent().contains("1"));
  171. allMessagesIterator = sender2Chat1.getAllMessagesContent().iterator();
  172. logger.info("all messages received from sender2Chat1 : ");
  173. while (allMessagesIterator.hasNext()) {
  174. String message = (String) allMessagesIterator.next();
  175. logger.info(message);
  176. }
  177. assertEquals(1, sender1Chat1.getAllMessagesContent().size());
  178. assertTrue(sender2Chat1.getAllMessagesContent().contains("2"));
  179. allMessagesIterator = sender3Chat1.getAllMessagesContent().iterator();
  180. logger.info("all messages received from sender3Chat1 : ");
  181. while (allMessagesIterator.hasNext()) {
  182. String message = (String) allMessagesIterator.next();
  183. logger.info(message);
  184. }
  185. assertEquals(1, sender1Chat1.getAllMessagesContent().size());
  186. assertTrue(sender3Chat1.getAllMessagesContent().contains("3"));
  187. allMessagesIterator = sender1Chat2.getAllMessagesContent().iterator();
  188. logger.info("all messages received from sender1Chat2 : ");
  189. while (allMessagesIterator.hasNext()) {
  190. String message = (String) allMessagesIterator.next();
  191. logger.info(message);
  192. }
  193. assertEquals(1, sender1Chat1.getAllMessagesContent().size());
  194. assertTrue(sender1Chat2.getAllMessagesContent().contains("1"));
  195. }
  196. /**
  197. * This test check that this is not possible to modify the request in the @SipApplicationKey annotated method
  198. * @throws InterruptedException
  199. * @throws SipException
  200. * @throws ParseException
  201. * @throws InvalidArgumentException
  202. */
  203. public void testSipApplicationKeyStaticField() throws InterruptedException, SipException, ParseException, InvalidArgumentException {
  204. String fromName = "staticField";
  205. String fromSipAddress = "sip-servlets.com";
  206. SipURI fromAddress = sender1Chat1ProtocolObjects.addressFactory.createSipURI(
  207. fromName, fromSipAddress);
  208. String toUser = "chatroom";
  209. String toSipAddress = "sip-servlets.com";
  210. SipURI toAddress = sender1Chat1ProtocolObjects.addressFactory.createSipURI(
  211. toUser, toSipAddress);
  212. sender1Chat1.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
  213. Thread.sleep(TIMEOUT);
  214. sender1Chat1.sendBye();
  215. Thread.sleep(TIMEOUT);
  216. assertTrue(sender1Chat1.getOkToByeReceived());
  217. }
  218. /**
  219. * This test check that this is not possible to modify the request in the @SipApplicationKey annotated method
  220. * @throws InterruptedException
  221. * @throws SipException
  222. * @throws ParseException
  223. * @throws InvalidArgumentException
  224. */
  225. public void testSipApplicationKeyModifyingSipServletRequest() throws InterruptedException, SipException, ParseException, InvalidArgumentException {
  226. String fromName = "modifyRequest";
  227. String fromSipAddress = "sip-servlets.com";
  228. SipURI fromAddress = sender1Chat1ProtocolObjects.addressFactory.createSipURI(
  229. fromName, fromSipAddress);
  230. String toUser = "chatroom";
  231. String toSipAddress = "sip-servlets.com";
  232. SipURI toAddress = sender1Chat1ProtocolObjects.addressFactory.createSipURI(
  233. toUser, toSipAddress);
  234. sender1Chat1.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
  235. Thread.sleep(TIMEOUT);
  236. assertTrue(sender1Chat1.isFinalResponseReceived());
  237. assertTrue(!sender1Chat1.isAckSent());
  238. }
  239. @Override
  240. protected void tearDown() throws Exception {
  241. sender1Chat1ProtocolObjects.destroy();
  242. if(sender2Chat1ProtocolObjects != null){
  243. sender2Chat1ProtocolObjects.destroy();
  244. }
  245. if(sender3Chat1ProtocolObjects != null){
  246. sender3Chat1ProtocolObjects.destroy();
  247. }
  248. if(sender1Chat2ProtocolObjects != null){
  249. sender1Chat2ProtocolObjects.destroy();
  250. }
  251. logger.info("Test completed");
  252. super.tearDown();
  253. }
  254. }