Skip to content

Commit

Permalink
Fix SDK bug. Add UT for Server Call apis.
Browse files Browse the repository at this point in the history
  • Loading branch information
zihzhan-msft committed Sep 10, 2021
1 parent 3f54f92 commit d0d2778
Show file tree
Hide file tree
Showing 13 changed files with 639 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ def add_participant(
alternate_caller_id = None if alternate_caller_id == None else PhoneNumberIdentifierModel(value=alternate_caller_id)

add_participant_request = AddParticipantRequestConverter.convert(
serialize_identifier(participant),
alternate_caller_id,
operation_context
participant = serialize_identifier(participant),
alternate_caller_id = alternate_caller_id,
operation_context = operation_context
)

add_participant_result = self.call_connection_client.add_participant(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def convert(
self,
participant: CommunicationIdentifierModel,
alternate_caller_id: PhoneNumberIdentifierModel = None,
operation_context: str = None
operation_context: str = None,
callback_uri: str = None
): # type: (...) -> AddParticipantRequest

if not participant:
Expand All @@ -73,5 +74,5 @@ def convert(
alternate_caller_id = alternate_caller_id,
participant = participant,
operation_context=operation_context,
callback_uri=None
callback_uri=callback_uri
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from ._generated.aio.operations import ServerCallsOperations
from ._generated.models import PlayAudioRequest, PhoneNumberIdentifierModel
from ._converters import PlayAudioRequestConverter, AddParticipantRequestConverter
from ._models import PlayAudioResult, AddParticipantResult
from ._communication_identifier_serializer import (deserialize_identifier,
serialize_identifier)
Expand All @@ -28,44 +29,23 @@ def __init__(

@distributed_trace()
def play_audio(
self,
self,
audio_file_uri, # type: str
loop, # type: bool
audio_file_id, # type: str
callback_uri, # type: str
operation_context = None, # type: Optional[str]
play_audio_options, # type: PlayAudioOptions
**kwargs, # type: str: Any
): # type: (...) -> PlayAudioResult

try:
if not audio_file_uri.lower().startswith('http'):
audio_file_uri = "https://" + audio_file_uri
except AttributeError:
raise ValueError("URL must be a string.")

if not audio_file_id:
raise ValueError("audio_File_id can not be None")

try:
if not callback_uri.lower().startswith('http'):
callback_uri = "https://" + callback_uri
except AttributeError:
raise ValueError("URL must be a string.")

if not operation_context:
raise ValueError("operation_context can not be None")

request = PlayAudioRequest(
audio_file_uri=audio_file_uri,
loop = False,
operation_context=operation_context,
audio_file_id=audio_file_id,
callback_uri=callback_uri
)
if not audio_file_uri:
raise ValueError("audio_file_uri can not be None")

if not play_audio_options:
raise ValueError("options can not be None")

play_audio_request = PlayAudioRequestConverter.convert(audio_file_uri, play_audio_options)

play_audio_result = self.server_call_client.play_audio(
server_call_id=self.server_call_id,
request=request,
request=play_audio_request,
**kwargs
)

Expand All @@ -84,12 +64,18 @@ def add_participant(
if not participant:
raise ValueError("participant can not be None")

alternate_caller_id = None if alternate_caller_id == None else PhoneNumberIdentifierModel(value=alternate_caller_id)

add_participant_request = AddParticipantRequestConverter.convert(
participant = serialize_identifier(participant),
alternate_caller_id = alternate_caller_id,
operation_context = operation_context,
callback_uri = callback_uri
)

add_participant_result = self.server_call_client.add_participant(
server_call_id=self.server_call_id,
participant=serialize_identifier(participant),
alternate_caller_id=None if alternate_caller_id == None else PhoneNumberIdentifierModel(value=alternate_caller_id.properties['value']),
callback_uri=callback_uri,
operation_context=operation_context,
add_participant_request=add_participant_request,
**kwargs
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ async def add_participant(
alternate_caller_id = None if alternate_caller_id == None else PhoneNumberIdentifierModel(value=alternate_caller_id)

add_participant_request = AddParticipantRequestConverter.convert(
serialize_identifier(participant),
alternate_caller_id,
operation_context
participant = serialize_identifier(participant),
alternate_caller_id = alternate_caller_id,
operation_context = operation_context
)

add_participant_result = await self.call_connection_client.add_participant(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .._generated.aio.operations import ServerCallsOperations
from .._generated.models import PlayAudioRequest, PhoneNumberIdentifierModel
from .._converters import PlayAudioRequestConverter, AddParticipantRequestConverter
from .._models import PlayAudioOptions, PlayAudioResult, AddParticipantResult
from .._communication_identifier_serializer import (deserialize_identifier,
serialize_identifier)
Expand All @@ -30,42 +31,21 @@ def __init__(
async def play_audio(
self,
audio_file_uri, # type: str
loop, # type: bool
audio_file_id, # type: str
callback_uri, # type: str
operation_context = None, # type: Optional[str]
play_audio_options, # type: PlayAudioOptions
**kwargs, # type: str: Any
): # type: (...) -> PlayAudioResult

try:
if not audio_file_uri.lower().startswith('http'):
audio_file_uri = "https://" + audio_file_uri
except AttributeError:
raise ValueError("URL must be a string.")

if not audio_file_id:
raise ValueError("audio_File_id can not be None")

try:
if not callback_uri.lower().startswith('http'):
callback_uri = "https://" + callback_uri
except AttributeError:
raise ValueError("URL must be a string.")

if not operation_context:
raise ValueError("operation_context can not be None")

request = PlayAudioRequest(
audio_file_uri=audio_file_uri,
loop = False,
operation_context=operation_context,
audio_file_id=audio_file_id,
callback_uri=callback_uri
)
if not audio_file_uri:
raise ValueError("audio_file_uri can not be None")

if not play_audio_options:
raise ValueError("options can not be None")

play_audio_request = PlayAudioRequestConverter.convert(audio_file_uri, play_audio_options)

play_audio_result = await self.server_call_client.play_audio(
server_call_id=self.server_call_id,
request=request,
request=play_audio_request,
**kwargs
)

Expand All @@ -84,12 +64,18 @@ async def add_participant(
if not participant:
raise ValueError("participant can not be None")

alternate_caller_id = None if alternate_caller_id == None else PhoneNumberIdentifierModel(value=alternate_caller_id)

add_participant_request = AddParticipantRequestConverter.convert(
participant = serialize_identifier(participant),
alternate_caller_id = alternate_caller_id,
operation_context = operation_context,
callback_uri = callback_uri
)

add_participant_result = await self.server_call_client.add_participant(
server_call_id=self.server_call_id,
participant=serialize_identifier(participant),
alternate_caller_id=None if alternate_caller_id == None else PhoneNumberIdentifierModel(value=alternate_caller_id.properties['value']),
callback_uri=callback_uri,
operation_context=operation_context,
add_participant_request=add_participant_request,
**kwargs
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
RESOURCE_SOURCE = "8:acs:resource_source"
RESOURCE_TARGET = "8:acs:resource_target"
CALL_ID = "cad9df7b-f3ac-4c53-96f7-c76e7437b3c1"
SERVER_CALL_ID = "0d11d342-297f-4584-bb92-13fcbb3ae3bd"
FAKE_ENDPOINT = "https://endpoint"
FAKE_TOKEN = "Fake Token"
CALL_SUBJECT = "testsubject"
Expand All @@ -18,13 +19,14 @@
}

# Common
Phone_Number = "+14255550123"
PHONE_NUMBER = "+14255550123"
OPERATION_CONTEXT = "dummyOperationContext"
ErrorPayload={"msg": "some error"}
ClientType_ConnectionString = "use connectionString"
ClientType_ManagedIdentity = "use managedIdentity"
CALLBACK_URI = "https://bot.contoso.io/callback"
CONNECTION_STRING = "endpoint=https://REDACTED.communication.azure.com/;accesskey=eyJhbG=="
PARTICIPANT_ID = "dummyParticipantId"

# CancelAllMediaOperaions
CancelAllMediaOperaionsResponsePayload = {
Expand Down Expand Up @@ -56,8 +58,5 @@

# AddParticipant
AddParticipantResultPayload = {
"participantId": "dummyparticipantid"
"participantId": PARTICIPANT_ID
}

# RemoveParticipant
ParticipantId = "dummyParticipantId"
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def create_mock_call_connection(call_connection_id, status_code, payload, is_asy
calling_server_client = create_mock_calling_server_client(status_code, payload, is_async, use_managed_identity)
return calling_server_client.get_call_connection(call_connection_id)

def create_mock_server_call(server_call_id, status_code, payload, is_async=False, use_managed_identity=False):
calling_server_client = create_mock_calling_server_client(status_code, payload, is_async, use_managed_identity)
return calling_server_client.initialize_server_call(server_call_id)

def create_mock_calling_server_client(status_code, payload, is_async=False, use_managed_identity=False):
async def async_mock_send(*_, **__):
return _mock_response(status_code=status_code, json_payload=payload)
Expand Down
Loading

0 comments on commit d0d2778

Please sign in to comment.