def start_listening_to_rabbit_queue(queue, on_message_callback, timeout=60): rabbit = RabbitContext(queue_name=queue) connection = rabbit.open_connection() connection.call_later( delay=timeout, callback=functools.partial(_timeout_callback, rabbit)) rabbit.channel.basic_consume( queue=queue, on_message_callback=on_message_callback) rabbit.channel.start_consuming()
def uninvalidate_address(context): context.bulk_uninvalidated_addresses = [] for invalid_address in context.case_created_events: invalid = {'CASE_ID': invalid_address['payload']['collectionCase']['id']} context.bulk_uninvalidated_addresses.append(invalid) message = json.dumps( { "event": { "type": "RM_UNINVALIDATE_ADDRESS", "source": "TESTY TEST", "channel": "AR", "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "rmUnInvalidateAddress": { "caseId": invalid_address['payload']['collectionCase']['id'] } } } ) with RabbitContext(exchange='') as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_UNINVALIDATE_ADDRESS_QUEUE)
def create_uac_fulfilment_message(context, fulfilment_code): requests.get(f'{Config.NOTIFY_STUB_SERVICE}/reset') context.fulfilment_requested_case_id = context.uac_created_events[0][ 'payload']['uac']['caseId'] context.individual_case_id = str(uuid.uuid4()) message = json.dumps({ "event": { "type": "FULFILMENT_REQUESTED", "source": "CONTACT_CENTRE_API", "channel": "CC", "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "fulfilmentRequest": { "fulfilmentCode": fulfilment_code, "caseId": context.fulfilment_requested_case_id, "individualCaseId": context.individual_case_id, "address": {}, "contact": { "telNo": "01234567", } } } }) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_FULFILMENT_REQUESTED_ROUTING_KEY)
def send_multiple_print_fulfilment_requests(context, fulfilment_code): context.print_cases = [ caze['payload']['collectionCase'] for caze in context.case_created_events ] messages = [] for caze in context.print_cases: messages.append( json.dumps({ "event": { "type": "FULFILMENT_REQUESTED", "source": "CONTACT_CENTRE_API", "channel": "CC", "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "fulfilmentRequest": { "fulfilmentCode": fulfilment_code, "caseId": caze['id'], "contact": { "title": "Mrs", "forename": "Test", "surname": "McTest" } } } })) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: for message in messages: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_FULFILMENT_REQUESTED_ROUTING_KEY)
def send_survey_launched_msg(case_id, qid, source="CONTACT_CENTRE_API", channel="CC"): message = json.dumps({ "event": { "type": "SURVEY_LAUNCHED", "source": source, "channel": channel, "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "c45de4dc-3c3b-11e9-b210-d663bd873d93" }, "payload": { "response": { "questionnaireId": qid, "caseId": case_id, "agentId": "cc_000351" } } }) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_SURVEY_LAUNCHED_ROUTING_KEY)
def _send_individual_hh_questionnaire_linked_msg_to_rabbit( questionnaire_id, case_id, individual_case_id=None): questionnaire_linked_message = { 'event': { 'type': 'QUESTIONNAIRE_LINKED', 'source': 'FIELDWORK_GATEWAY', 'channel': 'FIELD', "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "c45de4dc-3c3b-11e9-b210-d663bd873d93" }, 'payload': { 'uac': { "caseId": case_id, 'questionnaireId': questionnaire_id, } } } if individual_case_id: questionnaire_linked_message['payload']['uac'][ 'individualCaseId'] = str(individual_case_id) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=json.dumps(questionnaire_linked_message), content_type='application/json', routing_key=Config.RABBITMQ_QUESTIONNAIRE_LINKED_ROUTING_KEY)
def send_unaddressed_message(context, questionnaire_type): context.expected_questionnaire_type = questionnaire_type with RabbitContext( queue_name=Config.RABBITMQ_UNADDRESSED_REQUEST_QUEUE) as rabbit: rabbit.publish_message(message=json.dumps( {'questionnaireType': questionnaire_type}), content_type='application/json')
def non_compliance_msg_sent(context): context.non_compliance_case_id = context.case_created_events[0]['payload']['collectionCase']['id'] message = json.dumps( { "event": { "type": "SELECTED_FOR_NON_COMPLIANCE", "source": "NON_COMPLIANCE", "channel": "NC", "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "collectionCase": { "id": context.non_compliance_case_id, "nonComplianceStatus": 'NCF', } } } ) with RabbitContext(exchange='') as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_NONCOMPLIANCE_QUEUE)
def new_address_reported_event_with_minimal_fields(context, sender): context.case_id = str(uuid.uuid4()) context.collection_exercise_id = str(uuid.uuid4()) context.sourceCaseId = str(context.case_created_events[0]['payload']['collectionCase']['id']) message = json.dumps( { "event": { "type": "NEW_ADDRESS_REPORTED", "source": "FIELDWORK_GATEWAY", "channel": sender, "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "d9126d67-2830-4aac-8e52-47fb8f84d3b9" }, "payload": { "newAddress": { "sourceCaseId": context.sourceCaseId, "collectionCase": { "id": context.case_id, "caseType": "SPG", "address": { "region": "E00001234", "addressType": "SPG", "addressLevel": "U" } } } } } ) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_ADDRESS_ROUTING_KEY)
def new_address_reported_event_for_address_type_and_region(context, address_type, sender, address_level, region): context.case_id = str(uuid.uuid4()) message = json.dumps( { "event": { "type": "NEW_ADDRESS_REPORTED", "source": "FIELDWORK_GATEWAY", "channel": sender, "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "d9126d67-2830-4aac-8e52-47fb8f84d3b9" }, "payload": { "newAddress": { "collectionCase": { "id": context.case_id, "caseType": address_type, "address": { "region": region, "addressType": address_type, "addressLevel": address_level } } } } } ) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_ADDRESS_ROUTING_KEY) check_case_created_message_is_emitted(context)
def send_print_fulfilment_request(context, fulfilment_code): context.first_case = get_first_case(context) message = json.dumps({ "event": { "type": "FULFILMENT_REQUESTED", "source": "CONTACT_CENTRE_API", "channel": "CC", "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "fulfilmentRequest": { "fulfilmentCode": fulfilment_code, "caseId": context.first_case['id'], "contact": { "title": "Mrs", "forename": "Test", "surname": "McTest" } } } }) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_FULFILMENT_REQUESTED_ROUTING_KEY)
def an_rm_address_update_message_is_sent(context): rm_case_updated = { "caseId": context.first_case['id'], "treatmentCode": "TEST TreatmentCode CODE", "estabType": "ROYAL HOUSEHOLD", "oa": "TEST Oa CODE", "lsoa": "TEST Lsoa CODE", "msoa": "TEST Msoa CODE", "lad": "TEST Lad CODE", "fieldCoordinatorId": "TEST FieldCoordinatorId CODE", "fieldOfficerId": "TEST FieldOfficerId CODE", "latitude": "123.456", "longitude": "000.000" } message = json.dumps({ "event": { "type": "RM_CASE_UPDATED", "source": "ACCEPTANCE_TEST", "channel": "TEST", "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": str(uuid.uuid4()) }, "payload": { "rmCaseUpdated": rm_case_updated } }) context.rm_case_updated = rm_case_updated with RabbitContext(exchange='') as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.BULK_ADDRESS_UPDATE_ROUTING_KEY)
def check_no_msgs_sent_to_queue(context, queue, on_message_callback, timeout=5): context.messages_received = [] rabbit = RabbitContext(queue_name=queue) connection = rabbit.open_connection() connection.call_later( delay=timeout, callback=functools.partial(_timeout_callback_expected, rabbit)) rabbit.channel.basic_consume( queue=queue, on_message_callback=on_message_callback) rabbit.channel.start_consuming() if len(context.messages_received) > 0: test_helper.fail(f'Expected no messages on the queue {queue}, found {len(context.messages_received)}' f', message(s): {context.messages_received}')
def deactivate_uac_(context): context.bulk_deactivate_uac = [] for uac_updated in context.uac_created_events: context.bulk_deactivate_uac.append(uac_updated['payload']['uac']['questionnaireId']) message = json.dumps( { "event": { "type": "DEACTIVATE_UAC", "source": "TESTY TEST", "channel": "AR", "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "uac": { "questionnaireId": uac_updated['payload']['uac']['questionnaireId'], } } } ) with RabbitContext(exchange='') as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_DEACTIVATE_UAC_QUEUE)
def _send_ccs_case_list_msg_to_rabbit(message): json_message = json.dumps(message) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=json_message, content_type='application/json', routing_key=Config.RABBITMQ_CCS_PROPERTY_LISTING_ROUTING_KEY)
def _get_second_qid(context, qid_needed, questionnaire_type): if qid_needed == 'True': context.first_case = context.case_created_events[0]['payload']['collectionCase'] with RabbitContext(queue_name=Config.RABBITMQ_UNADDRESSED_REQUEST_QUEUE) as rabbit: rabbit.publish_message( message=json.dumps({'questionnaireType': questionnaire_type}), content_type='application/json') listen_for_ad_hoc_uac_updated_message(context, questionnaire_type) send_questionnaire_linked_msg_to_rabbit(context.requested_qid, context.first_case['id']) _get_emitted_uac(context) # Throw away the linked message - we don't need it and it breaks subsequent steps context.qid_to_receipt = context.requested_qid
def ce_new_address_reported_event_with_source_case_id_and_secureType_true(context, sender, secure_type): context.case_id = str(uuid.uuid4()) context.collection_exercise_id = str(uuid.uuid4()) context.first_case = context.case_created_events[0]['payload']['collectionCase'] context.sourceCaseId = str(context.first_case['id']) message = json.dumps( { "event": { "type": "NEW_ADDRESS_REPORTED", "source": "FIELDWORK_GATEWAY", "channel": sender, "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "d9126d67-2830-4aac-8e52-47fb8f84d3b9" }, "payload": { "newAddress": { "sourceCaseId": context.sourceCaseId, "collectionCase": { "id": context.case_id, "caseType": "CE", "survey": "CENSUS", "fieldCoordinatorId": "SO_23", "fieldOfficerId": "SO_23_123", "collectionExerciseId": context.collection_exercise_id, "ceExpectedCapacity": 5, "address": { "addressLine1": "123", "addressLine2": "Fake caravan park", "addressLine3": "The long road", "townName": "Trumpton", "postcode": "SO190PG", "region": "E00001234", "addressType": "CE", "addressLevel": "E", "latitude": "50.917428", "longitude": "-1.238193", "estabType": "HOSPITAL", "secureType": secure_type, "uprn": "1214242" } } } } } ) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_ADDRESS_ROUTING_KEY)
def create_individual_uac_sms_fulfilment_message_without_ind_case_id( context, fulfilment_code): requests.get(f'{Config.NOTIFY_STUB_SERVICE}/reset') context.first_case = get_first_case(context) context.fulfilment_requested_case_id = context.uac_created_events[0][ 'payload']['uac']['caseId'] message = json.dumps({ "event": { "type": "FULFILMENT_REQUESTED", "source": "CONTACT_CENTRE_API", "channel": "CC", "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "fulfilmentRequest": { "fulfilmentCode": fulfilment_code, "caseId": context.fulfilment_requested_case_id, "individualCaseId": None, "address": { "addressLine1": "1 main street", "addressLine2": "upper upperingham", "addressLine3": "", "townName": "upton", "postcode": "UP103UP", "region": "E", "latitude": "50.863849", "longitude": "-1.229710", "uprn": "XXXXXXXXXXXXX", "addressType": "CE", "estabType": "XXX" }, "contact": { "title": "Ms", "forename": "jo", "surname": "smith", "email": "*****@*****.**", "telNo": "01234567" } } } }) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_FULFILMENT_REQUESTED_ROUTING_KEY)
def new_address_reported_event_without_source_case_id_with_address_type(context, sender, region, address_type, address_level): context.case_id = str(uuid.uuid4()) context.collection_exercise_id = str(uuid.uuid4()) message = json.dumps( { "event": { "type": "NEW_ADDRESS_REPORTED", "source": "FIELDWORK_GATEWAY", "channel": sender, "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "d9126d67-2830-4aac-8e52-47fb8f84d3b9" }, "payload": { "newAddress": { "sourceCaseId": None, "collectionCase": { "id": context.case_id, "caseType": address_type, "survey": "CENSUS", "fieldCoordinatorId": "SO_23", "fieldOfficerId": "SO_23_123", "collectionExerciseId": context.collection_exercise_id, "address": { "addressLine1": "123", "addressLine2": "Fake caravan park", "addressLine3": "The long road", "townName": "Trumpton", "postcode": "SO190PG", "region": region, "addressType": address_type, "addressLevel": address_level, "latitude": "50.917428", "longitude": "-1.238193" } } } } } ) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_ADDRESS_ROUTING_KEY) check_case_created_message_is_emitted(context)
def send_address_modified_event(context): message = json.dumps( { "event": { "type": "ADDRESS_MODIFIED", "source": "CONTACT_CENTRE_API", "channel": "CC", "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "c45de4dc-3c3b-11e9-b210-d663bd873d93" }, "payload": { "addressModification": { "collectionCase": { "id": str(context.case_created_events[0]['payload']['collectionCase']['id']), }, "originalAddress": { "addressLine1": "nope", "addressLine2": "nope", "addressLine3": "nope", "townName": "nope", "postcode": "nope", "region": "nope", "uprn": "nope", "estabType": "nope", "organisationName": "nope" }, "newAddress": { "addressLine1": "1a main street", "addressLine2": "upper upperingham", "addressLine3": "thingy", "organisationName": "Bedlam", "estabType": "HOSPITAL" } } } } ) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_ADDRESS_ROUTING_KEY)
def publish_bad_message(context): message_hashes = [] for queue in Config.RABBITMQ_QUEUES_WITH_DLQS: message = json.dumps( { "message": "This is a dodgy message", "queueName": queue, "uniqueness": str(uuid.uuid4()) } ) with RabbitContext(queue_name=queue) as rabbit: rabbit.publish_message( message=message, content_type='application/json') message_hashes.append(hashlib.sha256(message.encode('utf-8')).hexdigest()) context.message_hashes = message_hashes
def _send_refusal_msg_to_rabbit(case_id, refusal_type, channel): message = json.dumps({ "event": { "type": "REFUSAL_RECEIVED", "source": "CONTACT_CENTRE_API", "channel": channel, "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "refusal": { "type": refusal_type, "agentId": "110001", "callId": "8f04b136-d13c-4d88-9068-331560a26bec", "isHouseholder": True, "collectionCase": { "id": case_id }, "contact": { "title": "LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tCmhRSU1BN0dwbm5rWFFIVytBUS8rTDZyLzZr", "forename": "WFFIVytBUS8rTDZyLzZrWHRMVnNwN3d1elE1enRrd3h5c", "surname": "DhqVmo3b1dyL0pWNldsS1FqZWYKdGZrWU04MUIzSy9zdFBzaU9JSVYrVVFKSG1" }, "address": { "addressLine1": "123", "addressLine2": "Fake Street", "addressLine3": "", "townName": "Test Town", "postcode": "XX1 XX1", "region": "W", "uprn": "123456789143" } } } }) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message(message=message, content_type='application/json', routing_key=Config.RABBITMQ_REFUSAL_ROUTING_KEY)
def send_invalid_address_message_to_rabbit(case_id, sender): message = json.dumps({ "event": { "type": "ADDRESS_NOT_VALID", "source": "FIELDWORK_GATEWAY", "channel": sender, "dateTime": "2019-07-07T22:37:11.988+0000", "transactionId": "d2541acb-230a-4ade-8123-eee2310c9143" }, "payload": { "invalidAddress": { "reason": "DEMOLISHED", "collectionCase": { "id": case_id } } } }) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message(message=message, content_type='application/json', routing_key=Config.RABBITMQ_ADDRESS_ROUTING_KEY)
def send_field_case_updated_message(context, expected_capacity): case_id = context.first_case['id'] message = json.dumps({ "event": { "type": "FIELD_CASE_UPDATED", "source": "FIELDWORK_GATEWAY", "channel": "FIELD", "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "c45de4dc-3c3b-11e9-b210-d663bd873d93" }, "payload": { "collectionCase": { "id": case_id, "ceExpectedCapacity": expected_capacity } } }) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_FIELD_CASE_UPDATED_ROUTING_KEY)
def address_type_changed_event_is_sent(context, type): context.old_case = context.case_created_events[0]['payload']['collectionCase'] context.new_case_id = context.case_id = str(uuid.uuid4()) context.event = { "event": { "type": "ADDRESS_TYPE_CHANGED", "source": "FIELDWORK_GATEWAY", "channel": "FIELD", "dateTime": "2011-08-12T20:17:46.384Z", "transactionId": "c45de4dc-3c3b-11e9-b210-d663bd873d93" }, "payload": { "addressTypeChange": { "newCaseId": context.new_case_id, "collectionCase": { "id": str(context.old_case["id"]), "ceExpectedCapacity": "20", "address": { "addressType": type, "organisationName": "The Grand Budapest Hotel", "estabType": "HOTEL", "addressLine1": "Testy test", "addressLine2": "Main Street", "addressLine3": "" } } } } } message = json.dumps(context.event) with RabbitContext(exchange=Config.RABBITMQ_EVENT_EXCHANGE) as rabbit: rabbit.publish_message( message=message, content_type='application/json', routing_key=Config.RABBITMQ_ADDRESS_ROUTING_KEY)
def purge_queues(): all_queues = _get_all_queues() with RabbitContext() as rabbit: for queue in all_queues: rabbit.channel.queue_purge(queue=queue)