Skip to content

Commit

Permalink
Merge pull request Azure#1018 from daschult/master
Browse files Browse the repository at this point in the history
Improve AzureResourceSchema generator
  • Loading branch information
tbombach committed May 13, 2016
2 parents 68b02be + 30ed59c commit f69f5ce
Show file tree
Hide file tree
Showing 32 changed files with 52,564 additions and 462 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Rest.Generator.AzureResourceSchema;
using Microsoft.Rest.Modeler.Swagger.Tests;
using System.IO;
using Xunit;

namespace AutoRest.Generator.AzureResourceSchema.Tests
{
[Collection("AutoRest Azure Resource Schema Tests")]
public static class AcceptanceTests
{
[Fact]
public static void Storage()
{
RunSwaggerTest("storage.json", "Storage");
}

[Fact]
public static void Batch()
{
RunSwaggerTest("BatchManagement.json", "Batch");
}

[Fact]
public static void Cdn()
{
RunSwaggerTest("cdn.json", "CDN");
}

[Fact]
public static void Compute()
{
RunSwaggerTest("compute.json", "Compute");
}

[Fact]
public static void Network()
{
RunSwaggerTest("network.json", "Network");
}

[Fact]
public static void Web()
{
RunSwaggerTest("web.json", "Web");
}

private static void RunSwaggerTest(string swaggerFileName, string expectedFolderName)
{
SwaggerSpecHelper.RunTests<AzureResourceSchemaCodeGenerator>(
Path.Combine("Swagger", swaggerFileName),
Path.Combine("Expected", expectedFolderName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AcceptanceTests.cs" />
<Compile Include="AzureResourceSchemaCodeGeneratorTests.cs" />
<Compile Include="JSONSchemaTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ResourceSchemaParserTests.cs" />
<Compile Include="ResourceSchemaWriterTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\AutoRest.Core\AutoRest.Core.csproj">
Expand All @@ -75,11 +79,57 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="Expected\Storage\Microsoft.Storage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\Batch\Microsoft.Batch.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\CDN\Microsoft.Cdn.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\Compute\Microsoft.Compute.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\Network\Microsoft.Network.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\Web\Microsoft.Web.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\Web\Microsoft.DomainRegistration.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Expected\Web\Microsoft.CertificateRegistration.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<None Include="packages.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="Swagger\storage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Swagger\BatchManagement.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Swagger\cdn.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Swagger\compute.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Swagger\network.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Swagger\web.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Modelers\Swagger.Tests\AutoRest.Modeler.Swagger.Tests.csproj">
<Project>{c6c4e139-d7af-486c-95ba-2b879f58f18d}</Project>
<Name>AutoRest.Modeler.Swagger.Tests</Name>
</ProjectReference>
<ProjectReference Include="..\AzureResourceSchema\AutoRest.Generator.AzureResourceSchema.csproj">
<Project>{654344a5-0556-49c7-bfb3-59676d7440d3}</Project>
<Name>AutoRest.Generator.AzureResourceSchema</Name>
Expand All @@ -88,6 +138,7 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Name()
public void UsageInstructionsWithNoOutputFileSetting()
{
AzureResourceSchemaCodeGenerator codeGen = CreateGenerator();
Assert.Equal("Your Azure Resource Schema can be found at " + codeGen.SchemaPath, codeGen.UsageInstructions);
Assert.Equal("Your Azure Resource Schema(s) can be found in " + codeGen.Settings.OutputDirectory, codeGen.UsageInstructions);
}

[Fact]
Expand All @@ -47,61 +47,7 @@ public void UsageInstructionsWithOutputFileSetting()
};
AzureResourceSchemaCodeGenerator codeGen = CreateGenerator(settings);

Assert.Equal("Your Azure Resource Schema can be found at " + codeGen.SchemaPath, codeGen.UsageInstructions);
}

[Fact]
public async void GenerateWithEmptyServiceClient()
{
await TestGenerate(new string[0],
@"{
'id': 'http://schema.management.azure.com/schemas//Microsoft.Storage.json#',
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Microsoft.Storage',
'description': 'Microsoft Storage Resource Types',
'resourceDefinitions': { }
}");
}

[Fact]
public async void GenerateWithServiceClientWithOneType()
{
await TestGenerate(new string[]
{
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Mock.Provider/mockType"
},
@"{
'id': 'http://schema.management.azure.com/schemas//Microsoft.Storage.json#',
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Microsoft.Storage',
'description': 'Microsoft Storage Resource Types',
'resourceDefinitions': {
'mockType': {
}
}
}");
}

[Fact]
public async void GenerateWithServiceClientWithTwoTypes()
{
await TestGenerate(new string[]
{
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Mock.Provider/mockType1",
"/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Mock.Provider/mockType2"
},
@"{
'id': 'http://schema.management.azure.com/schemas//Microsoft.Storage.json#',
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Microsoft.Storage',
'description': 'Microsoft Storage Resource Types',
'resourceDefinitions': {
'mockType1': {
},
'mockType2': {
}
}
}");
Assert.Equal("Your Azure Resource Schema(s) can be found in " + settings.OutputDirectory, codeGen.UsageInstructions);
}

[Fact]
Expand All @@ -122,19 +68,21 @@ private static AzureResourceSchemaCodeGenerator CreateGenerator(Settings setting
return new AzureResourceSchemaCodeGenerator(settings);
}

private static async Task TestGenerate(string[] methodUrls, string expectedJsonString)
private static async Task TestGenerate(string apiVersion, string[] methodUrls, string expectedJsonString)
{
MemoryFileSystem fileSystem = new MemoryFileSystem();

Settings settings = new Settings();
settings.FileSystem = fileSystem;

ServiceClient serviceClient = new ServiceClient();
serviceClient.ApiVersion = apiVersion;
foreach(string methodUrl in methodUrls)
{
serviceClient.Methods.Add(new Method()
{
Url = methodUrl
Url = methodUrl,
HttpMethod = HttpMethod.Put,
});
}
await CreateGenerator(settings).Generate(serviceClient);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"id": "http://schema.management.azure.com/schemas/2015-12-01/Microsoft.Batch.json#",
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Microsoft.Batch",
"description": "Microsoft Batch Resource Types",
"resourceDefinitions": {
"Microsoft.Batch_batchAccounts": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"Microsoft.Batch/batchAccounts"
]
},
"apiVersion": {
"type": "string",
"enum": [
"2015-12-01"
]
},
"location": {
"type": "string",
"description": "The region in which the account is created."
},
"tags": {
"oneOf": [
{
"type": "object",
"additionalProperties": {
"type": "string"
}
},
{
"$ref": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#/definitions/expression"
}
],
"description": "The user specified tags associated with the account."
},
"properties": {
"oneOf": [
{
"$ref": "#/definitions/AccountBaseProperties"
},
{
"$ref": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#/definitions/expression"
}
],
"description": "The properties of the account."
}
},
"required": [
"type",
"apiVersion",
"properties"
],
"description": "Microsoft.Batch/batchAccounts"
}
},
"definitions": {
"AccountBaseProperties": {
"type": "object",
"properties": {
"autoStorage": {
"oneOf": [
{
"$ref": "#/definitions/AutoStorageBaseProperties"
},
{
"$ref": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#/definitions/expression"
}
],
"description": "The properties related to auto storage account."
}
},
"description": "The properties of a Batch account."
},
"AutoStorageBaseProperties": {
"type": "object",
"properties": {
"storageAccountId": {
"type": "string",
"description": "The resource id of the storage account to be used for auto storage account."
}
},
"required": [
"storageAccountId"
],
"description": "The properties related to auto storage account."
}
}
}
Loading

0 comments on commit f69f5ce

Please sign in to comment.