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
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="$([MSBuild]::GetPathOfFileAbove('AzSdk.test.reference.props'))" />
<PropertyGroup>
<PackageId>ApiManagement.Tests</PackageId>
<Description>ApiManagement.Tests Class Library</Description>
<Version>1.0.0-preview</Version>
<Authors>Microsoft Corporation</Authors>
<AssemblyName>ApiManagementManagement.Tests</AssemblyName>
</PropertyGroup>

<PropertyGroup>
<TargetFrameworks>netcoreapp1.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<!--<PackageReference Include="Microsoft.Azure.Management.ApiManagement" Version="2.1.0-ForTest" />-->
<ProjectReference Include="..\Management.ApiManagement\Microsoft.Azure.Management.ApiManagement.csproj" />
<PackageReference Include="Microsoft.Azure.Management.EventHub" Version="1.2.0" />

<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
<PackageReference Include="Microsoft.Azure.Management.Storage" Version="[6.0.0-preview,7.0)" />
<PackageReference Include="Microsoft.Azure.Management.Network" Version="10.0.0-preview" />
</ItemGroup>

<ItemGroup>
<None Update="SessionRecords\**\*.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<!--Do not remove until VS Test Tools fixes #472-->
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<None Update="Resources\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
158 changes: 158 additions & 0 deletions src/SDKs/ApiManagement/ApiManagement.Tests/ApiManagementTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
//

using System;
using System.Linq;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System.Collections.Generic;
using Microsoft.Azure.Management.ApiManagement;
using Microsoft.Azure.Management.ApiManagement.Models;
using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Network;
using Xunit;
using Microsoft.Azure.Management.EventHub;

namespace ApiManagement.Tests
{
public class ApiManagementTestBase : TestBase
{
private const string SubIdKey = "SubId";
private const string ServiceNameKey = "ServiceName";
private const string ResourceGroupNameKey = "ResourceGroup";
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; }
public ResourceManagementClient resourcesClient { get; set; }
public StorageManagementClient storageClient { get; set; }
public NetworkManagementClient networkClient { get; set; }
public EventHubManagementClient eventHubClient { get; set; }
public string rgName { get; internal set; }
public Dictionary<string, string> tags { get; internal set; }
public string serviceName { get; internal set; }
public ApiManagementServiceResource serviceProperties { get; internal set; }
public string base64EncodedTestCertificateData { get; internal set; }
public string testCertificatePassword { get; internal set; }

public ApiManagementTestBase(MockContext context)
{
this.client = context.GetServiceClient<ApiManagementClient>();
this.resourcesClient = context.GetServiceClient<ResourceManagementClient>();
this.storageClient = context.GetServiceClient<StorageManagementClient>();
this.networkClient = context.GetServiceClient<NetworkManagementClient>();
this.eventHubClient = context.GetServiceClient<EventHubManagementClient>();

Initialize();
}

private void Initialize()
{
var testEnv = TestEnvironmentFactory.GetTestEnvironment();

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

this.location = testEnv.ConnectionString.KeyValuePairs[LocationKey];
if (string.IsNullOrEmpty(this.location))
{
this.location = GetLocation();
}

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

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

this.subscriptionId = testEnv.SubscriptionId;
HttpMockServer.Variables[SubIdKey] = subscriptionId;
HttpMockServer.Variables[ServiceNameKey] = this.serviceName;
HttpMockServer.Variables[LocationKey] = this.location;
HttpMockServer.Variables[ResourceGroupNameKey] = this.rgName;
}
else if (HttpMockServer.Mode == HttpRecorderMode.Playback)
{
this.subscriptionId = testEnv.SubscriptionId;
subscriptionId = HttpMockServer.Variables[SubIdKey];
rgName = HttpMockServer.Variables[ResourceGroupNameKey];
serviceName = HttpMockServer.Variables[ServiceNameKey];
location = HttpMockServer.Variables[LocationKey];
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))
{
this.testCertificatePassword = testCertificatePwd;
}
}

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

serviceProperties = new ApiManagementServiceResource
{
Sku = new ApiManagementServiceSkuProperties
{
Name = SkuType.Developer,
Capacity = 1
},
Location = location,
PublisherEmail = "apim@autorestsdk.com",
PublisherName = "autorestsdk",
Tags = tags
};
}

public void TryCreateApiManagementService()
{
this.client.ApiManagementService.CreateOrUpdate(
resourceGroupName: this.rgName,
serviceName: this.serviceName,
parameters: this.serviceProperties);

var service = this.client.ApiManagementService.Get(this.rgName, this.serviceName);
Assert.Equal(this.serviceName, service.Name);
}

public string GetLocation(string regionIn = "US")
{
var provider = this.resourcesClient.Providers.Get("Microsoft.ApiManagement");
return provider.ResourceTypes.Where(
(resType) =>
{
if (resType.ResourceType == "service")
return true;
else
return false;
}
).First().Locations.Where(l => l.Contains(regionIn)).FirstOrDefault();
}
}
}
41 changes: 41 additions & 0 deletions src/SDKs/ApiManagement/ApiManagement.Tests/Helpers/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;

namespace ApiManagementManagement.Tests.Helpers
{
public static class Extensions
{
public static bool DictionaryEqual<TKey, TValue>(this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@solankisamir
nit: Can you not use Linq extension methods first.Except(second) or better use Linq to do the comparison.
I assume you only want to compare keys and not their values, in that case you can still do that using the above

{
return first.DictionaryEqual(second, null);
}

public static bool DictionaryEqual<TKey, TValue>(
this IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second,
IEqualityComparer<TValue> valueComparer)
{
if (first == second) return true;
if ((first == null) || (second == null)) return false;
if (first.Count != second.Count) return false;

valueComparer = valueComparer ?? EqualityComparer<TValue>.Default;

foreach (var kvp in first)
{
TValue secondValue;
if (!second.TryGetValue(kvp.Key, out secondValue)) return false;
if (!valueComparer.Equals(kvp.Value, secondValue)) return false;
}
return true;
}

public static Stream ToStream(this XDocument doc)
{
var stream = new MemoryStream();
doc.Save(stream);
stream.Position = 0;
return stream;
}
}
}
Loading