Skip to content

Commit 490ccd2

Browse files
committed
refactor: use TheoryData<> to simplify testing across multiple regions for Voice
1 parent 8883e88 commit 490ccd2

File tree

1 file changed

+127
-100
lines changed

1 file changed

+127
-100
lines changed

Vonage.Test/Voice/VoiceTests.cs

Lines changed: 127 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace Vonage.Test.Voice;
1717
[Trait("Category", "Legacy")]
1818
public class VoiceTests : TestBase
1919
{
20-
private const string BaseUri = "https://api.nexmo.com/v1/calls";
20+
private const string BaseUri = "https://api.nexmo.com";
2121
private const string ApacUri = "https://api-ap.vonage.com";
2222
private const string EuUri = "https://api-eu.vonage.com";
2323
private const string UsUri = "https://api-us.vonage.com";
@@ -34,6 +34,14 @@ public VoiceTests()
3434
this.client = this.BuildVonageClient(this.BuildCredentialsForBearerAuthentication());
3535
}
3636

37+
public static TheoryData<VoiceTestsSetup> GetSetups =>
38+
[
39+
new VoiceTestsSetup(null, string.Concat(BaseUri, Endpoint)),
40+
new VoiceTestsSetup(VonageUrls.Region.APAC, string.Concat(ApacUri, Endpoint)),
41+
new VoiceTestsSetup(VonageUrls.Region.EU, string.Concat(EuUri, Endpoint)),
42+
new VoiceTestsSetup(VonageUrls.Region.US, string.Concat(UsUri, Endpoint)),
43+
];
44+
3745
[Fact]
3846
public void AdvancedMachineDetectionPropertiesWithInvalidBeepTimeout()
3947
{
@@ -49,33 +57,25 @@ public void AdvancedMachineDetectionPropertiesWithValidBeepTimeout()
4957
properties.ShouldHaveValidAdvancedMachineDetectionProperties(45);
5058
}
5159

52-
[Fact]
53-
public async Task CreateCall()
54-
{
55-
this.Setup(BaseUri, this.helper.GetResponseJson(), this.helper.GetRequestJson());
56-
var response = await this.client.VoiceClient.CreateCallAsync(VoiceTestData.CreateCallCommand());
57-
response.ShouldMatchExpectedCallResponse();
58-
}
59-
6060
[Theory]
61-
[InlineData(VonageUrls.Region.APAC, ApacUri)]
62-
[InlineData(VonageUrls.Region.EU, EuUri)]
63-
[InlineData(VonageUrls.Region.US, UsUri)]
64-
public async Task CreateCallWithRegion(VonageUrls.Region region, string expectedUri)
61+
[MemberData(nameof(GetSetups))]
62+
public async Task CreateCall(VoiceTestsSetup setup)
6563
{
66-
this.Setup(expectedUri + Endpoint, this.helper.GetResponseJson(nameof(this.CreateCall)),
67-
this.helper.GetRequestJson(nameof(this.CreateCall)));
68-
var response = await this.client.VoiceClient.WithRegion(region)
69-
.CreateCallAsync(VoiceTestData.CreateCallCommand());
64+
this.Setup(setup.BaseUri, this.helper.GetResponseJson(), this.helper.GetRequestJson());
65+
var response = await this.SetupClient(setup.Region).CreateCallAsync(VoiceTestData.CreateCallCommand());
7066
response.ShouldMatchExpectedCallResponse();
7167
}
7268

73-
[Fact]
74-
public async Task CreateCallWithCredentials()
69+
private IVoiceClient SetupClient(VonageUrls.Region? region) =>
70+
region.HasValue ? this.client.VoiceClient.WithRegion(region.Value) : this.client.VoiceClient;
71+
72+
[Theory]
73+
[MemberData(nameof(GetSetups))]
74+
public async Task CreateCallWithCredentials(VoiceTestsSetup setup)
7575
{
76-
this.Setup(BaseUri, this.helper.GetResponseJson(nameof(this.CreateCall)),
76+
this.Setup(setup.BaseUri, this.helper.GetResponseJson(nameof(this.CreateCall)),
7777
this.helper.GetRequestJson(nameof(this.CreateCall)));
78-
var response = await this.client.VoiceClient.CreateCallAsync(VoiceTestData.CreateCallCommand(),
78+
var response = await this.SetupClient(setup.Region).CreateCallAsync(VoiceTestData.CreateCallCommand(),
7979
this.BuildCredentialsForBearerAuthentication());
8080
response.ShouldMatchExpectedCallResponse();
8181
}
@@ -89,21 +89,23 @@ await act.Should().ThrowExactlyAsync<VonageAuthenticationException>()
8989
.WithMessage("AppId or Private Key Path missing.");
9090
}
9191

92-
[Fact]
93-
public async Task GetRecordings()
92+
[Theory]
93+
[MemberData(nameof(GetSetups))]
94+
public async Task GetRecordings(VoiceTestsSetup setup)
9495
{
9596
var expectedResponse = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
9697
this.Setup(VoiceTestData.GetValidRecordingUri(), expectedResponse);
97-
var response = await this.client.VoiceClient.GetRecordingAsync(VoiceTestData.GetValidRecordingUri());
98+
var response = await this.SetupClient(setup.Region).GetRecordingAsync(VoiceTestData.GetValidRecordingUri());
9899
response.ShouldMatchExpectedRecording(expectedResponse);
99100
}
100101

101-
[Fact]
102-
public async Task GetRecordingsWithCredentials()
102+
[Theory]
103+
[MemberData(nameof(GetSetups))]
104+
public async Task GetRecordingsWithCredentials(VoiceTestsSetup setup)
103105
{
104106
var expectedResponse = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
105107
this.Setup(VoiceTestData.GetValidRecordingUri(), expectedResponse);
106-
var response = await this.client.VoiceClient.GetRecordingAsync(VoiceTestData.GetValidRecordingUri(),
108+
var response = await this.SetupClient(setup.Region).GetRecordingAsync(VoiceTestData.GetValidRecordingUri(),
107109
this.BuildCredentialsForBearerAuthentication());
108110
response.ShouldMatchExpectedRecording(expectedResponse);
109111
}
@@ -122,175 +124,200 @@ public async Task GetRecordingsWithInvalidUri()
122124
await act.Should().ThrowAsync<VonageException>().WithMessage("Invalid uri");
123125
}
124126

125-
[Fact]
126-
public async Task GetSpecificCall()
127+
[Theory]
128+
[MemberData(nameof(GetSetups))]
129+
public async Task GetSpecificCall(VoiceTestsSetup setup)
127130
{
128131
var uuid = this.fixture.Create<Guid>().ToString();
129-
this.Setup($"{BaseUri}/{uuid}", this.helper.GetResponseJson());
130-
var callRecord = await this.client.VoiceClient.GetCallAsync(uuid);
132+
this.Setup($"{setup.BaseUri}/{uuid}", this.helper.GetResponseJson());
133+
var callRecord = await this.SetupClient(setup.Region).GetCallAsync(uuid);
131134
callRecord.ShouldMatchExpectedCallRecord();
132135
}
133136

134-
[Fact]
135-
public async Task GetSpecificCallWithCredentials()
137+
[Theory]
138+
[MemberData(nameof(GetSetups))]
139+
public async Task GetSpecificCallWithCredentials(VoiceTestsSetup setup)
136140
{
137141
var uuid = this.fixture.Create<Guid>().ToString();
138-
this.Setup($"{BaseUri}/{uuid}", this.helper.GetResponseJson(nameof(this.GetSpecificCall)));
142+
this.Setup($"{setup.BaseUri}/{uuid}", this.helper.GetResponseJson(nameof(this.GetSpecificCall)));
139143
var callRecord =
140-
await this.client.VoiceClient.GetCallAsync(uuid, this.BuildCredentialsForBearerAuthentication());
144+
await this.SetupClient(setup.Region).GetCallAsync(uuid, this.BuildCredentialsForBearerAuthentication());
141145
callRecord.ShouldMatchExpectedCallRecord();
142146
}
143147

144-
[Fact]
145-
public async Task ListCalls()
148+
[Theory]
149+
[MemberData(nameof(GetSetups))]
150+
public async Task ListCalls(VoiceTestsSetup setup)
146151
{
147-
this.Setup($"{this.ApiUrl}/v1/calls", this.helper.GetResponseJson());
148-
var callList = await this.client.VoiceClient.GetCallsAsync(VoiceTestData.CreateBasicCallSearchFilter());
152+
this.Setup($"{setup.BaseUri}", this.helper.GetResponseJson());
153+
var callList = await this.SetupClient(setup.Region).GetCallsAsync(VoiceTestData.CreateBasicCallSearchFilter());
149154
callList.ShouldMatchExpectedCallsList();
150155
}
151156

152-
[Fact]
153-
public async Task ListCallsWithCredentials()
157+
[Theory]
158+
[MemberData(nameof(GetSetups))]
159+
public async Task ListCallsWithCredentials(VoiceTestsSetup setup)
154160
{
155161
this.Setup(
156-
$"{BaseUri}?status=started&date_start={WebUtility.UrlEncode("2016-11-14T07:45:14Z").ToUpper()}&date_end={WebUtility.UrlEncode("2016-11-14T07:45:14Z").ToUpper()}&page_size=10&record_index=0&order=asc&conversation_uuid=CON-f972836a-550f-45fa-956c-12a2ab5b7d22&",
162+
$"{setup.BaseUri}?status=started&date_start={WebUtility.UrlEncode("2016-11-14T07:45:14Z").ToUpper()}&date_end={WebUtility.UrlEncode("2016-11-14T07:45:14Z").ToUpper()}&page_size=10&record_index=0&order=asc&conversation_uuid=CON-f972836a-550f-45fa-956c-12a2ab5b7d22&",
157163
this.helper.GetResponseJson(nameof(this.ListCalls)));
158-
var callList = await this.client.VoiceClient.GetCallsAsync(VoiceTestData.CreateComplexCallSearchFilter(),
164+
var callList = await this.SetupClient(setup.Region).GetCallsAsync(VoiceTestData.CreateComplexCallSearchFilter(),
159165
this.BuildCredentialsForBearerAuthentication());
160166
callList.ShouldMatchExpectedCallsList();
161167
}
162168

163-
[Fact]
164-
public async Task StartDtmf()
169+
[Theory]
170+
[MemberData(nameof(GetSetups))]
171+
public async Task StartDtmf(VoiceTestsSetup setup)
165172
{
166173
var uuid = this.fixture.Create<string>();
167-
this.Setup($"{BaseUri}/{uuid}/dtmf", this.helper.GetResponseJson(), this.helper.GetRequestJson());
168-
var response = await this.client.VoiceClient.StartDtmfAsync(uuid, VoiceTestData.CreateDtmfCommand());
174+
this.Setup($"{setup.BaseUri}/{uuid}/dtmf", this.helper.GetResponseJson(),
175+
this.helper.GetRequestJson());
176+
var response = await this.SetupClient(setup.Region).StartDtmfAsync(uuid, VoiceTestData.CreateDtmfCommand());
169177
response.ShouldMatchExpectedDtmfResponse();
170178
}
171179

172-
[Fact]
173-
public async Task StartDtmfWithCredentials()
180+
[Theory]
181+
[MemberData(nameof(GetSetups))]
182+
public async Task StartDtmfWithCredentials(VoiceTestsSetup setup)
174183
{
175184
var uuid = this.fixture.Create<string>();
176-
this.Setup($"{BaseUri}/{uuid}/dtmf", this.helper.GetResponseJson(nameof(this.StartDtmf)),
185+
this.Setup($"{setup.BaseUri}/{uuid}/dtmf", this.helper.GetResponseJson(nameof(this.StartDtmf)),
177186
this.helper.GetRequestJson(nameof(this.StartDtmf)));
178-
var response = await this.client.VoiceClient.StartDtmfAsync(uuid, VoiceTestData.CreateDtmfCommand(),
187+
var response = await this.SetupClient(setup.Region).StartDtmfAsync(uuid, VoiceTestData.CreateDtmfCommand(),
179188
this.BuildCredentialsForBearerAuthentication());
180189
response.ShouldMatchExpectedDtmfResponse();
181190
}
182191

183-
[Fact]
184-
public async Task StartStream()
192+
[Theory]
193+
[MemberData(nameof(GetSetups))]
194+
public async Task StartStream(VoiceTestsSetup setup)
185195
{
186196
var uuid = this.fixture.Create<string>();
187-
this.Setup($"{BaseUri}/{uuid}/stream", this.helper.GetResponseJson(), this.helper.GetRequestJson());
188-
var response = await this.client.VoiceClient.StartStreamAsync(uuid, VoiceTestData.CreateStreamCommand());
197+
this.Setup($"{setup.BaseUri}/{uuid}/stream", this.helper.GetResponseJson(),
198+
this.helper.GetRequestJson());
199+
var response = await this.SetupClient(setup.Region).StartStreamAsync(uuid, VoiceTestData.CreateStreamCommand());
189200
response.ShouldMatchExpectedStreamResponse();
190201
}
191202

192-
[Fact]
193-
public async Task StartStreamWithCredentials()
203+
[Theory]
204+
[MemberData(nameof(GetSetups))]
205+
public async Task StartStreamWithCredentials(VoiceTestsSetup setup)
194206
{
195207
var uuid = this.fixture.Create<string>();
196-
this.Setup($"{BaseUri}/{uuid}/stream", this.helper.GetResponseJson(nameof(this.StartStream)),
208+
this.Setup($"{setup.BaseUri}/{uuid}/stream", this.helper.GetResponseJson(nameof(this.StartStream)),
197209
this.helper.GetRequestJson(nameof(this.StartStream)));
198-
var response = await this.client.VoiceClient.StartStreamAsync(uuid, VoiceTestData.CreateStreamCommand(),
210+
var response = await this.SetupClient(setup.Region).StartStreamAsync(uuid, VoiceTestData.CreateStreamCommand(),
199211
this.BuildCredentialsForBearerAuthentication());
200212
response.ShouldMatchExpectedStreamResponse();
201213
}
202214

203-
[Fact]
204-
public async Task StartTalk()
215+
[Theory]
216+
[MemberData(nameof(GetSetups))]
217+
public async Task StartTalk(VoiceTestsSetup setup)
205218
{
206219
var uuid = this.fixture.Create<string>();
207-
this.Setup($"{BaseUri}/{uuid}/talk", this.helper.GetResponseJson(), this.helper.GetRequestJson());
208-
var response = await this.client.VoiceClient.StartTalkAsync(uuid, VoiceTestData.CreateTalkCommand());
220+
this.Setup($"{setup.BaseUri}/{uuid}/talk", this.helper.GetResponseJson(),
221+
this.helper.GetRequestJson());
222+
var response = await this.SetupClient(setup.Region).StartTalkAsync(uuid, VoiceTestData.CreateTalkCommand());
209223
response.ShouldMatchExpectedTalkResponse();
210224
}
211225

212-
[Fact]
213-
public async Task StartTalkWithCredentials()
226+
[Theory]
227+
[MemberData(nameof(GetSetups))]
228+
public async Task StartTalkWithCredentials(VoiceTestsSetup setup)
214229
{
215230
var uuid = this.fixture.Create<string>();
216-
this.Setup($"{BaseUri}/{uuid}/talk", this.helper.GetResponseJson(nameof(this.StartTalk)),
231+
this.Setup($"{setup.BaseUri}/{uuid}/talk", this.helper.GetResponseJson(nameof(this.StartTalk)),
217232
this.helper.GetRequestJson(nameof(this.StartTalk)));
218-
var response = await this.client.VoiceClient.StartTalkAsync(uuid, VoiceTestData.CreateTalkCommand(),
233+
var response = await this.SetupClient(setup.Region).StartTalkAsync(uuid, VoiceTestData.CreateTalkCommand(),
219234
this.BuildCredentialsForBearerAuthentication());
220235
response.ShouldMatchExpectedTalkResponse();
221236
}
222237

223-
[Fact]
224-
public async Task StopStream()
238+
[Theory]
239+
[MemberData(nameof(GetSetups))]
240+
public async Task StopStream(VoiceTestsSetup setup)
225241
{
226242
var uuid = this.fixture.Create<string>();
227-
this.Setup($"{BaseUri}/{uuid}/stream", this.helper.GetResponseJson());
228-
var response = await this.client.VoiceClient.StopStreamAsync(uuid);
243+
this.Setup($"{setup.BaseUri}/{uuid}/stream", this.helper.GetResponseJson());
244+
var response = await this.SetupClient(setup.Region).StopStreamAsync(uuid);
229245
response.ShouldMatchExpectedStopStreamResponse();
230246
}
231247

232-
[Fact]
233-
public async Task StopStreamWithCredentials()
248+
[Theory]
249+
[MemberData(nameof(GetSetups))]
250+
public async Task StopStreamWithCredentials(VoiceTestsSetup setup)
234251
{
235252
var uuid = this.fixture.Create<string>();
236-
this.Setup($"{BaseUri}/{uuid}/stream", this.helper.GetResponseJson(nameof(this.StopStream)));
253+
this.Setup($"{setup.BaseUri}/{uuid}/stream", this.helper.GetResponseJson(nameof(this.StopStream)));
237254
var response =
238-
await this.client.VoiceClient.StopStreamAsync(uuid, this.BuildCredentialsForBearerAuthentication());
255+
await this.SetupClient(setup.Region).StopStreamAsync(uuid, this.BuildCredentialsForBearerAuthentication());
239256
response.ShouldMatchExpectedStopStreamResponse();
240257
}
241258

242-
[Fact]
243-
public async Task StopTalk()
259+
[Theory]
260+
[MemberData(nameof(GetSetups))]
261+
public async Task StopTalk(VoiceTestsSetup setup)
244262
{
245263
var uuid = this.fixture.Create<string>();
246-
this.Setup($"{BaseUri}/{uuid}/stream", this.helper.GetResponseJson());
247-
var response = await this.client.VoiceClient.StopStreamAsync(uuid);
264+
this.Setup($"{setup.BaseUri}/{uuid}/stream", this.helper.GetResponseJson());
265+
var response = await this.SetupClient(setup.Region).StopStreamAsync(uuid);
248266
response.ShouldMatchExpectedStopTalkResponse();
249267
}
250268

251-
[Fact]
252-
public async Task StopTalkWithCredentials()
269+
[Theory]
270+
[MemberData(nameof(GetSetups))]
271+
public async Task StopTalkWithCredentials(VoiceTestsSetup setup)
253272
{
254273
var uuid = this.fixture.Create<string>();
255-
this.Setup($"{BaseUri}/{uuid}/stream", this.helper.GetResponseJson(nameof(this.StopTalk)));
274+
this.Setup($"{setup.BaseUri}/{uuid}/stream", this.helper.GetResponseJson(nameof(this.StopTalk)));
256275
var response =
257-
await this.client.VoiceClient.StopStreamAsync(uuid, this.BuildCredentialsForBearerAuthentication());
276+
await this.SetupClient(setup.Region).StopStreamAsync(uuid, this.BuildCredentialsForBearerAuthentication());
258277
response.ShouldMatchExpectedStopTalkResponse();
259278
}
260279

261-
[Fact]
262-
public async Task SubscribeRealTimeDtmf()
280+
[Theory]
281+
[MemberData(nameof(GetSetups))]
282+
public async Task SubscribeRealTimeDtmf(VoiceTestsSetup setup)
263283
{
264-
this.Setup($"{BaseUri}/ID-123/input/dtmf", Maybe<string>.None, this.helper.GetRequestJson());
265-
await this.client.VoiceClient.SubscribeRealTimeDtmf("ID-123", VoiceTestData.GetRealTimeDtmfUri());
284+
this.Setup($"{setup.BaseUri}/ID-123/input/dtmf", Maybe<string>.None, this.helper.GetRequestJson());
285+
await this.SetupClient(setup.Region).SubscribeRealTimeDtmf("ID-123", VoiceTestData.GetRealTimeDtmfUri());
266286
}
267287

268-
[Fact]
269-
public async Task UnsubscribeRealTimeDtmf()
288+
[Theory]
289+
[MemberData(nameof(GetSetups))]
290+
public async Task UnsubscribeRealTimeDtmf(VoiceTestsSetup setup)
270291
{
271-
this.Setup($"{BaseUri}/ID-123/input/dtmf", Maybe<string>.None, string.Empty);
272-
await this.client.VoiceClient.UnsubscribeRealTimeDtmf("ID-123");
292+
this.Setup($"{setup.BaseUri}/ID-123/input/dtmf", Maybe<string>.None, string.Empty);
293+
await this.SetupClient(setup.Region).UnsubscribeRealTimeDtmf("ID-123");
273294
}
274295

275-
[Fact]
276-
public async Task UpdateCall()
296+
[Theory]
297+
[MemberData(nameof(GetSetups))]
298+
public async Task UpdateCall(VoiceTestsSetup setup)
277299
{
278300
var uuid = this.fixture.Create<Guid>().ToString();
279-
this.Setup($"{BaseUri}/{uuid}", Maybe<string>.None, this.helper.GetRequestJson());
280-
var response = await this.client.VoiceClient.UpdateCallAsync(uuid, VoiceTestData.CreateCallEditCommand());
301+
this.Setup($"{setup.BaseUri}/{uuid}", Maybe<string>.None, this.helper.GetRequestJson());
302+
var response = await this.SetupClient(setup.Region)
303+
.UpdateCallAsync(uuid, VoiceTestData.CreateCallEditCommand());
281304
response.ShouldBeTrue();
282305
}
283306

284-
[Fact]
285-
public async Task UpdateCallWithCredentials()
307+
[Theory]
308+
[MemberData(nameof(GetSetups))]
309+
public async Task UpdateCallWithCredentials(VoiceTestsSetup setup)
286310
{
287311
var uuid = this.fixture.Create<Guid>().ToString();
288-
this.Setup($"{BaseUri}/{uuid}", Maybe<string>.None, this.helper.GetRequestJson(nameof(this.UpdateCall)));
289-
var response = await this.client.VoiceClient.UpdateCallAsync(uuid, VoiceTestData.CreateCallEditCommand(),
312+
this.Setup($"{setup.BaseUri}/{uuid}", Maybe<string>.None,
313+
this.helper.GetRequestJson(nameof(this.UpdateCall)));
314+
var response = await this.SetupClient(setup.Region).UpdateCallAsync(uuid, VoiceTestData.CreateCallEditCommand(),
290315
this.BuildCredentialsForBearerAuthentication());
291316
response.ShouldBeTrue();
292317
}
293318

294319
private VonageClient BuildClientWithBasicAuthentication() =>
295320
new VonageClient(this.BuildCredentialsForBasicAuthentication());
296-
}
321+
}
322+
323+
public record VoiceTestsSetup(VonageUrls.Region? Region, string BaseUri);

0 commit comments

Comments
 (0)