|
| 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.Linq; |
| 7 | +using System.Net.WebSockets; |
| 8 | +using System.Text; |
| 9 | +using System.Text.RegularExpressions; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using Azure.Communication.Administration.Models; |
| 12 | +using Azure.Core.TestFramework; |
| 13 | +using NUnit.Framework; |
| 14 | + |
| 15 | +namespace Azure.Communication.Administration.Tests |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// The suite of tests for the <see cref="PhoneNumberAdministrationClient"/> class. |
| 19 | + /// </summary> |
| 20 | + /// <remarks> |
| 21 | + /// These tests have a dependency on live Azure services and may incur costs for the associated |
| 22 | + /// Azure subscription. |
| 23 | + /// </remarks> |
| 24 | + public class PhoneNumberAdministrationClientLiveTests : PhoneNumberAdministrationClientLiveTestBase |
| 25 | + { |
| 26 | + /// <summary> |
| 27 | + /// Initializes a new instance of the <see cref="PhoneNumberAdministrationClientLiveTests"/> class. |
| 28 | + /// </summary> |
| 29 | + /// <param name="isAsync">A flag used by the Azure Core Test Framework to differentiate between tests for asynchronous and synchronous methods.</param> |
| 30 | + public PhoneNumberAdministrationClientLiveTests(bool isAsync) : base(isAsync) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + [Test] |
| 35 | + [TestCase(null, TestName = "GetAllSupportedCountries")] |
| 36 | + [TestCase("en-US", TestName = "GetAllSupportedCountriesEnUsLocale")] |
| 37 | + public async Task GetAllSupportedCountries(string? locale) |
| 38 | + { |
| 39 | + var client = CreateClient(); |
| 40 | + |
| 41 | + var countries = client.GetAllSupportedCountriesAsync(locale); |
| 42 | + |
| 43 | + await foreach (var country in countries) |
| 44 | + { |
| 45 | + Assert.IsFalse(string.IsNullOrEmpty(country.CountryCode)); |
| 46 | + Assert.IsFalse(string.IsNullOrEmpty(country.LocalizedName)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [Test] |
| 51 | + public async Task GetAllPhoneNumbers() |
| 52 | + { |
| 53 | + var client = CreateClient(); |
| 54 | + |
| 55 | + var numbersPagable = client.GetAllPhoneNumbersAsync(); |
| 56 | + var numbers = await numbersPagable.ToEnumerableAsync(); |
| 57 | + |
| 58 | + Assert.IsNotNull(numbers); |
| 59 | + Assert.IsNotEmpty(numbers); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + [TestCase(null, null)] |
| 64 | + [TestCase("en-US", null)] |
| 65 | + [TestCase("en-US", false)] |
| 66 | + [TestCase("en-US", true)] |
| 67 | + public async Task GetPlanGroups(string? locale, bool? includeRateInformation) |
| 68 | + { |
| 69 | + var client = CreateClient(); |
| 70 | + var countryCode = "US"; |
| 71 | + |
| 72 | + var pageablePhonePlanGroups = client.GetPhonePlanGroupsAsync(countryCode, locale, includeRateInformation); |
| 73 | + |
| 74 | + var phonePlanGroups = await pageablePhonePlanGroups.ToEnumerableAsync(); |
| 75 | + |
| 76 | + Assert.IsNotNull(phonePlanGroups); |
| 77 | + Assert.AreEqual(3, phonePlanGroups.Count); |
| 78 | + |
| 79 | + var firstGroup = phonePlanGroups.First(group => group.PhoneNumberType == PhoneNumberType.Geographic); |
| 80 | + |
| 81 | + Assert.IsNotNull(firstGroup.LocalizedName); |
| 82 | + Assert.IsNotNull(firstGroup.LocalizedDescription); |
| 83 | + |
| 84 | + if (includeRateInformation == true) |
| 85 | + { |
| 86 | + Assert.IsNotNull(firstGroup.RateInformation); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + Assert.IsNull(firstGroup.RateInformation); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + [Test] |
| 95 | + [TestCase(null)] |
| 96 | + [TestCase("en-US")] |
| 97 | + public async Task GetPhonePlans(string? locale) |
| 98 | + { |
| 99 | + var client = CreateClient(); |
| 100 | + var countryCode = "US"; |
| 101 | + |
| 102 | + var pageablePhonePlanGroups = client.GetPhonePlanGroupsAsync(countryCode, locale); |
| 103 | + var phonePlanGroups = await pageablePhonePlanGroups.ToEnumerableAsync().ConfigureAwait(false); |
| 104 | + |
| 105 | + var pageablePhonePlans = client.GetPhonePlansAsync(countryCode, phonePlanGroups.First().PhonePlanGroupId, locale); |
| 106 | + var phonePlans = await pageablePhonePlans.ToEnumerableAsync(); |
| 107 | + |
| 108 | + Assert.IsNotNull(phonePlans); |
| 109 | + Assert.IsNotEmpty(phonePlans); |
| 110 | + } |
| 111 | + |
| 112 | + [Test] |
| 113 | + [TestCase(null)] |
| 114 | + [TestCase("en-US")] |
| 115 | + public async Task GetAreaCodesForPlan(string? locale) |
| 116 | + { |
| 117 | + var client = CreateClient(); |
| 118 | + var countryCode = "US"; |
| 119 | + |
| 120 | + var pageablePhonePlanGroups = client.GetPhonePlanGroupsAsync(countryCode, locale); |
| 121 | + var phonePlanGroups = await pageablePhonePlanGroups.ToEnumerableAsync().ConfigureAwait(false); |
| 122 | + |
| 123 | + string phonePlanGroupId = phonePlanGroups.First(group => group.PhoneNumberType == PhoneNumberType.Geographic).PhonePlanGroupId; |
| 124 | + var pageablePhonePlans = client.GetPhonePlansAsync(countryCode, phonePlanGroupId, locale); |
| 125 | + var phonePlans = await pageablePhonePlans.ToEnumerableAsync(); |
| 126 | + var phonePlanId = phonePlans.First().PhonePlanId; |
| 127 | + |
| 128 | + var locationOptionsResponse = await client.GetPhonePlanLocationOptionsAsync(countryCode, phonePlanGroupId, phonePlanId).ConfigureAwait(false); |
| 129 | + |
| 130 | + var locationOptions = new List<LocationOptionsQuery> |
| 131 | + { |
| 132 | + new LocationOptionsQuery |
| 133 | + { |
| 134 | + LabelId = "state", |
| 135 | + OptionsValue = "NY" |
| 136 | + }, |
| 137 | + new LocationOptionsQuery |
| 138 | + { |
| 139 | + LabelId = "city", |
| 140 | + OptionsValue = "NOAM-US-NY-NY" |
| 141 | + } |
| 142 | + }; |
| 143 | + |
| 144 | + var areaCodes = await client.GetAllAreaCodesAsync("selection", countryCode, phonePlanId, locationOptions); |
| 145 | + |
| 146 | + Assert.IsNotNull(areaCodes.Value.PrimaryAreaCodes); |
| 147 | + Assert.IsNotEmpty(areaCodes.Value.PrimaryAreaCodes); |
| 148 | + } |
| 149 | + |
| 150 | + [Test] |
| 151 | + [TestCase(null)] |
| 152 | + [TestCase("en-US")] |
| 153 | + [AsyncOnly] |
| 154 | + public async Task CreateReservationErrorState(string? locale) |
| 155 | + { |
| 156 | + var client = CreateClient(); |
| 157 | + var countryCode = "US"; |
| 158 | + |
| 159 | + var pageablePhonePlanGroups = client.GetPhonePlanGroupsAsync(countryCode, locale); |
| 160 | + var phonePlanGroups = await pageablePhonePlanGroups.ToEnumerableAsync().ConfigureAwait(false); |
| 161 | + |
| 162 | + string phonePlanGroupId = phonePlanGroups.First(group => group.PhoneNumberType == PhoneNumberType.TollFree).PhonePlanGroupId; |
| 163 | + var pageablePhonePlans = client.GetPhonePlansAsync(countryCode, phonePlanGroupId, locale); |
| 164 | + var phonePlan = (await pageablePhonePlans.ToEnumerableAsync()).First(); |
| 165 | + var tollFreeAreaCode = phonePlan.AreaCodes.First(); |
| 166 | + |
| 167 | + string geographicPhonePlanGroupId = phonePlanGroups.First(group => group.PhoneNumberType == PhoneNumberType.Geographic).PhonePlanGroupId; |
| 168 | + var geographicPhonePlanId = (await client.GetPhonePlansAsync(countryCode, geographicPhonePlanGroupId, locale).ToEnumerableAsync()).First().PhonePlanId; |
| 169 | + |
| 170 | + var reservationOptions = new CreateReservationOptions("My reservation", "my description", new[] { geographicPhonePlanId }, tollFreeAreaCode); |
| 171 | + reservationOptions.Quantity = 1; |
| 172 | + var reservationOperation = await client.StartReservationAsync(reservationOptions); |
| 173 | + |
| 174 | + try |
| 175 | + { |
| 176 | + await reservationOperation.WaitForCompletionAsync().ConfigureAwait(false); |
| 177 | + } |
| 178 | + catch (Exception ex) |
| 179 | + { |
| 180 | + Assert.AreEqual("Reservation has failed.", ex.Message); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + Assert.Fail("WaitForCompletionAsync should have thrown an exception."); |
| 185 | + } |
| 186 | + |
| 187 | + [Test] |
| 188 | + [TestCase(null)] |
| 189 | + [TestCase("en-US")] |
| 190 | + [AsyncOnly] |
| 191 | + public async Task CreateReservation(string? locale) |
| 192 | + { |
| 193 | + var client = CreateClient(); |
| 194 | + var countryCode = "US"; |
| 195 | + |
| 196 | + var pageablePhonePlanGroups = client.GetPhonePlanGroupsAsync(countryCode, locale); |
| 197 | + var phonePlanGroups = await pageablePhonePlanGroups.ToEnumerableAsync().ConfigureAwait(false); |
| 198 | + |
| 199 | + string phonePlanGroupId = phonePlanGroups.First(group => group.PhoneNumberType == PhoneNumberType.TollFree).PhonePlanGroupId; |
| 200 | + var pageablePhonePlans = client.GetPhonePlansAsync(countryCode, phonePlanGroupId, locale); |
| 201 | + var phonePlan = (await pageablePhonePlans.ToEnumerableAsync()).First(); |
| 202 | + var areaCode = phonePlan.AreaCodes.First(); |
| 203 | + |
| 204 | + var reservationOptions = new CreateReservationOptions("My reservation", "my description", new[] { phonePlan.PhonePlanId }, areaCode); |
| 205 | + reservationOptions.Quantity = 1; |
| 206 | + var reservationOperation = await client.StartReservationAsync(reservationOptions); |
| 207 | + |
| 208 | + await reservationOperation.WaitForCompletionAsync().ConfigureAwait(false); |
| 209 | + |
| 210 | + Assert.IsNotNull(reservationOperation); |
| 211 | + Assert.IsTrue(reservationOperation.HasCompleted); |
| 212 | + Assert.IsTrue(reservationOperation.HasValue); |
| 213 | + |
| 214 | + var reservation = reservationOperation.Value; |
| 215 | + Assert.IsNotNull(reservation); |
| 216 | + |
| 217 | + Assert.AreEqual(ReservationStatus.Reserved, reservation.Status); |
| 218 | + Assert.AreEqual(areaCode, reservation.AreaCode); |
| 219 | + Assert.IsNull(reservation.ErrorCode); |
| 220 | + Assert.AreEqual(1, reservation.PhoneNumbers?.Count); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments