PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/servers/sip-servlets/sip-servlets-test-suite/testsuite/src/test/java/org/mobicents/servlet/sip/testsuite/subsnotify/InDialogNotifierSipServletTest.java

http://mobicents.googlecode.com/
Java | 184 lines | 116 code | 34 blank | 34 comment | 5 complexity | b89834b9f0ff640b905a29d7f8917706 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.subsnotify;
  23. import gov.nist.javax.sip.header.SubscriptionState;
  24. import java.text.ParseException;
  25. import javax.sip.InvalidArgumentException;
  26. import javax.sip.SipException;
  27. import javax.sip.SipProvider;
  28. import javax.sip.address.SipURI;
  29. import javax.sip.message.Request;
  30. import org.apache.log4j.Logger;
  31. import org.mobicents.servlet.sip.SipServletTestCase;
  32. import org.mobicents.servlet.sip.testsuite.ProtocolObjects;
  33. import org.mobicents.servlet.sip.testsuite.TestSipListener;
  34. public class InDialogNotifierSipServletTest extends SipServletTestCase {
  35. private static transient Logger logger = Logger.getLogger(InDialogNotifierSipServletTest.class);
  36. private static final String TRANSPORT = "udp";
  37. private static final boolean AUTODIALOG = true;
  38. private static final int TIMEOUT = 5000;
  39. // private static final int TIMEOUT = 100000000;
  40. private static final String[] SUBSCRIPTION_STATES = new String[]{
  41. SubscriptionState.PENDING.toLowerCase(), SubscriptionState.ACTIVE.toLowerCase(), SubscriptionState.TERMINATED.toLowerCase()
  42. };
  43. private static final String SESSION_INVALIDATED = new String("sipSessionReadyToBeInvalidated");
  44. TestSipListener sender;
  45. ProtocolObjects senderProtocolObjects;
  46. public InDialogNotifierSipServletTest(String name) {
  47. super(name);
  48. }
  49. @Override
  50. public void deployApplication() {
  51. assertTrue(tomcat.deployContext(
  52. projectHome + "/sip-servlets-test-suite/applications/notifier-servlet/src/main/sipapp",
  53. "sip-test-context", "sip-test"));
  54. }
  55. @Override
  56. protected String getDarConfigurationFile() {
  57. return "file:///" + projectHome + "/sip-servlets-test-suite/testsuite/src/test/resources/" +
  58. "org/mobicents/servlet/sip/testsuite/subsnotify/indialog-notifier-sip-servlet-dar.properties";
  59. }
  60. @Override
  61. protected void setUp() {
  62. try {
  63. super.setUp();
  64. senderProtocolObjects =new ProtocolObjects(
  65. "sender", "gov.nist", TRANSPORT, AUTODIALOG, null, null, null);
  66. sender = new TestSipListener(5080, 5070, senderProtocolObjects, false);
  67. SipProvider senderProvider = sender.createProvider();
  68. senderProvider.addSipListener(sender);
  69. senderProtocolObjects.start();
  70. } catch (Exception ex) {
  71. ex.printStackTrace();
  72. fail("unexpected exception ");
  73. }
  74. }
  75. /*
  76. * Test the fact that a sip servlet receive SUBSCRIBE and sends NOTIFYs in response in an existing dialog (INVITE call).
  77. * Check that everything works correctly included the Sip Session Termination.
  78. * Session Termination should occur only when the last NOTIFY comes in is since the NOTIFY
  79. * containing Subscription State of Terminated is sent after the BYE
  80. */
  81. public void testInDialogSubscriptionByeSentBeforeTerminatingNotify() throws InterruptedException, SipException, ParseException, InvalidArgumentException {
  82. String fromName = "sender";
  83. String fromSipAddress = "sip-servlets.com";
  84. SipURI fromAddress = senderProtocolObjects.addressFactory.createSipURI(
  85. fromName, fromSipAddress);
  86. String toUser = "receiver";
  87. String toSipAddress = "sip-servlets.com";
  88. SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(
  89. toUser, toSipAddress);
  90. sender.sendSipRequest(Request.INVITE, fromAddress, toAddress, null, null, false);
  91. Thread.sleep(TIMEOUT);
  92. assertTrue(sender.isAckSent());
  93. sender.setSendByeBeforeTerminatingNotify(true);
  94. sender.sendInDialogSipRequest(Request.SUBSCRIBE, null, null, null, null, null);
  95. Thread.sleep(TIMEOUT*2);
  96. assertTrue(sender.getOkToByeReceived());
  97. for (String subscriptionState : sender.getAllSubscriptionState()) {
  98. logger.info("Subscription state :" + subscriptionState);
  99. }
  100. assertEquals(3, sender.getAllSubscriptionState().size());
  101. for (String subscriptionState : SUBSCRIPTION_STATES) {
  102. assertTrue(subscriptionState + " not present",sender.getAllSubscriptionState().contains(subscriptionState));
  103. }
  104. Thread.sleep(TIMEOUT);
  105. for (String message : sender.getAllMessagesContent()) {
  106. logger.info("Message :" + message);
  107. }
  108. assertTrue(sender.getAllMessagesContent().size() >= 1);
  109. assertTrue("session not invalidated after receiving Terminated Subscription State", sender.getAllMessagesContent().contains(SESSION_INVALIDATED));
  110. }
  111. /*
  112. * Test the fact that a sip servlet receive SUBSCRIBE and sends NOTIFYs in response in an existing dialog (INVITE call).
  113. * Check that everything works correctly included the Sip Session Termination.
  114. * Session Termination should occur only when the BYE is sent since the NOTIFY
  115. * containing Subscription State of Terminated is sent before the BYE
  116. */
  117. public void testInDialogSubscriptionByeSentAfterTerminatingNotify() throws InterruptedException, SipException, ParseException, InvalidArgumentException {
  118. String fromName = "sender";
  119. String fromSipAddress = "sip-servlets.com";
  120. SipURI fromAddress = senderProtocolObjects.addressFactory.createSipURI(
  121. fromName, fromSipAddress);
  122. String toUser = "receiver";
  123. String toSipAddress = "sip-servlets.com";
  124. SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(
  125. toUser, toSipAddress);
  126. sender.sendSipRequest(Request.INVITE, fromAddress, toAddress, null, null, false);
  127. Thread.sleep(TIMEOUT);
  128. assertTrue(sender.isAckSent());
  129. sender.setSendByeAfterTerminatingNotify(true);
  130. sender.sendInDialogSipRequest(Request.SUBSCRIBE, null, null, null, null, null);
  131. Thread.sleep(TIMEOUT*2);
  132. assertTrue(sender.getOkToByeReceived());
  133. for (String subscriptionState : sender.getAllSubscriptionState()) {
  134. logger.info("Subscription state :" + subscriptionState);
  135. }
  136. assertEquals(3, sender.getAllSubscriptionState().size());
  137. for (String subscriptionState : SUBSCRIPTION_STATES) {
  138. assertTrue(subscriptionState + " not present",sender.getAllSubscriptionState().contains(subscriptionState));
  139. }
  140. Thread.sleep(TIMEOUT);
  141. assertTrue(sender.getAllMessagesContent().size() >= 1);
  142. assertTrue("session not invalidated after receiving Terminated Subscription State", sender.getAllMessagesContent().contains(SESSION_INVALIDATED));
  143. }
  144. @Override
  145. protected void tearDown() throws Exception {
  146. senderProtocolObjects.destroy();
  147. logger.info("Test completed");
  148. super.tearDown();
  149. }
  150. }