-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from microsoft/po/refactorTests
Refactor tests
- Loading branch information
Showing
34 changed files
with
1,218 additions
and
436 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Microsoft.OpenApi.ApiManifest.Helpers | ||
{ | ||
internal static class ValidationHelpers | ||
{ | ||
internal static void ValidateNullOrWhitespace(string parameterName, string? value, string parentName) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
throw new ArgumentNullException(parameterName, string.Format(ErrorMessage.FieldIsRequired, parameterName, parentName)); | ||
} | ||
|
||
internal static void ValidateLength(string parameterName, string? value, int maxLength) | ||
{ | ||
if (value?.Length > maxLength) | ||
throw new ArgumentOutOfRangeException(parameterName, string.Format(ErrorMessage.FieldLengthExceeded, parameterName, maxLength)); | ||
} | ||
|
||
internal static void ValidateEmail(string parameterName, string? value, string parentName) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) | ||
throw new ArgumentNullException(parameterName, string.Format(ErrorMessage.FieldIsRequired, parameterName, parentName)); | ||
else | ||
ValidateEmail(parameterName, value); | ||
} | ||
|
||
internal static void ValidateBaseUrl(string parameterName, string? baseUrl) | ||
{ | ||
// Check if the baseUrl is a valid URL and ends in a slash. | ||
if (string.IsNullOrWhiteSpace(baseUrl) || !baseUrl.EndsWith("/", StringComparison.Ordinal) || !Uri.TryCreate(baseUrl, UriKind.Absolute, out _)) | ||
throw new ArgumentException(string.Format(ErrorMessage.BaseUrlIsNotValid, nameof(baseUrl)), parameterName); | ||
} | ||
|
||
private static readonly Regex s_emailRegex = new(@"^[^@\s]+@[^@\s]+$", RegexOptions.Compiled, Constants.DefaultRegexTimeout); | ||
private static void ValidateEmail(string parameterName, string value) | ||
{ | ||
if (!s_emailRegex.IsMatch(value)) | ||
throw new ArgumentException(string.Format(ErrorMessage.FieldIsNotValid, parameterName), parameterName); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,64 @@ | ||
|
||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
using Microsoft.OpenApi.ApiManifest.Helpers; | ||
using System.Text.Json; | ||
|
||
namespace Microsoft.OpenApi.ApiManifest.OpenAI; | ||
|
||
public class Api | ||
{ | ||
private const string TypeProperty = "type"; | ||
private const string UrlProperty = "url"; | ||
private const string IsUserAuthenticatedProperty = "is_user_authenticated"; | ||
public string? Type { get; set; } | ||
public string? Url { get; set; } | ||
public bool? IsUserAuthenticated { get; set; } | ||
|
||
public Api(string type, string url) | ||
{ | ||
Type = type; | ||
Url = url; | ||
Validate(this); | ||
} | ||
|
||
internal Api(JsonElement value) | ||
{ | ||
ParsingHelpers.ParseMap(value, this, handlers); | ||
Validate(this); | ||
} | ||
|
||
public static Api Load(JsonElement value) | ||
{ | ||
var api = new Api(); | ||
var api = new Api(value); | ||
ParsingHelpers.ParseMap(value, api, handlers); | ||
return api; | ||
} | ||
|
||
// Create handlers FixedFieldMap for Api | ||
private static readonly FixedFieldMap<Api> handlers = new() | ||
{ | ||
{ "type", (o,v) => {o.Type = v.GetString(); } }, | ||
{ "url", (o,v) => {o.Url = v.GetString(); } }, | ||
{ "is_user_authenticated", (o,v) => {o.IsUserAuthenticated = v.GetBoolean(); }}, | ||
{ TypeProperty, (o,v) => {o.Type = v.GetString(); } }, | ||
{ UrlProperty, (o,v) => {o.Url = v.GetString(); } }, | ||
{ IsUserAuthenticatedProperty, (o,v) => {o.IsUserAuthenticated = v.GetBoolean(); }}, | ||
}; | ||
|
||
public void Write(Utf8JsonWriter writer) | ||
{ | ||
Validate(this); | ||
writer.WriteStartObject(); | ||
writer.WriteString("type", Type); | ||
writer.WriteString("url", Url); | ||
writer.WriteBoolean("is_user_authenticated", IsUserAuthenticated ?? false); | ||
writer.WriteString(TypeProperty, Type); | ||
writer.WriteString(UrlProperty, Url); | ||
writer.WriteBoolean(IsUserAuthenticatedProperty, IsUserAuthenticated ?? false); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
private void Validate(Api api) | ||
{ | ||
ValidationHelpers.ValidateNullOrWhitespace(nameof(Type), api.Type, nameof(Api)); | ||
ValidationHelpers.ValidateNullOrWhitespace(nameof(Url), api.Url, nameof(Api)); | ||
} | ||
} | ||
|
||
|
Oops, something went wrong.