Skip to content

Commit

Permalink
Adding response to NexmoNumberResponseException, changing name of Num…
Browse files Browse the repository at this point in the history
…berInsightsResponseException to NexmoNumberInsightResponseException adding Number tests fixing minor issues with Number class structure
  • Loading branch information
slorello89 committed Apr 20, 2020
1 parent ace90e7 commit 6b7607b
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Nexmo.Api.Test.Unit/NumberInsightsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public void TestFailedAsyncRequest()
//ASSERT
Assert.True(false, "Auto fail because request returned without throwing exception");
}
catch (NumberInsights.NumberInsightResponseException ex)
catch (NumberInsights.NexmoNumberInsightResponseException ex)
{
//ASSERT
Assert.Equal(4, ex.Response.Status);
Expand Down Expand Up @@ -403,7 +403,7 @@ public void TestFailedAdvancedRequest()
//ASSERT
Assert.True(false, "Auto fail because request returned without throwing exception");
}
catch (NumberInsights.NumberInsightResponseException ex)
catch (NumberInsights.NexmoNumberInsightResponseException ex)
{
//ASSERT
Assert.Equal(4, ex.Response.Status);
Expand Down
233 changes: 233 additions & 0 deletions Nexmo.Api.Test.Unit/NumbersTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Nexmo.Api.Numbers;
using Xunit;
namespace Nexmo.Api.Test.Unit
{
public class NumbersTests : TestBase
{
[Theory]
[InlineData(true,true)]
[InlineData(false,false)]
public void TestSearchNumbers(bool passCreds, bool kitchenSink)
{
var expetedResponse = @"{
""count"": 1234,
""numbers"": [
{
""country"": ""GB"",
""msisdn"": ""447700900000"",
""type"": ""mobile-lvn"",
""cost"": ""1.25"",
""features"": [
""VOICE"",
""SMS""
]
}
]
}";
var expectedUri = $"{RestUrl}/number/search";
NumberSearchRequest request;
if (kitchenSink)
{
expectedUri += $"?country=GB&type=mobile-lvn&pattern=12345&search_pattern=0&features=SMS&size=10&index=1&api_key={ApiKey}&api_secret={ApiSecret}&";
request = new NumberSearchRequest { Country = "GB", Type = "mobile-lvn", Pattern = "12345", SearchPattern = 0, Features = "SMS", Size = 10, Index = 1 };
}
else
{
expectedUri += $"?country=GB&api_key={ApiKey}&api_secret={ApiSecret}&";
request = new NumberSearchRequest { Country = "GB" };
}
Setup(expectedUri, expetedResponse);
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);

NumbersSearchResponse response;
if (passCreds)
{
response = client.NumbersClient.GetAvailableNumbers(request, creds);
}
else
{
response = client.NumbersClient.GetAvailableNumbers(request);
}

var number = response.Numbers[0];
Assert.Equal(1234, response.Count);
Assert.Equal("GB", number.Country);
Assert.Equal("447700900000", number.Msisdn);
Assert.Equal("mobile-lvn", number.Type);
Assert.Equal("1.25", number.Cost);
Assert.Equal("VOICE", number.Features[0]);
Assert.Equal("SMS", number.Features[1]);
}

[Theory]
[InlineData(true,true)]
[InlineData(false,false)]
public void TestBuyNumber(bool passCreds, bool kitchenSink)
{
var expectedResponse = @"{
""error-code"": ""200"",
""error-code-label"": ""success""
}";
var expectedUri = $"{RestUrl}/number/buy";
string expectdRequestContent = $"country=GB&msisdn=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
NumberTransactionRequest request;

if (kitchenSink)
{
expectdRequestContent = $"country=GB&msisdn=447700900000&target_api_key=12345&api_key={ApiKey}&api_secret={ApiSecret}&";
request = new NumberTransactionRequest { Country = "GB", Msisdn = "447700900000", TargetApiKey="12345" };
}
else
{
expectdRequestContent = $"country=GB&msisdn=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
request = new NumberTransactionRequest { Country = "GB", Msisdn = "447700900000" };
}

Setup(expectedUri, expectedResponse, expectdRequestContent);

var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
NumberTransactionResponse response;
if (passCreds)
{
response = client.NumbersClient.BuyANumber(request, creds);
}
else
{
response = client.NumbersClient.BuyANumber(request);
}

Assert.Equal("200",response.ErrorCode);
Assert.Equal("success", response.ErrorCodeLabel);
}

[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void TestCancelNumber(bool passCreds, bool kitchenSink)
{
var expectedResponse = @"{
""error-code"": ""200"",
""error-code-label"": ""success""
}";
var expectedUri = $"{RestUrl}/number/cancel";
string expectdRequestContent = $"country=GB&msisdn=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
NumberTransactionRequest request;

if (kitchenSink)
{
expectdRequestContent = $"country=GB&msisdn=447700900000&target_api_key=12345&api_key={ApiKey}&api_secret={ApiSecret}&";
request = new NumberTransactionRequest { Country = "GB", Msisdn = "447700900000", TargetApiKey = "12345" };
}
else
{
expectdRequestContent = $"country=GB&msisdn=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
request = new NumberTransactionRequest { Country = "GB", Msisdn = "447700900000" };
}

Setup(expectedUri, expectedResponse, expectdRequestContent);

var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
NumberTransactionResponse response;
if (passCreds)
{
response = client.NumbersClient.CancelANumber(request, creds);
}
else
{
response = client.NumbersClient.CancelANumber(request);
}

Assert.Equal("200", response.ErrorCode);
Assert.Equal("success", response.ErrorCodeLabel);
}

[Theory]
[InlineData(true,true)]
[InlineData(false,false)]
public void TestUpdateNumber(bool passCreds, bool kitchenSink)
{
var expectedResponse = @"{
""error-code"": ""200"",
""error-code-label"": ""success""
}";
var expectedUri = $"{RestUrl}/number/update";
string expectdRequestContent;
UpdateNumberRequest request;

if (kitchenSink)
{
expectdRequestContent = $"country=GB&msisdn=447700900000&app_id=aaaaaaaa-bbbb-cccc-dddd-0123456789abc&moHttpUrl={HttpUtility.UrlEncode("https://example.com/webhooks/inbound-sms")}&" +
$"moSmppSysType=inbound&voiceCallbackType=tel&voiceCallbackValue=447700900000&voiceStatusCallback={HttpUtility.UrlEncode("https://example.com/webhooks/status")}&" +
$"api_key={ApiKey}&api_secret={ApiSecret}&";
request = new UpdateNumberRequest
{
Country = "GB",
Msisdn = "447700900000",
AppId= "aaaaaaaa-bbbb-cccc-dddd-0123456789abc",
MoHttpUrl= "https://example.com/webhooks/inbound-sms",
MoSmppSysType="inbound",
VoiceCallbackType="tel",
VoiceCallbackValue= "447700900000",
VoiceStatusCallback= "https://example.com/webhooks/status"
};
}
else
{
expectdRequestContent = $"country=GB&msisdn=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
request = new UpdateNumberRequest { Country = "GB", Msisdn = "447700900000" };
}

Setup(expectedUri, expectedResponse, expectdRequestContent);

var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
NumberTransactionResponse response;
if (passCreds)
{
response = client.NumbersClient.UpdateANumber(request, creds);
}
else
{
response = client.NumbersClient.UpdateANumber(request);
}

Assert.Equal("200", response.ErrorCode);
Assert.Equal("success", response.ErrorCodeLabel);
}

[Fact]
public void TestFailedPurchase()
{
var expectedResponse = @"{
""error-code"": ""401"",
""error-code-label"": ""authentifcation failed""
}";
var expectedUri = $"{RestUrl}/number/buy";
string expectdRequestContent = $"country=GB&msisdn=447700900000&api_key={ApiKey}&api_secret={ApiSecret}&";
Setup(expectedUri, expectedResponse, expectdRequestContent);
var request = new NumberTransactionRequest { Country = "GB", Msisdn = "447700900000" };
var creds = Request.Credentials.FromApiKeyAndSecret(ApiKey, ApiSecret);
var client = new NexmoClient(creds);
try
{
client.NumbersClient.BuyANumber(request);
Assert.True(false, "Failin because exception was not thrown");
}
catch (NexmoNumberResponseException ex)
{
Assert.Equal("401", ex.Response.ErrorCode);
Assert.Equal("authentifcation failed", ex.Response.ErrorCodeLabel);
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace Nexmo.Api.NumberInsights
{
public class NumberInsightResponseException : NexmoException
public class NexmoNumberInsightResponseException : NexmoException
{
public NumberInsightResponseException(string message) : base(message) { }
public NexmoNumberInsightResponseException(string message) : base(message) { }

public NumberInsightResponseBase Response { get; set; }
}
Expand Down
4 changes: 2 additions & 2 deletions Nexmo.Api/NumberInsights/NumberInsightClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ public void ValidateNumberInsightResponse(NumberInsightResponseBase response)
{
if(response is AdvancedInsightsAsyncResponse asyncResponse)
{
throw new NumberInsightResponseException($"Advanced Insights Async response failed with status: {asyncResponse.Status} with error message: {asyncResponse.ErrorText}") { Response = response};
throw new NexmoNumberInsightResponseException($"Advanced Insights Async response failed with status: {asyncResponse.Status} with error message: {asyncResponse.ErrorText}") { Response = response};
}
else if(response is BasicInsightResponse basicInsightResponse)
{
throw new NumberInsightResponseException($"Number insight request failed with status: {basicInsightResponse.Status} and error message: {basicInsightResponse.StatusMessage}") { Response=response};
throw new NexmoNumberInsightResponseException($"Number insight request failed with status: {basicInsightResponse.Status} and error message: {basicInsightResponse.StatusMessage}") { Response=response};
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Nexmo.Api/Numbers/NexmoNumberResponseException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ namespace Nexmo.Api.Numbers
public class NexmoNumberResponseException : NexmoException
{
public NexmoNumberResponseException(string message) : base(message) { }
public NumberTransactionResponse Response { get; set; }
}
}
4 changes: 2 additions & 2 deletions Nexmo.Api/Numbers/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class Number
public string Type { get; set; }

[JsonProperty("features")]
public IEnumerable<string> Features { get; set; }
public string[] Features { get; set; }

[JsonProperty("cost")]
public decimal Cost { get; set; }
public string Cost { get; set; }
}
}
3 changes: 1 addition & 2 deletions Nexmo.Api/Numbers/NumbersClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public static void ValidateNumbersResponse(NumberTransactionResponse response)
const string SUCCESS = "200";
if (response.ErrorCode != SUCCESS)
{
throw new NexmoNumberResponseException
($"Number Transaction failed with error code:{response.ErrorCode} and label {response.ErrorCodeLabel}");
throw new NexmoNumberResponseException($"Number Transaction failed with error code:{response.ErrorCode} and label {response.ErrorCodeLabel}"){ Response = response};
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Nexmo.Api/Numbers/UpdateNumberRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class UpdateNumberRequest
[JsonProperty("msisdn")]
public string Msisdn { get; set; }

[JsonProperty("app_id")]
public string AppId { get; set; }

[JsonProperty("moHttpUrl")]
public string MoHttpUrl { get; set; }

Expand Down

0 comments on commit 6b7607b

Please sign in to comment.