Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
graphql schema value fix changes (#728)
Browse files Browse the repository at this point in the history
* graphql schema value fix changes

* code changes according to comments

* code changes according to discussion
  • Loading branch information
Anand0046 committed Jun 1, 2022
1 parent 34f94ed commit 1bd13d4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/ArmTemplates/Common/Constants/ResourceTypeConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static class ResourceTypeConstants
public const string Tag = "Microsoft.ApiManagement/service/tags";
public const string Gateway = "Microsoft.ApiManagement/service/gateways";
public const string GatewayApi = "Microsoft.ApiManagement/service/gateways/apis";

public const string ArmDeployments = "Microsoft.Resources/deployments";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ string GetSchemaValueBasedOnContentType(ApiSchemaProperties schemaTemplateProper
{
"application/vnd.ms-azure-apim.swagger.definitions+json" => schemaTemplateProperties?.Document?.Definitions?.Serialize(),
"application/vnd.ms-azure-apim.xsd+xml" => schemaTemplateProperties?.Document?.Definitions?.Serialize(),
"application/vnd.ms-azure-apim.graphql.schema" => schemaTemplateProperties?.Document?.Value?.ToString(),
_ => string.Empty
};

Expand Down
73 changes: 73 additions & 0 deletions tests/ArmTemplates.Tests/Extractor/Scenarios/ApiExtractorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using FluentAssertions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Commands.Executors;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.FileHandlers;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
Expand Down Expand Up @@ -155,5 +156,77 @@ public async Task GenerateApiTemplates_ProperlyLaysTheInformation()
apiTemplate.TypedResources.ApiOperationsPolicies.Count().Should().Be(2);
apiTemplate.TypedResources.ApiOperations.All(x => x.Properties is not null).Should().BeTrue();
}

[Fact]
public async Task GenerateGraphQLApiTemplates()
{
FileReader fileReader = new FileReader();
string fileLocation = Path.Combine("Resources", "Schemas", "schema.gql");

Task<string> fileReadingTask = fileReader.RetrieveFileContentsAsync(fileLocation);

// arrange
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateGraphQLApiTemplates));

var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(
sourceApimName: string.Empty,
destinationApimName: string.Empty,
resourceGroup: string.Empty,
fileFolder: string.Empty,
apiName: string.Empty);
var extractorParameters = new ExtractorParameters(extractorConfig);

// mocked clients
var mockedApiClient = MockApisClient.GetMockedApiClientWithDefaultValues();
var mockedProductClient = MockProductsClient.GetMockedApiClientWithDefaultValues();
var mockedApiSchemaClient = MockApiSchemaClient.GetMockedApiClientWithGraphQLSchemaValues();
var mockedPolicyClient = MockPolicyClient.GetMockedApiClientWithDefaultValues();
var mockedTagClient = MockTagClient.GetMockedApiClientWithDefaultValues();
var mockedApiOperationClient = MockApiOperationClient.GetMockedApiClientWithDefaultValues();
var mockedDiagnosticClient = MockDiagnosticClient.GetMockedClientWithApiDependentValues();

// mocked extractors
var mockedDiagnosticExtractor = new DiagnosticExtractor(this.GetTestLogger<DiagnosticExtractor>(), mockedDiagnosticClient);
var mockedApiSchemaExtractor = new ApiSchemaExtractor(this.GetTestLogger<ApiSchemaExtractor>(), mockedApiSchemaClient);
var mockedPolicyExtractor = new PolicyExtractor(this.GetTestLogger<PolicyExtractor>(), mockedPolicyClient, new TemplateBuilder());
var mockedProductApisExtractor = new ProductApisExtractor(this.GetTestLogger<ProductApisExtractor>(), mockedProductClient, mockedApiClient, new TemplateBuilder());
var mockedTagExtractor = new TagExtractor(this.GetTestLogger<TagExtractor>(), mockedTagClient, new TemplateBuilder());
var mockedApiOperationExtractor = new ApiOperationExtractor(this.GetTestLogger<ApiOperationExtractor>(), mockedApiOperationClient);

var apiExtractor = new ApiExtractor(
this.GetTestLogger<ApiExtractor>(),
new TemplateBuilder(),
mockedApiClient,
mockedDiagnosticExtractor,
mockedApiSchemaExtractor,
mockedPolicyExtractor,
mockedProductApisExtractor,
mockedTagExtractor,
mockedApiOperationExtractor);

var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
this.GetTestLogger<ExtractorExecutor>(),
apiExtractor: apiExtractor);
extractorExecutor.SetExtractorParameters(extractorParameters);

// act
var apiTemplate = await extractorExecutor.GenerateApiTemplateAsync(
singleApiName: It.IsAny<string>(),
multipleApiNames: It.IsAny<List<string>>(),
currentTestDirectory);

// assert
File.Exists(Path.Combine(currentTestDirectory, apiTemplate.TypedResources.FileName)).Should().BeTrue();

//schema name
string schemaContentType = "application/vnd.ms-azure-apim.graphql.schema";

// api schemas
apiTemplate.TypedResources.ApiSchemas.Count().Should().Be(2);
apiTemplate.TypedResources.ApiSchemas.All(x => x.Type == ResourceTypeConstants.APISchema).Should().BeTrue();
apiTemplate.TypedResources.ApiSchemas.All(x => x.Properties is not null).Should().BeTrue();
apiTemplate.TypedResources.ApiSchemas.All(x => x.Properties.Document.Value.ToString().Equals(fileReadingTask.Result.ToString())).Should().BeTrue();
apiTemplate.TypedResources.ApiSchemas.All(x => x.Properties.ContentType.Equals(schemaContentType)).Should().BeTrue();
}
}
}
35 changes: 35 additions & 0 deletions tests/ArmTemplates.Tests/Moqs/ApiClients/MockApiSchemaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
// --------------------------------------------------------------------------

using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.FileHandlers;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.ApiSchemas;
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
using Moq;
Expand Down Expand Up @@ -41,5 +44,37 @@ public static IApiSchemaClient GetMockedApiClientWithDefaultValues()

return mockApiSchemaClient.Object;
}

public static IApiSchemaClient GetMockedApiClientWithGraphQLSchemaValues()
{
// arrange
FileReader fileReader = new FileReader();
string fileLocation = Path.Combine("Resources", "Schemas", "schema.gql");

Task<string> fileReadingTask = fileReader.RetrieveFileContentsAsync(fileLocation);

var mockApiSchemaClient = new Mock<IApiSchemaClient>(MockBehavior.Strict);

mockApiSchemaClient
.Setup(x => x.GetApiSchemasAsync(It.IsAny<string>(), It.IsAny<ExtractorParameters>()))
.ReturnsAsync(new List<ApiSchemaTemplateResource>
{
new ApiSchemaTemplateResource
{
Name = ApiSchemaName,
Properties = new ApiSchemaProperties
{
ContentType = "application/vnd.ms-azure-apim.graphql.schema",
Document = new ApiSchemaDocument
{
Components = ApiSchemaDocComponents,
Value = fileReadingTask.Result.ToString()
}
}
}
});

return mockApiSchemaClient.Object;
}
}
}
9 changes: 9 additions & 0 deletions tests/ArmTemplates.Tests/Resources/Schemas/schema.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Book {
title: String
author: Author
}

type Author {
name: String
books: [Book]
}

0 comments on commit 1bd13d4

Please sign in to comment.