Skip to content

Commit

Permalink
SWI-4281 Add addVerbs method to Root Class (#16)
Browse files Browse the repository at this point in the history
* SWI-4281 Add `addVerbs` method to Root Class

* add tests

* add tests for other nestable verbs

* update name of gather add verbs method

* update all nested verb accepting methods to accept single or multiple verbs

* update tests

* comment

* small test updates
  • Loading branch information
ckoegel authored Jan 16, 2024
1 parent 0ca692e commit f557ef7
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 48 deletions.
4 changes: 2 additions & 2 deletions models/bxml/Bxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export class Bxml extends Root {

/**
* Creates an instance of Bxml
* @param nestedVerbs [Array<Verb>] Array of nested verbs
* @param nestedVerbs [Verb | Array<Verb>] Nested Verb or Array of Nested Verbs
*/
constructor(nestedVerbs?: Verb[]) {
constructor(nestedVerbs?: Verb | Verb[]) {
super('Bxml', nestedVerbs)
}
}
14 changes: 11 additions & 3 deletions models/bxml/NestableVerb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ export class NestableVerb extends Verb {
* @param name [string] Name of the XML element
* @param content [string] Content of the XML element
* @param attributes [object] Attributes of the XML element
* @param nestedVerbs [Array<Verb>] Array of nested verbs
* @param nestedVerbs [Verb | Array<Verb>] Nested Verb or Array of Nested Verbs
*/
constructor(name: string, content?: string, attributes?: object, nestedVerbs?: Verb[]) {
constructor(name: string, content?: string, attributes?: object, nestedVerbs?: Verb | Verb[]) {
super(name, content, attributes);
this.nestedVerbs = nestedVerbs || [];
if (nestedVerbs) {
if (nestedVerbs instanceof Array) {
this.nestedVerbs = nestedVerbs;
} else {
this.nestedVerbs = [nestedVerbs];
}
} else {
this.nestedVerbs = [];
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions models/bxml/Response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export class Response extends Root {

/**
* Creates an instance of Response
* @param nestedVerbs [Array<Verb>] Array of nested verbs
* @param nestedVerbs [Verb | Array<Verb>] Nested Verb or Array of Nested Verbs
*/
constructor(nestedVerbs?: Verb[]) {
constructor(nestedVerbs?: Verb | Verb[]) {
super('Response', nestedVerbs)
}
}
22 changes: 19 additions & 3 deletions models/bxml/Root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ export class Root {
/**
* Creates an instance of Root
* @param name [string] Name of the XML element
* @param nestedVerbs [Array<Verb>] Array of nested verbs
* @param nestedVerbs [Verb | Array<Verb>] Nested Verb or Array of Nested Verbs
*/
constructor(name: string, nestedVerbs?: Verb[]) {
constructor(name: string, nestedVerbs?: Verb | Verb[]) {
this.name = name;
this.nestedVerbs = nestedVerbs;
if (nestedVerbs) {
if (nestedVerbs instanceof Array) {
this.nestedVerbs = nestedVerbs;
} else {
this.nestedVerbs = [nestedVerbs];
}
} else {
this.nestedVerbs = [];
}
}

/**
Expand All @@ -33,6 +41,14 @@ export class Root {
return xml;
}

/**
* Add a verb or verbs to the root element
* @param {Verb | Verb[]} verbs The verb or verbs to add
*/
addVerbs(verbs: Verb | Verb[]): void {
this.nestedVerbs = this.nestedVerbs.concat(verbs);
}

/**
* Return BXML representation of this element
* @param options XML Serialization options
Expand Down
12 changes: 6 additions & 6 deletions models/bxml/verbs/Gather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export class Gather extends NestableVerb {
/**
* Creates an instance of Gather
* @param {GatherAttributes} attributes The attributes to add to the element
* @param {AudioVerbs} audioVerbs The audio verbs to be played
* @param {PlayAudio | SpeakSentence | AudioVerbs} audioVerbs The audio verbs to be played
*/
constructor(attributes?: GatherAttributes, audioVerbs?: AudioVerbs) {
constructor(attributes?: GatherAttributes, audioVerbs?: PlayAudio | SpeakSentence | AudioVerbs) {
super('Gather', undefined, attributes, audioVerbs);
}

Expand All @@ -50,10 +50,10 @@ export class Gather extends NestableVerb {
}

/**
* Add an audio verb to the gather
* @param {AudioVerbs} audioVerb The audio verb or verbs to add
* Add an audio verb or verbs to the gather
* @param {PlayAudio | SpeakSentence | AudioVerbs} audioVerbs The audio verb or verbs to add
*/
addAudioVerb(audioVerb: PlayAudio | SpeakSentence | AudioVerbs): void {
this.nestedVerbs = this.nestedVerbs.concat(audioVerb);
addAudioVerbs(audioVerbs: PlayAudio | SpeakSentence | AudioVerbs): void {
this.nestedVerbs = this.nestedVerbs.concat(audioVerbs);
}
}
12 changes: 6 additions & 6 deletions models/bxml/verbs/StartStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ export class StartStream extends NestableVerb {
/**
* Creates an instance of StartStream
* @param {StartStreamAttributes} attributes The attributes to add to the element
* @param {StreamParam[]} streamParams The stream params to add to the element
* @param {StreamParam | StreamParam[]} streamParams The stream params to add to the element
*/
constructor(attributes?: StartStreamAttributes, streamParams?: StreamParam[]) {
constructor(attributes?: StartStreamAttributes, streamParams?: StreamParam | StreamParam[]) {
super('StartStream', undefined, attributes, streamParams);
}

/**
* Add a stream param to the StartStream
* @param {StreamParam} streamParam The stream param to add
* Add a stream param or params to the StartStream
* @param {StreamParam | StreamParam[]} streamParams The stream param or params to add
*/
addStreamParam(streamParam: StreamParam | StreamParam[]): void {
this.nestedVerbs = this.nestedVerbs.concat(streamParam);
addStreamParams(streamParams: StreamParam | StreamParam[]): void {
this.nestedVerbs = this.nestedVerbs.concat(streamParams);
}
}
12 changes: 6 additions & 6 deletions models/bxml/verbs/StartTranscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ export class StartTranscription extends NestableVerb {
/**
* Creates an instance of StartTranscription
* @param {StartTranscriptionAttributes} attributes The attributes to add to the element
* @param {CustomParam[]} customParams The custom params to add to the element
* @param {CustomParam | CustomParam[]} customParams The custom params to add to the element
*/
constructor(attributes?: StartTranscriptionAttributes, customParams?: CustomParam[]) {
constructor(attributes?: StartTranscriptionAttributes, customParams?: CustomParam | CustomParam[]) {
super('StartTranscription', undefined, attributes, customParams);
}

/**
* Add a custom param to the StartTranscription
* @param {CustomParam} customParam The custom param to add
* Add a custom param or params to the StartTranscription
* @param {CustomParam | CustomParam[]} customParams The custom param or params to add
*/
addCustomParam(customParam: CustomParam | CustomParam[]): void {
this.nestedVerbs = this.nestedVerbs.concat(customParam);
addCustomParams(customParams: CustomParam | CustomParam[]): void {
this.nestedVerbs = this.nestedVerbs.concat(customParams);
}
}
12 changes: 6 additions & 6 deletions models/bxml/verbs/Transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ export class Transfer extends NestableVerb {
/**
* Creates an instance of Transfer
* @param {TransferAttributes} attributes The attributes to add to the element
* @param {NumberEntities} transferTo The number entities to transfer to
* @param {PhoneNumber | SipUri | NumberEntities} transferTo The number entities to transfer to
*/
constructor(attributes?: TransferAttributes, transferTo?: NumberEntities) {
constructor(attributes?: TransferAttributes, transferTo?: PhoneNumber | SipUri | NumberEntities) {
super('Transfer', undefined, attributes, transferTo);
}

/**
* Add a number entity to the transfer
* @param {NumberEntities} recipient The number entity or entities to add
* Add a number entity or entities to the transfer
* @param {PhoneNumber | SipUri | NumberEntities} recipients The number entity or entities to add
*/
addTransferRecipient(recipient: PhoneNumber | SipUri | NumberEntities): void {
this.nestedVerbs = this.nestedVerbs.concat(recipient);
addTransferRecipients(recipients: PhoneNumber | SipUri | NumberEntities): void {
this.nestedVerbs = this.nestedVerbs.concat(recipients);
}
}
33 changes: 33 additions & 0 deletions tests/unit/bxml/Bxml.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const { Bxml } = require('../../../models/bxml/Bxml');
const { PauseRecording } = require('../../../models/bxml/verbs/PauseRecording');
const { Root } = require('../../../models/bxml/Root');

describe('Bxml', () => {
const pauseRecording = new PauseRecording();

test('should create a bxml object', () => {
const bxml = new Bxml();
const expected = '<?xml version="1.0" encoding="UTF-8"?><Bxml/>';
Expand All @@ -10,4 +13,34 @@ describe('Bxml', () => {
expect(bxml).toBeInstanceOf(Root);
expect(bxml.toBxml()).toBe(expected);
});

test('should initialize with a single nested verb', () => {
const bxml = new Bxml(pauseRecording);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Bxml><PauseRecording/></Bxml>';

expect(bxml.toBxml()).toBe(expected);
});

test('should initialize with multiple nested verbs', () => {
const bxml = new Bxml([pauseRecording, pauseRecording]);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Bxml><PauseRecording/><PauseRecording/></Bxml>';

expect(bxml.toBxml()).toBe(expected);
});

test('should add a single nested verb', () => {
const bxml = new Bxml();
bxml.addVerbs(pauseRecording);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Bxml><PauseRecording/></Bxml>';

expect(bxml.toBxml()).toBe(expected);
});

test('should add multiple nested verbs', () => {
const bxml = new Bxml();
bxml.addVerbs([pauseRecording, pauseRecording]);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Bxml><PauseRecording/><PauseRecording/></Bxml>';

expect(bxml.toBxml()).toBe(expected);
});
});
37 changes: 35 additions & 2 deletions tests/unit/bxml/Response.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
const { Response } = require('../../../models/bxml/Response');
const { PauseRecording } = require('../../../models/bxml/verbs/PauseRecording');
const { Root } = require('../../../models/bxml/Root');

describe('Bxml', () => {
test('should create a bxml object', () => {
describe('Response', () => {
const pauseRecording = new PauseRecording();

test('should create a response object', () => {
const response = new Response();
const expected = '<?xml version="1.0" encoding="UTF-8"?><Response/>';

expect(response).toBeInstanceOf(Response);
expect(response).toBeInstanceOf(Root);
expect(response.toBxml()).toBe(expected);
});

test('should initialize with a single nested verb', () => {
const response = new Response(pauseRecording);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Response><PauseRecording/></Response>';

expect(response.toBxml()).toBe(expected);
});

test('should initialize with multiple nested verbs', () => {
const response = new Response([pauseRecording, pauseRecording]);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Response><PauseRecording/><PauseRecording/></Response>';

expect(response.toBxml()).toBe(expected);
});

test('should add a single nested verb', () => {
const response = new Response();
response.addVerbs(pauseRecording);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Response><PauseRecording/></Response>';

expect(response.toBxml()).toBe(expected);
});

test('should add multiple nested verbs', () => {
const response = new Response();
response.addVerbs([pauseRecording, pauseRecording]);
const expected = '<?xml version="1.0" encoding="UTF-8"?><Response><PauseRecording/><PauseRecording/></Response>';

expect(response.toBxml()).toBe(expected);
});
});
14 changes: 11 additions & 3 deletions tests/unit/bxml/verbs/Gather.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('Gather', () => {
});

test('should create a Gather Verb with nested PlayAudio and SpeakSentence', () => {
let gather = new Gather(attributes, [playAudio]);
let gather = new Gather(attributes, playAudio);
const expected = '<Gather gatherUrl="https://initial.com" gatherMethod="POST" gatherFallbackUrl="https://initial.com" gatherFallbackMethod="POST" username="initialUsername" password="initialPassword" fallbackUsername="initialFallbackUsername" fallbackPassword="initialFallbackPassword" tag="initialTag" terminatingDigits="5" maxDigits="5" interDigitTimeout="5" firstDigitTimeout="5" repeatCount="5"><PlayAudio>https://audio.url/audio1.wav</PlayAudio></Gather>';
const expectedSingle = '<Gather gatherUrl="https://initial.com" gatherMethod="POST" gatherFallbackUrl="https://initial.com" gatherFallbackMethod="POST" username="initialUsername" password="initialPassword" fallbackUsername="initialFallbackUsername" fallbackPassword="initialFallbackPassword" tag="initialTag" terminatingDigits="5" maxDigits="5" interDigitTimeout="5" firstDigitTimeout="5" repeatCount="5"><PlayAudio>https://audio.url/audio1.wav</PlayAudio><SpeakSentence><lang xml:lang="es-MX">Hola</lang>nodejs speak sentence <emphasis>SSML test</emphasis></SpeakSentence></Gather>';
const expectedMultiple = '<Gather gatherUrl="https://initial.com" gatherMethod="POST" gatherFallbackUrl="https://initial.com" gatherFallbackMethod="POST" username="initialUsername" password="initialPassword" fallbackUsername="initialFallbackUsername" fallbackPassword="initialFallbackPassword" tag="initialTag" terminatingDigits="5" maxDigits="5" interDigitTimeout="5" firstDigitTimeout="5" repeatCount="5"><PlayAudio>https://audio.url/audio1.wav</PlayAudio><SpeakSentence><lang xml:lang="es-MX">Hola</lang>nodejs speak sentence <emphasis>SSML test</emphasis></SpeakSentence><SpeakSentence><lang xml:lang="es-MX">Hola</lang>nodejs speak sentence <emphasis>SSML test</emphasis></SpeakSentence><PlayAudio>https://audio.url/audio1.wav</PlayAudio></Gather>';
Expand All @@ -43,10 +43,18 @@ describe('Gather', () => {
expect(gather).toBeInstanceOf(Verb);
expect(gather.toBxml()).toBe(expected);

gather.addAudioVerb(speakSentence);
gather.addAudioVerbs(speakSentence);
expect(gather.toBxml()).toBe(expectedSingle);

gather.addAudioVerb([speakSentence, playAudio]);
gather.addAudioVerbs([speakSentence, playAudio]);
expect(gather.toBxml()).toBe(expectedMultiple);
});

test('should test the addAudioVerbs method when no verbs are initially nested', () => {
const gather = new Gather(attributes);
const expected = '<Gather gatherUrl="https://initial.com" gatherMethod="POST" gatherFallbackUrl="https://initial.com" gatherFallbackMethod="POST" username="initialUsername" password="initialPassword" fallbackUsername="initialFallbackUsername" fallbackPassword="initialFallbackPassword" tag="initialTag" terminatingDigits="5" maxDigits="5" interDigitTimeout="5" firstDigitTimeout="5" repeatCount="5"><PlayAudio>https://audio.url/audio1.wav</PlayAudio></Gather>';

gather.addAudioVerbs(playAudio);
expect(gather.toBxml()).toBe(expected);
});
});
14 changes: 11 additions & 3 deletions tests/unit/bxml/verbs/StartStream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('StartStream', () => {
});

test('should create a StartStream Verb with nested StreamParams', () => {
let startStream = new StartStream(attributes, [streamParam1]);
let startStream = new StartStream(attributes, streamParam1);
const expected = '<StartStream name="initialName" tracks="inbound" destination="https://initial.com" streamEventUrl="https://initial.com" streamEventMethod="POST" username="initialUsername" password="initialPassword"><StreamParam name="streamParamName1" value="streamParamValue1"/></StartStream>';
const expectedSingle = '<StartStream name="initialName" tracks="inbound" destination="https://initial.com" streamEventUrl="https://initial.com" streamEventMethod="POST" username="initialUsername" password="initialPassword"><StreamParam name="streamParamName1" value="streamParamValue1"/><StreamParam name="streamParamName2" value="streamParamValue2"/></StartStream>';
const expectedMultiple = '<StartStream name="initialName" tracks="inbound" destination="https://initial.com" streamEventUrl="https://initial.com" streamEventMethod="POST" username="initialUsername" password="initialPassword"><StreamParam name="streamParamName1" value="streamParamValue1"/><StreamParam name="streamParamName2" value="streamParamValue2"/><StreamParam name="streamParamName1" value="streamParamValue1"/><StreamParam name="streamParamName2" value="streamParamValue2"/></StartStream>';
Expand All @@ -35,10 +35,18 @@ describe('StartStream', () => {
expect(startStream).toBeInstanceOf(Verb);
expect(startStream.toBxml()).toBe(expected);

startStream.addStreamParam(streamParam2);
startStream.addStreamParams(streamParam2);
expect(startStream.toBxml()).toBe(expectedSingle);

startStream.addStreamParam([streamParam1, streamParam2]);
startStream.addStreamParams([streamParam1, streamParam2]);
expect(startStream.toBxml()).toBe(expectedMultiple);
});

test('should test the addStreamParams method when no verbs are initially nested', () => {
const startStream = new StartStream(attributes);
const expected = '<StartStream name="initialName" tracks="inbound" destination="https://initial.com" streamEventUrl="https://initial.com" streamEventMethod="POST" username="initialUsername" password="initialPassword"><StreamParam name="streamParamName1" value="streamParamValue1"/></StartStream>';

startStream.addStreamParams(streamParam1);
expect(startStream.toBxml()).toBe(expected);
});
});
Loading

0 comments on commit f557ef7

Please sign in to comment.