From eddf8e8207a41a72681af8275d705b2e8da50d2e Mon Sep 17 00:00:00 2001 From: Darrel Miller Date: Fri, 28 Jul 2023 14:33:26 -0700 Subject: [PATCH] added a few more tests --- src/lib/ApiManifestDocument.cs | 7 ++++++ src/lib/Extensions.cs | 5 ++-- src/lib/OpenAI/OpenApiPluginFactory.cs | 11 +++++++++ src/lib/Publisher.cs | 23 +++++++++++------ src/tests/BasicTests.cs | 34 ++++++++++++++++++++++---- src/tests/CreateTests.cs | 14 +++++------ 6 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 src/lib/OpenAI/OpenApiPluginFactory.cs diff --git a/src/lib/ApiManifestDocument.cs b/src/lib/ApiManifestDocument.cs index a8e8778..9f50063 100644 --- a/src/lib/ApiManifestDocument.cs +++ b/src/lib/ApiManifestDocument.cs @@ -12,6 +12,13 @@ public class ApiManifestDocument private const string ApiDependenciesProperty = "apiDependencies"; private const string ExtensionsProperty = "extensions"; + public ApiManifestDocument() + { + if (Publisher != null) { + if (string.IsNullOrEmpty(Publisher.ContactEmail)) throw new ArgumentNullException(nameof(Publisher.ContactEmail)); + } + } + // Write method public void Write(Utf8JsonWriter writer) { diff --git a/src/lib/Extensions.cs b/src/lib/Extensions.cs index 7b7eb0f..0d501ce 100644 --- a/src/lib/Extensions.cs +++ b/src/lib/Extensions.cs @@ -3,7 +3,7 @@ namespace Microsoft.OpenApi.ApiManifest; -public class Extensions : Dictionary +public class Extensions : Dictionary { public static Extensions Load(JsonElement value) { @@ -11,7 +11,8 @@ public static Extensions Load(JsonElement value) foreach(var property in value.EnumerateObject()) { if (property.Value.ValueKind != JsonValueKind.Null) { - extensions.Add(property.Name, JsonSerializer.Deserialize(property.Value.GetRawText())); + var extensionValue = JsonSerializer.Deserialize(property.Value.GetRawText()); + extensions.Add(property.Name, extensionValue); } } return extensions; diff --git a/src/lib/OpenAI/OpenApiPluginFactory.cs b/src/lib/OpenAI/OpenApiPluginFactory.cs new file mode 100644 index 0000000..36e9b7e --- /dev/null +++ b/src/lib/OpenAI/OpenApiPluginFactory.cs @@ -0,0 +1,11 @@ + +namespace Microsoft.OpenApi.ApiManifest.OpenAI; + +public class OpenApiPluginFactory { + + public static OpenAIPluginManifest CreateOpenAIPluginManifest() { + var manifest = new OpenAIPluginManifest(); + manifest.SchemaVersion = "v1"; + return manifest; + } +} \ No newline at end of file diff --git a/src/lib/Publisher.cs b/src/lib/Publisher.cs index a3f88bd..4870af1 100644 --- a/src/lib/Publisher.cs +++ b/src/lib/Publisher.cs @@ -5,11 +5,23 @@ namespace Microsoft.OpenApi.ApiManifest; public class Publisher { public string? Name { get; set; } - public string? ContactEmail { get; set; } - + public string ContactEmail { get; set; } private const string NameProperty = "name"; private const string ContactEmailProperty = "contactEmail"; + public Publisher(string contactEmail) + { + if (String.IsNullOrWhiteSpace(contactEmail)) throw new ArgumentNullException("Contact email is a required property of Publisher."); + ContactEmail = contactEmail; + } + private Publisher(JsonElement value) + { + ParsingHelpers.ParseMap(value, this, handlers); + // Validate that Name and ContactEmail are not null + if (String.IsNullOrWhiteSpace(this.Name)) throw new ArgumentNullException("Name is a required property of publisher."); + if (String.IsNullOrWhiteSpace(this.ContactEmail)) throw new ArgumentNullException("Contact email is a required property of Publisher."); + } + // Write method public void Write(Utf8JsonWriter writer) { @@ -23,12 +35,7 @@ public void Write(Utf8JsonWriter writer) // Load method internal static Publisher Load(JsonElement value) { - var publisher = new Publisher(); - ParsingHelpers.ParseMap(value, publisher, handlers); - // Validate that Name and ContactEmail are not null - if (String.IsNullOrWhiteSpace(publisher.Name)) throw new ArgumentNullException("Name is a required property of publisher."); - if (String.IsNullOrWhiteSpace(publisher.ContactEmail)) throw new ArgumentNullException("Contact email is a required property of Publisher."); - return publisher; + return new Publisher(value); } private static FixedFieldMap handlers = new() diff --git a/src/tests/BasicTests.cs b/src/tests/BasicTests.cs index 52651a6..d57d7d1 100644 --- a/src/tests/BasicTests.cs +++ b/src/tests/BasicTests.cs @@ -55,16 +55,40 @@ public void DeserializeDocument() Assert.Equivalent(exampleApiManifest.ApiDependencies["example"].ApiDescripionUrl, apiManifest.ApiDependencies["example"].ApiDescripionUrl ); var expectedAuth = exampleApiManifest.ApiDependencies["example"].Auth; var actualAuth = apiManifest.ApiDependencies["example"].Auth; - Assert.Equivalent(expectedAuth.ClientIdentifier, actualAuth.ClientIdentifier ); - Assert.Equivalent(expectedAuth.Access[0].Content.ToJsonString(), actualAuth.Access[0].Content.ToJsonString() ); + Assert.Equivalent(expectedAuth?.ClientIdentifier, actualAuth?.ClientIdentifier ); + Assert.Equivalent(expectedAuth?.Access[0].Content.ToJsonString(), actualAuth.Access[0].Content.ToJsonString() ); + } + + + // Create an empty document + [Fact] + public void CreateEmptyDocument() + { + var doc = new ApiManifestDocument(); + Assert.NotNull(doc); + Assert.NotNull(doc.ApiDependencies); + Assert.Empty(doc.ApiDependencies); + } + + // Create a document with a publisher that is missing contactEmail + [Fact] + public void CreateDocumentWithMissingContactEmail() + { + Assert.Throws(()=> { + var doc = new ApiManifestDocument() { + Publisher = new("") { + Name = "Microsoft" + } + }; + } + ); } private static ApiManifestDocument CreateDocument() { return new ApiManifestDocument() { - Publisher = new() { - Name = "Microsoft", - ContactEmail = "example@example.org" + Publisher = new("example@example.org") { + Name = "Microsoft" }, ApiDependencies = new() { { "example", new() diff --git a/src/tests/CreateTests.cs b/src/tests/CreateTests.cs index 32a0774..2fbe548 100644 --- a/src/tests/CreateTests.cs +++ b/src/tests/CreateTests.cs @@ -12,9 +12,8 @@ public void CreateEmptyApiManifestDocument() { [Fact] public void CreatePublisher() { - var publisher = new Publisher() { - Name = "Contoso", - ContactEmail = "foo@bar.com" + var publisher = new Publisher(contactEmail: "foo@bar.com") { + Name = "Contoso" }; Assert.Equal("Contoso", publisher.Name); Assert.Equal("foo@bar.com", publisher.ContactEmail); @@ -26,10 +25,9 @@ public void CreatePublisher() { public void CreateApiManifestWithAuth() { var apiManifest = new ApiManifestDocument() { - Publisher = new() + Publisher = new(contactEmail: "foo@bar.com") { - Name = "Contoso", - ContactEmail = "foo@bar.com" + Name = "Contoso" }, ApiDependencies = new() { { "Contoso.Api", new() { @@ -48,8 +46,8 @@ public void CreateApiManifestWithAuth() { } }; Assert.NotNull(apiManifest.ApiDependencies["Contoso.Api"].Auth); - Assert.Equal("2143234-234324-234234234-234", apiManifest.ApiDependencies["Contoso.Api"].Auth.ClientIdentifier); - Assert.Equal("oauth2", apiManifest.ApiDependencies["Contoso.Api"].Auth.Access[0].Type); + Assert.Equal("2143234-234324-234234234-234", apiManifest?.ApiDependencies["Contoso.Api"]?.Auth?.ClientIdentifier); + Assert.Equal("oauth2", apiManifest?.ApiDependencies["Contoso.Api"]?.Auth?.Access[0].Type); } } \ No newline at end of file