Re: [Asterisk-java-users] How to test a FastAGI script without any Asterisk server ?
Brought to you by:
srt
From: Stefan R. <sr...@re...> - 2006-06-28 10:42:52
|
Hi Oliver, Olivier wrote: > From reading Asterisk, the Future of Telephony, I understood that an > Asterisk server simply uses stdin, stdout and stderr files when calling= > an AGI script : > - Asterisk sends line of text like : > agi_request: test.py > agi_channel: Zap/1-1 >=20 > - Asterisk receives: > SET EXTENTION 122 > SET VARIABLE var1 150 It's not that easy. what goes on is more like: AGI Tx >> agi_network_script: test.agi AGI Tx >> agi_request: agi://endorsed/test.agi AGI Tx >> agi_channel: SIP/1310-f620 AGI Tx >> AGI Rx << ANSWER AGI Tx >> 200 result=3D0 AGI Rx << STREAM FILE "vm-instructions" "1" AGI Tx >> 200 result=3D49 endpos=3D45760 AGI Rx << HANGUP AGI Tx >> 200 result=3D1 So whenever you send a command asterisk sends a reply, this must be handled by your test driver. Therefore the far easier option is to use mock objects for testing and not dealing with I/O at all. I'll show you what this might look like for a simple example: You want to test this script: import org.asteriskjava.fastagi.AgiChannel; import org.asteriskjava.fastagi.AgiException; import org.asteriskjava.fastagi.AgiRequest; import org.asteriskjava.fastagi.AgiScript; public class MyAgiScript implements AgiScript { public void service(AgiRequest request, AgiChannel channel) throws AgiException { char choice; choice =3D channel.getOption("welcome-please-press-1", "1"); if (choice =3D=3D '1') { channel.streamFile("thank-you"); } else { channel.streamFile("you-didnt-do-what-i-said"); channel.hangup(); } } } so this is the JUnit test case: import org.asteriskjava.fastagi.AgiChannel; import org.asteriskjava.fastagi.AgiRequest; import junit.framework.TestCase; import static org.easymock.EasyMock.*; public class MyAgiScriptTest extends TestCase { AgiRequest request; AgiChannel channel; MyAgiScript myAgiScript =3D new MyAgiScript(); public void setUp() { request =3D createMock(AgiRequest.class); channel =3D createMock(AgiChannel.class); } public void testServiceUserPresses1() throws Exception { // we expect the script to play 'welcome-please-press-1' and the // user presses 1 expect(channel.getOption("welcome-please-press-1", "1")).andReturn('1'); // next the script should say 'thank-you' channel.streamFile("thank-you"); // replay the mock object replay(channel); myAgiScript.service(request, channel); // and verify all expected methods have been called verify(channel); } public void testServiceUserPresses2() throws Exception { // we expect the script to play 'welcome-please-press-1' and the // user presses 2 expect(channel.getOption("welcome-please-press-1", "1")).andReturn('2'); // next the script should say 'you-didnt-do-what-i-said'... channel.streamFile("you-didnt-do-what-i-said"); // and hangup channel.hangup(); // replay the mock object replay(channel); myAgiScript.service(request, channel); // and verify all expected methods have been called verify(channel); } } You'll find more information about using mock objects in general and easymock in particular on easymocks homepage at http://www.easymock.org/.= =3DStefan --=20 reuter network consulting Neusser Str. 110 50760 Koeln Germany Telefon: +49 221 1305699-0 Telefax: +49 221 1305699-90 E-Mail: sr...@re... |