|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Threading; |
| 7 | +using Azure.AI.MetricsAdvisor.Administration; |
| 8 | +using Azure.AI.MetricsAdvisor.Models; |
| 9 | +using Azure.Core.TestFramework; |
| 10 | +using NUnit.Framework; |
| 11 | + |
| 12 | +namespace Azure.AI.MetricsAdvisor.Tests |
| 13 | +{ |
| 14 | + public class DataFeedTests : ClientTestBase |
| 15 | + { |
| 16 | + public DataFeedTests(bool isAsync) : base(isAsync) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + private string FakeGuid => "00000000-0000-0000-0000-000000000000"; |
| 21 | + |
| 22 | + [Test] |
| 23 | + public void CreateDataFeedValidatesArguments() |
| 24 | + { |
| 25 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 26 | + |
| 27 | + Assert.That(() => adminClient.CreateDataFeedAsync(null), Throws.InstanceOf<ArgumentNullException>()); |
| 28 | + |
| 29 | + Assert.That(() => adminClient.CreateDataFeed(null), Throws.InstanceOf<ArgumentNullException>()); |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + public void CreateDataFeedRespectsTheCancellationToken() |
| 34 | + { |
| 35 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 36 | + |
| 37 | + var dataSource = new AzureTableDataFeedSource("connectionString", "table", "query"); |
| 38 | + var granularity = new DataFeedGranularity(DataFeedGranularityType.Daily); |
| 39 | + var schema = new DataFeedSchema(new List<DataFeedMetric>() { new DataFeedMetric("metricName") }); |
| 40 | + var ingestionSettings = new DataFeedIngestionSettings(DateTimeOffset.UtcNow); |
| 41 | + var dataFeed = new DataFeed("dataFeedName", dataSource, granularity, schema, ingestionSettings); |
| 42 | + |
| 43 | + using var cancellationSource = new CancellationTokenSource(); |
| 44 | + cancellationSource.Cancel(); |
| 45 | + |
| 46 | + Assert.That(() => adminClient.CreateDataFeedAsync(dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 47 | + Assert.That(() => adminClient.CreateDataFeed(dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + public void UpdateDataFeedValidatesArguments() |
| 52 | + { |
| 53 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 54 | + |
| 55 | + var dataSource = new AzureTableDataFeedSource("connectionString", "table", "query"); |
| 56 | + var granularity = new DataFeedGranularity(DataFeedGranularityType.Daily); |
| 57 | + var schema = new DataFeedSchema(new List<DataFeedMetric>() { new DataFeedMetric("metricName") }); |
| 58 | + var ingestionSettings = new DataFeedIngestionSettings(DateTimeOffset.UtcNow); |
| 59 | + var dataFeed = new DataFeed("dataFeedName", dataSource, granularity, schema, ingestionSettings); |
| 60 | + |
| 61 | + Assert.That(() => adminClient.UpdateDataFeedAsync(null, dataFeed), Throws.InstanceOf<ArgumentNullException>()); |
| 62 | + Assert.That(() => adminClient.UpdateDataFeedAsync("", dataFeed), Throws.InstanceOf<ArgumentException>()); |
| 63 | + Assert.That(() => adminClient.UpdateDataFeedAsync("dataFeedId", dataFeed), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); |
| 64 | + Assert.That(() => adminClient.UpdateDataFeedAsync(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>()); |
| 65 | + |
| 66 | + Assert.That(() => adminClient.UpdateDataFeed(null, dataFeed), Throws.InstanceOf<ArgumentNullException>()); |
| 67 | + Assert.That(() => adminClient.UpdateDataFeed("", dataFeed), Throws.InstanceOf<ArgumentException>()); |
| 68 | + Assert.That(() => adminClient.UpdateDataFeed("dataFeedId", dataFeed), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); |
| 69 | + Assert.That(() => adminClient.UpdateDataFeed(FakeGuid, null), Throws.InstanceOf<ArgumentNullException>()); |
| 70 | + } |
| 71 | + |
| 72 | + [Test] |
| 73 | + public void UpdateDataFeedRespectsTheCancellationToken() |
| 74 | + { |
| 75 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 76 | + |
| 77 | + var dataSource = new AzureTableDataFeedSource("connectionString", "table", "query"); |
| 78 | + var granularity = new DataFeedGranularity(DataFeedGranularityType.Daily); |
| 79 | + var schema = new DataFeedSchema(new List<DataFeedMetric>() { new DataFeedMetric("metricName") }); |
| 80 | + var ingestionSettings = new DataFeedIngestionSettings(DateTimeOffset.UtcNow); |
| 81 | + var dataFeed = new DataFeed("dataFeedName", dataSource, granularity, schema, ingestionSettings); |
| 82 | + |
| 83 | + using var cancellationSource = new CancellationTokenSource(); |
| 84 | + cancellationSource.Cancel(); |
| 85 | + |
| 86 | + Assert.That(() => adminClient.UpdateDataFeedAsync(FakeGuid, dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 87 | + Assert.That(() => adminClient.UpdateDataFeed(FakeGuid, dataFeed, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void GetDataFeedValidatesArguments() |
| 92 | + { |
| 93 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 94 | + |
| 95 | + Assert.That(() => adminClient.GetDataFeedAsync(null), Throws.InstanceOf<ArgumentNullException>()); |
| 96 | + Assert.That(() => adminClient.GetDataFeedAsync(""), Throws.InstanceOf<ArgumentException>()); |
| 97 | + Assert.That(() => adminClient.GetDataFeedAsync("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); |
| 98 | + |
| 99 | + Assert.That(() => adminClient.GetDataFeed(null), Throws.InstanceOf<ArgumentNullException>()); |
| 100 | + Assert.That(() => adminClient.GetDataFeed(""), Throws.InstanceOf<ArgumentException>()); |
| 101 | + Assert.That(() => adminClient.GetDataFeed("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); |
| 102 | + } |
| 103 | + |
| 104 | + [Test] |
| 105 | + public void GetDataFeedRespectsTheCancellationToken() |
| 106 | + { |
| 107 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 108 | + |
| 109 | + using var cancellationSource = new CancellationTokenSource(); |
| 110 | + cancellationSource.Cancel(); |
| 111 | + |
| 112 | + Assert.That(() => adminClient.GetDataFeedAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 113 | + Assert.That(() => adminClient.GetDataFeed(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 114 | + } |
| 115 | + |
| 116 | + [Test] |
| 117 | + public void GetDataFeedsRespectsTheCancellationToken() |
| 118 | + { |
| 119 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 120 | + |
| 121 | + using var cancellationSource = new CancellationTokenSource(); |
| 122 | + cancellationSource.Cancel(); |
| 123 | + |
| 124 | + IAsyncEnumerator<DataFeed> asyncEnumerator = adminClient.GetDataFeedsAsync(cancellationToken: cancellationSource.Token).GetAsyncEnumerator(); |
| 125 | + Assert.That(async () => await asyncEnumerator.MoveNextAsync(), Throws.InstanceOf<OperationCanceledException>()); |
| 126 | + |
| 127 | + IEnumerator<DataFeed> enumerator = adminClient.GetDataFeeds(cancellationToken: cancellationSource.Token).GetEnumerator(); |
| 128 | + Assert.That(() => enumerator.MoveNext(), Throws.InstanceOf<OperationCanceledException>()); |
| 129 | + } |
| 130 | + |
| 131 | + [Test] |
| 132 | + public void DeleteDataFeedValidatesArguments() |
| 133 | + { |
| 134 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 135 | + |
| 136 | + Assert.That(() => adminClient.DeleteDataFeedAsync(null), Throws.InstanceOf<ArgumentNullException>()); |
| 137 | + Assert.That(() => adminClient.DeleteDataFeedAsync(""), Throws.InstanceOf<ArgumentException>()); |
| 138 | + Assert.That(() => adminClient.DeleteDataFeedAsync("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); |
| 139 | + |
| 140 | + Assert.That(() => adminClient.DeleteDataFeed(null), Throws.InstanceOf<ArgumentNullException>()); |
| 141 | + Assert.That(() => adminClient.DeleteDataFeed(""), Throws.InstanceOf<ArgumentException>()); |
| 142 | + Assert.That(() => adminClient.DeleteDataFeed("dataFeedId"), Throws.InstanceOf<ArgumentException>().With.InnerException.TypeOf(typeof(FormatException))); |
| 143 | + } |
| 144 | + |
| 145 | + [Test] |
| 146 | + public void DeleteDataFeedRespectsTheCancellationToken() |
| 147 | + { |
| 148 | + MetricsAdvisorAdministrationClient adminClient = GetMetricsAdvisorAdministrationClient(); |
| 149 | + |
| 150 | + using var cancellationSource = new CancellationTokenSource(); |
| 151 | + cancellationSource.Cancel(); |
| 152 | + |
| 153 | + Assert.That(() => adminClient.DeleteDataFeedAsync(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 154 | + Assert.That(() => adminClient.DeleteDataFeed(FakeGuid, cancellationSource.Token), Throws.InstanceOf<OperationCanceledException>()); |
| 155 | + } |
| 156 | + |
| 157 | + private MetricsAdvisorAdministrationClient GetMetricsAdvisorAdministrationClient() |
| 158 | + { |
| 159 | + var fakeEndpoint = new Uri("http://notreal.azure.com"); |
| 160 | + var fakeCredential = new MetricsAdvisorKeyCredential("fakeSubscriptionKey", "fakeApiKey"); |
| 161 | + |
| 162 | + return new MetricsAdvisorAdministrationClient(fakeEndpoint, fakeCredential); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments