Skip to content

Commit

Permalink
Richardcho/cancel live test (Azure#36980)
Browse files Browse the repository at this point in the history
* add event handler for remove participant events

* add live test

* update test

---------

Co-authored-by: root <root@DESKTOP-6GBNLER>
  • Loading branch information
richardcho-msft and root authored Oct 3, 2023
1 parent 5987be9 commit d9f2071
Showing 1 changed file with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import com.azure.communication.callautomation.models.AnswerCallResult;
import com.azure.communication.callautomation.models.CallInvite;
import com.azure.communication.callautomation.models.CallParticipant;
import com.azure.communication.callautomation.models.CancelAddParticipantResult;
import com.azure.communication.callautomation.models.CreateCallResult;
import com.azure.communication.callautomation.models.CreateGroupCallOptions;
import com.azure.communication.callautomation.models.RemoveParticipantResult;
import com.azure.communication.callautomation.models.events.AddParticipantSucceeded;
import com.azure.communication.callautomation.models.events.CallConnected;
import com.azure.communication.callautomation.models.events.RemoveParticipantSucceeded;
import com.azure.communication.callautomation.models.events.AddParticipantCancelled;
import com.azure.communication.common.CommunicationIdentifier;
import com.azure.communication.common.CommunicationUserIdentifier;
import com.azure.communication.identity.CommunicationIdentityAsyncClient;
Expand Down Expand Up @@ -154,4 +156,106 @@ public void createVOIPCallAndAnswerThenAddParticipantFinallyRemoveParticipantAut
}
}
}

@DoNotRecord(skipInPlayback = true)
@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
@DisabledIfEnvironmentVariable(
named = "SKIP_LIVE_TEST",
matches = "(?i)(true)",
disabledReason = "Requires environment to be set up")
public void createVOIPCallAndAnswerThenAddParticipantFinallyCancelAddParticipant(HttpClient httpClient) {
/* Test case: ACS to ACS call
* 1. create a CallAutomationClient.
* 2. create a call from source to one ACS target.
* 3. get updated call properties and check for the connected state.
* 4. add one more ACS target to the call.
* 5. cancel the add participant request.
*/
CommunicationIdentityAsyncClient identityAsyncClient = getCommunicationIdentityClientUsingConnectionString(httpClient)
.addPolicy((context, next) -> logHeaders("createVOIPCallAndAnswerThenAddParticipantFinallyCancelAddParticipant", next))
.buildAsyncClient();

List<CallConnectionAsync> callDestructors = new ArrayList<>();

try {
// create caller and receiver
CommunicationUserIdentifier caller = identityAsyncClient.createUser().block();
CommunicationUserIdentifier receiver = identityAsyncClient.createUser().block();
CommunicationUserIdentifier anotherReceiver = identityAsyncClient.createUser().block();

String uniqueId = serviceBusWithNewCall(caller, receiver);

// Create call automation client for caller.
CallAutomationAsyncClient callerAsyncClient = getCallAutomationClientUsingConnectionString(httpClient)
.addPolicy((context, next) -> logHeaders("createVOIPCallAndAnswerThenAddParticipantFinallyCancelAddParticipant", next))
.sourceIdentity(caller)
.buildAsyncClient();

// Create call automation client for receivers.
CallAutomationAsyncClient receiverAsyncClient = getCallAutomationClientUsingConnectionString(httpClient)
.addPolicy((context, next) -> logHeaders("createVOIPCallAndAnswerThenAddParticipantFinallyCancelAddParticipant", next))
.buildAsyncClient();

// create a call
List<CommunicationIdentifier> targetParticipants = new ArrayList<>(Arrays.asList(receiver));
CreateGroupCallOptions createCallOptions = new CreateGroupCallOptions(targetParticipants,
DISPATCHER_CALLBACK + String.format("?q=%s", uniqueId));
Response<CreateCallResult> createCallResultResponse = callerAsyncClient.createGroupCallWithResponse(createCallOptions).block();
assertNotNull(createCallResultResponse);
CreateCallResult createCallResult = createCallResultResponse.getValue();
assertNotNull(createCallResult);
assertNotNull(createCallResult.getCallConnectionProperties());
String callerConnectionId = createCallResult.getCallConnectionProperties().getCallConnectionId();
assertNotNull(callerConnectionId);

// wait for the incomingCallContext
String incomingCallContext = waitForIncomingCallContext(uniqueId, Duration.ofSeconds(10));
assertNotNull(incomingCallContext);

// answer the call
AnswerCallOptions answerCallOptions = new AnswerCallOptions(incomingCallContext,
DISPATCHER_CALLBACK + String.format("?q=%s", uniqueId));
AnswerCallResult answerCallResult = Objects.requireNonNull(receiverAsyncClient.answerCallWithResponse(answerCallOptions).block()).getValue();
assertNotNull(answerCallResult);
assertNotNull(answerCallResult.getCallConnectionAsync());
assertNotNull(answerCallResult.getCallConnectionProperties());
String receiverConnectionId = answerCallResult.getCallConnectionProperties().getCallConnectionId();
callDestructors.add(answerCallResult.getCallConnectionAsync());

// wait for callConnected
CallConnected callConnected = waitForEvent(CallConnected.class, receiverConnectionId, Duration.ofSeconds(10));
assertNotNull(callConnected);

// add another receiver to the call
AddParticipantOptions addParticipantsOptions = new AddParticipantOptions(new CallInvite(anotherReceiver));
Response<AddParticipantResult> addParticipantsResultResponse = createCallResult.getCallConnectionAsync().addParticipantWithResponse(addParticipantsOptions).block();
assertNotNull(addParticipantsResultResponse);

// ensure invitation is sent
Thread.sleep(3000);

// cancel add participant
CancelAddParticipantResult cancelAddParticipantResponse = createCallResult
.getCallConnectionAsync()
.cancelAddParticipant(addParticipantsResultResponse.getValue().getInvitationId())
.block();
assertNotNull(cancelAddParticipantResponse);

// wait for addParticipantSucceed
AddParticipantCancelled addParticipantCancelled = waitForEvent(AddParticipantCancelled.class, callerConnectionId, Duration.ofSeconds(10));
assertNotNull(addParticipantCancelled);
} catch (Exception ex) {
fail("Unexpected exception received", ex);
} finally {
if (!callDestructors.isEmpty()) {
try {
callDestructors.forEach(callConnection -> callConnection.hangUpWithResponse(true).block());
} catch (Exception ignored) {
// Some call might have been terminated during the test, and it will cause exceptions here.
// Do nothing and iterate to next call connection.
}
}
}
}
}

0 comments on commit d9f2071

Please sign in to comment.