|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.communication.callingserver; |
| 5 | + |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
| 13 | +import java.util.AbstractMap.SimpleEntry; |
| 14 | + |
| 15 | +import com.azure.communication.callingserver.implementation.models.ResultInfoInternal; |
| 16 | +import com.azure.communication.callingserver.models.AddParticipantResult; |
| 17 | +import com.azure.communication.callingserver.models.CreateCallOptions; |
| 18 | +import com.azure.communication.callingserver.models.EventSubscriptionType; |
| 19 | +import com.azure.communication.callingserver.models.JoinCallOptions; |
| 20 | +import com.azure.communication.callingserver.models.MediaType; |
| 21 | +import com.azure.communication.callingserver.models.OperationStatus; |
| 22 | +import com.azure.communication.callingserver.models.PlayAudioOptions; |
| 23 | +import com.azure.communication.callingserver.models.PlayAudioResult; |
| 24 | +import com.azure.communication.common.CommunicationIdentifier; |
| 25 | +import com.azure.communication.common.CommunicationUserIdentifier; |
| 26 | +import com.azure.core.http.rest.Response; |
| 27 | +import com.azure.core.util.Context; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +public class CallConnectionUnitTests { |
| 32 | + |
| 33 | + static final String CALL_CONNECTION_ID = "callConnectionId"; |
| 34 | + static final String OPERATION_ID = "operationId"; |
| 35 | + static final String NEW_PARTICIPANT_ID = "newParticipantId"; |
| 36 | + |
| 37 | + @Test |
| 38 | + public void createConnectionWithResponse() { |
| 39 | + CallingServerClient callingServerClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList<SimpleEntry<String, Integer>>( |
| 40 | + Arrays.asList( |
| 41 | + new SimpleEntry<String, Integer>(CallingServerResponseMocker.generateCreateCallResult(CallingServerResponseMocker.CALL_CONNECTION_ID), 201) |
| 42 | + ))); |
| 43 | + |
| 44 | + CommunicationUserIdentifier sourceUser = new CommunicationUserIdentifier("id"); |
| 45 | + List<CommunicationIdentifier> targetUsers = new ArrayList<CommunicationIdentifier>(); |
| 46 | + targetUsers.add(new CommunicationUserIdentifier("id2")); |
| 47 | + |
| 48 | + CreateCallOptions options = new CreateCallOptions( |
| 49 | + "serverCallId", |
| 50 | + Collections.singletonList(MediaType.AUDIO), |
| 51 | + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); |
| 52 | + |
| 53 | + Response<CallConnection> callConnectionAsyncResponse = callingServerClient.createCallConnectionWithResponse(sourceUser, targetUsers, options, Context.NONE); |
| 54 | + assertEquals(201, callConnectionAsyncResponse.getStatusCode()); |
| 55 | + assertNotNull(callConnectionAsyncResponse.getValue()); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void getCallConnectionCallingServerClient() { |
| 60 | + CallingServerClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList<SimpleEntry<String, Integer>>( |
| 61 | + Arrays.asList())); |
| 62 | + |
| 63 | + CallConnection callConnection = callingServerAsyncClient.getCallConnection(CallingServerResponseMocker.CALL_CONNECTION_ID); |
| 64 | + assertNotNull(callConnection); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void playAudioWithResponse() { |
| 69 | + CallConnection callConnection = getCallConnection(); |
| 70 | + |
| 71 | + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); |
| 72 | + Response<PlayAudioResult> playAudioResultResponse = callConnection.playAudioWithResponse("audioFileUri", playAudioOptions, Context.NONE); |
| 73 | + assertEquals(202, playAudioResultResponse.getStatusCode()); |
| 74 | + PlayAudioResult playAudioResult = playAudioResultResponse.getValue(); |
| 75 | + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void playAudio() { |
| 80 | + CallConnection callConnection = getCallConnection(); |
| 81 | + |
| 82 | + PlayAudioOptions playAudioOptions = new PlayAudioOptions().setAudioFileId("audioFileId").setCallbackUri("callbackUri"); |
| 83 | + PlayAudioResult playAudioResult = callConnection.playAudio("audioFileUri", playAudioOptions); |
| 84 | + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void playAudioWithoutPlayAudioOptions() { |
| 89 | + CallConnection callConnection = getCallConnection(); |
| 90 | + |
| 91 | + PlayAudioResult playAudioResult = callConnection.playAudio("audioFileUri", false, "audioFieldId", "callbackUri", "operationContext"); |
| 92 | + assertEquals(OperationStatus.COMPLETED, playAudioResult.getStatus()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void appParticipantWithResponse() { |
| 97 | + CallConnection callConnection = getAddParticipantCallConnection(); |
| 98 | + |
| 99 | + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); |
| 100 | + Response<AddParticipantResult> addParticipantResultResponse = callConnection.addParticipantWithResponse(user, "alternateCallerId", "operationContext", Context.NONE); |
| 101 | + assertEquals(202, addParticipantResultResponse.getStatusCode()); |
| 102 | + AddParticipantResult addParticipantResult = addParticipantResultResponse.getValue(); |
| 103 | + assertEquals(user.getId(), addParticipantResult.getParticipantId()); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void appParticipant() { |
| 108 | + CallConnection callConnection = getAddParticipantCallConnection(); |
| 109 | + |
| 110 | + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); |
| 111 | + AddParticipantResult addParticipantResult = callConnection.addParticipant(user, "alternateCallerId", "operationContext"); |
| 112 | + assertEquals(user.getId(), addParticipantResult.getParticipantId()); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void joinCall() { |
| 117 | + CallingServerClient callingServerClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList<SimpleEntry<String, Integer>>( |
| 118 | + Arrays.asList( |
| 119 | + new SimpleEntry<String, Integer>(CallingServerResponseMocker.generateJoinCallResult(NEW_PARTICIPANT_ID), 202) |
| 120 | + ))); |
| 121 | + |
| 122 | + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); |
| 123 | + JoinCallOptions options = new JoinCallOptions( |
| 124 | + CallingServerResponseMocker.URI_CALLBACK, |
| 125 | + Collections.singletonList(MediaType.VIDEO), |
| 126 | + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); |
| 127 | + CallConnection callConnection = callingServerClient.joinCall(CallingServerResponseMocker.SERVER_CALL_ID, (CommunicationIdentifier) user, options); |
| 128 | + assertNotNull(callConnection); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void joinCallWithResponse() { |
| 133 | + CallingServerClient callingServerAsyncClient = CallingServerResponseMocker.getCallingServerClient(new ArrayList<SimpleEntry<String, Integer>>( |
| 134 | + Arrays.asList( |
| 135 | + new SimpleEntry<String, Integer>(CallingServerResponseMocker.generateJoinCallResult(NEW_PARTICIPANT_ID), 202) |
| 136 | + ))); |
| 137 | + |
| 138 | + CommunicationUserIdentifier user = new CommunicationUserIdentifier(NEW_PARTICIPANT_ID); |
| 139 | + JoinCallOptions options = new JoinCallOptions( |
| 140 | + CallingServerResponseMocker.URI_CALLBACK, |
| 141 | + Collections.singletonList(MediaType.VIDEO), |
| 142 | + Collections.singletonList(EventSubscriptionType.PARTICIPANTS_UPDATED)); |
| 143 | + Response<CallConnection> callConnectionResponse = callingServerAsyncClient.joinCallWithResponse(CallingServerResponseMocker.SERVER_CALL_ID, (CommunicationIdentifier) user, options, Context.NONE); |
| 144 | + assertEquals(202, callConnectionResponse.getStatusCode()); |
| 145 | + assertNotNull(callConnectionResponse.getValue()); |
| 146 | + } |
| 147 | + |
| 148 | + private CallConnection getCallConnection() { |
| 149 | + return CallingServerResponseMocker.getCallConnection(new ArrayList<SimpleEntry<String, Integer>>( |
| 150 | + Arrays.asList( |
| 151 | + new SimpleEntry<String, Integer>(CallingServerResponseMocker.generateCreateCallResult(CALL_CONNECTION_ID), 201), |
| 152 | + new SimpleEntry<String, Integer>(CallingServerResponseMocker.generatePlayAudioResult( |
| 153 | + OPERATION_ID, |
| 154 | + OperationStatus.COMPLETED, |
| 155 | + new ResultInfoInternal().setCode(202).setSubcode(0).setMessage("message")), |
| 156 | + 202) |
| 157 | + ))); |
| 158 | + } |
| 159 | + |
| 160 | + private CallConnection getAddParticipantCallConnection() { |
| 161 | + return CallingServerResponseMocker.getCallConnection(new ArrayList<SimpleEntry<String, Integer>>( |
| 162 | + Arrays.asList( |
| 163 | + new SimpleEntry<String, Integer>(CallingServerResponseMocker.generateCreateCallResult(CALL_CONNECTION_ID), 201), |
| 164 | + new SimpleEntry<String, Integer>(CallingServerResponseMocker.generateAddParticipantResult(NEW_PARTICIPANT_ID), 202) |
| 165 | + ))); |
| 166 | + } |
| 167 | +} |
0 commit comments