forked from Azure/Commercial-Marketplace-SaaS-Accelerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeteredApiTest.cs
112 lines (101 loc) · 4.32 KB
/
MeteredApiTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
namespace Microsoft.Marketplace.SaasKit.UnitTest
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Marketplace.SaasKit.Configurations;
using Microsoft.Marketplace.SaasKit.Helpers;
using Microsoft.Marketplace.SaasKit.Models;
using Microsoft.Marketplace.SaasKit.Services;
using Microsoft.VisualStudio.TestTools.UnitTesting;
/// <summary>
/// Metered API Test.
/// </summary>
[TestClass]
public class MeteredApiTest
{
/// <summary>The client.</summary>
private MeteredBillingApiClient client;
/// <summary>The client.</summary>
private FulfillmentApiClient fulfillmentClient;
/// <summary>The configuration.</summary>
private SaaSApiClientConfiguration configuration = new SaaSApiClientConfiguration();
/// <summary>Initializes a new instance of the <see cref="MeteredApiTest"/> class.</summary>
public MeteredApiTest()
{
var builder = new ConfigurationBuilder();
IConfigurationRoot config = new ConfigurationBuilder()
.AddJsonFile("appsettings.test.json")
.Build();
this.configuration = config.GetSection("AppSetting").Get<SaaSApiClientConfiguration>();
this.client = new MeteredBillingApiClient(this.configuration, null);
this.fulfillmentClient = new FulfillmentApiClient(this.configuration, null);
}
/// <summary>
/// Check Authentication.
/// </summary>
/// <returns>Test Authentication.</returns>
[TestMethod]
public async Task CheckAuthentication()
{
var accessTokenResult = await ADAuthenticationHelper.GetAccessToken(this.configuration).ConfigureAwait(false);
Assert.IsNotNull(accessTokenResult);
Assert.IsNotNull(accessTokenResult?.AccessToken);
}
/// <summary>
/// Gets the subscription by identifier.
/// </summary>
/// <returns>Test Subscription Usage.</returns>
[TestMethod]
public async Task TestSubscriptionUsage()
{
var allSubscriptions = await this.fulfillmentClient.GetAllSubscriptionAsync().ConfigureAwait(false);
var defaultSubscription = allSubscriptions.FirstOrDefault();
MeteringUsageRequest subscriptionUsageRequest = new MeteringUsageRequest()
{
Dimension = "Test",
EffectiveStartTime = DateTime.UtcNow,
PlanId = defaultSubscription?.PlanId,
Quantity = 5,
ResourceId = defaultSubscription.Id,
};
var response = this.client.EmitUsageEventAsync(subscriptionUsageRequest).Result;
Assert.AreEqual(response.Status, "Accepted");
Assert.AreEqual(response.ResourceId, defaultSubscription?.Id);
Assert.AreEqual(response.PlanId, defaultSubscription?.PlanId);
}
/// <summary>
/// Test subscription batch usage.
/// </summary>
/// <returns>Test Subscription Batch Usage.</returns>
[TestMethod]
public async Task TestSubscriptionBatchUsage()
{
var allSubscriptions = await this.fulfillmentClient.GetAllSubscriptionAsync().ConfigureAwait(false);
var defaultSubscription = allSubscriptions.FirstOrDefault();
var subscriptionUsageRequest = new List<MeteringUsageRequest>
{
new MeteringUsageRequest()
{
Dimension = "Test",
EffectiveStartTime = DateTime.UtcNow,
PlanId = defaultSubscription?.PlanId,
Quantity = 5,
ResourceId = defaultSubscription.Id,
},
new MeteringUsageRequest()
{
Dimension = "Test",
EffectiveStartTime = DateTime.UtcNow,
PlanId = defaultSubscription?.PlanId,
Quantity = 5,
ResourceId = defaultSubscription.Id,
},
};
var response = await this.client.EmitBatchUsageEventAsync(subscriptionUsageRequest);
Assert.AreEqual(response.Count, 2);
}
}
}