Skip to content

Commit

Permalink
Correcting enum capitalisation for Messaging and Number Insights
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-lethargic committed May 23, 2022
1 parent 0a8bd54 commit 7bd3a17
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 83 deletions.
4 changes: 2 additions & 2 deletions Vonage.Test.Unit/MessagingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void SendSmsWithAllPropertiesSet(bool passCreds)
Text = "Hello World!",
Title = "welcome",
Ttl = 900000,
Type = SmsType.text,
Type = SmsType.Text,
Udh = "06050415811581",
Validity = "300000",
Vcal = "none",
Expand Down Expand Up @@ -89,7 +89,7 @@ public async Task SendSmsAsyncWithAllPropertiesSet(bool passCreds)
Text = "Hello World!",
Title = "welcome",
Ttl = 900000,
Type = SmsType.text,
Type = SmsType.Text,
Udh = "06050415811581",
Validity = "300000",
Vcal = "none",
Expand Down
34 changes: 17 additions & 17 deletions Vonage.Test.Unit/NumberInsightsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public void TestStandardNIRequest(bool passCreds, bool kitchenSink)
Assert.Equal("Acme Inc", response.Roaming.RoamingNetworkName);
Assert.Equal("12345", response.Roaming.RoamingNetworkCode);
Assert.Equal("US", response.Roaming.RoamingCountryCode);
Assert.Equal(RoamingStatus.roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.not_ported, response.Ported);
Assert.Equal(RoamingStatus.Roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.NotPorted, response.Ported);
Assert.Equal("12345", response.OriginalCarrier.NetworkCode);
Assert.Equal("Acme Inc", response.OriginalCarrier.Name);
Assert.Equal("GB", response.OriginalCarrier.Country);
Expand Down Expand Up @@ -231,8 +231,8 @@ public void TestStandardNIRequestWithoutRoaming(bool passCreds, bool kitchenSink
Assert.Equal("John", response.CallerIdentity.FirstName);
Assert.Equal("John Smith", response.CallerIdentity.CallerName);
Assert.Equal(CallerType.consumer, response.CallerIdentity.CallerType);
Assert.Equal(RoamingStatus.unknown, response.Roaming.Status);
Assert.Equal(PortedStatus.not_ported, response.Ported);
Assert.Equal(RoamingStatus.Unknown, response.Roaming.Status);
Assert.Equal(PortedStatus.NotPorted, response.Ported);
Assert.Equal("12345", response.OriginalCarrier.NetworkCode);
Assert.Equal("Acme Inc", response.OriginalCarrier.Name);
Assert.Equal("GB", response.OriginalCarrier.Country);
Expand Down Expand Up @@ -337,8 +337,8 @@ public void TestStandardNIRequestWithNullCarrier(bool passCreds, bool kitchenSin
Assert.Equal("John", response.CallerIdentity.FirstName);
Assert.Equal("John Smith", response.CallerIdentity.CallerName);
Assert.Equal(CallerType.consumer, response.CallerIdentity.CallerType);
Assert.Equal(RoamingStatus.unknown, response.Roaming.Status);
Assert.Equal(PortedStatus.not_ported, response.Ported);
Assert.Equal(RoamingStatus.Unknown, response.Roaming.Status);
Assert.Equal(PortedStatus.NotPorted, response.Ported);
Assert.Equal("12345", response.OriginalCarrier.NetworkCode);
Assert.Equal("Acme Inc", response.OriginalCarrier.Name);
Assert.Equal("GB", response.OriginalCarrier.Country);
Expand Down Expand Up @@ -409,8 +409,8 @@ public async void TestStandardNIRequestAsync(bool passCreds, bool kitchenSink)
Assert.Equal("Acme Inc", response.Roaming.RoamingNetworkName);
Assert.Equal("12345", response.Roaming.RoamingNetworkCode);
Assert.Equal("US", response.Roaming.RoamingCountryCode);
Assert.Equal(RoamingStatus.roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.not_ported, response.Ported);
Assert.Equal(RoamingStatus.Roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.NotPorted, response.Ported);
Assert.Equal("12345", response.OriginalCarrier.NetworkCode);
Assert.Equal("Acme Inc", response.OriginalCarrier.Name);
Assert.Equal("GB", response.OriginalCarrier.Country);
Expand Down Expand Up @@ -473,7 +473,7 @@ public void TestAdvancedNIRequestSync(bool passCreds, bool kitchenSink)
}

//ASSERT
Assert.Equal(NumberReachability.reachable, response.Reachable);
Assert.Equal(NumberReachability.Reachable, response.Reachable);
Assert.Equal(NumberValidity.valid, response.ValidNumber);
Assert.Equal("Success", response.LookupOutcomeMessage);
Assert.Equal(0, response.LookupOutcome);
Expand All @@ -488,8 +488,8 @@ public void TestAdvancedNIRequestSync(bool passCreds, bool kitchenSink)
Assert.Equal("Acme Inc", response.Roaming.RoamingNetworkName);
Assert.Equal("12345", response.Roaming.RoamingNetworkCode);
Assert.Equal("US", response.Roaming.RoamingCountryCode);
Assert.Equal(RoamingStatus.roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.not_ported, response.Ported);
Assert.Equal(RoamingStatus.Roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.NotPorted, response.Ported);
Assert.Equal("12345", response.OriginalCarrier.NetworkCode);
Assert.Equal("Acme Inc", response.OriginalCarrier.Name);
Assert.Equal("GB", response.OriginalCarrier.Country);
Expand Down Expand Up @@ -552,7 +552,7 @@ public async void TestAdvancedNIRequestSyncAsync(bool passCreds, bool kitchenSin
}

//ASSERT
Assert.Equal(NumberReachability.reachable, response.Reachable);
Assert.Equal(NumberReachability.Reachable, response.Reachable);
Assert.Equal(NumberValidity.valid, response.ValidNumber);
Assert.Equal("Success", response.LookupOutcomeMessage);
Assert.Equal(0, response.LookupOutcome);
Expand All @@ -567,8 +567,8 @@ public async void TestAdvancedNIRequestSyncAsync(bool passCreds, bool kitchenSin
Assert.Equal("Acme Inc", response.Roaming.RoamingNetworkName);
Assert.Equal("12345", response.Roaming.RoamingNetworkCode);
Assert.Equal("US", response.Roaming.RoamingCountryCode);
Assert.Equal(RoamingStatus.roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.not_ported, response.Ported);
Assert.Equal(RoamingStatus.Roaming, response.Roaming.Status);
Assert.Equal(PortedStatus.NotPorted, response.Ported);
Assert.Equal("12345", response.OriginalCarrier.NetworkCode);
Assert.Equal("Acme Inc", response.OriginalCarrier.Name);
Assert.Equal("GB", response.OriginalCarrier.Country);
Expand Down Expand Up @@ -855,9 +855,9 @@ public void AdvancedNIRequestSyncWithNotRoamingStatus()
Assert.Equal("mobile", response.OriginalCarrier.NetworkType);

Assert.Equal(NumberValidity.valid, response.ValidNumber);
Assert.Equal(NumberReachability.reachable, response.Reachable);
Assert.Equal(PortedStatus.not_ported, response.Ported);
Assert.Equal(RoamingStatus.not_roaming, response.Roaming.Status);
Assert.Equal(NumberReachability.Reachable, response.Reachable);
Assert.Equal(PortedStatus.NotPorted, response.Ported);
Assert.Equal(RoamingStatus.NotRoaming, response.Roaming.Status);

Assert.Null(response.Roaming.RoamingNetworkName);
Assert.Null(response.Roaming.RoamingCountryCode);
Expand Down
3 changes: 2 additions & 1 deletion Vonage.Test.Unit/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -60,7 +61,7 @@ private static string AssemblyDirectory

protected void Setup(string uri, string responseContent, string requestContent = null, HttpStatusCode expectedCode = HttpStatusCode.OK)
{
Setup(uri, new StringContent(responseContent), expectedCode, requestContent);
Setup(uri, new StringContent(responseContent, Encoding.UTF8, "application/json"), expectedCode, requestContent);
}

protected void Setup(string uri, byte[] responseContent, HttpStatusCode expectedCode = HttpStatusCode.OK)
Expand Down
24 changes: 24 additions & 0 deletions Vonage/EnumExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Linq;
using System.Runtime.Serialization;

namespace Vonage
{
internal static class EnumExtensions
{
internal static T ToEnum<T>(this string str)
where T : Enum
{
var enumType = typeof(T);
foreach (var name in Enum.GetNames(enumType))
{
var enumMemberAttribute = ((EnumMemberAttribute[]) enumType.GetField(name)
.GetCustomAttributes(typeof(EnumMemberAttribute), true)).Single();
if (enumMemberAttribute.Value == str)
return (T) Enum.Parse(enumType, name);
}

return default;
}
}
}
4 changes: 2 additions & 2 deletions Vonage/Messaging/ISmsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface ISmsClient
/// <param name="type">The format of the message body.</param>
/// <param name="creds"></param>
/// <returns></returns>
Task<SendSmsResponse> SendAnSmsAsync(string from, string to, string text, SmsType type = SmsType.text, Credentials creds = null);
Task<SendSmsResponse> SendAnSmsAsync(string from, string to, string text, SmsType type = SmsType.Text, Credentials creds = null);

/// <summary>
/// Send an outbound SMS from your Vonage account
Expand All @@ -41,6 +41,6 @@ public interface ISmsClient
/// <param name="type">The format of the message body.</param>
/// <param name="creds"></param>
/// <returns></returns>
SendSmsResponse SendAnSms(string from, string to, string text, SmsType type = SmsType.text, Credentials creds = null);
SendSmsResponse SendAnSms(string from, string to, string text, SmsType type = SmsType.Text, Credentials creds = null);
}
}
4 changes: 2 additions & 2 deletions Vonage/Messaging/SmsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public SendSmsResponse SendAnSms(SendSmsRequest request, Credentials creds = nul
return result;
}

public Task<SendSmsResponse> SendAnSmsAsync(string from, string to, string text, SmsType type = SmsType.text, Credentials creds = null)
public Task<SendSmsResponse> SendAnSmsAsync(string from, string to, string text, SmsType type = SmsType.Text, Credentials creds = null)
{
return SendAnSmsAsync(new SendSmsRequest { From = from, To = to, Type = type, Text = text }, creds);
}

public SendSmsResponse SendAnSms(string from, string to, string text, SmsType type = SmsType.text, Credentials creds = null)
public SendSmsResponse SendAnSms(string from, string to, string text, SmsType type = SmsType.Text, Credentials creds = null)
{
return SendAnSms(new SendSmsRequest { From = from, To = to, Type = type, Text = text }, creds);
}
Expand Down
27 changes: 18 additions & 9 deletions Vonage/Messaging/SmsType.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace Vonage.Messaging
{
public enum SmsType
{
text = 1,
binary = 2,
wappush = 3,
unicode = 4,
vcal = 5,
vcar = 6
[EnumMember(Value = "text")]
Text = 1,

[EnumMember(Value = "binary")]
Binary = 2,

[EnumMember(Value = "wappush")]
Wappush = 3,

[EnumMember(Value = "unicode")]
Unicode = 4,

[EnumMember(Value = "vcal")]
VCal = 5,

[EnumMember(Value = "vcar")]
VCar = 6
}
}
18 changes: 7 additions & 11 deletions Vonage/NumberInsights/NumberConverterRoaming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@ namespace Vonage.NumberInsights
{
internal class NumberRoamingConverter : JsonConverter
{
public override bool CanWrite => false;

public override bool CanConvert(Type objectType)
{
return objectType == typeof(Roaming);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.Path.ToString() == "roaming" && reader.Value == null)
if (reader.Path == "roaming" && reader.Value == null)
{
if (reader.TokenType == JsonToken.StartObject) {
var obj = JObject.Load(reader);

RoamingStatus status;
if(!Enum.TryParse(obj["status"].ToString(), out status))
{
status = RoamingStatus.unknown;
}

RoamingStatus status = obj["status"].ToString().ToEnum<RoamingStatus>();

return new Roaming {
Status = status,
RoamingCountryCode = obj["roaming_country_code"]?.ToString(),
Expand All @@ -35,19 +33,17 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
return null;
}

if (reader.Path.ToString() == "roaming" && reader.Value.ToString() == RoamingStatus.unknown.ToString())
if (reader.Path == "roaming" && reader.Value.ToString() == "unknown")
{
return new Roaming
{
Status = RoamingStatus.unknown
Status = RoamingStatus.Unknown
};
}

throw new FormatException("Invalid Number Insights Roaming data detected");
}

public override bool CanWrite => base.CanWrite;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
Expand Down
23 changes: 15 additions & 8 deletions Vonage/NumberInsights/PortedStatus.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace Vonage.NumberInsights
{
public enum PortedStatus
{
unknown,
ported,
not_ported,
assumed_not_ported,
assumed_ported
[EnumMember(Value = "unknown")]
Unknown,

[EnumMember(Value = "ported")]
Ported,

[EnumMember(Value = "not_ported")]
NotPorted,

[EnumMember(Value = "assumed_not_ported")]
AssumedNotPorted,

[EnumMember(Value = "assumed_ported")]
AssumedPorted
}
}
27 changes: 18 additions & 9 deletions Vonage/NumberInsights/Reachability.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;

namespace Vonage.NumberInsights
{
public enum NumberReachability
{
unknown,
reachable,
undeliverable,
absent,
bad_number,
blacklisted
[EnumMember(Value = "unknown")]
Unknown,

[EnumMember(Value = "reachable")]
Reachable,

[EnumMember(Value = "undeliverable")]
Undeliverable,

[EnumMember(Value = "absent")]
Absent,

[EnumMember(Value = "bad_number")]
BadNumber,

[EnumMember(Value = "blacklisted")]
Blacklisted
}
}
18 changes: 12 additions & 6 deletions Vonage/NumberInsights/RoamingStatus.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Vonage.NumberInsights
{
[JsonConverter(typeof(StringEnumConverter))]
public enum RoamingStatus
{
unknown,
roaming,
not_roaming
[EnumMember(Value = "unknown")]
Unknown,

[EnumMember(Value = "roaming")]
Roaming,

[EnumMember(Value = "not_roaming")]
NotRoaming
}
}
Loading

0 comments on commit 7bd3a17

Please # to comment.