Skip to content

Commit

Permalink
adding Ncco Serializations tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slorello89 committed Apr 16, 2020
1 parent c0a27e2 commit 67b66df
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 14 deletions.
205 changes: 204 additions & 1 deletion Nexmo.Api.Test.Unit/NccoTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using Newtonsoft.Json;
using Nexmo.Api.Voice.Nccos;
using Nexmo.Api.Voice.Nccos.Endpoints;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -9,5 +12,205 @@ namespace Nexmo.Api.Test.Unit
{
public class NccoTests : TestBase
{
[Fact]
public void TestRecord()
{
var expectedJson = @"[{""format"":""mp3"",""split"":""conversation"",""channels"":2,""endOnSilence"":""3"",""endOnKey"":""#"",""timeOut"":""60"",""beepStart"":""true"",""eventUrl"":[""https://example.com/record""],""eventMethod"":""POST"",""action"":""record""}]";
var recordAction = new RecordAction
{
Format = RecordAction.AudioFormat.mp3,
Split = "conversation",
Channels = 2,
EndOnSilence = "3",
EndOnKey = "#",
TimeOut = "60",
BeepStart = "true",
EventUrl = new[] { "https://example.com/record" },
EventMethod = "POST"
};

var ncco = new Ncco(recordAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestConversation()
{
var expectedJson = @"[{""name"":""nexmo-conference-standard"",""musicOnHoldUrl"":[""https://example.com/music.mp3""],""startOnEnter"":""true"",""endOnExit"":""false"",""record"":""true"",""canSpeak"":[""6a4d6af0-55a6-4667-be90-8614e4c8e83c""],""canHear"":[""6a4d6af0-55a6-4667-be90-8614e4c8e83c""],""action"":""conversation""}]";
var conversationAction = new ConversationAction
{
Name = "nexmo-conference-standard",
MusicOnHoldUrl = new[] { "https://example.com/music.mp3" },
StartOnEnter = "true",
EndOnExit = "false",
Record = "true",
CanSpeak = new[] { "6a4d6af0-55a6-4667-be90-8614e4c8e83c" },
CanHear = new[] { "6a4d6af0-55a6-4667-be90-8614e4c8e83c" }
};
var ncco = new Ncco(conversationAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestConnect()
{
var expectedJson = @"[{""endpoint"":[{""number"":""447700900001"",""dtmfAnswer"":""2p02p"",""onAnswer"":{""url"":""https://example.com/answer"",""ringbackTone"":""http://example.com/ringbackTone.wav""},""type"":""phone""}],""from"":""447700900000"",""eventType"":""synchronous"",""timeout"":""60"",""limit"":""7200"",""machineDetection"":""continue"",""eventUrl"":[""https://exampe.com/webhooks/events""],""eventMethod"":""POST"",""ringbackTone"":""http://example.com/ringbackTone.wav"",""action"":""connect""}]";
var connectAction = new ConnectAction
{
Endpoint = new[]
{
new PhoneEndpoint
{
Number="447700900001",
DtmfAnswer="2p02p",
OnAnswer=new PhoneEndpoint.Answer
{
Url="https://example.com/answer",
RingbackTone="http://example.com/ringbackTone.wav"
}
}
},
From = "447700900000",
EventType = "synchronous",
Timeout = "60",
Limit = "7200",
MachineDetection = "continue",
EventUrl = new[] { "https://exampe.com/webhooks/events" },
RingbackTone = "http://example.com/ringbackTone.wav",
EventMethod = "POST"
};
var ncco = new Ncco(connectAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestTalk()
{
var expectedJson = @"[{""text"":""Hello World"",""bargeIn"":""true"",""loop"":""2"",""level"":""0"",""voiceName"":""kimberly"",""action"":""talk""}]";
var talkAction = new TalkAction
{
Text = "Hello World",
BargeIn = "true",
Loop = "2",
Level = "0",
VoiceName = "kimberly"
};
var ncco = new Ncco(talkAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestTalkBareBones()
{
var expectedJson = @"[{""text"":""Hello World"",""action"":""talk""}]";
var talkAction = new TalkAction
{
Text = "Hello World"
};
var ncco = new Ncco(talkAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestStream()
{
var expectedJson = @"[{""streamUrl"":[""https://acme.com/streams/music.mp3""],""level"":""0"",""bargeIn"":""true"",""loop"":""2"",""action"":""stream""}]";
var talkAction = new StreamAction
{
StreamUrl = new[] { "https://acme.com/streams/music.mp3"},
BargeIn = "true",
Loop = "2",
Level = "0",
};
var ncco = new Ncco(talkAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestInput()
{
var expectedJson = @"[{""timeOut"":""3"",""maxDigits"":""4"",""submitOnHash"":""true"",""eventUrl"":[""https://example.com/ivr""],""eventMethod"":""POST"",""action"":""input""}]";
var inputAction = new InputAction
{
TimeOut = "3",
MaxDigits = 4,
SubmitOnHash = "true",
EventUrl = new[] { "https://example.com/ivr" },
EventMethod = "POST"
};
var ncco = new Ncco(inputAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestNotify()
{
var expectedJson = @"[{""payload"":{""Bar"":""foo""},""eventUrl"":[""https://example.com/webhooks/events""],""eventMethod"":""POST"",""action"":""notify""}]";
var notifyAction = new NotifyAction
{
EventMethod = "POST",
Payload = new Foo
{
Bar = "foo"
},
EventUrl = new[] { "https://example.com/webhooks/events" }
};
var ncco = new Ncco(notifyAction);
Assert.Equal(expectedJson, ncco.ToString());
}

[Fact]
public void TestWebsocketEndpoint()
{
var expectedJson = @"{""uri"":""wss://example.com/ws"",""content-type"":""audio/l16;rate=16000"",""headers"":{""Bar"":""foo""},""type"":""websocket""}";
var endpoint = new WebsocketEndpoint
{
Uri = "wss://example.com/ws",
ContentType = "audio/l16;rate=16000",
Headers = new Foo { Bar = "foo" }
};
Assert.Equal(expectedJson, JsonConvert.SerializeObject(endpoint));
}

[Fact]
public void TestAppEndpoint()
{
var expectedJson = @"{""user"":""steve"",""type"":""app""}";
var endpoint = new AppEndpoint
{
User= "steve"
};
Assert.Equal(expectedJson, JsonConvert.SerializeObject(endpoint));
}

[Fact]
public void TestSipEndpoint()
{
var expectedJson = @"{""uri"":""sip:rebekka@sip.example.com"",""headers"":{""Bar"":""foo""},""type"":""sip""}";
var endpoint = new SipEndpoint
{
Uri = "sip:rebekka@sip.example.com",
Headers = new Foo { Bar="foo"}
};
Assert.Equal(expectedJson, JsonConvert.SerializeObject(endpoint));
}

[Fact]
public void TestVbcEndpoint()
{
var expectedJson = @"{""extension"":""4567"",""type"":""vbc""}";
var endpoint = new VbcEndpoint
{
Extension = "4567"
};
Assert.Equal(expectedJson, JsonConvert.SerializeObject(endpoint));

}
public class Foo
{
public string Bar { get; set; }
}


}
}
4 changes: 1 addition & 3 deletions Nexmo.Api.Test.Unit/WebhookStructsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ public void TestInput()
""conversation_uuid"":""CON-aaaaaaaa-bbbb-cccc-dddd-0123456789ab"",
""dtmf"":""42"",
""timed_out"":""true"",
""direction"":""outbound"",
""timestamp"":""2020-01-01T12:00:00.000Z""
}";

Expand All @@ -209,8 +208,7 @@ public void TestInput()
Assert.Equal("447700900000", inputWebhook.To);
Assert.Equal("42", inputWebhook.Dtmf);
Assert.Equal("aaaaaaaa-bbbb-cccc-dddd-0123456789ab", inputWebhook.Uuid);
Assert.Equal("CON-aaaaaaaa-bbbb-cccc-dddd-0123456789ab", inputWebhook.ConversationUuid);
Assert.Equal(Direction.outbound, inputWebhook.Direction);
Assert.Equal("CON-aaaaaaaa-bbbb-cccc-dddd-0123456789ab", inputWebhook.ConversationUuid);
Assert.Equal(DateTime.ParseExact("2020-01-01T12:00:00.000Z", "yyyy-MM-dd'T'HH:mm:ss.fff'Z'", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal |
DateTimeStyles.AdjustToUniversal), (inputWebhook.TimeStamp));

Expand Down
4 changes: 0 additions & 4 deletions Nexmo.Api/Voice/EventWebhooks/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@ public class Input : Event

[JsonProperty("to")]
public string To { get; set; }

[JsonProperty("direction")]
public Direction Direction { get; set; }

}
}
2 changes: 2 additions & 0 deletions Nexmo.Api/Voice/Nccos/ConnectAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class ConnectAction : NccoAction

[JsonProperty("eventMethod")]
public string EventMethod { get; set; }
[JsonProperty("ringbackTone")]
public string RingbackTone { get; set; }

public ConnectAction()
{
Expand Down
6 changes: 3 additions & 3 deletions Nexmo.Api/Voice/Nccos/ConversationAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ConversationAction : NccoAction
public string Name { get; set; }

[JsonProperty("musicOnHoldUrl")]
public string MusicOnHoldUrl { get; set; }
public string[] MusicOnHoldUrl { get; set; }

[JsonProperty("startOnEnter")]
public string StartOnEnter { get; set; }
Expand All @@ -26,10 +26,10 @@ public class ConversationAction : NccoAction
public string EventMethod { get; set; }

[JsonProperty("canSpeak")]
public string CanSpeak { get; set; }
public string[] CanSpeak { get; set; }

[JsonProperty("canHear")]
public string CanHear { get; set; }
public string[] CanHear { get; set; }

public ConversationAction()
{
Expand Down
10 changes: 9 additions & 1 deletion Nexmo.Api/Voice/Nccos/Endpoints/PhoneEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ public class PhoneEndpoint : Endpoint
public string DtmfAnswer { get; set; }

[JsonProperty("onAnswer")]
public string OnAnswer { get; set; }
public Answer OnAnswer { get; set; }

public PhoneEndpoint()
{
Type = EndpointType.phone;
}

public class Answer
{
[JsonProperty("url")]
public string Url { get; set; }

[JsonProperty("ringbackTone")]
public string RingbackTone { get; set; }
}
}
}
2 changes: 1 addition & 1 deletion Nexmo.Api/Voice/Nccos/Endpoints/SipEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class SipEndpoint : Endpoint
public string Uri { get; set; }

[JsonProperty("headers")]
public Dictionary<string,string> Headers { get; set; }
public object Headers { get; set; }

public SipEndpoint()
{
Expand Down
2 changes: 1 addition & 1 deletion Nexmo.Api/Voice/Nccos/Endpoints/WebsocketEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class WebsocketEndpoint : Endpoint
public string ContentType { get; set; }

[JsonProperty("headers")]
public Dictionary<string,string> Headers { get; set; }
public object Headers { get; set; }

public WebsocketEndpoint()
{
Expand Down

0 comments on commit 67b66df

Please sign in to comment.