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

Commit 1bd13d4

Browse files
authored
graphql schema value fix changes (#728)
* graphql schema value fix changes * code changes according to comments * code changes according to discussion
1 parent 34f94ed commit 1bd13d4

File tree

5 files changed

+118
-1
lines changed

5 files changed

+118
-1
lines changed

src/ArmTemplates/Common/Constants/ResourceTypeConstants.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public static class ResourceTypeConstants
3333
public const string Tag = "Microsoft.ApiManagement/service/tags";
3434
public const string Gateway = "Microsoft.ApiManagement/service/gateways";
3535
public const string GatewayApi = "Microsoft.ApiManagement/service/gateways/apis";
36-
3736
public const string ArmDeployments = "Microsoft.Resources/deployments";
3837
}
3938
}

src/ArmTemplates/Extractor/EntityExtractors/ApiSchemaExtractor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ string GetSchemaValueBasedOnContentType(ApiSchemaProperties schemaTemplateProper
6666
{
6767
"application/vnd.ms-azure-apim.swagger.definitions+json" => schemaTemplateProperties?.Document?.Definitions?.Serialize(),
6868
"application/vnd.ms-azure-apim.xsd+xml" => schemaTemplateProperties?.Document?.Definitions?.Serialize(),
69+
"application/vnd.ms-azure-apim.graphql.schema" => schemaTemplateProperties?.Document?.Value?.ToString(),
6970
_ => string.Empty
7071
};
7172

tests/ArmTemplates.Tests/Extractor/Scenarios/ApiExtractorTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using FluentAssertions;
1111
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Commands.Executors;
1212
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
13+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.FileHandlers;
1314
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders;
1415
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors;
1516
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
@@ -155,5 +156,77 @@ public async Task GenerateApiTemplates_ProperlyLaysTheInformation()
155156
apiTemplate.TypedResources.ApiOperationsPolicies.Count().Should().Be(2);
156157
apiTemplate.TypedResources.ApiOperations.All(x => x.Properties is not null).Should().BeTrue();
157158
}
159+
160+
[Fact]
161+
public async Task GenerateGraphQLApiTemplates()
162+
{
163+
FileReader fileReader = new FileReader();
164+
string fileLocation = Path.Combine("Resources", "Schemas", "schema.gql");
165+
166+
Task<string> fileReadingTask = fileReader.RetrieveFileContentsAsync(fileLocation);
167+
168+
// arrange
169+
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateGraphQLApiTemplates));
170+
171+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration(
172+
sourceApimName: string.Empty,
173+
destinationApimName: string.Empty,
174+
resourceGroup: string.Empty,
175+
fileFolder: string.Empty,
176+
apiName: string.Empty);
177+
var extractorParameters = new ExtractorParameters(extractorConfig);
178+
179+
// mocked clients
180+
var mockedApiClient = MockApisClient.GetMockedApiClientWithDefaultValues();
181+
var mockedProductClient = MockProductsClient.GetMockedApiClientWithDefaultValues();
182+
var mockedApiSchemaClient = MockApiSchemaClient.GetMockedApiClientWithGraphQLSchemaValues();
183+
var mockedPolicyClient = MockPolicyClient.GetMockedApiClientWithDefaultValues();
184+
var mockedTagClient = MockTagClient.GetMockedApiClientWithDefaultValues();
185+
var mockedApiOperationClient = MockApiOperationClient.GetMockedApiClientWithDefaultValues();
186+
var mockedDiagnosticClient = MockDiagnosticClient.GetMockedClientWithApiDependentValues();
187+
188+
// mocked extractors
189+
var mockedDiagnosticExtractor = new DiagnosticExtractor(this.GetTestLogger<DiagnosticExtractor>(), mockedDiagnosticClient);
190+
var mockedApiSchemaExtractor = new ApiSchemaExtractor(this.GetTestLogger<ApiSchemaExtractor>(), mockedApiSchemaClient);
191+
var mockedPolicyExtractor = new PolicyExtractor(this.GetTestLogger<PolicyExtractor>(), mockedPolicyClient, new TemplateBuilder());
192+
var mockedProductApisExtractor = new ProductApisExtractor(this.GetTestLogger<ProductApisExtractor>(), mockedProductClient, mockedApiClient, new TemplateBuilder());
193+
var mockedTagExtractor = new TagExtractor(this.GetTestLogger<TagExtractor>(), mockedTagClient, new TemplateBuilder());
194+
var mockedApiOperationExtractor = new ApiOperationExtractor(this.GetTestLogger<ApiOperationExtractor>(), mockedApiOperationClient);
195+
196+
var apiExtractor = new ApiExtractor(
197+
this.GetTestLogger<ApiExtractor>(),
198+
new TemplateBuilder(),
199+
mockedApiClient,
200+
mockedDiagnosticExtractor,
201+
mockedApiSchemaExtractor,
202+
mockedPolicyExtractor,
203+
mockedProductApisExtractor,
204+
mockedTagExtractor,
205+
mockedApiOperationExtractor);
206+
207+
var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
208+
this.GetTestLogger<ExtractorExecutor>(),
209+
apiExtractor: apiExtractor);
210+
extractorExecutor.SetExtractorParameters(extractorParameters);
211+
212+
// act
213+
var apiTemplate = await extractorExecutor.GenerateApiTemplateAsync(
214+
singleApiName: It.IsAny<string>(),
215+
multipleApiNames: It.IsAny<List<string>>(),
216+
currentTestDirectory);
217+
218+
// assert
219+
File.Exists(Path.Combine(currentTestDirectory, apiTemplate.TypedResources.FileName)).Should().BeTrue();
220+
221+
//schema name
222+
string schemaContentType = "application/vnd.ms-azure-apim.graphql.schema";
223+
224+
// api schemas
225+
apiTemplate.TypedResources.ApiSchemas.Count().Should().Be(2);
226+
apiTemplate.TypedResources.ApiSchemas.All(x => x.Type == ResourceTypeConstants.APISchema).Should().BeTrue();
227+
apiTemplate.TypedResources.ApiSchemas.All(x => x.Properties is not null).Should().BeTrue();
228+
apiTemplate.TypedResources.ApiSchemas.All(x => x.Properties.Document.Value.ToString().Equals(fileReadingTask.Result.ToString())).Should().BeTrue();
229+
apiTemplate.TypedResources.ApiSchemas.All(x => x.Properties.ContentType.Equals(schemaContentType)).Should().BeTrue();
230+
}
158231
}
159232
}

tests/ArmTemplates.Tests/Moqs/ApiClients/MockApiSchemaClient.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
// --------------------------------------------------------------------------
55

66
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Threading.Tasks;
79
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.API.Clients.Abstractions;
10+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.FileHandlers;
811
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.ApiSchemas;
912
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
1013
using Moq;
@@ -41,5 +44,37 @@ public static IApiSchemaClient GetMockedApiClientWithDefaultValues()
4144

4245
return mockApiSchemaClient.Object;
4346
}
47+
48+
public static IApiSchemaClient GetMockedApiClientWithGraphQLSchemaValues()
49+
{
50+
// arrange
51+
FileReader fileReader = new FileReader();
52+
string fileLocation = Path.Combine("Resources", "Schemas", "schema.gql");
53+
54+
Task<string> fileReadingTask = fileReader.RetrieveFileContentsAsync(fileLocation);
55+
56+
var mockApiSchemaClient = new Mock<IApiSchemaClient>(MockBehavior.Strict);
57+
58+
mockApiSchemaClient
59+
.Setup(x => x.GetApiSchemasAsync(It.IsAny<string>(), It.IsAny<ExtractorParameters>()))
60+
.ReturnsAsync(new List<ApiSchemaTemplateResource>
61+
{
62+
new ApiSchemaTemplateResource
63+
{
64+
Name = ApiSchemaName,
65+
Properties = new ApiSchemaProperties
66+
{
67+
ContentType = "application/vnd.ms-azure-apim.graphql.schema",
68+
Document = new ApiSchemaDocument
69+
{
70+
Components = ApiSchemaDocComponents,
71+
Value = fileReadingTask.Result.ToString()
72+
}
73+
}
74+
}
75+
});
76+
77+
return mockApiSchemaClient.Object;
78+
}
4479
}
4580
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type Book {
2+
title: String
3+
author: Author
4+
}
5+
6+
type Author {
7+
name: String
8+
books: [Book]
9+
}

0 commit comments

Comments
 (0)