Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,30 @@ Mono<Response<CreateCallResult>> createCallWithResponse(CommunicationIdentifier
}
}

/**
* Play audio in a call.
*
* @param callId The call id.
* @param audioFileUri The media resource uri of the play audio request.
* @param loop The flag indicating whether audio file needs to be played in loop or not.
* @param audioFileId An id for the media in the AudioFileUri, using which we cache the media.
* @param operationContext The value to identify context of the operation.
* @return the response payload for play audio operation.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<PlayAudioResult> playAudio(String callId, String audioFileUri, boolean loop, String audioFileId, String operationContext) {
try {
Objects.requireNonNull(callId, "'callId' cannot be null.");
Objects.requireNonNull(audioFileUri, "'audioFileUri' cannot be null.");

PlayAudioRequest playAudioRequest = new PlayAudioRequest().
setAudioFileUri(audioFileUri).setLoop(loop).setAudioFileId(audioFileId).setOperationContext(operationContext);
return playAudio(callId, playAudioRequest);
} catch (RuntimeException ex) {
return monoError(logger, ex);
}
}

/**
* Play audio in a call.
*
Expand All @@ -142,6 +166,24 @@ public Mono<PlayAudioResult> playAudio(String callId, PlayAudioRequest request)
}
}

/**
* Play audio in a call.
*
* @param callId The call id.
* @param audioFileUri The media resource uri of the play audio request.
* @param loop The flag indicating whether audio file needs to be played in loop or not.
* @param audioFileId An id for the media in the AudioFileUri, using which we cache the media.
* @param operationContext The value to identify context of the operation.
* @param context A {@link Context} representing the request context.
* @return the response payload for play audio operation.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<PlayAudioResult>> playAudioWithResponse(String callId, String audioFileUri, boolean loop, String audioFileId, String operationContext, Context context) {
PlayAudioRequest playAudioRequest = new PlayAudioRequest().
setAudioFileUri(audioFileUri).setLoop(loop).setAudioFileId(audioFileId).setOperationContext(operationContext);
return playAudioWithResponse(callId, playAudioRequest, context);
}

/**
* Play audio in a call.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.azure.communication.callingserver;

import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.UUID;

import com.azure.communication.callingserver.models.CallModality;
import com.azure.communication.callingserver.models.CreateCallOptions;
import com.azure.communication.callingserver.models.CreateCallResult;
import com.azure.communication.callingserver.models.EventSubscriptionType;
import com.azure.communication.callingserver.models.PlayAudioResult;
import com.azure.communication.common.CommunicationIdentifier;
import com.azure.communication.common.CommunicationUserIdentifier;
import com.azure.communication.common.PhoneNumberIdentifier;
import com.azure.core.http.HttpClient;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Set the AZURE_TEST_MODE environment variable to either PLAYBACK or RECORD to determine if tests are playback or
* live. By default, tests are run in playback mode.
*/
public class CallAsyncClientTests extends CallingServerTestBase {
private String from = "8:acs:016a7064-0581-40b9-be73-6dde64d69d72_0000000a-6198-4a66-02c3-593a0d00560d";
private String alternateId = "+18445764430";
private String to = "+15125189815";
private String callBackUri = "https://host.app/api/callback/calling";
private String audioFileUri = "https://acstestapp1.azurewebsites.net/audio/bot-callcenter-intro.wav";

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void runCreatePlayAudioHangupScenarioAsync(HttpClient httpClient) throws URISyntaxException, InterruptedException {
var builder = getCallClientUsingConnectionString(httpClient);
var callAsyncClient = setupAsyncClient(builder, "runCreatePlayAudioHangupScenarioAsync");

try {
// Establish a call
CreateCallOptions options = new CreateCallOptions(
callBackUri,
new LinkedList<CallModality>(Arrays.asList(CallModality.AUDIO)),
new LinkedList<EventSubscriptionType>(Arrays.asList(EventSubscriptionType.PARTICIPANTS_UPDATED)));

options.setAlternateCallerId(new PhoneNumberIdentifier(alternateId));

CreateCallResult createCallResult = callAsyncClient.createCall(
new CommunicationUserIdentifier(from),
new LinkedList<CommunicationIdentifier>(Arrays.asList(new PhoneNumberIdentifier(to))),
options).block();

CallingServerTestUtils.ValidateCreateCallResult(createCallResult);
var callId = createCallResult.getCallLegId();

// Play Audio
var operationContext = "ac794123-3820-4979-8e2d-50c7d3e07b12";
PlayAudioResult playAudioResult = callAsyncClient.playAudio(
callId,
audioFileUri,
false,
UUID.randomUUID().toString(),
operationContext).block();
CallingServerTestUtils.ValidatePlayAudioResult(playAudioResult, operationContext);

// Hang up
callAsyncClient.hangupCall(callId).block();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
throw e;
}
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void runCreatePlayAudioHangupScenarioWithResponseAsync(HttpClient httpClient) throws URISyntaxException, InterruptedException {
var builder = getCallClientUsingConnectionString(httpClient);
var callAsyncClient = setupAsyncClient(builder, "runCreatePlayAudioHangupScenarioWithResponseAsync");

try {
// Establish a call
CreateCallOptions options = new CreateCallOptions(
callBackUri,
new LinkedList<CallModality>(Arrays.asList(CallModality.AUDIO)),
new LinkedList<EventSubscriptionType>(Arrays.asList(EventSubscriptionType.PARTICIPANTS_UPDATED)));

options.setAlternateCallerId(new PhoneNumberIdentifier(alternateId));

Response<CreateCallResult> createCallResponse = callAsyncClient.createCallWithResponse(
new CommunicationUserIdentifier(from),
new LinkedList<CommunicationIdentifier>(Arrays.asList(new PhoneNumberIdentifier(to))),
options,
Context.NONE).block();

CallingServerTestUtils.ValidateCreateCallResponse(createCallResponse);
var callId = createCallResponse.getValue().getCallLegId();

// Play Audio
var operationContext = "ac794123-3820-4979-8e2d-50c7d3e07b12";
Response<PlayAudioResult> playAudioResponse = callAsyncClient.playAudioWithResponse(
callId,
audioFileUri,
false,
UUID.randomUUID().toString(),
operationContext,
Context.NONE).block();
CallingServerTestUtils.ValidatePlayAudioResponse(playAudioResponse, operationContext);

// Hang up
Response<Void> hangupResponse = callAsyncClient.hangupCallWithResponse(callId, Context.NONE).block();
CallingServerTestUtils.ValidateHangupResponse(hangupResponse);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
throw e;
}
}

private CallAsyncClient setupAsyncClient(CallClientBuilder builder, String testName) {
return addLoggingPolicy(builder, testName).buildAsyncClient();
}

protected CallClientBuilder addLoggingPolicy(CallClientBuilder builder, String testName) {
return builder.addPolicy((context, next) -> logHeaders(testName, next));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.azure.communication.callingserver;

import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.UUID;

import com.azure.communication.callingserver.models.CallModality;
import com.azure.communication.callingserver.models.CreateCallOptions;
import com.azure.communication.callingserver.models.CreateCallResult;
import com.azure.communication.callingserver.models.EventSubscriptionType;
import com.azure.communication.callingserver.models.PlayAudioResult;
import com.azure.communication.common.CommunicationIdentifier;
import com.azure.communication.common.CommunicationUserIdentifier;
import com.azure.communication.common.PhoneNumberIdentifier;
import com.azure.core.http.HttpClient;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* Set the AZURE_TEST_MODE environment variable to either PLAYBACK or RECORD to setup if tests are playback or
* live. By default, tests are run in playback mode.
*/
public class CallClientTests extends CallingServerTestBase {
private String from = "8:acs:016a7064-0581-40b9-be73-6dde64d69d72_0000000a-6198-4a66-02c3-593a0d00560d";
private String alternateId = "+18445764430";
private String to = "+15125189815";
private String callBackUri = "https://host.app/api/callback/calling";
private String audioFileUri = "https://acstestapp1.azurewebsites.net/audio/bot-callcenter-intro.wav";

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void runCreatePlayAudioHangupScenario(HttpClient httpClient) throws URISyntaxException, InterruptedException {
var builder = getCallClientUsingConnectionString(httpClient);
var callClient = setupClient(builder, "runCreatePlayAudioHangupScenario");

try {
// Establish a call
CreateCallOptions options = new CreateCallOptions(
callBackUri,
new LinkedList<CallModality>(Arrays.asList(CallModality.AUDIO)),
new LinkedList<EventSubscriptionType>(Arrays.asList(EventSubscriptionType.PARTICIPANTS_UPDATED)));

options.setAlternateCallerId(new PhoneNumberIdentifier(alternateId));

CreateCallResult createCallResult = callClient.createCall(
new CommunicationUserIdentifier(from),
new LinkedList<CommunicationIdentifier>(Arrays.asList(new PhoneNumberIdentifier(to))),
options);

CallingServerTestUtils.ValidateCreateCallResult(createCallResult);
var callId = createCallResult.getCallLegId();

// Play Audio
var operationContext = "ac794123-3820-4979-8e2d-50c7d3e07b12";
PlayAudioResult playAudioResult = callClient.playAudio(
callId,
audioFileUri,
false,
UUID.randomUUID().toString(),
operationContext);
CallingServerTestUtils.ValidatePlayAudioResult(playAudioResult, operationContext);

// Hang up
callClient.hangupCall(callId);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
throw e;
}
}

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void runCreatePlayAudioHangupScenarioWithResponse(HttpClient httpClient) throws URISyntaxException, InterruptedException {
var builder = getCallClientUsingConnectionString(httpClient);
var callClient = setupClient(builder, "runCreatePlayAudioHangupScenarioWithResponse");

try {
// Establish a call
CreateCallOptions options = new CreateCallOptions(
callBackUri,
new LinkedList<CallModality>(Arrays.asList(CallModality.AUDIO)),
new LinkedList<EventSubscriptionType>(Arrays.asList(EventSubscriptionType.PARTICIPANTS_UPDATED)));

options.setAlternateCallerId(new PhoneNumberIdentifier(alternateId));

Response<CreateCallResult> createCallResponse = callClient.createCallWithResponse(
new CommunicationUserIdentifier(from),
new LinkedList<CommunicationIdentifier>(Arrays.asList(new PhoneNumberIdentifier(to))),
options,
Context.NONE);

CallingServerTestUtils.ValidateCreateCallResponse(createCallResponse);
var callId = createCallResponse.getValue().getCallLegId();

// Play Audio
var operationContext = "ac794123-3820-4979-8e2d-50c7d3e07b12";
Response<PlayAudioResult> playAudioResponse = callClient.playAudioWithResponse(
callId,
audioFileUri,
false,
UUID.randomUUID().toString(),
operationContext,
Context.NONE);
CallingServerTestUtils.ValidatePlayAudioResponse(playAudioResponse, operationContext);

// Hang up
Response<Void> hangupResponse = callClient.hangupCallWithResponse(callId, Context.NONE);
CallingServerTestUtils.ValidateHangupResponse(hangupResponse);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
throw e;
}
}

private CallClient setupClient(CallClientBuilder builder, String testName) {
return addLoggingPolicy(builder, testName).buildClient();
}

protected CallClientBuilder addLoggingPolicy(CallClientBuilder builder, String testName) {
return builder.addPolicy((context, next) -> logHeaders(testName, next));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ protected ConversationClientBuilder getConversationClientWithToken(HttpClient ht
}

protected CallClientBuilder getCallClientUsingConnectionString(HttpClient httpClient) {
return null;
CallClientBuilder builder = new CallClientBuilder()
.connectionString(CONNECTION_STRING)
.httpClient(httpClient == null ? interceptorManager.getPlaybackClient() : httpClient);

if (getTestMode() == TestMode.RECORD) {
List<Function<String, String>> redactors = new ArrayList<>();
redactors.add(data -> redact(data, JSON_PROPERTY_VALUE_REDACTION_PATTERN.matcher(data), "REDACTED"));
builder.addPolicy(interceptorManager.getRecordPolicy(redactors));
}
return builder;
}

protected ConversationClientBuilder getConversationClientUsingConnectionString(HttpClient httpClient) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.azure.communication.callingserver;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.azure.communication.callingserver.models.CreateCallResult;
import com.azure.communication.callingserver.models.OperationStatus;
import com.azure.communication.callingserver.models.PlayAudioResult;

import com.azure.core.http.rest.Response;

public class CallingServerTestUtils {
protected static void ValidateCreateCallResponse(Response<CreateCallResult> createCallResponse)
{
assertNotNull(createCallResponse);
assertTrue(createCallResponse.getStatusCode() == 201);
assertNotNull(createCallResponse.getValue());
ValidateCreateCallResult(createCallResponse.getValue());
}

protected static void ValidateCreateCallResult(CreateCallResult createCallResult)
{
assertNotNull(createCallResult);
assertNotNull(createCallResult.getCallLegId());
assertTrue(!createCallResult.getCallLegId().isEmpty());
}

protected static void ValidatePlayAudioResponse(Response<PlayAudioResult> playAudioResponse, String operationContext)
{
assertNotNull(playAudioResponse);
assertTrue(playAudioResponse.getStatusCode() == 202);
assertNotNull(playAudioResponse.getValue());
ValidatePlayAudioResult(playAudioResponse.getValue(), operationContext);
}

protected static void ValidatePlayAudioResult(PlayAudioResult playAudioResult, String operationContext)
{
assertNotNull(playAudioResult);
assertNotNull(playAudioResult.getId());
assertTrue(!playAudioResult.getId().isEmpty());
assertNotNull(playAudioResult.getOperationContext());
assertTrue(playAudioResult.getOperationContext().equalsIgnoreCase(operationContext));
assertNotNull(playAudioResult.getStatus());
assertTrue(playAudioResult.getStatus() == OperationStatus.RUNNING);
}

protected static void ValidateHangupResponse(Response<Void> hangupResponse)
{
assertNotNull(hangupResponse);
assertTrue(hangupResponse.getStatusCode() == 202);
}
}
Loading