Skip to content

Commit

Permalink
added a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
darrelmiller committed Jul 28, 2023
1 parent d9853e1 commit eddf8e8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/lib/ApiManifestDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions src/lib/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

namespace Microsoft.OpenApi.ApiManifest;

public class Extensions : Dictionary<string, JsonNode>
public class Extensions : Dictionary<string, JsonNode?>
{
public static Extensions Load(JsonElement value)
{
var extensions = new Extensions();
foreach(var property in value.EnumerateObject())
{
if (property.Value.ValueKind != JsonValueKind.Null) {
extensions.Add(property.Name, JsonSerializer.Deserialize<JsonObject>(property.Value.GetRawText()));
var extensionValue = JsonSerializer.Deserialize<JsonObject>(property.Value.GetRawText());
extensions.Add(property.Name, extensionValue);
}
}
return extensions;
Expand Down
11 changes: 11 additions & 0 deletions src/lib/OpenAI/OpenApiPluginFactory.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 15 additions & 8 deletions src/lib/Publisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<Publisher> handlers = new()
Expand Down
34 changes: 29 additions & 5 deletions src/tests/BasicTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() );

Check warning on line 59 in src/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 59 in src/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 59 in src/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 59 in src/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.

Check warning on line 59 in src/tests/BasicTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
}


// 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<ArgumentNullException>(()=> {
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()
Expand Down
14 changes: 6 additions & 8 deletions src/tests/CreateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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() {
Expand All @@ -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);

Check warning on line 50 in src/tests/CreateTests.cs

View workflow job for this annotation

GitHub Actions / build-and-test

Dereference of a possibly null reference.
}

}

0 comments on commit eddf8e8

Please sign in to comment.