|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import unittest |
| 5 | +from typing import List |
| 6 | +from botbuilder.core import BotAdapter, TurnContext |
| 7 | +from botbuilder.schema import Activity, ConversationReference, ResourceResponse |
| 8 | + |
| 9 | + |
| 10 | +class SimpleAdapter(BotAdapter): |
| 11 | + # pylint: disable=unused-argument |
| 12 | + |
| 13 | + def __init__(self, call_on_send=None, call_on_update=None, call_on_delete=None): |
| 14 | + super(SimpleAdapter, self).__init__() |
| 15 | + self.test_aux = unittest.TestCase("__init__") |
| 16 | + self._call_on_send = call_on_send |
| 17 | + self._call_on_update = call_on_update |
| 18 | + self._call_on_delete = call_on_delete |
| 19 | + |
| 20 | + async def delete_activity( |
| 21 | + self, context: TurnContext, reference: ConversationReference |
| 22 | + ): |
| 23 | + self.test_aux.assertIsNotNone( |
| 24 | + reference,
8000
"SimpleAdapter.delete_activity: missing reference" |
| 25 | + ) |
| 26 | + if self._call_on_delete is not None: |
| 27 | + self._call_on_delete(reference) |
| 28 | + |
| 29 | + async def send_activities( |
| 30 | + self, context: TurnContext, activities: List[Activity] |
| 31 | + ) -> List[ResourceResponse]: |
| 32 | + self.test_aux.assertIsNotNone( |
| 33 | + activities, "SimpleAdapter.delete_activity: missing reference" |
| 34 | + ) |
| 35 | + self.test_aux.assertTrue( |
| 36 | + len(activities) > 0, |
| 37 | + "SimpleAdapter.send_activities: empty activities array.", |
| 38 | + ) |
| 39 | + |
| 40 | + if self._call_on_send is not None: |
| 41 | + self._call_on_send(activities) |
| 42 | + responses = [] |
| 43 | + |
| 44 | + for activity in activities: |
| 45 | + responses.append(ResourceResponse(id=activity.id)) |
| 46 | + |
| 47 | + return responses |
| 48 | + |
| 49 | + async def update_activity(self, context: TurnContext, activity: Activity): |
| 50 | + self.test_aux.assertIsNotNone( |
| 51 | + activity, "SimpleAdapter.update_activity: missing activity" |
| 52 | + ) |
| 53 | + if self._call_on_update is not None: |
| 54 | + self._call_on_update(activity) |
| 55 | + |
| 56 | + return ResourceResponse(activity.id) |
| 57 | + |
| 58 | + async def process_request(self, activity, handler): |
| 59 | + context = TurnContext(self, activity) |
| 60 | + return self.run_pipeline(context, handler) |
0 commit comments