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

Commit ffa1ff2

Browse files
authored
Merge branch 'master' into cxpcommunity
2 parents cf9a012 + 9818f65 commit ffa1ff2

25 files changed

+468
-782
lines changed
Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,95 @@
11
using System;
22
using Xunit;
33
using McMaster.Extensions.CommandLineUtils;
4+
using System.IO;
45

56
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
67
{
78
public class CreateTests
89
{
910

11+
private string configExamplesFolder;
12+
13+
public CreateTests()
14+
{
15+
this.configExamplesFolder = String.Concat("..", Path.DirectorySeparatorChar,
16+
"..", Path.DirectorySeparatorChar,
17+
"..", Path.DirectorySeparatorChar,
18+
"..", Path.DirectorySeparatorChar,
19+
"apimtemplate", Path.DirectorySeparatorChar,
20+
"Creator", Path.DirectorySeparatorChar,
21+
"Configurations", Path.DirectorySeparatorChar,
22+
"Examples", Path.DirectorySeparatorChar);
23+
}
24+
1025
[Fact]
1126
public void ShouldFailWithUnknownCommand()
1227
{
13-
1428
var createCommand = new CreateCommand();
15-
1629
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute("test"));
17-
//Console.WriteLine(ex.Message);
1830
Assert.Contains("Unrecognized command or argument 'test'", ex.Message);
1931
}
32+
33+
[Fact]
34+
public void ShouldFailWithUnknownOption()
35+
{
36+
var createCommand = new CreateCommand();
37+
string[] args = new string[] { "--configurationFile", String.Concat(this.configExamplesFolder, "valid.yml") };
38+
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute(args));
39+
Assert.Contains("Unrecognized option '--configurationFile'", ex.Message);
40+
}
41+
42+
[Fact]
43+
public void ShouldFailWithInvalidOutputLocation()
44+
{
45+
var createCommand = new CreateCommand();
46+
string[] args = new string[] { "--configFile", String.Concat(this.configExamplesFolder, "invalidOutputLocation.yml") };
47+
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute(args));
48+
Assert.Contains("Output location is required", ex.Message);
49+
}
50+
51+
[Fact]
52+
public void ShouldFailWithInvalidVersion()
53+
{
54+
var createCommand = new CreateCommand();
55+
string[] args = new string[] { "--configFile", String.Concat(this.configExamplesFolder, "invalidVersion.yml") };
56+
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute(args));
57+
Assert.Contains("Version is required", ex.Message);
58+
}
59+
60+
[Fact]
61+
public void ShouldFailWithInvalidAPIConfiguration()
62+
{
63+
var createCommand = new CreateCommand();
64+
string[] args = new string[] { "--configFile", String.Concat(this.configExamplesFolder, "invalidAPI.yml") };
65+
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute(args));
66+
Assert.Contains("API configuration is required", ex.Message);
67+
}
68+
69+
[Fact]
70+
public void ShouldFailWithInvalidOpenAPISpec()
71+
{
72+
var createCommand = new CreateCommand();
73+
string[] args = new string[] { "--configFile", String.Concat(this.configExamplesFolder, "invalidOpenAPISpec.yml") };
74+
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute(args));
75+
Assert.Contains("Open API Spec is required", ex.Message);
76+
}
77+
78+
[Fact]
79+
public void ShouldFailWithInvalidSuffix()
80+
{
81+
var createCommand = new CreateCommand();
82+
string[] args = new string[] { "--configFile", String.Concat(this.configExamplesFolder, "invalidSuffix.yml") };
83+
var ex = Assert.ThrowsAny<CommandParsingException>(() => createCommand.Execute(args));
84+
Assert.Contains("API suffix is required", ex.Message);
85+
}
86+
87+
[Fact]
88+
public void ShouldNotFailWithValidConfig()
89+
{
90+
var createCommand = new CreateCommand();
91+
string[] args = new string[] { "--configFile", String.Concat(this.configExamplesFolder, "valid.yml") };
92+
createCommand.Execute(args);
93+
}
2094
}
2195
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.IO;
2+
using Xunit;
3+
using Newtonsoft.Json.Linq;
4+
using System;
5+
6+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
7+
{
8+
public class ARMTemplateWriterTests
9+
{
10+
[Fact]
11+
public void ShouldWriteJSONToFile()
12+
{
13+
// arrange
14+
ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter();
15+
string location = String.Concat("..", Path.DirectorySeparatorChar,
16+
"..", Path.DirectorySeparatorChar,
17+
"..", Path.DirectorySeparatorChar,
18+
"Creator", Path.DirectorySeparatorChar,
19+
"example.json");
20+
APITemplate testObject = new APITemplate() { apiVersion = "" };
21+
JObject testJSON = JObject.FromObject(testObject);
22+
23+
// delete existing file if exists
24+
if (File.Exists(location))
25+
{
26+
File.Delete(location);
27+
}
28+
// write new
29+
armTemplateWriter.WriteJSONToFile(testJSON, location);
30+
31+
// assert
32+
Assert.True(File.Exists(location));
33+
File.Delete(location);
34+
}
35+
}
36+
}

src/APIM_ARMTemplate/apimtemplate.test/TemplateCreators/APITemplateCreatorTests.cs renamed to src/APIM_ARMTemplate/apimtemplate.test/Creator/TemplateCreatorTests/APITemplateCreatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
1+
using System.IO;
22
using Xunit;
3-
using McMaster.Extensions.CommandLineUtils;
3+
using Newtonsoft.Json.Linq;
44

55
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
66
{

src/APIM_ARMTemplate/apimtemplate.test/apimtemplate.test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<PackageReference Include="coverlet.msbuild" Version="2.5.0">
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
</PackageReference>
1418
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
1519
<PackageReference Include="xunit" Version="2.4.0" />
1620
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
Lines changed: 35 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System.Collections.Generic;
21
using McMaster.Extensions.CommandLineUtils;
3-
using Microsoft.OpenApi.Models;
42
using Colors.Net;
5-
using System;
6-
using Newtonsoft.Json;
73

84
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates
95
{
@@ -15,132 +11,54 @@ public CreateCommand()
1511
this.Description = Constants.CreateDescription;
1612

1713
// list command options
18-
CommandOption openAPISpecFile = this.Option("--openAPISpecFile <openAPISpecFile>", "Open API spec file location", CommandOptionType.SingleValue);
19-
CommandOption openAPISpecURL = this.Option("--openAPISpecURL <openAPISpecURL>", "Open API spec remote url", CommandOptionType.SingleValue);
20-
CommandOption outputLocation = this.Option("--outputLocation <outputLocation>", "Template output location", CommandOptionType.SingleValue).IsRequired();
21-
CommandOption xmlPolicyFile = this.Option("--xmlPolicyFile <xmlPolicyFile>", "XML policy file location", CommandOptionType.SingleValue);
22-
CommandOption xmlPolicyURL = this.Option("--xmlPolicyURL <xmlPolicyURL>", "XML policy remote url", CommandOptionType.SingleValue);
23-
CommandOption linked = this.Option("--linked <linked>", "Creates linked templates versus inlined into a single file", CommandOptionType.SingleValue);
24-
CommandOption path = this.Option("--path <path>", "API path", CommandOptionType.SingleValue);
25-
CommandOption apiRevision = this.Option("--apiRevision <apiRevision>", "API revision", CommandOptionType.SingleValue);
26-
CommandOption apiRevisionDescription = this.Option("--apiRevisionDescription <apiVersionSetId>", "Description of the API revision", CommandOptionType.SingleValue);
27-
CommandOption apiVersion = this.Option("--apiVersion <apiVersion>", "API version", CommandOptionType.SingleValue);
28-
CommandOption apiVersionDescription = this.Option("--apiVersionDescription <apiVersionSetId>", "Description of the API version", CommandOptionType.SingleValue);
29-
CommandOption apiVersionSetFile = this.Option("--apiVersionSetFile <apiVersionSetId>", "YAML file with object that follows the ApiVersionSetContractDetails object schema - https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2018-06-01-preview/service/apis#ApiVersionSetContractDetails", CommandOptionType.SingleValue);
30-
CommandOption authenticationSettingsFile = this.Option("--authenticationSettingsFile <apiVersionSetId>", "YAML file with object that follows the AuthenticationSettingsContract object schema - https://docs.microsoft.com/en-us/azure/templates/microsoft.apimanagement/2018-06-01-preview/service/apis#AuthenticationSettingsContract", CommandOptionType.SingleValue);
31-
CommandOption apiVersionSetId = this.Option("--apiVersionSetId <apiVersionSetId>", "API version set id", CommandOptionType.SingleValue);
32-
CommandOption productIds = this.Option("--productIds <productIds>", "Product ids to associate the API with", CommandOptionType.MultipleValue);
14+
CommandOption configFile = this.Option("--configFile <configFile>", "Config YAML file location", CommandOptionType.SingleValue).IsRequired();
3315

3416
this.HelpOption();
3517

3618
this.OnExecute(async () =>
3719
{
20+
// convert config file to CreatorConfig class
21+
YAMLReader yamlReader = new YAMLReader();
22+
CreatorConfig creatorConfig = yamlReader.ConvertConfigYAMLToCreatorConfig(configFile.Value());
23+
3824
// ensure required parameters have been passed in
39-
if ((openAPISpecFile.HasValue() || openAPISpecURL.HasValue()) && outputLocation.HasValue())
25+
if (creatorConfig.outputLocation == null)
4026
{
41-
// convert command options to CLIArguments class
42-
CLICreatorArguments cliArguments = new CLICreatorArguments()
43-
{
44-
openAPISpecFile = openAPISpecFile.Value(),
45-
openAPISpecURL = openAPISpecURL.Value(),
46-
outputLocation = outputLocation.Value(),
47-
xmlPolicyFile = xmlPolicyFile.Value(),
48-
xmlPolicyURL = xmlPolicyURL.Value(),
49-
linked = linked.HasValue(),
50-
path = path.Value(),
51-
apiRevision = apiRevision.Value(),
52-
apiRevisionDescription = apiRevisionDescription.Value(),
53-
apiVersion = apiVersion.Value(),
54-
apiVersionDescription = apiVersionDescription.Value(),
55-
apiVersionSetFile = apiVersionSetFile.Value(),
56-
apiVersionSetId = apiVersionSetId.Value(),
57-
authenticationSettingsFile = authenticationSettingsFile.Value(),
58-
productIds = productIds.Values
59-
};
60-
61-
if (apiVersionSetFile.HasValue() && AttemptAPIVersionSetConversion(cliArguments) != null)
62-
{
63-
// unable to convert version set argument into object, would cause failure down the line
64-
ColoredConsole.Error.WriteLine("Incorrect apiVersionSet object structure");
65-
return 0;
66-
}
67-
else if (authenticationSettingsFile.HasValue() && AttemptAuthenticationSettingsConversion(cliArguments) != null)
68-
{
69-
// unable to convert version set argument into object, would cause failure down the line
70-
ColoredConsole.Error.WriteLine("Incorrect authenticationSettings object structure");
71-
return 0;
72-
}
73-
else
74-
{
75-
// required parameters have been supplied and versionSet has correct object structure
76-
77-
// initialize helper classes
78-
OpenAPISpecReader openAPISpecReader = new OpenAPISpecReader();
79-
ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter();
80-
APITemplateCreator apiTemplateCreator = new APITemplateCreator();
81-
TagTemplateCreator tagTemplateCreator = new TagTemplateCreator();
82-
83-
// create OpenApiDocument from Open API spec file
84-
OpenApiDocument doc = new OpenApiDocument();
85-
if (cliArguments.openAPISpecFile != null)
86-
{
87-
doc = openAPISpecReader.ConvertLocalFileToOpenAPISpec(cliArguments.openAPISpecFile);
88-
}
89-
else
90-
{
91-
doc = await openAPISpecReader.ConvertRemoteURLToOpenAPISpecAsync(cliArguments.openAPISpecURL);
92-
}
93-
94-
// create templates from OpenApiDocument
95-
APITemplate apiTemplate = await apiTemplateCreator.CreateAPITemplateAsync(doc, cliArguments);
96-
List<TagTemplate> tagTemplates = tagTemplateCreator.CreateTagTemplates(doc);
97-
List<TagDescriptionTemplate> tagDescriptionTemplates = tagTemplateCreator.CreateTagDescriptionTemplates(doc);
98-
99-
// write templates to outputLocation
100-
armTemplateWriter.WriteAPITemplateToFile(apiTemplate, cliArguments.outputLocation);
101-
armTemplateWriter.WriteTagTemplatesToFile(tagTemplates, cliArguments.outputLocation);
102-
armTemplateWriter.WriteTagDescriptionTemplatesToFile(tagDescriptionTemplates, cliArguments.outputLocation);
103-
ColoredConsole.WriteLine("Templates written to output location");
104-
}
27+
throw new CommandParsingException(this, "Output location is required");
10528
}
106-
else if (!outputLocation.HasValue())
29+
else if (creatorConfig.version == null)
10730
{
108-
ColoredConsole.Error.WriteLine("Output location is required");
31+
throw new CommandParsingException(this, "Version is required");
10932
}
110-
else if (!(openAPISpecFile.HasValue() || openAPISpecURL.HasValue()))
33+
else if (creatorConfig.api == null)
11134
{
112-
ColoredConsole.Error.WriteLine("Open API spec file or remote url is required");
113-
};
114-
return 0;
115-
});
116-
}
35+
throw new CommandParsingException(this, "API configuration is required");
36+
}
37+
else if (creatorConfig.api.openApiSpec == null)
38+
{
39+
throw new CommandParsingException(this, "Open API Spec is required");
40+
}
41+
else if (creatorConfig.api.suffix == null)
42+
{
43+
throw new CommandParsingException(this, "API suffix is required");
44+
}
45+
else
46+
{
47+
// required parameters have been supplied
11748

118-
public Exception AttemptAPIVersionSetConversion(CLICreatorArguments cliArguments)
119-
{
120-
try
121-
{
122-
YAMLReader yamlReader = new YAMLReader();
123-
APITemplateVersionSet versionSet = yamlReader.ConvertYAMLFileToAPIVersionSet(cliArguments.apiVersionSetFile);
124-
return null;
125-
}
126-
catch (Exception ex)
127-
{
128-
return ex;
129-
}
130-
}
49+
// initialize helper classes
50+
APITemplateCreator apiTemplateCreator = new APITemplateCreator();
51+
ARMTemplateWriter armTemplateWriter = new ARMTemplateWriter();
13152

132-
public Exception AttemptAuthenticationSettingsConversion(CLICreatorArguments cliArguments)
133-
{
134-
try
135-
{
136-
YAMLReader yamlReader = new YAMLReader();
137-
APITemplateAuthenticationSettings authenticationSettings = yamlReader.ConvertYAMLFileToAuthenticationSettings(cliArguments.authenticationSettingsFile);
138-
return null;
139-
}
140-
catch (Exception ex)
141-
{
142-
return ex;
143-
}
53+
// create templates from provided configuration
54+
APITemplate apiTemplate = await apiTemplateCreator.CreateAPITemplateAsync(creatorConfig);
55+
56+
// write templates to outputLocation
57+
armTemplateWriter.WriteAPITemplateToFile(apiTemplate, creatorConfig.outputLocation);
58+
ColoredConsole.WriteLine("Templates written to output location");
59+
}
60+
return 0;
61+
});
14462
}
14563
}
14664
}

src/APIM_ARMTemplate/apimtemplate/Common/CLIArguments.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)