Skip to content

Commit

Permalink
Merge branch 'release/0.76.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed Apr 26, 2024
2 parents dfa2e0d + 7ed13a6 commit 111a2e1
Show file tree
Hide file tree
Showing 29 changed files with 492 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Source/ZoomNet.IntegrationTests/Tests/Chatbot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ZoomNet.IntegrationTests.Tests;

public class Chatbot : IIntegrationTest
{
/// <inheritdoc />
/// <inheritdoc/>
public async Task RunAsync(User myUser, string[] myPermissions, IZoomClient client, TextWriter log, CancellationToken cancellationToken)
{
var accountId = Environment.GetEnvironmentVariable("ZOOM_OAUTH_ACCOUNTID", EnvironmentVariableTarget.User);
Expand Down
4 changes: 2 additions & 2 deletions Source/ZoomNet.UnitTests/Models/PhoneCallUserProfileTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Text.Json;
using Shouldly;
using Shouldly;
using System.Text.Json;
using Xunit;
using ZoomNet.Json;
using ZoomNet.Models;
Expand Down
84 changes: 84 additions & 0 deletions Source/ZoomNet.UnitTests/Models/PhoneUserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Shouldly;
using System.Text.Json;
using Xunit;
using ZoomNet.Json;
using ZoomNet.Models;

namespace ZoomNet.UnitTests.Models
{
public class PhoneUserTests
{
#region constants

internal const string PHONE_USER = @"{
""id"": ""NL3cEpSdRc-c2t8aLoZqiw"",
""phone_user_id"": ""u7pnC468TaS46OuNoEw6GA"",
""email"": ""test_phone_user@testapi.com"",
""name"": ""test phone user"",
""extension_id"": ""CcrEGgmeQem1uyJsuIRKwA"",
""extension_number"": 123,
""status"": ""activate"",
""calling_plans"": [
{
""type"": 600,
""name"": ""Delhi billing"",
""billing_account_id"": ""3WWAEiEjTj2IQuyDiKMd_A"",
""billing_account_name"": ""Delhi billing""
}
],
""phone_numbers"": [
{
""id"": ""---M1padRvSUtw7YihN7sA"",
""number"": ""14232058798""
}
],
""site"": {
""id"": ""8f71O6rWT8KFUGQmJIFAdQ"",
""name"": ""Test Site""
},
""department"": ""Test"",
""cost_center"": ""Cost Test Center""
}";

#endregion

#region tests

[Fact]
public void Parse_Json_PhoneUserTests()
{
// Arrange

// Act
var result = JsonSerializer.Deserialize<PhoneUser>(
PHONE_USER, JsonFormatter.SerializerOptions);

// Assert
result.Id.ShouldBe("NL3cEpSdRc-c2t8aLoZqiw");
result.PhoneUserId.ShouldBe("u7pnC468TaS46OuNoEw6GA");
result.Email.ShouldBe("test_phone_user@testapi.com");
result.Name.ShouldBe("test phone user");
result.ExtensionId.ShouldBe("CcrEGgmeQem1uyJsuIRKwA");
result.ExtensionNumber.ShouldBe(123);
result.Status.ShouldBe(PhoneCallUserStatus.Active);
result.CallingPlans.ShouldNotBeNull();
result.CallingPlans.Length.ShouldBe(1);
result.PhoneNumbers.ShouldNotBeNull();
result.PhoneNumbers.Length.ShouldBe(1);
result.Department.ShouldBe("Test");
result.CostCenter.ShouldBe("Cost Test Center");
result.Site.Id.ShouldBe("8f71O6rWT8KFUGQmJIFAdQ");
result.Site.Name.ShouldBe("Test Site");

var callingPlan = result.CallingPlans[0];
callingPlan.BillingAccountId.ShouldBe("3WWAEiEjTj2IQuyDiKMd_A");
callingPlan.BillingAccountName.ShouldBe("Delhi billing");

var phoneNumber = result.PhoneNumbers[0];
phoneNumber.PhoneNumberId.ShouldBe("---M1padRvSUtw7YihN7sA");
phoneNumber.PhoneNumber.ShouldBe("14232058798");
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Net.Http;
using RichardSzalay.MockHttp;
using Shouldly;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using RichardSzalay.MockHttp;
using Shouldly;
using Xunit;
using ZoomNet.Resources;

Expand Down
114 changes: 114 additions & 0 deletions Source/ZoomNet.UnitTests/Resources/PhoneUserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using RichardSzalay.MockHttp;
using Shouldly;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using ZoomNet.Resources;

namespace ZoomNet.UnitTests.Resources
{
public class PhoneUserTests
{
#region constants

internal const string PHONE_USERS_PAGINATED_OBJECT = @"{
""next_page_token"": ""F2qwertyg5eIqRRgC2YMauur8ZHUaJqtS3i"",
""page_size"": 1,
""total_records"": 10,
""users"": [
{
""id"": ""NL3cEpSdRc-c2t8aLoZqiw"",
""phone_user_id"": ""u7pnC468TaS46OuNoEw6GA"",
""email"": ""test_phone_user@testapi.com"",
""name"": ""test phone user"",
""extension_id"": ""CcrEGgmeQem1uyJsuIRKwA"",
""extension_number"": 123,
""status"": ""activate"",
""calling_plans"": [
{
""type"": 600,
""name"": ""Delhi billing"",
""billing_account_id"": ""3WWAEiEjTj2IQuyDiKMd_A"",
""billing_account_name"": ""Delhi billing""
}
],
""phone_numbers"": [
{
""id"": ""---M1padRvSUtw7YihN7sA"",
""number"": ""14232058798""
}
],
""site"": {
""id"": ""8f71O6rWT8KFUGQmJIFAdQ"",
""name"": ""Test Site""
},
""department"": ""Test"",
""cost_center"": ""Cost Test Center""
}
]
}";

#endregion

#region tests

[Fact]
public async Task GetPhoneUsersPaginatedResponseTestsAsync()
{
// Arrange
var pageSize = 1;

var mockHttp = new MockHttpMessageHandler();
mockHttp
.Expect(
HttpMethod.Get,
Utils.GetZoomApiUri("phone/users"))
.Respond(
"application/json",
PHONE_USERS_PAGINATED_OBJECT);

var client = Utils.GetFluentClient(mockHttp);
var phone = new Phone(client);

// Act
var result = await phone
.ListPhoneUsersAsync(pageSize: pageSize)
.ConfigureAwait(true);

// Assert
mockHttp.VerifyNoOutstandingExpectation();
mockHttp.VerifyNoOutstandingRequest();
result.NextPageToken.ShouldNotBeNullOrEmpty();
result.PageSize.ShouldBe(1);
result.TotalRecords.ShouldBe(10);
result.Records.ShouldNotBeNull();
result.Records.Length.ShouldBe(1);
result.Records[0].Id.ShouldBe("NL3cEpSdRc-c2t8aLoZqiw");
result.Records[0].PhoneNumbers[0].PhoneNumberId.ShouldBe("---M1padRvSUtw7YihN7sA");
result.Records[0].PhoneNumbers[0].PhoneNumber.ShouldBe("14232058798");
}

[Theory]
[InlineData(0)]
[InlineData(101)]
public void InvalidPageSize_GetPhoneUsersPaginatedResponseTests(int pageSize)
{
// Arrange
var mockHttp = new MockHttpMessageHandler();

var client = Utils.GetFluentClient(mockHttp);
var phone = new Phone(client);

// Act and Assert
var exception = Assert.Throws<ArgumentOutOfRangeException>(() => phone
.ListPhoneUsersAsync(pageSize: pageSize)
.ConfigureAwait(true));

exception.ParamName.ShouldBe(nameof(pageSize));
exception.Message.ShouldStartWith("Records per page must be between 1 and 100");
}

#endregion
}
}
4 changes: 2 additions & 2 deletions Source/ZoomNet.UnitTests/Resources/SmsSessionTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Net.Http;
using System.Threading.Tasks;
using RichardSzalay.MockHttp;
using Shouldly;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using ZoomNet.Resources;

Expand Down
30 changes: 15 additions & 15 deletions Source/ZoomNet/Extensions/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,22 +500,22 @@ internal static string EnsureEndsWith(this string value, string suffix)

internal static T GetPropertyValue<T>(this JsonElement element, string name, T defaultValue)
{
return GetPropertyValue<T>(element, new[] { name }, defaultValue, false);
return element.GetPropertyValue(new[] { name }, defaultValue, false);
}

internal static T GetPropertyValue<T>(this JsonElement element, string[] names, T defaultValue)
{
return GetPropertyValue<T>(element, names, defaultValue, false);
return element.GetPropertyValue(names, defaultValue, false);
}

internal static T GetPropertyValue<T>(this JsonElement element, string name)
{
return GetPropertyValue<T>(element, new[] { name }, default, true);
return element.GetPropertyValue<T>(new[] { name }, default, true);
}

internal static T GetPropertyValue<T>(this JsonElement element, string[] names)
{
return GetPropertyValue<T>(element, names, default, true);
return element.GetPropertyValue<T>(names, default, true);
}

internal static async Task<TResult[]> ForEachAsync<T, TResult>(this IEnumerable<T> items, Func<T, Task<TResult>> action, int maxDegreeOfParalellism)
Expand Down Expand Up @@ -644,8 +644,8 @@ internal static IEnumerable<KeyValuePair<string, string>> ParseQuerystring(this

internal static DiagnosticInfo GetDiagnosticInfo(this IResponse response)
{
var diagnosticId = response.Message.RequestMessage.Headers.GetValue(DiagnosticHandler.DIAGNOSTIC_ID_HEADER_NAME);
DiagnosticHandler.DiagnosticsInfo.TryGetValue(diagnosticId, out DiagnosticInfo diagnosticInfo);
var diagnosticId = response.Message.RequestMessage.Headers.GetValue(DIAGNOSTIC_ID_HEADER_NAME);
DiagnosticsInfo.TryGetValue(diagnosticId, out DiagnosticInfo diagnosticInfo);
return diagnosticInfo;
}

Expand Down Expand Up @@ -687,8 +687,8 @@ internal static DiagnosticInfo GetDiagnosticInfo(this IResponse response)

if (rootJsonElement.ValueKind == JsonValueKind.Object)
{
errorCode = rootJsonElement.TryGetProperty("code", out JsonElement jsonErrorCode) ? (int?)jsonErrorCode.GetInt32() : (int?)null;
errorMessage = rootJsonElement.TryGetProperty("message", out JsonElement jsonErrorMessage) ? jsonErrorMessage.GetString() : (errorCode.HasValue ? $"Error code: {errorCode}" : errorMessage);
errorCode = rootJsonElement.TryGetProperty("code", out JsonElement jsonErrorCode) ? jsonErrorCode.GetInt32() : null;
errorMessage = rootJsonElement.TryGetProperty("message", out JsonElement jsonErrorMessage) ? jsonErrorMessage.GetString() : errorCode.HasValue ? $"Error code: {errorCode}" : errorMessage;
if (rootJsonElement.TryGetProperty("errors", out JsonElement jsonErrorDetails))
{
var errorDetails = string.Join(
Expand Down Expand Up @@ -749,7 +749,7 @@ internal static async Task<Stream> DecompressAsync(this Stream source)
internal static string ToEnumString<T>(this T enumValue)
where T : Enum
{
if (TryToEnumString(enumValue, out string stringValue)) return stringValue;
if (enumValue.TryToEnumString(out string stringValue)) return stringValue;
return enumValue.ToString();
}

Expand Down Expand Up @@ -796,7 +796,7 @@ internal static bool TryToEnumString<T>(this T enumValue, out string stringValue
internal static T ToEnum<T>(this string str)
where T : Enum
{
if (TryToEnum(str, out T enumValue)) return enumValue;
if (str.TryToEnum(out T enumValue)) return enumValue;

throw new ArgumentException($"There is no value in the {typeof(T).Name} enum that corresponds to '{str}'.");
}
Expand Down Expand Up @@ -907,7 +907,7 @@ private static async Task<T> AsObject<T>(this HttpContent httpContent, string pr
return JsonSerializer.Deserialize<T>(responseContent, options ?? JsonFormatter.DeserializerOptions);
}

var jsonDoc = JsonDocument.Parse(responseContent, (JsonDocumentOptions)default);
var jsonDoc = JsonDocument.Parse(responseContent, default);
if (jsonDoc.RootElement.TryGetProperty(propertyName, out JsonElement property))
{
return property.ToObject<T>(options);
Expand All @@ -933,7 +933,7 @@ private static async Task<JsonDocument> AsRawJsonDocument(this HttpContent httpC
{
var responseContent = await httpContent.ReadAsStringAsync(null, cancellationToken).ConfigureAwait(false);

var jsonDoc = JsonDocument.Parse(responseContent, (JsonDocumentOptions)default);
var jsonDoc = JsonDocument.Parse(responseContent, default);

if (string.IsNullOrEmpty(propertyName))
{
Expand All @@ -943,7 +943,7 @@ private static async Task<JsonDocument> AsRawJsonDocument(this HttpContent httpC
if (jsonDoc.RootElement.TryGetProperty(propertyName, out JsonElement property))
{
var propertyContent = property.GetRawText();
return JsonDocument.Parse(propertyContent, (JsonDocumentOptions)default);
return JsonDocument.Parse(propertyContent, default);
}
else if (throwIfPropertyIsMissing)
{
Expand Down Expand Up @@ -1106,7 +1106,7 @@ private static T GetPropertyValue<T>(this JsonElement element, string[] names, T
{
var underlyingType = Nullable.GetUnderlyingType(typeOfT);
var getElementValue = typeof(Internal)
.GetMethod(nameof(Internal.GetElementValue), BindingFlags.Static | BindingFlags.NonPublic)
.GetMethod(nameof(GetElementValue), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(underlyingType);

return (T)getElementValue.Invoke(null, new object[] { property.Value });
Expand All @@ -1116,7 +1116,7 @@ private static T GetPropertyValue<T>(this JsonElement element, string[] names, T
{
var elementType = typeOfT.GetElementType();
var getElementValue = typeof(Internal)
.GetMethod(nameof(Internal.GetElementValue), BindingFlags.Static | BindingFlags.NonPublic)
.GetMethod(nameof(GetElementValue), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(elementType);

var arrayList = new ArrayList(property.Value.GetArrayLength());
Expand Down
6 changes: 6 additions & 0 deletions Source/ZoomNet/Json/BooleanConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@ public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSer
{
switch (reader.TokenType)
{
case JsonTokenType.None:
case JsonTokenType.Null:
throw new JsonException($"Unable to convert a null value into a boolean value");

case JsonTokenType.True:
case JsonTokenType.False:
return reader.GetBoolean();

case JsonTokenType.Number:
return reader.GetByte() == 1;

default:
throw new JsonException($"Unable to convert the content of {reader.TokenType.ToEnumString()} JSON node into a boolean value");
}
Expand Down
8 changes: 7 additions & 1 deletion Source/ZoomNet/Json/DateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
{
switch (reader.TokenType)
{
case JsonTokenType.None:
case JsonTokenType.Null:
case JsonTokenType.String when string.IsNullOrEmpty(reader.GetString()):
throw new JsonException($"Unable to convert a null value to DateTime");

case JsonTokenType.String:
return reader.GetDateTime();

default:
throw new Exception($"Unable to convert {reader.TokenType.ToEnumString()} to DateTime");
throw new JsonException($"Unable to convert {reader.TokenType.ToEnumString()} to DateTime");
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/ZoomNet/Json/KeyValuePairConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override KeyValuePair<string, string>[] Read(ref Utf8JsonReader reader, T
return values.ToArray();
}

throw new Exception("Unable to read Key/Value pair");
throw new JsonException("Unable to read Key/Value pair");
}

public override void Write(Utf8JsonWriter writer, KeyValuePair<string, string>[] value, JsonSerializerOptions options)
Expand Down
Loading

0 comments on commit 111a2e1

Please sign in to comment.