Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
132 changes: 114 additions & 18 deletions src/SDKs/ApiManagement/ApiManagement.Tests/ApiManagementTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ApiManagementTestBase : TestBase
private const string LocationKey = "Location";
private const string TestCertificateKey = "TestCertificate";
private const string TestCertificatePasswordKey = "TestCertificatePassword";

public string location { get; set; }
public string subscriptionId { get; set; }
public ApiManagementClient client { get; set; }
Expand Down Expand Up @@ -59,38 +59,48 @@ private void Initialize()

if (HttpMockServer.Mode == HttpRecorderMode.Record)
{
this.serviceName = testEnv.ConnectionString.KeyValuePairs[ServiceNameKey];
if (string.IsNullOrEmpty(this.serviceName))
if (!testEnv.ConnectionString.KeyValuePairs.TryGetValue(ServiceNameKey, out string apimServiceName))
{
this.serviceName = TestUtilities.GenerateName("sdktestapim");
}
else
{
this.serviceName = apimServiceName;
}

this.location = testEnv.ConnectionString.KeyValuePairs[LocationKey];
if (string.IsNullOrEmpty(this.location))
if (!testEnv.ConnectionString.KeyValuePairs.TryGetValue(LocationKey, out string apimLocation))
{
this.location = GetLocation();
}
else
{
this.location = apimLocation;
}

this.rgName = testEnv.ConnectionString.KeyValuePairs[ResourceGroupNameKey];
if (string.IsNullOrEmpty(this.rgName))
if (!testEnv.ConnectionString.KeyValuePairs.TryGetValue(ResourceGroupNameKey, out string resourceGroupName))
{
rgName = TestUtilities.GenerateName("sdktestrg");
resourcesClient.ResourceGroups.CreateOrUpdate(rgName, new ResourceGroup { Location = this.location });
}

this.base64EncodedTestCertificateData = testEnv.ConnectionString.KeyValuePairs[TestCertificateKey];
if (!string.IsNullOrEmpty(this.base64EncodedTestCertificateData))
else
{
this.rgName = resourceGroupName;
}

if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(TestCertificateKey, out string base64EncodedCertificate))
{
this.base64EncodedTestCertificateData = base64EncodedCertificate;
HttpMockServer.Variables[TestCertificateKey] = base64EncodedTestCertificateData;
}
this.testCertificatePassword = testEnv.ConnectionString.KeyValuePairs[TestCertificatePasswordKey];
if(!string.IsNullOrEmpty(this.testCertificatePassword))

if (testEnv.ConnectionString.KeyValuePairs.TryGetValue(TestCertificatePasswordKey, out string testCertificatePassword))
{
this.testCertificatePassword = testCertificatePassword;
HttpMockServer.Variables[TestCertificatePasswordKey] = testCertificatePassword;
}

this.subscriptionId = testEnv.SubscriptionId;
HttpMockServer.Variables[SubIdKey] = subscriptionId;
HttpMockServer.Variables[SubIdKey] = subscriptionId;
HttpMockServer.Variables[ServiceNameKey] = this.serviceName;
HttpMockServer.Variables[LocationKey] = this.location;
HttpMockServer.Variables[ResourceGroupNameKey] = this.rgName;
Expand All @@ -102,17 +112,17 @@ private void Initialize()
rgName = HttpMockServer.Variables[ResourceGroupNameKey];
serviceName = HttpMockServer.Variables[ServiceNameKey];
location = HttpMockServer.Variables[LocationKey];
HttpMockServer.Variables.TryGetValue(TestCertificateKey, out var testcertificate);
if(!string.IsNullOrEmpty(testcertificate))
HttpMockServer.Variables.TryGetValue(TestCertificateKey, out var testcertificate);
if (!string.IsNullOrEmpty(testcertificate))
{
this.base64EncodedTestCertificateData = testcertificate;
}
HttpMockServer.Variables.TryGetValue(TestCertificatePasswordKey, out var testCertificatePwd);
if(!string.IsNullOrEmpty(testCertificatePwd))
if (!string.IsNullOrEmpty(testCertificatePwd))
{
this.testCertificatePassword = testCertificatePwd;
}
}
}

tags = new Dictionary<string, string> { { "tag1", "value1" }, { "tag2", "value2" }, { "tag3", "value3" } };

Expand Down Expand Up @@ -154,5 +164,91 @@ public string GetLocation(string regionIn = "US")
}
).First().Locations.Where(l => l.Contains(regionIn)).FirstOrDefault();
}

public static byte[] RandomBytes(int length)
{
if (HttpMockServer.Mode == HttpRecorderMode.Record)
{
var bytes = new byte[length];
Random rnd = new Random();
rnd.NextBytes(bytes);
HttpMockServer.Variables["RandomBytes"] = Convert.ToBase64String(bytes);
return bytes;
}
else
{
return Convert.FromBase64String(HttpMockServer.Variables["RandomBytes"]);
}
}

public OperationContract CreateOperationContract(string httpMethod)
{
return new OperationContract
{
DisplayName = "operation_" + TestUtilities.GenerateName(),
Description = "description_" + TestUtilities.GenerateName(),
UrlTemplate = "template_" + TestUtilities.GenerateName(),
Method = httpMethod,
Request = new RequestContract
{
Description = "description_" + TestUtilities.GenerateName(),
Headers = new[]
{
new ParameterContract
{
Name = "param_" + TestUtilities.GenerateName(),
Description = "description_" + TestUtilities.GenerateName(),
Type = "int",
DefaultValue = "b",
Required = true,
Values = new[] { "a", "b", "c" }
},
new ParameterContract
{
Name = "param_" + TestUtilities.GenerateName(),
Description = "description_" + TestUtilities.GenerateName(),
Type = "bool",
DefaultValue = "e",
Required = false,
Values = new[] { "d", "e", "f" }
}
},
Representations = new[]
{
new RepresentationContract
{
ContentType = "text/plain",
Sample = "sample_" + TestUtilities.GenerateName(),
},
new RepresentationContract
{
ContentType = "application/xml",
Sample = "sample_" + TestUtilities.GenerateName(),
}
}
},
Responses = new[]
{
new ResponseContract
{
StatusCode = 200,
Description = "description_" + TestUtilities.GenerateName(),
Representations = new[]
{
new RepresentationContract
{
ContentType = "application/json",
Sample = "sample_" + TestUtilities.GenerateName()
},
new RepresentationContract
{
ContentType = "application/xml",
Sample = "sample_" + TestUtilities.GenerateName()
}
}
}
}
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ public static Stream ToStream(this XDocument doc)
stream.Position = 0;
return stream;
}

public static IEnumerable<T> ToIEnumerable<T>(this IEnumerator<T> enumerator)
{
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
}
}
Loading