def test_ignores_duplicate_request_id(self, stage, op, iot_response, unhandled_error_handler): operation_flow.pass_event_to_previous_stage(stage.next, iot_response) assert_callback_succeeded(op=op) op.callback.reset_mock() operation_flow.pass_event_to_previous_stage(stage.next, iot_response) assert op.callback.call_count == 0 assert unhandled_error_handler.call_count == 0
def test_ignores_request_id_from_failure(self, stage, op, mocker, unhandled_error_handler): stage.next._execute_op = mocker.MagicMock(side_effect=Exception) stage.run_op(op) req = stage.next.run_op.call_args[0][0] resp = pipeline_events_base.IotResponseEvent( request_id=req.request_id, status_code=fake_status_code, response_body=fake_response_body, ) op.callback.reset_mock() operation_flow.pass_event_to_previous_stage(stage.next, resp) assert op.callback.call_count == 0 assert unhandled_error_handler.call_count == 0
def _handle_pipeline_event(self, event): """ Pipeline Event handler function to convert incoming MQTT messages into the appropriate DPS events, based on the topic of the message """ if isinstance(event, pipeline_events_mqtt.IncomingMQTTMessageEvent): topic = event.topic if mqtt_topic.is_dps_response_topic(topic): logger.info( "Received payload:{payload} on topic:{topic}".format( payload=event.payload, topic=topic)) key_values = mqtt_topic.extract_properties_from_topic(topic) status_code = mqtt_topic.extract_status_code_from_topic(topic) request_id = key_values["rid"][0] if event.payload is not None: response = event.payload.decode("utf-8") # Extract pertinent information from mqtt topic # like status code request_id and send it upwards. operation_flow.pass_event_to_previous_stage( self, pipeline_events_provisioning.RegistrationResponseEvent( request_id, status_code, key_values, response), ) else: logger.warning( "Unknown topic: {} passing up to next handler".format( topic)) operation_flow.pass_event_to_previous_stage(self, event) else: # all other messages get passed up operation_flow.pass_event_to_previous_stage(self, event)
def test_ignores_unknown_request_id(self, stage, op, iot_response, unhandled_error_handler): iot_response.request_id = fake_request_id operation_flow.pass_event_to_previous_stage(stage.next, iot_response) assert op.callback.call_count == 0 assert unhandled_error_handler.call_count == 0
def test_matching_request_id_with_no_previous_stage( self, stage, op, iot_response, unhandled_error_handler): stage.next.previous = None operation_flow.pass_event_to_previous_stage(stage.next, iot_response) assert unhandled_error_handler.call_count == 1
def test_completes_op_with_matching_request_id(self, stage, op, iot_response): operation_flow.pass_event_to_previous_stage(stage.next, iot_response) assert_callback_succeeded(op=op) assert op.status_code == iot_response.status_code assert op.response_body == iot_response.response_body
def _handle_pipeline_event(self, event): """ Pipeline Event handler function to convert incoming MQTT messages into the appropriate IoTHub events, based on the topic of the message """ if isinstance(event, pipeline_events_mqtt.IncomingMQTTMessageEvent): topic = event.topic if mqtt_topic_iothub.is_c2d_topic(topic, self.device_id): message = Message(event.payload) mqtt_topic_iothub.extract_properties_from_topic(topic, message) operation_flow.pass_event_to_previous_stage( self, pipeline_events_iothub.C2DMessageEvent(message)) elif mqtt_topic_iothub.is_input_topic(topic, self.device_id, self.module_id): message = Message(event.payload) mqtt_topic_iothub.extract_properties_from_topic(topic, message) input_name = mqtt_topic_iothub.get_input_name_from_topic(topic) operation_flow.pass_event_to_previous_stage( self, pipeline_events_iothub.InputMessageEvent( input_name, message)) elif mqtt_topic_iothub.is_method_topic(topic): request_id = mqtt_topic_iothub.get_method_request_id_from_topic( topic) method_name = mqtt_topic_iothub.get_method_name_from_topic( topic) method_received = MethodRequest( request_id=request_id, name=method_name, payload=json.loads(event.payload.decode("utf-8")), ) operation_flow.pass_event_to_previous_stage( self, pipeline_events_iothub.MethodRequestEvent(method_received)) elif mqtt_topic_iothub.is_twin_response_topic(topic): request_id = mqtt_topic_iothub.get_twin_request_id_from_topic( topic) status_code = int( mqtt_topic_iothub.get_twin_status_code_from_topic(topic)) operation_flow.pass_event_to_previous_stage( self, pipeline_events_base.IotResponseEvent( request_id=request_id, status_code=status_code, response_body=event.payload), ) elif mqtt_topic_iothub.is_twin_desired_property_patch_topic(topic): operation_flow.pass_event_to_previous_stage( self, pipeline_events_iothub.TwinDesiredPropertiesPatchEvent( patch=json.loads(event.payload.decode("utf-8"))), ) else: logger.debug( "Uunknown topic: {} passing up to next handler".format( topic)) operation_flow.pass_event_to_previous_stage(self, event) else: # all other messages get passed up operation_flow.pass_event_to_previous_stage(self, event)