Skip to content

Commit

Permalink
SDK Client Library for AzureRM.Billing and tests (Azure#2838)
Browse files Browse the repository at this point in the history
* SDK Client Library for AzureRM.Billing and tests
Add the newly generated SDK Client for Azure Resource Management Billing
Service and tests.

* Remove unnecessary projects in cs, fix version, fix tests as playback should not compare time with current time

* Update Test Project GUID to make it unique
  • Loading branch information
pandawang1123 authored and shahabhijeet committed Feb 24, 2017
1 parent f979c41 commit 4d37cc1
Show file tree
Hide file tree
Showing 36 changed files with 3,614 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/ResourceManagement/Billing/Billing.Tests/Billing.Tests.xproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>F4CDC178-3DBE-4535-A384-633AB0A802B9</ProjectGuid>
<RootNamespace>Billing.Tests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.Azure.Management.Billing;
using Microsoft.Azure.Test.HttpRecorder;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
using System.Net;
using System.Threading;

namespace Billing.Tests.Helpers
{
public static class BillingTestUtilities
{
public static BillingClient GetBillingManagementClient(MockContext context, RecordedDelegatingHandler handler = null)
{
if (handler != null)
{
handler.IsPassThrough = true;
}

var client = context.GetServiceClient<BillingClient>(handlers:
handler ?? new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
return client;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace Billing.Tests.Helpers
{
public class RecordedDelegatingHandler : DelegatingHandler
{
private HttpResponseMessage _response;

public RecordedDelegatingHandler()
{
StatusCodeToReturn = HttpStatusCode.Created;
SubsequentStatusCodeToReturn = StatusCodeToReturn;
}

public RecordedDelegatingHandler(HttpResponseMessage response)
{
StatusCodeToReturn = HttpStatusCode.Created;
SubsequentStatusCodeToReturn = StatusCodeToReturn;
_response = response;
}

public HttpStatusCode StatusCodeToReturn { get; set; }

public HttpStatusCode SubsequentStatusCodeToReturn { get; set; }

public string Request { get; private set; }

public HttpRequestHeaders RequestHeaders { get; private set; }

public HttpContentHeaders ContentHeaders { get; private set; }

public HttpMethod Method { get; private set; }

public Uri Uri { get; private set; }

public bool IsPassThrough { get; set; }

private int counter;

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
{
counter++;
// Save request
if (request.Content == null)
{
Request = string.Empty;
}
else
{
Request = await request.Content.ReadAsStringAsync();
}
RequestHeaders = request.Headers;
if (request.Content != null)
{
ContentHeaders = request.Content.Headers;
}
Method = request.Method;
Uri = request.RequestUri;

// Prepare response
if (IsPassThrough)
{
return await base.SendAsync(request, cancellationToken);
}
else
{
if (_response != null && counter == 1)
{
return _response;
}
else
{
var statusCode = StatusCodeToReturn;
if (counter > 1)
{
statusCode = SubsequentStatusCodeToReturn;
}
HttpResponseMessage response = new HttpResponseMessage(statusCode);
response.Content = new StringContent("");
return response;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Billing.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Billing.Tests")]
[assembly: AssemblyCopyright("Copyright (c) Microsoft Corporation")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: ComVisible(false)]

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Billing.Tests.Helpers;
using Microsoft.Azure.Management.Billing;
using Microsoft.Azure.Management.Billing.Models;
using Microsoft.Azure.Management.Resources;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System;
using System.Linq;
using System.Net;
using Xunit;

namespace Billing.Tests.ScenarioTests
{
public class InvoicesTests : TestBase
{
const string DownloadUrlExpand = "downloadUrl";
const string RangeFilter = "invoicePeriodEndDate ge 2017-01-31 and invoicePeriodEndDate le 2017-02-28";
const string InvoiceName = "2017-02-09-117646100066812";

[Fact]
public void ListInvoicesTest()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
var billingMgmtClient = BillingTestUtilities.GetBillingManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
var invoices = billingMgmtClient.Invoices.List();
Assert.NotNull(invoices);
Assert.True(invoices.Any());
Assert.False(invoices.Any(x => x.DownloadUrl != null));
}
}

[Fact]
public void ListInvoicesWithQueryParametersTest()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
var billingMgmtClient = BillingTestUtilities.GetBillingManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
var invoices = billingMgmtClient.Invoices.List(DownloadUrlExpand, RangeFilter, null, 1);
Assert.NotNull(invoices);
Assert.Equal(1, invoices.Count());
Assert.NotNull(invoices.First().DownloadUrl);
var invoice = invoices.First();
Assert.False(string.IsNullOrWhiteSpace(invoice.DownloadUrl.Url));
Assert.True(invoice.DownloadUrl.ExpiryTime.HasValue);
}
}

[Fact]
public void GetLatestInvoice()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
var billingMgmtClient = BillingTestUtilities.GetBillingManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
var invoice = billingMgmtClient.Invoices.GetLatest();
Assert.NotNull(invoice);
Assert.False(string.IsNullOrWhiteSpace(invoice.DownloadUrl.Url));
Assert.True(invoice.DownloadUrl.ExpiryTime.HasValue);
}
}

[Fact]
public void GetInvoiceWithName()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
var billingMgmtClient = BillingTestUtilities.GetBillingManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
var invoice = billingMgmtClient.Invoices.Get(InvoiceName);
Assert.NotNull(invoice);
Assert.Equal(InvoiceName, invoice.Name);
Assert.False(string.IsNullOrWhiteSpace(invoice.DownloadUrl.Url));
Assert.True(invoice.DownloadUrl.ExpiryTime.HasValue);
}
}

[Fact]
public void GetInvoicesNoResult()
{
string rangeFilter = "invoicePeriodEndDate lt 2016-01-31";
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
try
{
var billingMgmtClient = BillingTestUtilities.GetBillingManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });
billingMgmtClient.Invoices.List(DownloadUrlExpand, rangeFilter, null, 1);
Assert.False(true, "ErrorResponseException should have been thrown");
}
catch(ErrorResponseException e)
{
Assert.NotNull(e.Body);
Assert.NotNull(e.Body.Error);
Assert.Equal("ResourceNotFound", e.Body.Error.Code);
Assert.False(string.IsNullOrWhiteSpace(e.Body.Error.Message));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Billing.Tests.Helpers;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using System.Linq;
using System.Net;
using Xunit;
using Microsoft.Azure.Management.Billing;

namespace Billing.Tests.ScenarioTests
{
public class OperationsTests : TestBase
{
[Fact]
public void ListOperationsTest()
{
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
// Create client
var billingMgmtClient = BillingTestUtilities.GetBillingManagementClient(context, new RecordedDelegatingHandler { StatusCodeToReturn = HttpStatusCode.OK });

// Get operations
var operations = billingMgmtClient.Operations.List();

// Verify operations are returned
Assert.NotNull(operations);
Assert.True(operations.Any());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"Entries": [
{
"RequestUri": "/subscriptions/0347d63a-421a-43a7-836a-5f389c79cd07/providers/Microsoft.Billing/invoices/2017-02-09-117646100066812?api-version=2017-02-27-preview",
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvMDM0N2Q2M2EtNDIxYS00M2E3LTgzNmEtNWYzODljNzljZDA3L3Byb3ZpZGVycy9NaWNyb3NvZnQuQmlsbGluZy9pbnZvaWNlcy8yMDE3LTAyLTA5LTExNzY0NjEwMDA2NjgxMj9hcGktdmVyc2lvbj0yMDE3LTAyLTI3LXByZXZpZXc=",
"RequestMethod": "GET",
"RequestBody": "",
"RequestHeaders": {
"x-ms-client-request-id": [
"85f70922-65cc-4047-bb92-6b413ed8d44d"
],
"accept-language": [
"en-US"
],
"User-Agent": [
"FxVersion/4.6.24410.01",
"Microsoft.Azure.Management.Billing.BillingClient/1.0.0-preview"
]
},
"ResponseBody": "{\r\n \"id\": \"/subscriptions/0347d63a-421a-43a7-836a-5f389c79cd07/providers/Microsoft.Billing/invoices/2017-02-09-117646100066812\",\r\n \"type\": \"Microsoft.Billing/invoices\",\r\n \"name\": \"2017-02-09-117646100066812\",\r\n \"properties\": {\r\n \"downloadUrl\": {\r\n \"expiryTime\": \"2017-02-18T20:44:40Z\",\r\n \"url\": \"https://billinsstorev2test.blob.core.windows.net/invoices/0347d63a-421a-43a7-836a-5f389c79cd07-2017-02-09-117646100066812.pdf?sv=2014-02-14&sr=b&sig=AJW5hhGYlN3DD3%2FL%2B11N0Y40CnGf2B46ff4LXqiG5ds%3D&se=2017-02-18T20%3A44%3A40Z&sp=r\"\r\n },\r\n \"invoicePeriodEndDate\": \"2017-02-09\",\r\n \"invoicePeriodStartDate\": \"2017-01-10\"\r\n }\r\n}",
"ResponseHeaders": {
"Content-Type": [
"application/json; charset=utf-8"
],
"Expires": [
"-1"
],
"Cache-Control": [
"no-cache"
],
"Date": [
"Sat, 18 Feb 2017 19:44:42 GMT"
],
"Pragma": [
"no-cache"
],
"Transfer-Encoding": [
"chunked"
],
"Server": [
"Microsoft-IIS/8.5"
],
"Vary": [
"Accept-Encoding"
],
"Strict-Transport-Security": [
"max-age=31536000; includeSubDomains"
],
"X-AspNet-Version": [
"4.0.30319"
],
"X-Powered-By": [
"ASP.NET"
],
"x-ms-ratelimit-remaining-subscription-reads": [
"14998"
],
"x-ms-request-id": [
"d4b80d41-3aad-4354-8a08-d2082eeb0e64"
],
"x-ms-correlation-request-id": [
"d4b80d41-3aad-4354-8a08-d2082eeb0e64"
],
"x-ms-routing-request-id": [
"CENTRALUS:20170218T194442Z:d4b80d41-3aad-4354-8a08-d2082eeb0e64"
]
},
"StatusCode": 200
}
],
"Names": {},
"Variables": {
"SubscriptionId": "0347d63a-421a-43a7-836a-5f389c79cd07"
}
}
Loading

0 comments on commit 4d37cc1

Please sign in to comment.