def test_event_detection(self): proc1 = self.container.proc_manager.procs_by_name['user_notification'] # Create a user and get the user_id user = UserInfo(name='new_user') user_id, _ = self.rrc.create(user) # Create detection notification dfilt = DetectionFilterConfig() dfilt.processing['condition'] = 5 dfilt.processing['comparator'] = '>' dfilt.processing['filter_field'] = 'voltage' dfilt.delivery['message'] = 'I got my detection event!' notification_id = self.unsc.create_detection_filter( event_type='ExampleDetectableEvent', event_subtype=None, origin='Some_Resource_Agent_ID1', origin_type=None, user_id=user_id, filter_config=dfilt) #--------------------------------------------------------------------------------- # Create event subscription for resulting detection event #--------------------------------------------------------------------------------- # Create an email notification so that when the DetectionEventProcessor # detects an event and fires its own output event, this will caught by an # EmailEventProcessor and an email will be sent to the user notification_id_2 = self.unsc.create_email( event_type='DetectionEvent', event_subtype=None, origin='DetectionEventProcessor', origin_type=None, user_id=user_id, email='*****@*****.**', mode=DeliveryMode.UNFILTERED, message_header='Detection event', parser='parser', period=1) # Send event that is not detected # publish an event for each notification to generate the emails rle_publisher = EventPublisher("ExampleDetectableEvent") # since the voltage field in this event is less than 5, it will not be detected rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage=3) # Check at the end of the test to make sure this event never triggered a Detectable! # Send Event that is detected # publish an event for each notification to generate the emails # since the voltage field in this event is greater than 5, it WILL be detected rle_publisher = EventPublisher("ExampleDetectableEvent") rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage=10) #------------------------------------------------------- # make assertions #------------------------------------------------------- msg_tuple = proc1.event_processors[ notification_id_2].smtp_client.sentmail.get(timeout=4) # The first event never triggered an email because the voltage was less than 5, the queue is now empty self.assertTrue(proc1.event_processors[notification_id_2].smtp_client. sentmail.empty()) self.assertEquals(msg_tuple[1], '*****@*****.**') #self.assertEquals(msg_tuple[0], ION_NOTIFICATION_EMAIL_ADDRESS) # parse the message body message = msg_tuple[2] list_lines = message.split("\n") message_dict = {} for line in list_lines: key_item = line.split(": ") if key_item[0] == 'Subject': message_dict['Subject'] = key_item[1] + key_item[2] else: try: message_dict[key_item[0]] = key_item[1] except IndexError as exc: # these IndexError exceptions happen only because the message sometimes # has successive /r/n (i.e. new lines) and therefore, # the indexing goes out of range. These new lines # can just be ignored. So we ignore the exceptions here. pass #self.assertEquals(message_dict['From'], ION_NOTIFICATION_EMAIL_ADDRESS) self.assertEquals(message_dict['To'], '*****@*****.**') self.assertEquals(message_dict['Event'].rstrip('\r'), 'DetectionEvent') self.assertEquals(message_dict['Originator'].rstrip('\r'), 'DetectionEventProcessor') self.assertEquals(message_dict['Description'].rstrip('\r'), 'Event was detected by DetectionEventProcessor')
def test_event_detection(self): proc1 = self.container.proc_manager.procs_by_name['user_notification'] # Create a user and get the user_id user = UserInfo(name = 'new_user') user_id, _ = self.rrc.create(user) # Create detection notification dfilt = DetectionFilterConfig() dfilt.processing['condition'] = 5 dfilt.processing['comparator'] = '>' dfilt.processing['filter_field'] = 'voltage' dfilt.delivery['message'] = 'I got my detection event!' notification_id = self.unsc.create_detection_filter(event_type='ExampleDetectableEvent', event_subtype=None, origin='Some_Resource_Agent_ID1', origin_type=None, user_id=user_id, filter_config=dfilt ) #--------------------------------------------------------------------------------- # Create event subscription for resulting detection event #--------------------------------------------------------------------------------- # Create an email notification so that when the DetectionEventProcessor # detects an event and fires its own output event, this will caught by an # EmailEventProcessor and an email will be sent to the user notification_id_2 = self.unsc.create_email(event_type='DetectionEvent', event_subtype=None, origin='DetectionEventProcessor', origin_type=None, user_id=user_id, email='*****@*****.**', mode = DeliveryMode.UNFILTERED, message_header='Detection event', parser='parser', period=1) # Send event that is not detected # publish an event for each notification to generate the emails rle_publisher = EventPublisher("ExampleDetectableEvent") # since the voltage field in this event is less than 5, it will not be detected rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage = 3) # Check at the end of the test to make sure this event never triggered a Detectable! # Send Event that is detected # publish an event for each notification to generate the emails # since the voltage field in this event is greater than 5, it WILL be detected rle_publisher = EventPublisher("ExampleDetectableEvent") rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage = 10) #------------------------------------------------------- # make assertions #------------------------------------------------------- msg_tuple = proc1.event_processors[notification_id_2].smtp_client.sentmail.get(timeout=4) # The first event never triggered an email because the voltage was less than 5, the queue is now empty self.assertTrue(proc1.event_processors[notification_id_2].smtp_client.sentmail.empty()) self.assertEquals(msg_tuple[1], '*****@*****.**' ) #self.assertEquals(msg_tuple[0], ION_NOTIFICATION_EMAIL_ADDRESS) # parse the message body message = msg_tuple[2] list_lines = message.split("\n") message_dict = {} for line in list_lines: key_item = line.split(": ") if key_item[0] == 'Subject': message_dict['Subject'] = key_item[1] + key_item[2] else: try: message_dict[key_item[0]] = key_item[1] except IndexError as exc: # these IndexError exceptions happen only because the message sometimes # has successive /r/n (i.e. new lines) and therefore, # the indexing goes out of range. These new lines # can just be ignored. So we ignore the exceptions here. pass #self.assertEquals(message_dict['From'], ION_NOTIFICATION_EMAIL_ADDRESS) self.assertEquals(message_dict['To'], '*****@*****.**') self.assertEquals(message_dict['Event'].rstrip('\r'), 'DetectionEvent') self.assertEquals(message_dict['Originator'].rstrip('\r'), 'DetectionEventProcessor') self.assertEquals(message_dict['Description'].rstrip('\r'), 'Event was detected by DetectionEventProcessor')
def test_event_detection(self): proc1 = self.container.proc_manager.procs_by_name['user_notification'] # Create a user and get the user_id user = UserInfo(name = 'new_user') user_id, _ = self.rrc.create(user) # Create detection notification dfilt = DetectionFilterConfig() field = 'voltage' lower_bound = 5 upper_bound = 10 instrument = 'instrument_1' search_string1 = "SEARCH '%s' VALUES FROM %s TO %s FROM '%s'" \ % (field, lower_bound, upper_bound, instrument) field = 'voltage' value = 15 instrument = 'instrument_2' search_string2 = "or search '%s' is '%s' from '%s'" % (field, value, instrument) field = 'voltage' lower_bound = 8 upper_bound = 14 instrument = 'instrument_3' search_string3 = "and SEARCH '%s' VALUES FROM %s TO %s FROM '%s'" \ % (field, lower_bound, upper_bound, instrument) dfilt.processing['search_string'] = search_string1 + search_string2 + search_string3 dfilt.delivery['message'] = 'I got my detection event!' notification_id = self.unsc.create_detection_filter(event_type='ExampleDetectableEvent', event_subtype=None, origin='Some_Resource_Agent_ID1', origin_type=None, user_id=user_id, filter_config=dfilt ) #--------------------------------------------------------------------------------- # Create event subscription for resulting detection event #--------------------------------------------------------------------------------- # Create an email notification so that when the DetectionEventProcessor # detects an event and fires its own output event, this will caught by an # EmailEventProcessor and an email will be sent to the user notification_id_2 = self.unsc.create_email(event_type='DetectionEvent', event_subtype=None, origin='DetectionEventProcessor', origin_type=None, user_id=user_id, email='*****@*****.**', mode = DeliveryMode.UNFILTERED, message_header='Detection event', parser='parser', period=1) rle_publisher = EventPublisher("ExampleDetectableEvent") #------------------------------------------------------------------------------------------------ # this event will not be detected because the 'and query' will fail the match condition, #------------------------------------------------------------------------------------------------ rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage = 5) # The smtp_client's sentmail queue should now empty self.assertTrue(proc1.event_processors[notification_id_2].smtp_client.sentmail.empty()) #---------------------------------------------------------------------- # This event will generate an event because it will pass the OR query #---------------------------------------------------------------------- rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage = 15) msg_tuple = proc1.event_processors[notification_id_2].smtp_client.sentmail.get(timeout=4) # check that a non empty message was generated for email self.assertEquals(msg_tuple[1], '*****@*****.**' ) # check that the sentmail queue is empty again after having extracted the message self.assertTrue(proc1.event_processors[notification_id_2].smtp_client.sentmail.empty()) #---------------------------------------------------------------------- # This event WILL not be detected because it will fail all the queries #---------------------------------------------------------------------- rle_publisher = EventPublisher("ExampleDetectableEvent") rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage = 4) # The smtp_client's sentmail queue should now empty self.assertTrue(proc1.event_processors[notification_id_2].smtp_client.sentmail.empty()) #------------------------------------------------------------------------------ # this event WILL be detected. It will pass the main query and the AND query #------------------------------------------------------------------------------ rle_publisher.publish_event(origin='Some_Resource_Agent_ID1', description="RLE test event", voltage = 8) msg_tuple = proc1.event_processors[notification_id_2].smtp_client.sentmail.get(timeout=4) #---------------------------------------------------------------------- # Make assertions regarding the message generated for email #---------------------------------------------------------------------- self.assertEquals(msg_tuple[1], '*****@*****.**' ) #self.assertEquals(msg_tuple[0], ION_NOTIFICATION_EMAIL_ADDRESS) # parse the message body message = msg_tuple[2] list_lines = message.split("\n") message_dict = {} for line in list_lines: key_item = line.split(": ") if key_item[0] == 'Subject': message_dict['Subject'] = key_item[1] + key_item[2] else: try: message_dict[key_item[0]] = key_item[1] except IndexError as exc: # these IndexError exceptions happen only because the message sometimes # has successive /r/n (i.e. new lines) and therefore, # the indexing goes out of range. These new lines # can just be ignored. So we ignore the exceptions here. pass #self.assertEquals(message_dict['From'], ION_NOTIFICATION_EMAIL_ADDRESS) self.assertEquals(message_dict['To'], '*****@*****.**') self.assertEquals(message_dict['Event'].rstrip('\r'), 'DetectionEvent') self.assertEquals(message_dict['Originator'].rstrip('\r'), 'DetectionEventProcessor') self.assertEquals(message_dict['Description'].rstrip('\r'), 'Event was detected by DetectionEventProcessor')