public void testProxyCalleeSendBye() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    receiver.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isAckSent());
    assertTrue(sender.isAckReceived());
    receiver.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(sender.getByeReceived());
    assertTrue(receiver.getOkToByeReceived());
  }
  /*
   * https://code.google.com/p/sipservlets/issues/detail?id=21
   */
  public void testProxyCallerFinalResponseOnSubsequentRequest() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location-final-response-subsequent";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertEquals(491, sender.getFinalResponseStatus());
    assertFalse(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
  }
  /*
   * Non regression test for https://code.google.com/p/sipservlets/issues/detail?id=202
   */
  public void testProxyModifySDP() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "modify-SDP";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.setTimeToWaitBeforeAck(2000);
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    assertEquals(
        "SDP modified successfully",
        new String(sender.getFinalResponse().getRawContent(), "UTF-8"));
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    assertEquals(
        "SDP modified successfully",
        new String(sender.getFinalResponse().getRawContent(), "UTF-8"));
    receiver.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isAckSent());
    assertTrue(sender.isAckReceived());
    assertEquals(
        "SDP modified successfully",
        new String(receiver.getFinalResponse().getRawContent(), "UTF-8"));
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
  }
  // Check branchId of the ACK in case the reINVITE has an INFO in between to make sure that
  // the ACK to 200 OK reINVITE is different than the INFO branchId
  public void testProxyReinviteINFOCheckACKBranchId() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String infoBranch = ((MessageExt) receiver.getInfoRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    assertFalse(infoBranch.equals(ackBranch));
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
  }
  // https://code.google.com/p/sipservlets/issues/detail?id=238
  public void testProxyINFOLeak() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(500);
    sender.sendInDialogSipRequest("INFO", null, null, null, null, null);
    Thread.sleep(TIMEOUT * 2);
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
    assertNotNull(sender.getFinalResponse().getHeader("X-Proxy-Transactions"));
    assertEquals(
        "1", ((HeaderExt) sender.getFinalResponse().getHeader("X-Proxy-Transactions")).getValue());
  }
  // Test for http://code.google.com/p/sipservlets/issues/detail?id=44
  public void testProxyReinviteAckSeenByApp() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location-ack-seen-by-app";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    receiver.setFinalResponseToSend(491);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());

    Iterator<String> allMessagesIterator = sender.getAllMessagesContent().iterator();
    while (allMessagesIterator.hasNext()) {
      String message = (String) allMessagesIterator.next();
      logger.info(message);
    }
    assertFalse(sender.getAllMessagesContent().contains("ack-seen-by-app"));
  }
  public void testProxyCallerSendBye() throws Exception {
    setupPhones(ListeningPoint.UDP);
    String fromName = "unique-location";
    String fromSipAddress = "sip-servlets.com";
    SipURI fromAddress =
        senderProtocolObjects.addressFactory.createSipURI(fromName, fromSipAddress);

    String toSipAddress = "sip-servlets.com";
    String toUser = "******";
    SipURI toAddress = senderProtocolObjects.addressFactory.createSipURI(toUser, toSipAddress);

    // part of non regression test for Issue
    // http://code.google.com/p/mobicents/issues/detail?id=2359
    // allow to check if ACK retrans keep the same different branch id
    sender.setTimeToWaitBeforeAck(2000);
    sender.sendSipRequest("INVITE", fromAddress, toAddress, null, null, false);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    // non regression test for Issue http://code.google.com/p/mobicents/issues/detail?id=2359
    String inviteBranch =
        ((MessageExt) receiver.getInviteRequest()).getTopmostViaHeader().getBranch();
    String ackBranch = ((MessageExt) receiver.getAckRequest()).getTopmostViaHeader().getBranch();
    assertFalse(inviteBranch.equals(ackBranch));
    receiver.setAckReceived(false);
    sender.setAckSent(false);
    sender.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(sender.isAckSent());
    assertTrue(receiver.isAckReceived());
    receiver.setAckReceived(false);
    receiver.setAckSent(false);
    sender.setAckSent(false);
    sender.setAckReceived(false);
    receiver.sendInDialogSipRequest("INVITE", null, null, null, null, null);
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.isAckSent());
    assertTrue(sender.isAckReceived());
    sender.sendBye();
    Thread.sleep(TIMEOUT);
    assertTrue(receiver.getByeReceived());
    assertTrue(sender.getOkToByeReceived());
    logger.info(
        "RequestsProcessedByMethod "
            + tomcat.getSipService().getSipApplicationDispatcher().getRequestsProcessedByMethod());
    logger.info(
        "RequestsSentByMethod "
            + tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod());
    logger.info(
        "ResponsesProcessedByStatusCode "
            + tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getResponsesProcessedByStatusCode());
    logger.info(
        "ResponsesSentByStatusCode "
            + tomcat.getSipService().getSipApplicationDispatcher().getResponsesSentByStatusCode());
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getRequestsProcessedByMethod(Request.INVITE)
            > 1);
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getRequestsProcessedByMethod(Request.ACK)
            > 1);
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getRequestsProcessedByMethod(Request.BYE)
            > 0);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getResponsesSentByStatusCode("2XX")
            > 1);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod(Request.INVITE)
            > 1);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod(Request.ACK)
            > 1);
    assertTrue(
        tomcat.getSipService().getSipApplicationDispatcher().getRequestsSentByMethod(Request.BYE)
            > 0);
    assertTrue(
        tomcat
                .getSipService()
                .getSipApplicationDispatcher()
                .getResponsesProcessedByStatusCode("2XX")
            > 1);
  }