diff --git a/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/CallConnectionAsync.java b/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/CallConnectionAsync.java index 5638660cf0df6..c42c73288d227 100644 --- a/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/CallConnectionAsync.java +++ b/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/CallConnectionAsync.java @@ -162,30 +162,6 @@ public Mono> playAudioWithResponse( return playAudioWithResponseInternal(audioFileUri, playAudioOptions, null); } - Mono> playAudioWithResponseInternal( - String audioFileUri, - boolean loop, - String audioFileId, - String callbackUri, - String operationContext, - Context context) { - try { - Objects.requireNonNull(audioFileUri, "'audioFileUri' cannot be null."); - PlayAudioRequest playAudioRequest = - new PlayAudioRequest() - .setAudioFileUri(audioFileUri) - .setLoop(loop) - .setAudioFileId(audioFileId) - .setOperationContext(operationContext) - .setCallbackUri(callbackUri); - return playAudioWithResponseInternal(playAudioRequest, context); - } catch (RuntimeException ex) { - return monoError(logger, ex); - } - - - } - Mono> playAudioWithResponseInternal( String audioFileUri, PlayAudioOptions playAudioOptions, diff --git a/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/ServerCallAsync.java b/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/ServerCallAsync.java index d28cdafea76b7..b32b8d5d86e75 100644 --- a/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/ServerCallAsync.java +++ b/sdk/communication/azure-communication-callingserver/src/main/java/com/azure/communication/callingserver/ServerCallAsync.java @@ -543,31 +543,6 @@ public Mono> playAudioWithResponse( return playAudioWithResponseInternal(audioFileUri, playAudioOptions, null); } - Mono> playAudioWithResponseInternal( - String audioFileUri, - String audioFileId, - String callbackUri, - String operationContext, - Context context) { - try { - Objects.requireNonNull(audioFileUri, "'audioFileUri' cannot be null."); - - //Currently we do not support loop on the audio media for out-call, thus setting the loop to false - PlayAudioRequest playAudioRequest = - new PlayAudioRequest() - .setAudioFileUri(audioFileUri) - .setLoop(false) - .setAudioFileId(audioFileId) - .setOperationContext(operationContext) - .setCallbackUri(callbackUri); - return playAudioWithResponse(playAudioRequest, context); - } catch (RuntimeException ex) { - return monoError(logger, ex); - } - - - } - Mono> playAudioWithResponseInternal( String audioFileUri, PlayAudioOptions playAudioOptions, diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionAsyncUnitTests.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionAsyncUnitTests.java new file mode 100644 index 0000000000000..e7700230e85bf --- /dev/null +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionAsyncUnitTests.java @@ -0,0 +1,165 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.communication.callingserver; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.AbstractMap.SimpleEntry; + +import com.azure.communication.callingserver.implementation.models.ResultInfoInternal; +import com.azure.communication.callingserver.models.AddParticipantResult; +import com.azure.communication.callingserver.models.CreateCallOptions; +import com.azure.communication.callingserver.models.EventSubscriptionType; +import com.azure.communication.callingserver.models.JoinCallOptions; +import com.azure.communication.callingserver.models.MediaType; +import com.azure.communication.callingserver.models.OperationStatus; +import com.azure.communication.callingserver.models.PlayAudioOptions; +import com.azure.communication.callingserver.models.PlayAudioResult; +import com.azure.communication.common.CommunicationIdentifier; +import com.azure.communication.common.CommunicationUserIdentifier; +import com.azure.core.http.rest.Response; +import com.azure.core.util.Context; + +import org.junit.jupiter.api.Test; + +public class CallConnectionAsyncUnitTests { + + static final String CALL_CONNECTION_ID = "callConnectionId"; + static final String OPERATION_ID = "operationId"; + static final String NEW_PARTICIPANT_ID = "newParticipantId"; + + @Test + public void createConnectionWithResponse() { + CallingServerAsyncClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateCreateCallResult(CallingServerResponseMocker.CALL_CONNECTION_ID), 201) + ))); + + CommunicationUserIdentifier sourceUser = new CommunicationUserIdentifier("id"); + List targetUsers = new ArrayList(); + targetUsers.add(new CommunicationUserIdentifier("id2")); + + CreateCallOptions options = new CreateCallOptions( + "serverCallId", + Collections.singletonList(MediaType.AUDIO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + + Response callConnectionAsyncResponse = callingServerAsyncClient.createCallConnectionWithResponse(sourceUser, targetUsers, options).block(); + assertEquals(201, callConnectionAsyncResponse.getStatusCode()); + assertNotNull(callConnectionAsyncResponse.getValue()); + } + + @Test + public void getCallConnectionCallingServerAsyncClient() { + CallingServerAsyncClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList())); + + CallConnectionAsync callConnection = callingServerAsyncClient.getCallConnection(CallingServerResponseMocker.CALL_CONNECTION_ID); + assertNotNull(callConnection); + } + + @Test + public void playAudioWithResponse() { + CallConnectionAsync callConnectionAsync = getPlayAudioCallConnection(); + + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); + Response playAudioResultResponse = callConnectionAsync.playAudioWithResponse("audioFileUri", playAudioOptions).block(); + assertEquals(202, playAudioResultResponse.getStatusCode()); + } + + @Test + public void playAudio() { + CallConnectionAsync callConnectionAsync = getPlayAudioCallConnection(); + + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); + PlayAudioResult playAudioResult = callConnectionAsync.playAudio("audioFileUri", playAudioOptions).block(); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void playAudioWithoutPlayAudioOptions202() { + CallConnectionAsync callConnectionAsync = getPlayAudioCallConnection(); + + PlayAudioResult playAudioResult = callConnectionAsync.playAudio("audioFileUri", false, "audioFieldId", "callbackUri", "operationContext").block(); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void appParticipantWithResponse() { + CallConnectionAsync callConnectionAsync = getAddParticipantCallConnection(); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + Response addParticipantResultResponse = callConnectionAsync.addParticipantWithResponse(user, "alternateCallerId", "operationContext", Context.NONE).block(); + assertEquals(202, addParticipantResultResponse.getStatusCode()); + AddParticipantResult addParticipantResult = addParticipantResultResponse.getValue(); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void appParticipant() { + CallConnectionAsync callConnectionAsync = getAddParticipantCallConnection(); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + AddParticipantResult addParticipantResult = callConnectionAsync.addParticipant(user, "alternateCallerId", "operationContext").block(); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void joinCallWithResponse() { + CallingServerAsyncClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateJoinCallResult(NEW_PARTICIPANT_ID), 202) + ))); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + JoinCallOptions options = new JoinCallOptions( + CallingServerResponseMocker.URI_CALLBACK, + Collections.singletonList(MediaType.VIDEO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + Response callConnectionAsyncResponse = callingServerAsyncClient.joinCallWithResponse(CallingServerResponseMocker.SERVER_CALL_ID, (CommunicationIdentifier) user, options).block(); + assertEquals(202, callConnectionAsyncResponse.getStatusCode()); + assertNotNull(callConnectionAsyncResponse.getValue()); + } + + @Test + public void joinCall() { + CallingServerAsyncClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateJoinCallResult(NEW_PARTICIPANT_ID), 202) + ))); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + JoinCallOptions options = new JoinCallOptions( + CallingServerResponseMocker.URI_CALLBACK, + Collections.singletonList(MediaType.VIDEO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + CallConnectionAsync callConnectionAsync = callingServerAsyncClient.joinCall(CallingServerResponseMocker.SERVER_CALL_ID, (CommunicationIdentifier) user, options).block(); + assertNotNull(callConnectionAsync); + } + + private CallConnectionAsync getPlayAudioCallConnection() { + return CallingServerResponseMocker.getCallConnectionAsync(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateCreateCallResult(CALL_CONNECTION_ID), 201), + new SimpleEntry(CallingServerResponseMocker.generatePlayAudioResult( + OPERATION_ID, + OperationStatus.COMPLETED, + new ResultInfoInternal().setCode(202).setSubcode(0).setMessage("message")), + 202) + ))); + } + + private CallConnectionAsync getAddParticipantCallConnection() { + return CallingServerResponseMocker.getCallConnectionAsync(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateCreateCallResult(CALL_CONNECTION_ID), 201), + new SimpleEntry(CallingServerResponseMocker.generateAddParticipantResult(NEW_PARTICIPANT_ID), 202) + ))); + } +} diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionUnitTests.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionUnitTests.java new file mode 100644 index 0000000000000..12b5f1d4f89b2 --- /dev/null +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallConnectionUnitTests.java @@ -0,0 +1,167 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.communication.callingserver; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.AbstractMap.SimpleEntry; + +import com.azure.communication.callingserver.implementation.models.ResultInfoInternal; +import com.azure.communication.callingserver.models.AddParticipantResult; +import com.azure.communication.callingserver.models.CreateCallOptions; +import com.azure.communication.callingserver.models.EventSubscriptionType; +import com.azure.communication.callingserver.models.JoinCallOptions; +import com.azure.communication.callingserver.models.MediaType; +import com.azure.communication.callingserver.models.OperationStatus; +import com.azure.communication.callingserver.models.PlayAudioOptions; +import com.azure.communication.callingserver.models.PlayAudioResult; +import com.azure.communication.common.CommunicationIdentifier; +import com.azure.communication.common.CommunicationUserIdentifier; +import com.azure.core.http.rest.Response; +import com.azure.core.util.Context; + +import org.junit.jupiter.api.Test; + +public class CallConnectionUnitTests { + + static final String CALL_CONNECTION_ID = "callConnectionId"; + static final String OPERATION_ID = "operationId"; + static final String NEW_PARTICIPANT_ID = "newParticipantId"; + + @Test + public void createConnectionWithResponse() { + CallingServerClient callingServerClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateCreateCallResult(CallingServerResponseMocker.CALL_CONNECTION_ID), 201) + ))); + + CommunicationUserIdentifier sourceUser = new CommunicationUserIdentifier("id"); + List targetUsers = new ArrayList(); + targetUsers.add(new CommunicationUserIdentifier("id2")); + + CreateCallOptions options = new CreateCallOptions( + "serverCallId", + Collections.singletonList(MediaType.AUDIO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + + Response callConnectionAsyncResponse = callingServerClient.createCallConnectionWithResponse(sourceUser, targetUsers, options, Context.NONE); + assertEquals(201, callConnectionAsyncResponse.getStatusCode()); + assertNotNull(callConnectionAsyncResponse.getValue()); + } + + @Test + public void getCallConnectionCallingServerClient() { + CallingServerClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList>( + Arrays.asList())); + + CallConnection callConnection = callingServerAsyncClient.getCallConnection(CallingServerResponseMocker.CALL_CONNECTION_ID); + assertNotNull(callConnection); + } + + @Test + public void playAudioWithResponse() { + CallConnection callConnection = getCallConnection(); + + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); + Response playAudioResultResponse = callConnection.playAudioWithResponse("audioFileUri", playAudioOptions, Context.NONE); + assertEquals(202, playAudioResultResponse.getStatusCode()); + PlayAudioResult playAudioResult = playAudioResultResponse.getValue(); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void playAudio() { + CallConnection callConnection = getCallConnection(); + + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); + PlayAudioResult playAudioResult = callConnection.playAudio("audioFileUri", playAudioOptions); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void playAudioWithoutPlayAudioOptions() { + CallConnection callConnection = getCallConnection(); + + PlayAudioResult playAudioResult = callConnection.playAudio("audioFileUri", false, "audioFieldId", "callbackUri", "operationContext"); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void appParticipantWithResponse() { + CallConnection callConnection = getAddParticipantCallConnection(); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + Response addParticipantResultResponse = callConnection.addParticipantWithResponse(user, "alternateCallerId", "operationContext", Context.NONE); + assertEquals(202, addParticipantResultResponse.getStatusCode()); + AddParticipantResult addParticipantResult = addParticipantResultResponse.getValue(); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void appParticipant() { + CallConnection callConnection = getAddParticipantCallConnection(); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + AddParticipantResult addParticipantResult = callConnection.addParticipant(user, "alternateCallerId", "operationContext"); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void joinCall() { + CallingServerClient callingServerClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateJoinCallResult(NEW_PARTICIPANT_ID), 202) + ))); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + JoinCallOptions options = new JoinCallOptions( + CallingServerResponseMocker.URI_CALLBACK, + Collections.singletonList(MediaType.VIDEO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + CallConnection callConnection = callingServerClient.joinCall(CallingServerResponseMocker.SERVER_CALL_ID, (CommunicationIdentifier) user, options); + assertNotNull(callConnection); + } + + @Test + public void joinCallWithResponse() { + CallingServerClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateJoinCallResult(NEW_PARTICIPANT_ID), 202) + ))); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); + JoinCallOptions options = new JoinCallOptions( + CallingServerResponseMocker.URI_CALLBACK, + Collections.singletonList(MediaType.VIDEO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + Response callConnectionResponse = callingServerAsyncClient.joinCallWithResponse(CallingServerResponseMocker.SERVER_CALL_ID, (CommunicationIdentifier) user, options, Context.NONE); + assertEquals(202, callConnectionResponse.getStatusCode()); + assertNotNull(callConnectionResponse.getValue()); + } + + private CallConnection getCallConnection() { + return CallingServerResponseMocker.getCallConnection(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateCreateCallResult(CALL_CONNECTION_ID), 201), + new SimpleEntry(CallingServerResponseMocker.generatePlayAudioResult( + OPERATION_ID, + OperationStatus.COMPLETED, + new ResultInfoInternal().setCode(202).setSubcode(0).setMessage("message")), + 202) + ))); + } + + private CallConnection getAddParticipantCallConnection() { + return CallingServerResponseMocker.getCallConnection(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateCreateCallResult(CALL_CONNECTION_ID), 201), + new SimpleEntry(CallingServerResponseMocker.generateAddParticipantResult(NEW_PARTICIPANT_ID), 202) + ))); + } +} diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallingServerResponseMocker.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallingServerResponseMocker.java new file mode 100644 index 0000000000000..87f033c38c646 --- /dev/null +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallingServerResponseMocker.java @@ -0,0 +1,183 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. +package com.azure.communication.callingserver; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.AbstractMap.SimpleEntry; + +import com.azure.communication.callingserver.implementation.models.AddParticipantResultInternal; +import com.azure.communication.callingserver.implementation.models.CreateCallResultInternal; +import com.azure.communication.callingserver.implementation.models.JoinCallResultInternal; +import com.azure.communication.callingserver.implementation.models.PlayAudioResultInternal; +import com.azure.communication.callingserver.implementation.models.ResultInfoInternal; +import com.azure.communication.callingserver.models.CreateCallOptions; +import com.azure.communication.callingserver.models.EventSubscriptionType; +import com.azure.communication.callingserver.models.MediaType; +import com.azure.communication.callingserver.models.OperationStatus; +import com.azure.communication.common.CommunicationIdentifier; +import com.azure.communication.common.CommunicationUserIdentifier; +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaders; +import com.azure.core.http.HttpRequest; +import com.azure.core.http.HttpResponse; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import reactor.core.publisher.Flux; +import reactor.core.publisher.Mono; + +public class CallingServerResponseMocker { + public static final String CALL_CONNECTION_ID = "callConnectionId"; + public static final String OPERATION_ID = "operationId"; + public static final String MOCK_CONNECTION_STRING = "endpoint=https://REDACTED.communication.azure.com/;accesskey=eyJhbG"; + public static final String NEW_PARTICIPANT_ID = "newParticipantId"; + public static final String URI_CALLBACK = "https://uri.com"; + public static final String SERVER_CALL_ID = "serverCallId"; + + public static String generateCreateCallResult(String callConnectionId) { + + CreateCallResultInternal result = new CreateCallResultInternal() + .setCallConnectionId(callConnectionId); + + return serializeObject(result); + } + + public static String generateAddParticipantResult(String participantId) { + + AddParticipantResultInternal result = new AddParticipantResultInternal() + .setParticipantId(participantId); + + return serializeObject(result); + } + + public static String generateDownloadResult(String content) { + return content; + } + + public static String generatePlayAudioResult(String operationId, OperationStatus status, ResultInfoInternal resultInfo) { + + PlayAudioResultInternal result = new PlayAudioResultInternal() + .setOperationContext("operationContext") + .setOperationId(operationId) + .setStatus(status) + .setResultInfo(resultInfo); + + return serializeObject(result); + } + + public static String generateJoinCallResult(String callConnectionId) { + JoinCallResultInternal result = new JoinCallResultInternal() + .setCallConnectionId(callConnectionId); + + return serializeObject(result); + } + + public static CallConnectionAsync getCallConnectionAsync(ArrayList> responses) { + CallingServerAsyncClient callingServerAsyncClient = getCallingServerAsyncClient(responses); + + CommunicationUserIdentifier sourceUser = new CommunicationUserIdentifier("id"); + List targetUsers = new ArrayList(); + CreateCallOptions options = new CreateCallOptions( + "serverCallId", + Collections.singletonList(MediaType.AUDIO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + + return callingServerAsyncClient.createCallConnection(sourceUser, targetUsers, options).block(); + } + + public static CallConnection getCallConnection(ArrayList> responses) { + CallingServerClient callingServerClient = getCallingServerClient(responses); + + CommunicationUserIdentifier sourceUser = new CommunicationUserIdentifier("id"); + List targetUsers = new ArrayList(); + CreateCallOptions options = new CreateCallOptions( + "serverCallId", + Collections.singletonList(MediaType.AUDIO), + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); + + return callingServerClient.createCallConnection(sourceUser, targetUsers, options); + } + + public static ServerCallAsync getServerCallAsync(ArrayList> responses) { + CallingServerAsyncClient callingServerClient = getCallingServerAsyncClient(responses); + return callingServerClient.initializeServerCall("serverCallId"); + } + + public static ServerCall getServerCall(ArrayList> responses) { + CallingServerClient callingServerClient = getCallingServerClient(responses); + return callingServerClient.initializeServerCall("serverCallId"); + } + + public static CallingServerAsyncClient getCallingServerAsyncClient(ArrayList> responses) { + HttpClient mockHttpClient = new MockHttpClient(responses); + + return new CallingServerClientBuilder() + .httpClient(mockHttpClient) + .connectionString(MOCK_CONNECTION_STRING) + .buildAsyncClient(); + } + + public static CallingServerClient getCallingServerClient(ArrayList> responses) { + HttpClient mockHttpClient = new MockHttpClient(responses); + + return new CallingServerClientBuilder() + .httpClient(mockHttpClient) + .connectionString(MOCK_CONNECTION_STRING) + .buildClient(); + } + + public static HttpResponse generateMockResponse(String body, HttpRequest request, int statusCode) { + return new HttpResponse(request) { + @Override + public int getStatusCode() { + return statusCode; + } + + @Override + public String getHeaderValue(String name) { + return null; + } + + @Override + public HttpHeaders getHeaders() { + return new HttpHeaders(); + } + + @Override + public Flux getBody() { + return Flux.just(ByteBuffer.wrap(body.getBytes(StandardCharsets.UTF_8))); + } + + @Override + public Mono getBodyAsByteArray() { + return Mono.just(body.getBytes(StandardCharsets.UTF_8)); + } + + @Override + public Mono getBodyAsString() { + return Mono.just(body); + } + + @Override + public Mono getBodyAsString(Charset charset) { + return Mono.just(body); + } + }; + } + + private static String serializeObject(Object o) { + ObjectMapper mapper = new ObjectMapper(); + String body = null; + try { + body = mapper.writeValueAsString(o); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return body; + } +} diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallingServerTestBase.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallingServerTestBase.java index f1ea73fdb1a69..adf6b27ebf1ee 100644 --- a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallingServerTestBase.java +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/CallingServerTestBase.java @@ -44,7 +44,7 @@ public class CallingServerTestBase extends TestBase { protected static final String AZURE_TENANT_ID = Configuration.getGlobalConfiguration() .get("COMMUNICATION_LIVETEST_STATIC_RESOURCE_IDENTIFIER", - "016a7064-0581-40b9-be73-6dde64d69d72"); + "016a7064-0581-40b9-be73-6dde64d69d72"); protected static final String FROM_PHONE_NUMBER = Configuration.getGlobalConfiguration() .get("AZURE_PHONE_NUMBER", "+15551234567"); @@ -109,7 +109,7 @@ protected String getGroupId(String testName) { have unique groupId's so they do not conflict with other recording tests running in live mode. */ - if (getTestMode() == TestMode.LIVE) { + if (getTestMode() == TestMode.LIVE) { return UUID.randomUUID().toString(); } diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/DownloadContentAsyncUnitTests.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/DownloadContentAsyncUnitTests.java new file mode 100644 index 0000000000000..d66736dc646a8 --- /dev/null +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/DownloadContentAsyncUnitTests.java @@ -0,0 +1,101 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.communication.callingserver; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystems; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.UUID; +import java.util.AbstractMap.SimpleEntry; + +import com.azure.communication.callingserver.models.ParallelDownloadOptions; +import com.azure.core.http.HttpRange; +import com.azure.core.http.rest.Response; + +import org.junit.jupiter.api.Test; + +import reactor.core.publisher.Flux; + +public class DownloadContentAsyncUnitTests { + @Test + public void downloadStream() throws IOException { + String contents = "VideoContents"; + CallingServerAsyncClient callingServerClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateDownloadResult(contents), 200) + ))); + + Flux fluxByteBuffer = callingServerClient.downloadStream("https://url.com", new HttpRange(contents.length())); + + String resultContents = new String(fluxByteBuffer.next().block().array(), StandardCharsets.UTF_8); + assertEquals("VideoContents", resultContents); + } + + @Test + public void downloadStreamWithResponse() throws IOException { + String contents = "VideoContents"; + CallingServerAsyncClient callingServerClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateDownloadResult(contents), 200) + ))); + + Response> fluxByteBufferResponse = callingServerClient.downloadStreamWithResponse("https://url.com", new HttpRange(contents.length())).block(); + assertEquals(200, fluxByteBufferResponse.getStatusCode()); + Flux fluxByteBuffer = fluxByteBufferResponse.getValue(); + String resultContents = new String(fluxByteBuffer.next().block().array(), StandardCharsets.UTF_8); + assertEquals("VideoContents", resultContents); + } + + @Test + public void downloadStreamWithResponseThrowException() throws IOException { + String contents = "VideoContents"; + CallingServerAsyncClient callingServerClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList( + new SimpleEntry("", 416) + ))); + + Response> fluxByteBufferResponse = callingServerClient.downloadStreamWithResponse("https://url.com", new HttpRange(contents.length())).block(); + Flux fluxByteBuffer = fluxByteBufferResponse.getValue(); + assertThrows(NullPointerException.class, () -> fluxByteBuffer.next().block()); + } + + @Test + public void downloadToWithResponse() throws IOException { + String contents = "VideoContents"; + CallingServerAsyncClient callingServerClient = CallingServerResponseMocker.getCallingServerAsyncClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateDownloadResult(contents), 200) + ))); + String fileName = "./" + UUID.randomUUID().toString().replace("-", "") + ".txt"; + Path path = FileSystems.getDefault().getPath(fileName); + ParallelDownloadOptions options = new ParallelDownloadOptions().setMaxConcurrency(1).setBlockSize(512L); + File file = null; + + try { + Response response = callingServerClient.downloadToWithResponse("https://url.com", path, options, true).block(); + assertEquals(200, response.getStatusCode()); + + file = path.toFile(); + assertTrue(file.exists(), "file does not exist"); + BufferedReader br = new BufferedReader(new FileReader(file)); + assertEquals("VideoContents", br.readLine()); + br.close(); + } finally { + if (file != null && file.exists()) { + file.delete(); + } + } + } +} diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/DownloadContentUnitTests.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/DownloadContentUnitTests.java new file mode 100644 index 0000000000000..8297c614637af --- /dev/null +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/DownloadContentUnitTests.java @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.communication.callingserver; + + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.AbstractMap.SimpleEntry; + +import com.azure.core.http.HttpRange; +import com.azure.core.http.rest.Response; +import com.azure.core.util.Context; + +import org.junit.jupiter.api.Test; + +public class DownloadContentUnitTests { + @Test + public void downloadTo() throws IOException { + CallingServerClient callingServerClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateDownloadResult("VideoContents"), 200) + ))); + + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + callingServerClient.downloadTo("https://url.com", stream, new HttpRange(1)); + Reader reader = new InputStreamReader(new ByteArrayInputStream(stream.toByteArray())); + BufferedReader b = new BufferedReader(reader); + assertEquals("VideoContents", b.readLine()); + } + + @Test + public void downloadToWithResponse() throws IOException { + CallingServerClient callingServerClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateDownloadResult("VideoContents"), 200) + ))); + + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Response response = callingServerClient.downloadToWithResponse("https://url.com", stream, new HttpRange(1), Context.NONE); + assertEquals(200, response.getStatusCode()); + Reader reader = new InputStreamReader(new ByteArrayInputStream(stream.toByteArray())); + BufferedReader b = new BufferedReader(reader); + assertEquals("VideoContents", b.readLine()); + } +} diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/EventTests.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/EventTests.java new file mode 100644 index 0000000000000..960ea4e9dd71d --- /dev/null +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/EventTests.java @@ -0,0 +1,222 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.communication.callingserver; + +import com.azure.communication.callingserver.implementation.models.AddParticipantResultEventInternal; +import com.azure.communication.callingserver.implementation.models.CallConnectionStateChangedEventInternal; +import com.azure.communication.callingserver.implementation.models.CallParticipantInternal; +import com.azure.communication.callingserver.implementation.models.CallRecordingStateChangeEventInternal; +import com.azure.communication.callingserver.implementation.models.CommunicationIdentifierModel; +import com.azure.communication.callingserver.implementation.models.ParticipantsUpdatedEventInternal; +import com.azure.communication.callingserver.implementation.models.PhoneNumberIdentifierModel; +import com.azure.communication.callingserver.implementation.models.PlayAudioResultEventInternal; +import com.azure.communication.callingserver.implementation.models.ResultInfoInternal; +import com.azure.communication.callingserver.implementation.models.ToneInfoInternal; +import com.azure.communication.callingserver.implementation.models.ToneReceivedEventInternal; +import com.azure.communication.callingserver.models.CallConnectionState; +import com.azure.communication.callingserver.models.CallRecordingState; +import com.azure.communication.callingserver.models.OperationStatus; +import com.azure.communication.callingserver.models.ToneValue; +import com.azure.communication.callingserver.models.events.AddParticipantResultEvent; +import com.azure.communication.callingserver.models.events.CallConnectionStateChangedEvent; +import com.azure.communication.callingserver.models.events.CallRecordingStateChangeEvent; +import com.azure.communication.callingserver.models.events.ParticipantsUpdatedEvent; +import com.azure.communication.callingserver.models.events.PlayAudioResultEvent; +import com.azure.communication.callingserver.models.events.ToneReceivedEvent; +import com.azure.communication.common.PhoneNumberIdentifier; +import com.azure.core.util.BinaryData; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.OffsetDateTime; +import java.util.Collections; +import java.util.LinkedList; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class EventTests { + @Test + public void callConnectionStateChangedEventTest() throws JsonProcessingException { + String serverCallId = UUID.randomUUID().toString(); + String callConnectionId = UUID.randomUUID().toString(); + CallConnectionStateChangedEventInternal internalEvent = + new CallConnectionStateChangedEventInternal() + .setCallConnectionId(callConnectionId) + .setServerCallId(serverCallId) + .setCallConnectionState(CallConnectionState.CONNECTED); + + BinaryData binaryData = getBinaryData(internalEvent); + CallConnectionStateChangedEvent event = + assertDoesNotThrow(() -> CallConnectionStateChangedEvent.deserialize(binaryData)); + + assertNotNull(event); + assertEquals(event.getCallConnectionId(), callConnectionId); + assertEquals(event.getServerCallId(), serverCallId); + assertEquals(event.getCallConnectionState(), CallConnectionState.CONNECTED); + } + + @Test + public void addParticipantResultEventTest() throws JsonProcessingException { + String operationContext = UUID.randomUUID().toString(); + String message = "Participant added."; + AddParticipantResultEventInternal internalEvent = + new AddParticipantResultEventInternal() + .setOperationContext(operationContext) + .setStatus(OperationStatus.COMPLETED) + .setResultInfo(new ResultInfoInternal().setCode(100).setSubcode(200).setMessage(message)); + + BinaryData binaryData = getBinaryData(internalEvent); + AddParticipantResultEvent event = + assertDoesNotThrow(() -> AddParticipantResultEvent.deserialize(binaryData)); + + assertNotNull(event); + assertEquals(event.getOperationContext(), operationContext); + assertEquals(event.getStatus(), OperationStatus.COMPLETED); + assertNotNull(event.getResultInfo()); + assertEquals(event.getResultInfo().getCode(), 100); + assertEquals(event.getResultInfo().getSubcode(), 200); + assertEquals(event.getResultInfo().getMessage(), message); + } + + @Test + public void callRecordingStateChangeEventTest() throws JsonProcessingException { + String serverCallId = UUID.randomUUID().toString(); + String recordingId = UUID.randomUUID().toString(); + CallRecordingStateChangeEventInternal internalEvent = + new CallRecordingStateChangeEventInternal() + .setServerCallId(serverCallId) + .setRecordingId(recordingId) + .setState(CallRecordingState.ACTIVE) + .setStartDateTime(OffsetDateTime.MIN); + + BinaryData binaryData = getBinaryData(internalEvent); + CallRecordingStateChangeEvent event = + assertDoesNotThrow(() -> CallRecordingStateChangeEvent.deserialize(binaryData)); + + assertNotNull(event); + assertEquals(event.getRecordingId(), recordingId); + assertEquals(event.getServerCallId(), serverCallId); + assertEquals(event.getState(), CallRecordingState.ACTIVE); + assertEquals(event.getStartDateTime(), OffsetDateTime.MIN); + } + + @Test + public void playAudioResultEventTest() throws JsonProcessingException { + String operationContext = UUID.randomUUID().toString(); + String message = "Media operation executed."; + PlayAudioResultEventInternal internalEvent = + new PlayAudioResultEventInternal() + .setOperationContext(operationContext) + .setStatus(OperationStatus.COMPLETED) + .setResultInfo(new ResultInfoInternal().setCode(100).setSubcode(200).setMessage(message)); + + BinaryData binaryData = getBinaryData(internalEvent); + PlayAudioResultEvent event = + assertDoesNotThrow(() -> PlayAudioResultEvent.deserialize(binaryData)); + + assertNotNull(event); + assertEquals(event.getOperationContext(), operationContext); + assertEquals(event.getStatus(), OperationStatus.COMPLETED); + assertNotNull(event.getResultInfo()); + assertEquals(event.getResultInfo().getCode(), 100); + assertEquals(event.getResultInfo().getSubcode(), 200); + assertEquals(event.getResultInfo().getMessage(), message); + } + + @Test + public void toneReceivedEventTest() throws JsonProcessingException { + String callConnectionId = UUID.randomUUID().toString(); + ToneReceivedEventInternal internalEvent = + new ToneReceivedEventInternal() + .setCallConnectionId(callConnectionId) + .setToneInfo(new ToneInfoInternal() + .setSequenceId(1) + .setTone(ToneValue.TONE1)); + + BinaryData binaryData = getBinaryData(internalEvent); + ToneReceivedEvent event = + assertDoesNotThrow(() -> ToneReceivedEvent.deserialize(binaryData)); + + assertNotNull(event); + assertEquals(event.getCallConnectionId(), callConnectionId); + assertNotNull(event.getToneInfo()); + assertEquals(event.getToneInfo().getSequenceId(), 1); + assertEquals(event.getToneInfo().getTone(), ToneValue.TONE1); + } + + @Test + public void participantsUpdatedEventTest() throws JsonProcessingException { + String callConnectionId = UUID.randomUUID().toString(); + String participantId = UUID.randomUUID().toString(); + String phoneNumber = "+18881112222"; + ParticipantsUpdatedEventInternal internalEvent = + new ParticipantsUpdatedEventInternal() + .setCallConnectionId(callConnectionId) + .setParticipants( + new LinkedList<>(Collections.singletonList( + new CallParticipantInternal() + .setParticipantId(participantId) + .setIsMuted(false) + .setIdentifier(new CommunicationIdentifierModel() + .setPhoneNumber(new PhoneNumberIdentifierModel().setValue(phoneNumber)))))); + + BinaryData binaryData = getBinaryData(internalEvent); + ParticipantsUpdatedEvent event = + assertDoesNotThrow(() -> ParticipantsUpdatedEvent.deserialize(binaryData)); + + assertNotNull(event); + assertEquals(event.getCallConnectionId(), callConnectionId); + assertNotNull(event.getParticipants()); + assertEquals(1, event.getParticipants().size()); + assertEquals(event.getParticipants().get(0).getParticipantId(), participantId); + assertFalse(event.getParticipants().get(0).isMuted()); + assertEquals(((PhoneNumberIdentifier) event.getParticipants().get(0).getIdentifier()).getPhoneNumber(), phoneNumber); + } + + private BinaryData getBinaryData(Object eventObject) throws JsonProcessingException { + ObjectWriter objectWriter = getObjectMapper().writer().withDefaultPrettyPrinter(); + String jsonData = objectWriter.writeValueAsString(eventObject); + return BinaryData.fromString(jsonData); + } + + private ObjectMapper getObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JavaTimeModule()); + SimpleModule simpleModule = new SimpleModule(); + simpleModule.addSerializer(OffsetDateTime.class, new JsonSerializer() { + @Override + public void serialize( + OffsetDateTime offsetDateTime, + JsonGenerator jsonGenerator, + SerializerProvider serializerProvider) throws IOException { + jsonGenerator.writeString(offsetDateTime.toString()); + } + }); + simpleModule.addDeserializer(OffsetDateTime.class, new JsonDeserializer() { + @Override + public OffsetDateTime deserialize( + JsonParser jsonParser, + DeserializationContext deserializationContext) throws IOException { + return OffsetDateTime.parse(jsonParser.getText()); + } + }); + objectMapper.registerModule(simpleModule); + return objectMapper; + } +} diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/MockHttpClient.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/MockHttpClient.java new file mode 100644 index 0000000000000..3a3c5061ae0d6 --- /dev/null +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/MockHttpClient.java @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.communication.callingserver; + +import java.util.ArrayList; +import java.util.List; +import java.util.AbstractMap.SimpleEntry; + +import com.azure.communication.callingserver.CallingServerClientBuilderUnitTests.NoOpHttpClient; +import com.azure.communication.callingserver.models.CallingServerErrorException; +import com.azure.core.http.HttpRequest; +import com.azure.core.http.HttpResponse; + +import reactor.core.publisher.Mono; + +public class MockHttpClient extends NoOpHttpClient { + public static final String THROW_TEST_EXCEPTION = "exception"; + private ArrayList> responses = new ArrayList>(); + private ArrayList requests = new ArrayList(); + + public MockHttpClient(ArrayList> responses) { + this.responses = responses; + } + + public List getRequests() { + return requests; + } + + @Override + public Mono send(HttpRequest request) { + requests.add(request); + + if (responses.size() > 0) { + SimpleEntry entry = responses.get(0); + responses.remove(entry); + HttpResponse response = CallingServerResponseMocker.generateMockResponse(entry.getKey(), request, entry.getValue()); + if (entry.getKey().startsWith(THROW_TEST_EXCEPTION)) { + return Mono.error(new CallingServerErrorException("Mock error", response)); + } + + return Mono.just(CallingServerResponseMocker.generateMockResponse(entry.getKey(), request, entry.getValue())); + } + + return Mono.just(CallingServerResponseMocker.generateMockResponse("", request, 500)); + } +} diff --git a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/ServerCallUnitTests.java b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/ServerCallUnitTests.java index e79c9e38217b1..8bd48492fcf4e 100644 --- a/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/ServerCallUnitTests.java +++ b/sdk/communication/azure-communication-callingserver/src/test/java/com/azure/communication/callingserver/ServerCallUnitTests.java @@ -3,9 +3,23 @@ package com.azure.communication.callingserver; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import java.security.InvalidParameterException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.AbstractMap.SimpleEntry; + +import com.azure.communication.callingserver.implementation.models.ResultInfoInternal; +import com.azure.communication.callingserver.models.AddParticipantResult; +import com.azure.communication.callingserver.models.OperationStatus; +import com.azure.communication.callingserver.models.PlayAudioOptions; +import com.azure.communication.callingserver.models.PlayAudioResult; +import com.azure.communication.common.CommunicationUserIdentifier; +import com.azure.core.http.rest.Response; import com.azure.core.test.http.NoOpHttpClient; +import com.azure.core.util.Context; + import org.junit.jupiter.api.Test; public class ServerCallUnitTests { @@ -65,4 +79,173 @@ public void startRecordingAsyncFails() { () -> serverCall.startRecording("/not/absolute/uri") .block()); } + + @Test + public void playAudioWithResponse() { + ServerCall serverCall = getServerCall(); + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); + Response playAudioResultResponse = serverCall.playAudioWithResponse("audioFileUri", playAudioOptions, Context.NONE); + assertEquals(202, playAudioResultResponse.getStatusCode()); + PlayAudioResult playAudioResult = playAudioResultResponse.getValue(); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void playAudio() { + ServerCall serverCall = getServerCall(); + + PlayAudioResult playAudioResult = serverCall.playAudio("audioFileUri", "audioFieldId", "callbackUri", "operationContext"); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void playAudioWithResponseAsync() { + ServerCallAsync serverCall = getServerCallAsync(); + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); + Response playAudioResultResponse = serverCall.playAudioWithResponse("audioFileUri", playAudioOptions).block(); + assertEquals(202, playAudioResultResponse.getStatusCode()); + PlayAudioResult playAudioResult = playAudioResultResponse.getValue(); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void playAudioAsync() { + ServerCallAsync serverCall = getServerCallAsync(); + + PlayAudioResult playAudioResult = serverCall.playAudio("audioFileUri", "audioFieldId", "callbackUri", "operationContext").block(); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void playAudioAsyncUsingOptions() { + ServerCallAsync serverCall = getServerCallAsync(); + + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); + PlayAudioResult playAudioResult = serverCall.playAudio("audioFileUri", playAudioOptions).block(); + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); + } + + @Test + public void appParticipantServerCall() { + ServerCall serverCall = CallingServerResponseMocker.getServerCall(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateAddParticipantResult(CallingServerResponseMocker.NEW_PARTICIPANT_ID), 202) + )) + ); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(CallingServerResponseMocker.NEW_PARTICIPANT_ID); + AddParticipantResult addParticipantResult = serverCall.addParticipant(user, CallingServerResponseMocker.URI_CALLBACK, "alternateCallerId", "operationContext"); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void appParticipantServerCallWithResponse() { + ServerCall serverCall = CallingServerResponseMocker.getServerCall(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateAddParticipantResult(CallingServerResponseMocker.NEW_PARTICIPANT_ID), 202) + )) + ); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(CallingServerResponseMocker.NEW_PARTICIPANT_ID); + Response addParticipantResultResponse = serverCall.addParticipantWithResponse(user, CallingServerResponseMocker.URI_CALLBACK, "alternateCallerId", "operationContext", Context.NONE); + assertEquals(202, addParticipantResultResponse.getStatusCode()); + AddParticipantResult addParticipantResult = addParticipantResultResponse.getValue(); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void appParticipantServerCallAsync() { + ServerCallAsync serverCall = CallingServerResponseMocker.getServerCallAsync(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateAddParticipantResult(CallingServerResponseMocker.NEW_PARTICIPANT_ID), 202) + )) + ); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(CallingServerResponseMocker.NEW_PARTICIPANT_ID); + AddParticipantResult addParticipantResult = serverCall.addParticipant(user, CallingServerResponseMocker.URI_CALLBACK, "alternateCallerId", "operationContext").block(); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void appParticipantServerCallAsyncWithResponse() { + ServerCallAsync serverCall = CallingServerResponseMocker.getServerCallAsync(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generateAddParticipantResult(CallingServerResponseMocker.NEW_PARTICIPANT_ID), 202) + )) + ); + + CommunicationUserIdentifier user = new CommunicationUserIdentifier(CallingServerResponseMocker.NEW_PARTICIPANT_ID); + Response addParticipantResultResponse = serverCall.addParticipantWithResponse(user, CallingServerResponseMocker.URI_CALLBACK, "alternateCallerId", "operationContext").block(); + assertEquals(202, addParticipantResultResponse.getStatusCode()); + AddParticipantResult addParticipantResult = addParticipantResultResponse.getValue(); + assertEquals(user.getId(), addParticipantResult.getParticipantId()); + } + + @Test + public void removeParticipantServerCall() { + ServerCall serverCall = CallingServerResponseMocker.getServerCall(new ArrayList>( + Arrays.asList( + new SimpleEntry("", 202) + )) + ); + + serverCall.removeParticipant(CallingServerResponseMocker.NEW_PARTICIPANT_ID); + } + + @Test + public void removeParticipantServerCallWithResponse() { + ServerCall serverCall = CallingServerResponseMocker.getServerCall(new ArrayList>( + Arrays.asList( + new SimpleEntry("", 202) + )) + ); + + Response removeParticipantResultResponse = serverCall.removeParticipantWithResponse(CallingServerResponseMocker.NEW_PARTICIPANT_ID, Context.NONE); + assertEquals(202, removeParticipantResultResponse.getStatusCode()); + } + + @Test + public void removeParticipantServerCallAsync() { + ServerCallAsync serverCall = CallingServerResponseMocker.getServerCallAsync(new ArrayList>( + Arrays.asList( + new SimpleEntry("", 202) + )) + ); + + serverCall.removeParticipant(CallingServerResponseMocker.NEW_PARTICIPANT_ID).block(); + } + + @Test + public void removeParticipantServerCallAsyncWithResponse() { + ServerCallAsync serverCall = CallingServerResponseMocker.getServerCallAsync(new ArrayList>( + Arrays.asList( + new SimpleEntry("", 202) + )) + ); + + Response removeParticipantResultResponse = serverCall.removeParticipantWithResponse(CallingServerResponseMocker.NEW_PARTICIPANT_ID, Context.NONE).block(); + assertEquals(202, removeParticipantResultResponse.getStatusCode()); + } + + private ServerCall getServerCall() { + return CallingServerResponseMocker.getServerCall(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generatePlayAudioResult( + CallingServerResponseMocker.OPERATION_ID, + OperationStatus.COMPLETED, + new ResultInfoInternal().setCode(202).setSubcode(0).setMessage("message")), + 202) + ))); + } + + private ServerCallAsync getServerCallAsync() { + return CallingServerResponseMocker.getServerCallAsync(new ArrayList>( + Arrays.asList( + new SimpleEntry(CallingServerResponseMocker.generatePlayAudioResult( + CallingServerResponseMocker.OPERATION_ID, + OperationStatus.COMPLETED, + new ResultInfoInternal().setCode(202).setSubcode(0).setMessage("message")), + 202) + ))); + } } diff --git a/sdk/communication/azure-communication-callingserver/tests.yml b/sdk/communication/azure-communication-callingserver/tests.yml index afd6c1d779248..27671325fa64a 100644 --- a/sdk/communication/azure-communication-callingserver/tests.yml +++ b/sdk/communication/azure-communication-callingserver/tests.yml @@ -1,8 +1,9 @@ trigger: none stages: - - template: /sdk/communication/communication-tests-template.yml - parameters: - PackageName: azure-communication-callingserver - SafeName: azurecommunicationcallingserver - +- template: /sdk/communication/communication-tests-template.yml + parameters: + PackageName: azure-communication-callingserver + SafeName: azurecommunicationcallingserver + Clouds: 'Public' + TestMode: 'LIVE' diff --git a/sdk/communication/communication-tests-template.yml b/sdk/communication/communication-tests-template.yml index 627005f89d7e4..bddcfbd01e480 100644 --- a/sdk/communication/communication-tests-template.yml +++ b/sdk/communication/communication-tests-template.yml @@ -1,6 +1,9 @@ parameters: PackageName: not-specified - SafeName: notspecified + SafeName: not-specified + Clouds: 'Public,Int' + TestMode: 'LIVE' + EnvVars: {} stages: - template: /eng/pipelines/templates/stages/archetype-sdk-tests.yml @@ -15,7 +18,8 @@ stages: SubscriptionConfigurations: - $(sub-config-communication-int-test-resources-common) - $(sub-config-communication-int-test-resources-java) - Clouds: Public, Int + Clouds: ${{ parameters.Clouds }} + TestMode: ${{ parameters.TestMode }} Artifacts: - name: ${{ parameters.PackageName }} groupId: com.azure @@ -33,4 +37,6 @@ stages: reportDirectory: sdk/communication/${{ parameters.PackageName }}/target/site/jacoco/ failIfCoverageEmpty: true EnvVars: - SKIP_LIVE_TEST: TRUE \ No newline at end of file + SKIP_LIVE_TEST: TRUE + ${{ each var in parameters.EnVars }}: + ${{ var.key }}: ${{ var.value }} \ No newline at end of file