Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 796 lines (608 sloc) 27.089 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
package com.tropo.grails

import groovy.util.GroovyTestCase;

import java.util.Map


class TropoBuilderTests extends GroovyTestCase {

public void testAskAction() {

def builder = new TropoBuilder()
builder.tropo {
ask(name : 'foo', bargein: true, timeout: 30, required: true, interdigitTimeout: 5)
}
assert builder.text() == """{"tropo":[{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true,"interdigitTimeout":5}}]}"""
}

public void testAskWithSayBlock() {

def builder = new TropoBuilder()
builder.tropo {
ask(name : 'foo', bargein: true, timeout: 30, required: true, choices: '[5 DIGITS]') {
say('Please say your account number')
}
}
assert builder.text() == """{"tropo":[{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true,"choices":["5 DIGITS"],"say":[{"value":"Please say your account number"}]}}]}"""

}

public void testAskWithSayAndOnBlocks() {

def builder = new TropoBuilder()
builder.tropo {
ask(name : 'foo', bargein: true, timeout: 30, required: true) {
say('Please say your account number')
choices(value: '[5 DIGITS]')
}
on(event:'success',next:'/result.json')
}
assert builder.text() == """{"tropo":[{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true,"say":[{"value":"Please say your account number"}],"choices":{"value":"[5 DIGITS]"}}},{"on":{"event":"success","next":"/result.json"}}]}"""
}
public void testFailsAskWithNoNameParameter() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
ask(foo:'bar')
}
}
assert message == "Missing required property: 'name'"
}
public void testChoice() {

def builder = new TropoBuilder()
builder.tropo {
choices(value: '[5 DIGITS]')
}
assert builder.text() == """{"tropo":[{"choices":{"value":"[5 DIGITS]"}}]}"""
}
public void testFailsChoicesWithUnsupportedMode() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
choices(value:'[5 DIGITS]', mode:'frootloops')
}
}
assert message == "If mode is provided, only 'dtmf', 'speech' or 'any' is supported"
}
public void testChoiceWithMode() {
def builder = new TropoBuilder()
builder.tropo {
choices(value: '[5 DIGITS]', mode: 'dtmf')
}
assert builder.text() == """{"tropo":[{"choices":{"value":"[5 DIGITS]","mode":"dtmf"}}]}"""
}
public void testConference() {
def builder = new TropoBuilder()
builder.tropo {
conference(name : 'foo', id: '1234', mute: false, send_tones: false, exit_tone: '#', interdigitTimeout: 5)
}
assert builder.text() == """{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"send_tones":false,"exit_tone":"#","interdigitTimeout":5}}]}"""
}

public void testConferenceWithOnAndSayBlocks() {

def builder = new TropoBuilder()
builder.tropo {
conference(name: 'foo', id: '1234', mute: false, send_tones: false, exit_tone: '#') {
on(event: 'join') {
say(value: 'Welcome to the conference')
}
on(event:'leave') {
say(value: 'Someone has left the conference')
}
}
}
assert builder.text() == """{"tropo":[{"conference":{"name":"foo","id":"1234","mute":false,"send_tones":false,"exit_tone":"#","on":[{"event":"join","say":[{"value":"Welcome to the conference"}]},{"event":"leave","say":[{"value":"Someone has left the conference"}]}]}}]}"""
}
public void testFailsConferenceWithNoNameParameter() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
conference(foo:'bar')
}
}
assert message == "Missing required property: 'name'"
}
public void testFailsConferenceWithNoIdParameter() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
conference(name:'bar')
}
}
assert message == "Missing required property: 'id'"
}
public void testHangup() {
def builder = new TropoBuilder()
builder.tropo {
hangup()
}
assert builder.text() == """{"tropo":[{"hangup":null}]}"""
}
public void testOn() {

def builder = new TropoBuilder()
builder.tropo {
on(event: 'hangup', next: 'myresource')
}
assert builder.text() == """{"tropo":[{"on":{"event":"hangup","next":"myresource"}}]}"""
}
public void testFailsOnWithNoEventParameter() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
on(foo: 'bar', next: 'myresource')
}
}
assert message == """Missing required property: 'event'"""
}
public void testRecord() {
def builder = new TropoBuilder()
builder.tropo {
record(name: 'foo', url: 'http://sendme.com/tropo', beep: true, send_tones: false, interdigitTimeout: 5) {
choices(terminator: '#')
}
}
assert builder.text() == """{"tropo":[{"record":{"name":"foo","url":"http://sendme.com/tropo","beep":true,"send_tones":false,"interdigitTimeout":5,"choices":{"terminator":"#"}}}]}"""
}
public void testFailsRecordWithNoNameParameter() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
record(foo:'bar')
}
}
assert message == "Missing required property: 'name'"
}
public void testFailsRecordWithNoUrlParameter() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
record(name:'bar')
}
}
assert message == "Missing required property: 'url'"
}
public void testFailsRecordWithInvalidUrl() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
record(name: 'foo', url: 'invalid' )
}
}
assert message == "The 'url' parameter must be a valid URL"
}
public void testRecordAcceptsEmailAddress() {
def builder = new TropoBuilder()
builder.tropo {
record(name: 'foo', url: 'mailto:foo@bar.com', beep: true, send_tones: false) {
choices(terminator:'#')
}
}
assert builder.text() == """{"tropo":[{"record":{"name":"foo","url":"foo@bar.com","beep":true,"send_tones":false,"choices":{"terminator":"#"}}}]}"""
}
public void testRedirect() {
def builder = new TropoBuilder()
builder.tropo {
redirect(to: 'sip:1234', from: '4155551212')
}
assert builder.text() == """{"tropo":[{"redirect":{"to":"sip:1234","from":"4155551212"}}]}"""
}
public void testFailsNestedRedirect() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
conference(name: 'foo', id: '2334' ) {
redirect(to: 'sip:1234', from: '4155551212')
}
}
}
assert message == "Redirect should only be used alone and before the session is answered, use transfer instead"
}
public void testFailsRedirectWithNoTo() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
redirect(from: '4155551212')
}
}
assert message == "Missing required property: 'to'"
}
public void testReject() {
def builder = new TropoBuilder()
builder.tropo {
reject()
}
assert builder.text() == """{"tropo":[{"reject":null}]}"""

}
public void testFailsOnEmptyBuilder() {
def message = shouldFail(TropoBuilderException) {
new TropoBuilder().text()
}
assert message == "You need a tropo root node!"
}
public void testTropoRoot() {
def builder = new TropoBuilder()
builder.tropo {}
assert builder.text() == """{"tropo":[]}"""
}
public void testSay() {
def builder = new TropoBuilder()
builder.tropo {
say('1234')
}
assert builder.text() == """{"tropo":[{"say":[{"value":"1234"}]}]}"""
}
public void testSayWithNumbersAndDobleQuotes() {
def builder = new TropoBuilder()
builder.tropo {
say("Your zipcode is ${14000}. oh yes.")
}
assert builder.text() == """{"tropo":[{"say":[{"value":"Your zipcode is 14000. oh yes."}]}]}"""
}
public void testSayWithMapArgument() {
def builder = new TropoBuilder()
builder.tropo {
say(value:'1234')
}
assert builder.text() == """{"tropo":[{"say":[{"value":"1234"}]}]}"""
}
public void testFailsWithNumbers() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
say(1234)
}
}
assert message == "An invalid paramater type class java.lang.Integer has been passed"
}
public void testSayWithArrayArgument() {
def builder = new TropoBuilder()
builder.tropo {
say([value: '1234'], [value: 'abcd', event: 'nomatch:1'])
}
assert builder.text() == """{"tropo":[{"say":[{"value":"1234"},{"value":"abcd","event":"nomatch:1"}]}]}"""
}
public void testSayWithArrayArgument2() {
def builder = new TropoBuilder()
builder.tropo {
say([[value: '1234'], [value: 'abcd', event: 'nomatch:1'],[value: '1234', event: 'timeout:1'],[value: '1234', event: 'timeout:2']])
}
println builder.text()
assert builder.text() == """{"tropo":[{"say":[{"value":"1234"},{"value":"abcd","event":"nomatch:1"},{"value":"1234","event":"timeout:1"},{"value":"1234","event":"timeout:2"}]}]}"""
}
public void testSayWithMoreThanTwoArrayArguments() {
def builder = new TropoBuilder()
builder.tropo {
say([[value: '1234'], [value: 'abcd', event: 'nomatch:1'], [value: 'zywx', event: 'nomatch:2']])
}
assert builder.text() == """{"tropo":[{"say":[{"value":"1234"},{"value":"abcd","event":"nomatch:1"},{"value":"zywx","event":"nomatch:2"}]}]}"""
}

public void testFailsWithNoValuePassed() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
say(name:'blah')
}
}
assert message == "Missing required property: 'value'"
}
public void testSayAndOn() {
TropoBuilder builder = new TropoBuilder()
builder.tropo {
say(value:'blah')
on(event: 'error', next: 'error.json')
}
assert builder.text() == """{"tropo":[{"say":[{"value":"blah"}]},{"on":{"event":"error","next":"error.json"}}]}"""
}
public void testStartRecording() {
TropoBuilder builder = new TropoBuilder()
builder.tropo {
startRecording(url:'http://postrecording.com/tropo')
}
assert builder.text() == """{"tropo":[{"startRecording":{"url":"http://postrecording.com/tropo"}}]}"""
}

public void testStopRecording() {

def builder = new TropoBuilder()
builder.tropo {
stopRecording()
}
assert builder.text() == """{"tropo":[{"stopRecording":null}]}"""

}

public void testTransfer() {

def builder = new TropoBuilder()
builder.tropo {
transfer(to: 'tel:+14157044517')
}
assert builder.text() == """{"tropo":[{"transfer":{"to":"tel:+14157044517"}}]}"""
}

public void testTransferWithOnAndChoices() {

def builder = new TropoBuilder()
builder.tropo {
transfer(to: 'tel:+14157044517') {
on(event: 'unbounded', next: '/error.json')
choices(value: '[5 DIGITS]')
}
}
assert builder.text() == """{"tropo":[{"transfer":{"to":"tel:+14157044517","on":[{"event":"unbounded","next":"/error.json"}],"choices":{"value":"[5 DIGITS]"}}}]}"""
}
public void testFailsTransferWithNoToParameter() {
def builder = new TropoBuilder()
def message = shouldFail(TropoBuilderException) {
builder.tropo {
transfer()
}
}
assert message == "Missing required property: 'to'"
}

public void testGenerateJsonWhenBlockIsPassed() {
def builder = new TropoBuilder()
builder.tropo {
say([value: '1234'], [value: 'abcd', event: 'nomatch:1'])
say([value: '0987'], [value: 'zyxw', event: 'nomatch:2'])
}
assert builder.text() == """{"tropo":[{"say":[{"value":"1234"},{"value":"abcd","event":"nomatch:1"}]},{"say":[{"value":"0987"},{"value":"zyxw","event":"nomatch:2"}]}]}"""
}

public void testParseJsonSession() {

def builder = new TropoBuilder()
def json_session = """{"session":{"id":"dih06n","accountId":"33932","timestamp":"2010-01-19T23:18:48.562Z","userType":"HUMAN","to":{"id":"tropomessaging@bot.im","name":"unknown","channel":"TEXT","network":"JABBER"},"from":{"id":"john_doe@gmail.com","name":"unknown","channel":"TEXT","network":"JABBER"}}}"""
def map = builder.parse(json_session)
def t = map.session.timestamp
assert map.session.timestamp == '2010-01-19T23:18:48.562Z'
}
public void testShouldSeeObjectOutsideBlock() {
def builder = new TropoBuilder()
def t = 'foobar'
def newt
builder.tropo {
newt = t
say(value:'blah')
on(event:'error', next:'error.json')
}
assert newt == 'foobar'
}

public void testTwoSaysInBlock() {
def builder = new TropoBuilder()
builder.tropo {
say('foo')
say('bar')
}
assert builder.text() == """{"tropo":[{"say":[{"value":"foo"}]},{"say":[{"value":"bar"}]}]}"""
}

public void testTwoSaysSeparated() {
def builder = new TropoBuilder()
builder.say('foo')
builder.say('bar')

assert builder.text() == """{"tropo":[{"say":[{"value":"foo"}]},{"say":[{"value":"bar"}]}]}"""
}

public void testSayOnAndRecordSeparated() {

def builder = new TropoBuilder()
builder.say('Welcome to the app')
builder.on(event: 'hangup', next: '/hangup.json')
builder.record(name: 'foo', beep: true, send_tones: false, exit_tone: '#', url: 'http://sendme.com/tropo') {
say(value: 'Please say your account number')
choices(value: '[5 DIGITS]')
}

assert builder.text() == """{"tropo":[{"say":[{"value":"Welcome to the app"}]},{"on":{"event":"hangup","next":"/hangup.json"}},{"record":{"name":"foo","beep":true,"send_tones":false,"exit_tone":"#","url":"http://sendme.com/tropo","say":[{"value":"Please say your account number"}],"choices":{"value":"[5 DIGITS]"}}}]}"""
}

public void testReset() {

def builder = new TropoBuilder()
builder.ask(name : 'foo', bargein: true, timeout: 30, required: true)
assert builder.text() == """{"tropo":[{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true}}]}"""

builder.reset()
assert builder.text() == """{"tropo":[]}"""
}

public void testResultWithActionIsParsed() {

def builder = new TropoBuilder()
def json_result = """{"result":{"sessionId":"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20","callState":"ANSWERED","sessionDuration":2,"sequence":1,"complete":true,"error":null,"actions":{"name":"zip","attempts":1,"disposition":"SUCCESS","confidence":100,"interpretation":"12345","utterance":"1 2 3 4 5"}}}"""
def map = builder.parse(json_result)

assert map.result.actions.name == 'zip'
}

public void testObjectArrivesInJson() {

def builder = new TropoBuilder()
def json_result = """{"result":{"sessionId":"CCFD9C86-1DD1-11B2-B76D-B9B253E4B7FB@161.253.55.20","callState":"ANSWERED","sessionDuration":2,"sequence":1,"complete":true,"error":null,"actions":{"name":"zip","attempts":1,"disposition":"SUCCESS","confidence":100,"interpretation":"12345","utterance":"1 2 3 4 5"}}}"""
def map = builder.parse(json_result)

def a = map.result
assert map['result']['callState'] == 'ANSWERED'
assert map.result.callState == 'ANSWERED'
}

public void testStartRecordingLargeScript() {

def builder = new TropoBuilder()

builder.on(event: 'error', next:'/error.json') // For fatal programming errors. Log some details so we can fix it
builder.on(event: 'hangup', next:'/hangup.json') // When a user hangs or call is done. We will want to log some details.
builder.on(event: 'continue', next:'/next.json')
builder.say('Hello')
builder.startRecording(url: 'http://heroku-voip.marksilver.net/post_audio_to_s3?filename=foo.wav&unique_id=bar')
// [From this point, until stop_recording(), we will record what the caller *and* the IVR say]
builder.say 'You are now on the record.'
// Prompt the user to incriminate themselve on-the-record
builder.say 'Go ahead, sing-along.'
builder.say "http://denalidomain.com/music/keepers/HappyHappyBirthdaytoYou-Disney.mp3"

assert builder.text() == """{"tropo":[{"on":{"event":"error","next":"/error.json"}},{"on":{"event":"hangup","next":"/hangup.json"}},{"on":{"event":"continue","next":"/next.json"}},{"say":[{"value":"Hello"}]},{"startRecording":{"url":"http://heroku-voip.marksilver.net/post_audio_to_s3?filename=foo.wav&unique_id=bar"}},{"say":[{"value":"You are now on the record."}]},{"say":[{"value":"Go ahead, sing-along."}]},{"say":[{"value":"http://denalidomain.com/music/keepers/HappyHappyBirthdaytoYou-Disney.mp3"}]}]}"""
}

/*
public void testGenerateVoiceSession() {
def builder = new TropoBuilder()
def json_session = "{"session":{"id":"0-13c4-4b563da3-7aecefda-46af-1d10bdd0","accountId":"33932","timestamp":"2010-01-19T23:18:00.854Z","userType":"HUMAN","to":{"id":"9991427589","name":"unknown","channel":"VOICE","network":"PSTN"},"from":{"id":"jsgoecke","name":"unknown","channel":"VOICE","network":"PSTN"}}}"
def map = builder.parse(json_session)
assert builder.voiceSession == true
assert builder.textSession == false
}
public void testGenerateTextSession() {
def builder = new TropoBuilder()
def json_session = "{"session":{"id":"dih06n","accountId":"33932","timestamp":"2010-01-19T23:18:48.562Z","userType":"HUMAN","to":{"id":"tropomessaging@bot.im","name":"unknown","channel":"TEXT","network":"JABBER"},"from":{"id":"john_doe@gmail.com","name":"unknown","channel":"TEXT","network":"JABBER"}}}"
def map = builder.parse(json_session)
assert builder.voiceSession == true
assert builder.textSession == false
}
*/

public void testCall() {

def builder = new TropoBuilder()

builder.call(to: 'foo', from: 'bar', network: 'SMS', channel: 'TEXT', timeout: 10, answerOnMedia: false) {
headers(foo: 'foo', bar: 'bar')
recording(url: 'http://foobar', method: 'POST', format: 'audio/mp3', username: 'jose', password: 'passwd')
}

assert builder.text() == """{"tropo":[{"call":{"to":"foo","from":"bar","network":"SMS","channel":"TEXT","timeout":10,"answerOnMedia":false,"headers":{"foo":"foo","bar":"bar"},"recording":{"url":"http://foobar","method":"POST","format":"audio/mp3","username":"jose","password":"passwd"}}}]}"""
}

public void testMessage() {

def builder = new TropoBuilder()

builder.message(to: 'foo', from: 'bar', network: 'SMS', channel: 'TEXT', timeout: 10, answerOnMedia: false) {
headers(foo: 'foo', bar: 'bar')
recording(url: 'http://foobar', method: 'POST', format: 'audio/mp3', username: 'jose', password: 'passwd')
say('Please say your account number')
}

assert builder.text() == """{"tropo":[{"message":{"to":"foo","from":"bar","network":"SMS","channel":"TEXT","timeout":10,"answerOnMedia":false,"headers":{"foo":"foo","bar":"bar"},"recording":{"url":"http://foobar","method":"POST","format":"audio/mp3","username":"jose","password":"passwd"},"say":[{"value":"Please say your account number"}]}}]}"""
}

public void testRecordWithTranscriptionRequest() {

def builder = new TropoBuilder()

builder.record(name: 'foo', url: 'http://sendme.com/tropo', beep: true, sendTones: true, exitTone: '#') {
transcription(id: 'bling', url:'mailto:jose@voxeo.com', emailFormat: 'encoded')
say('Please say your account number')
choices(value: '[5 DIGITS]')
}

assert builder.text() == """{"tropo":[{"record":{"name":"foo","url":"http://sendme.com/tropo","beep":true,"sendTones":true,"exitTone":"#","transcription":{"id":"bling","url":"mailto:jose@voxeo.com","emailFormat":"encoded"},"say":[{"value":"Please say your account number"}],"choices":{"value":"[5 DIGITS]"}}}]}"""
}


public void testUseMapsInsteadOfMethods() {

def builder = new TropoBuilder()

def help_stop_choices = '0(0,help,i do not know, agent, operator, assistance, representative, real person, human), 9(9,quit,stop,shut up)'
def yes_no_choices = 'true(1,yes,sure,affirmative), false(2,no,no thank you,negative),' + help_stop_choices
builder.ask(name: 'donate_to_id', bargein: true, timeout: 10, silenceTimeout: 10, attempts: 4) {
say([[event: 'timeout', value: 'Sorry, I did not hear anything.'],
[event: 'nomatch:1 nomatch:2 nomatch:3', value: "Sorry, that wasn't a valid answer. You can press or say 1 for 'yes', or 2 for 'no'."],
[value: 'You chose organization foobar. Are you ready to donate to them? If you say no, I will tell you a little more about the organization.'],
[event: 'nomatch:3', value: 'This is your last attempt.']])
choices(value: yes_no_choices)

}

assert builder.text() == """{"tropo":[{"ask":{"name":"donate_to_id","bargein":true,"timeout":10,"silenceTimeout":10,"attempts":4,"say":[{"event":"timeout","value":"Sorry, I did not hear anything."},{"event":"nomatch:1 nomatch:2 nomatch:3","value":"Sorry, that wasn't a valid answer. You can press or say 1 for 'yes', or 2 for 'no'."},{"value":"You chose organization foobar. Are you ready to donate to them? If you say no, I will tell you a little more about the organization."},{"event":"nomatch:3","value":"This is your last attempt."}],"choices":{"value":"true(1,yes,sure,affirmative), false(2,no,no thank you,negative),0(0,help,i do not know, agent, operator, assistance, representative, real person, human), 9(9,quit,stop,shut up)"}}}]}"""
}
public void testVoice() {
def builder = new TropoBuilder()
assert builder.voice() == null
builder = new TropoBuilder(voice: 'barnie')
assert builder.voice() == 'barnie'
builder = new TropoBuilder()
builder.voice = 'barnie'
assert builder.voice() == 'barnie'
}
public void testVoiceHandling() {
def builder = new TropoBuilder()
builder.say('Hi There')
assert builder.json().tropo[0].say[0].voice == null
builder = new TropoBuilder()
builder.say('Hi there', voice: 'barnie')
assert builder.json().tropo[0].say[0].voice == 'barnie'
builder = new TropoBuilder(voice: 'barnie')
builder.say('Hi There')
builder.say('Wow!')
assert builder.json().tropo[0].say[0].voice == 'barnie'
assert builder.json().tropo[1].say[0].voice == 'barnie'

builder = new TropoBuilder(voice: 'barnie')
builder.say('Hi There')
builder.say('Wow!', voice: 'jack')
assert builder.json().tropo[0].say[0].voice == 'barnie'
assert builder.json().tropo[1].say[0].voice == 'jack'
builder = new TropoBuilder()
builder.voice = 'barnie'
builder.say('Hi There')
builder.say('Wow!', voice: 'jack')
assert builder.json().tropo[0].say[0].voice == 'barnie'
assert builder.json().tropo[1].say[0].voice == 'jack'
}
public void testRecognizer() {
def builder = new TropoBuilder()
assert builder.recognizer() == null
builder = new TropoBuilder(recognizer: 'fr-fr')
assert builder.recognizer() == 'fr-fr'
builder = new TropoBuilder()
builder.recognizer = 'fr-fr'
assert builder.recognizer() == 'fr-fr'
}
public void testRecognizerHandling() {
def builder = new TropoBuilder()
builder.ask(name: 'foo', bargein: true, timeout: 30, required: true)
assert builder.json().tropo[0].ask.recognizer == null
builder = new TropoBuilder(recognizer: 'fr-fr')
builder.ask(name: 'foo', bargein: true, timeout: 30, required: true)
assert builder.json().tropo[0].ask.recognizer == 'fr-fr'

builder = new TropoBuilder(recognizer: 'fr-fr')
builder.recognizer = 'fr-fr'
builder.ask(name: 'foo', bargein: true, timeout: 30, required: true)
assert builder.json().tropo[0].ask.recognizer == 'fr-fr'

builder = new TropoBuilder(recognizer: 'fr-fr')
builder.recognizer = 'fr-fr'
builder.ask(name: 'foo', bargein: true, timeout: 30, required: true)
builder.ask(name: 'foo', bargein: true, timeout: 30, required: true, recognizer: 'de-de')
assert builder.json().tropo[0].ask.recognizer == 'fr-fr'
assert builder.json().tropo[1].ask.recognizer == 'de-de'
}
public void testShouldParseJsonStringsAndMaps() {
def json_session = """{"session":{"id":"dih06n","accountId":"33932","timestamp":"2010-01-19T23:18:48.562Z","userType":"HUMAN","to":{"id":"tropomessaging@bot.im","name":"unknown","channel":"TEXT","network":"JABBER"},"from":{"id":"john_doe@gmail.com","name":"unknown","channel":"TEXT","network":"JABBER"}}}"""
def map = new TropoBuilder().parse(json_session)
assert map.session.userType == 'HUMAN'

map = new TropoBuilder().parse(map)
assert map.session.userType == 'HUMAN'
}
public void testAppendOnBlock() {
def builder1 = new TropoBuilder()
builder1.tropo {
on(event:'success',next:'/result.json')
}
def builder2 = new TropoBuilder()
builder2.tropo {
ask(name : 'foo', bargein: true, timeout: 30, required: true) {
say('Please say your account number')
choices(value: '[5 DIGITS]')
}
append(builder1)
}
assert builder2.text() == """{"tropo":[{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true,"say":[{"value":"Please say your account number"}],"choices":{"value":"[5 DIGITS]"}}},{"on":{"event":"success","next":"/result.json"}}]}"""
}

public void testAppendBlockWithMultipleElements() {

def builder1 = new TropoBuilder()
builder1.tropo {
say('Hello tropo')
hangup()
}

def builder2 = new TropoBuilder()
builder2.tropo {
ask(name : 'foo', bargein: true, timeout: 30, required: true) {
say('Please say your account number')
choices(value: '[5 DIGITS]')
}
append(builder1)
}
assert builder2.text() == """{"tropo":[{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true,"say":[{"value":"Please say your account number"}],"choices":{"value":"[5 DIGITS]"}}},{"say":[{"value":"Hello tropo"}]},{"hangup":null}]}"""
}

public void testAppendBlockStart() {

def builder1 = new TropoBuilder()
builder1.tropo {
say('Hello tropo')
hangup()
}

def builder2 = new TropoBuilder()
builder2.tropo {
append(builder1)
ask(name : 'foo', bargein: true, timeout: 30, required: true) {
say('Please say your account number')
choices(value: '[5 DIGITS]')
}
}
assert builder2.text() == """{"tropo":[{"say":[{"value":"Hello tropo"}]},{"hangup":null},{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true,"say":[{"value":"Please say your account number"}],"choices":{"value":"[5 DIGITS]"}}}]}"""
}


public void testAppendBlockMiddle() {

def builder1 = new TropoBuilder()
builder1.tropo {
say('Hello tropo')
hangup()
}

def builder2 = new TropoBuilder()
builder2.tropo {
say('Whatsup')
append(builder1)
ask(name : 'foo', bargein: true, timeout: 30, required: true) {
say('Please say your account number')
choices(value: '[5 DIGITS]')
}
}
assert builder2.text() == """{"tropo":[{"say":[{"value":"Whatsup"}]},{"say":[{"value":"Hello tropo"}]},{"hangup":null},{"ask":{"name":"foo","bargein":true,"timeout":30,"required":true,"say":[{"value":"Please say your account number"}],"choices":{"value":"[5 DIGITS]"}}}]}"""
}

public void testIsEmpty() {

def builder = new TropoBuilder()
assertTrue builder.isEmpty()

builder.tropo {}
assertTrue builder.isEmpty()

builder.tropo {
say('hello')
}
assertFalse builder.isEmpty()

builder.reset()
assertTrue builder.isEmpty()
}
}
Something went wrong with that request. Please try again.