Introduction
The Mobicents Sip Servlet extension to Arquillian 1.0.0-CR1 release introduces two Maven archetypes to help you quickly setup a test project.
This tutorial will walk you through the basic steps to create a testing project.
First pick one on of the Mobicent Sip Servlet Arquillian containers depending on your needs:
- For embedded Tomcat 6 container based on Mobicents Sip Servlets 1.7.0 (Java Servlets 2.5) create a maven project using the following maven archetype:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-sipapp-test -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=mss-testing-embeddedTomcat6-archetype -DarchetypeGroupId=org.mobicents.servlet.sip.archetypes -DarchetypeVersion=1.0
- For embedded Tomcat 7 container based on Mobicents Sip Servlets 2.0.0 (Java Servlets 3.0) create a maven project using the following maven archetype:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-sipapp-test -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=mss-testing-embeddedTomcat7-archetype -DarchetypeGroupId=org.mobicents.servlet.sip.archetypes -DarchetypeVersion=1.0
The created project will contain everything you need to get started:
- The required dependencies
- Mobicents Sip Servlet Arquillian container
- SipUnit
- A SIP servlet class
- The test class
Create the project
So lets get started:
- Open a console window and issue the command:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-sipapp-test -Dversion=1.0-SNAPSHOT -DarchetypeArtifactId=mss-testing-embeddedTomcat6-archetype -DarchetypeGroupId=org.mobicents.servlet.sip.archetypes -DarchetypeVersion=1.0
Once the Maven Archetype plugin creates the project, change to the my-sipapp-test directory
- You can quickly check that everything is setup correctly by running the test using the command: mvn test
- Now lets prepare the project for import to Eclipse with the command: mvn clean eclipse:clean eclipse:eclipse
- Next go to Eclipse and import the project to the workspace
Project Structure
The structure of the created project will be:
Project Structure
- The folder src/test/java contains the test class.
- The folder src/test/resources contains the required fot the test, configuration and resource files.
- The folder src/main/java contains the SIP servlet class. This is the actual SIP Servlet application we need to test.
Next lets discus each of the folder’s contents.
SIP Servlet application
The SimpleSipServlet.java class is the SIP Servlet application that we need to test.
@javax.servlet.sip.annotation.SipServlet(loadOnStartup=1, applicationName="SimpleSipServletApplication")
public class SimpleSipServlet extends SipServlet {
private static final long serialVersionUID = -7779271585280911979L;
private static Logger logger = Logger.getLogger(SimpleSipServlet.class);
@Override
protected void doInvite(SipServletRequest req) throws ServletException,IOException {
logger.info(“INVITE message received: “+req.toString());
logger.info(“Sending 100 TRYING”);
SipServletResponse trying = req.createResponse(SipServletResponse.SC_TRYING);
trying.send();
logger.info(“Sending 180 RINGING”);
SipServletResponse ringing = req.createResponse(SipServletResponse.SC_RINGING);
ringing.send();
logger.info(“Sending 200 OK”);
SipServletResponse ok = req.createResponse(SipServletResponse.SC_OK);
ok.send();
}
@Override
protected void doAck(SipServletRequest req) throws ServletException, IOException {
logger.info(“ACK received: “+req.toString());
super.doAck(req);
}
@Override
protected void doBye(SipServletRequest req) throws ServletException, IOException {
logger.info(“BYE received: “+req.toString());
SipServletResponse ok = req.createResponse(SipServletResponse.SC_OK);
ok.send();
}
}
The application will handle INVITE, ACK and BYE messages.
When a new INVITE message arrives, the application will send 100 TRYING, 180 RINGING and finally 200 OK responses. When a BYE received, the application will send a 200 OK response.
This is the behavior that we will assert later in the test method.
Test resources and configuration
- arquillian.xml: Arquillian controlled containers, such as Mobicents Sip Servlet Arquillian Embedded Tomcat 6 container, need arquillian.xml in order to configure parts of the server such as HTTP and SIP connectors ports and the IP Address that the connectors will bind to.
- log4j.xml: This is the configuration for Log4J. You can change the loglevel here
- in-container-sip.xml: The sip.xml deployment descriptor that will be used to created test archive.
- test-dar.properties: The configuration needed for the Sip Servlet Application Router.
Test class
The SimpleSipServletTest.java is the test class of the project. Here we assembly the test archive, we setup and tear down the pre-requisite components for the test , such as the SIP client, and we define the test methods.
@RunWith(Arquillian.class)
public class SimpleSipServletTest {
private static Logger logger = Logger.getLogger(SimpleSipServletTest.class);
//SipUnit related components
private SipStack sipUnitStack;
private SipCall sipUnitCall;
private SipPhone sipUnitPhone;
private static SipStackTool sipStackTool;
private static String toUri = “sip:sipservletapp@127.0.0.1:5070″;
@BeforeClass
public static void beforeClass(){
sipStackTool = new SipStackTool(“mySipUnitStackTool”);
}
@Before
public void setUp() throws Exception
{
logger.info(“Setting up SipUnit”);
//Create the sipCall and start listening for messages
//.SipStackTool.initializeSipStack(String myTransport, String myHost, String myPort, String outboundProxy)
sipUnitStack = sipStackTool.initializeSipStack(SipStack.PROTOCOL_UDP, “127.0.0.1″, “5080″, “127.0.0.1:5070″);
//SipStack.createSipPhone(String proxyHost, String proxyProto, int proxyPort, String myURI)
sipUnitPhone = sipUnitStack.createSipPhone(“127.0.0.1″, SipStack.PROTOCOL_UDP, 5070, “sip:sipunit@here.com”);
sipUnitCall = sipUnitPhone.createSipCall();
sipUnitCall.listenForIncomingCall();
}
@After
public void tearDown() throws Exception
{
logger.info(“Tear down SipUnit”);
if(sipUnitCall != null) sipUnitCall.disposeNoBye();
if(sipUnitPhone != null) sipUnitPhone.dispose();
if(sipUnitStack != null) sipUnitStack.dispose();
}
@Deployment(name=”simple”, managed=true, testable=false)
public static WebArchive createTestArchive()
{
//Create a test archive named: simplesipservlet.war
WebArchive webArchive = ShrinkWrap.create(WebArchive.class, “simplesipservlet.war”);
//Include the SimpleSipServlet.class from /src/main/java
webArchive.addClasses(SimpleSipServlet.class);
//Include as WEB-INF resource sip.xml the in-container-sip.xml from src/test/resources
webArchive.addAsWebInfResource(“in-container-sip.xml”, “sip.xml”);
return webArchive;
}
@Test
public void testInitiateCall() {
//Initiate a new call
assertTrue(sipUnitCall.initiateOutgoingCall(toUri, null));
//Wait for answer
assertTrue(sipUnitCall.waitForAnswer(5000));
assertEquals(SipServletResponse.SC_OK, sipUnitCall.getLastReceivedResponse().getStatusCode());
//Send ACK to 200 OK
assertTrue(sipUnitCall.sendInviteOkAck());
//Disconnect call and wait for 200 ok to BYE
assertTrue(sipUnitCall.disconnect());
assertEquals(SipServletResponse.SC_OK,sipUnitCall.getLastReceivedResponse().getStatusCode());
}
}
Lets go through the important parts of the test class:
- We need to annotate our test class with @RunWith(Arquillian.class) in order Arquillian to pick up the test
- Using JUnit 4 @Before, @After and @BeforeClass annotations, we setup the SipUnit, the SIP client that will be used in our test method later
- The methods createTestArchive() that is annotated with @Deployment, will be called by Arquillian to get the test archive and deploy it. In this method:
- We create a test archive with name simplesipservlet.war archive:
WebArchive webArchive = ShrinkWrap.create(WebArchive.class, “simplesipservlet.war”);
- We add the SIP servlet SimpleSipServlet.class class:
webArchive.addClasses(SimpleSipServlet.class);
- We add the required SIP Servlets deployment descriptor sip.xml:
webArchive.addAsWebInfResource(“in-container-sip.xml”, “sip.xml”);
- And finally we return the test archive:
return webArchive;
- Last is the test method annotated with JUnit 4 @Test. Here SipUnit will interact with the SIP Servlet application and will assert the results.
- SipUnit will initiate a call to the application
assertTrue(sipUnitCall.initiateOutgoingCall(toUri, null));
- Then will wait for an answer and will check that the answer is a 200 OK
assertTrue(sipUnitCall.waitForAnswer(5000));
assertEquals(SipServletResponse.SC_OK, sipUnitCall.getLastReceivedResponse().getStatusCode());
- Next will send an ACK
assertTrue(sipUnitCall.sendInviteOkAck());
- And finally will disconnect this call:
assertTrue(sipUnitCall.disconnect());
assertEquals(SipServletResponse.SC_OK,sipUnitCall.getLastReceivedResponse().getStatusCode());
Running the test
Now we are ready to run the test.
Right click on the SimpleSipServletTest.java and choose Run As -> JUnit test. You will see a lot of messages in the console and finally the GREEN bar of the JUnit result:
JUnit Result
You can also run the test from the console using Maven surefire plugin. Just open a console in the project’s directory and run the test with the command: mvn test.
Resources