def test_create_one_user_notification(self): # mocks user = Mock() objects = [Mock()] user_id = 'user_id' self.mock_rr_client.create.return_value = ('notification_id', 'rev') self.mock_rr_client.read.return_value = user self.mock_rr_client.find_objects.return_value = objects, None delivery_config = DeliveryConfig() # Create a notification object notification_request = NotificationRequest( name='Setting_email', origin='origin', origin_type='origin_type', event_type='event_type', event_subtype='event_subtype', delivery_config=delivery_config) # execution notification_id = self.user_notification.create_notification( notification_request, user_id) # assertions #@todo - change to asserting called with! self.assertEquals('notification_id', notification_id) self.assertTrue(self.mock_rr_client.create.called) self.assertTrue(self.mock_rr_client.find_objects.return_value)
def create_sms(self, name='', description='', event_type='', event_subtype='', origin='', origin_type='', user_id='', phone='', provider='', message_header='', parser=''): ''' Creates a NotificationRequest object for the specified User Id. Associate the Notification resource with the user. Setup subscription and call back to send an sms to their phone @todo - is the user email automatically selected from the user id? ''' if not phone: raise BadRequest("No phone provided.") if not provider: raise BadRequest("No provider provided.") #------------------------------------------------------------------------------------- # Build the sms delivery config #------------------------------------------------------------------------------------- #@todo get the process_definition_id - Find it when the service starts... bootstrap processing = {'message_header': message_header, 'parsing': parser} delivery = {'phone_number': phone, 'provider': provider} sms_delivery_config = SMSDeliveryConfig(processing=processing, delivery=delivery) log.info("SMS delivery config: %s" % str(sms_delivery_config)) #------------------------------------------------------------------------------------- # Create a notification object #------------------------------------------------------------------------------------- notification_request = NotificationRequest( name=name, description=description, type=NotificationType.SMS, origin=origin, origin_type=origin_type, event_type=event_type, event_subtype=event_subtype, delivery_config=sms_delivery_config) log.info("Notification Request: %s" % str(notification_request)) #------------------------------------------------------------------------------------- # Set up things so that the user gets notified for the particular notification request #------------------------------------------------------------------------------------- notification_id = self.create_notification( notification=notification_request, user_id=user_id) return notification_id
def _create_notification(self, user_name='', instrument_id='', product_id=''): #-------------------------------------------------------------------------------------- # Make notification request objects #-------------------------------------------------------------------------------------- notification_request_1 = NotificationRequest( name='notification_1', origin=instrument_id, origin_type="instrument", event_type='ResourceLifecycleEvent') notification_request_2 = NotificationRequest( name='notification_2', origin=product_id, origin_type="data product", event_type='DetectionEvent') #-------------------------------------------------------------------------------------- # Create a user and get the user_id #-------------------------------------------------------------------------------------- user = UserInfo() user.name = user_name user.contact.email = '*****@*****.**' % user_name user_id, _ = self.rrclient.create(user) #-------------------------------------------------------------------------------------- # Create notification #-------------------------------------------------------------------------------------- self.usernotificationclient.create_notification( notification=notification_request_1, user_id=user_id) self.usernotificationclient.create_notification( notification=notification_request_2, user_id=user_id) log.debug( "test_activateInstrumentSample: create_user_notifications user_id %s", str(user_id)) return user_id
def create_detection_filter(self, name='', description='', event_type='', event_subtype='', origin='', origin_type='', user_id='', filter_config=None): ''' Creates a NotificationRequest object for the specified User Id. Associate the Notification resource with the user. Setup subscription and call back do a detection filter of some type... @todo - is the user email automatically selected from the user id? ''' #@todo for now the delivery config is the filter config. Later there may be some smarts here to set up the config for the event processor delivery_config = filter_config or {} #------------------------------------------------------------------------------------- # Create a notification object #------------------------------------------------------------------------------------- #@todo get the process_definition_id - in this case, should it be added to the interface for this method? notification_request = NotificationRequest( name=name, description=description, type=NotificationType.FILTER, origin=origin, origin_type=origin_type, event_type=event_type, event_subtype=event_subtype, delivery_config=delivery_config) #------------------------------------------------------------------------------------- # Set up things so that the user gets subscribed to receive this notification request #------------------------------------------------------------------------------------- notification_id = self.create_notification( notification=notification_request, user_id=user_id) return notification_id
def test_create_notification_validation(self): #------------------------------------------------------------------------------------------------------ # Test with no user provided #------------------------------------------------------------------------------------------------------ delivery_config = DeliveryConfig() # Create a notification object notification_request = NotificationRequest( name='Setting_email', origin='origin', origin_type='origin_type', event_type='event_type', event_subtype='event_subtype', delivery_config=delivery_config) with self.assertRaises(BadRequest) as br: notification_id = self.user_notification.create_notification( notification=notification_request) self.assertEquals(br.exception.message, '''User id not provided.''')
def test_create_data_product(self): #------------------------------------------------------------------------------------------------ # create a stream definition for the data from the ctd simulator #------------------------------------------------------------------------------------------------ parameter_dictionary = self.dataset_management.read_parameter_dictionary_by_name( 'ctd_parsed_param_dict') ctd_stream_def_id = self.pubsubcli.create_stream_definition( name='Simulated CTD data', parameter_dictionary_id=parameter_dictionary._id) log.debug("Created stream def id %s" % ctd_stream_def_id) #------------------------------------------------------------------------------------------------ # test creating a new data product w/o a stream definition #------------------------------------------------------------------------------------------------ dp_obj = IonObject(RT.DataProduct, name='DP1', description='some new dp') dp_obj.geospatial_bounds.geospatial_latitude_limit_north = 10.0 dp_obj.geospatial_bounds.geospatial_latitude_limit_south = -10.0 dp_obj.geospatial_bounds.geospatial_longitude_limit_east = 10.0 dp_obj.geospatial_bounds.geospatial_longitude_limit_west = -10.0 dp_obj.ooi_product_name = "PRODNAME" #------------------------------------------------------------------------------------------------ # Create a set of ParameterContext objects to define the parameters in the coverage, add each to the ParameterDictionary #------------------------------------------------------------------------------------------------ dp_id = self.dpsc_cli.create_data_product( data_product=dp_obj, stream_definition_id=ctd_stream_def_id) # Assert that the data product has an associated stream at this stage stream_ids, _ = self.rrclient.find_objects(dp_id, PRED.hasStream, RT.Stream, True) self.assertNotEquals(len(stream_ids), 0) # Assert that the data product has an associated stream def at this stage stream_ids, _ = self.rrclient.find_objects(dp_id, PRED.hasStreamDefinition, RT.StreamDefinition, True) self.assertNotEquals(len(stream_ids), 0) self.dpsc_cli.activate_data_product_persistence(dp_id) dp_obj = self.dpsc_cli.read_data_product(dp_id) self.assertIsNotNone(dp_obj) self.assertEquals(dp_obj.geospatial_point_center.lat, 0.0) log.debug('Created data product %s', dp_obj) #------------------------------------------------------------------------------------------------ # test creating a new data product with a stream definition #------------------------------------------------------------------------------------------------ log.debug('Creating new data product with a stream definition') dp_obj = IonObject(RT.DataProduct, name='DP2', description='some new dp') dp_id2 = self.dpsc_cli.create_data_product(dp_obj, ctd_stream_def_id) self.dpsc_cli.activate_data_product_persistence(dp_id2) log.debug('new dp_id = %s' % dp_id2) #------------------------------------------------------------------------------------------------ #make sure data product is associated with stream def #------------------------------------------------------------------------------------------------ streamdefs = [] streams, _ = self.rrclient.find_objects(dp_id2, PRED.hasStream, RT.Stream, True) for s in streams: log.debug("Checking stream %s" % s) sdefs, _ = self.rrclient.find_objects(s, PRED.hasStreamDefinition, RT.StreamDefinition, True) for sd in sdefs: log.debug("Checking streamdef %s" % sd) streamdefs.append(sd) self.assertIn(ctd_stream_def_id, streamdefs) group_names = self.dpsc_cli.get_data_product_group_list() self.assertIn("PRODNAME", group_names) #---------------------------------------------------------------------------------------- # Create users then notifications to this data product for each user #---------------------------------------------------------------------------------------- # user_1 user_1 = UserInfo() user_1.name = 'user_1' user_1.contact.email = '*****@*****.**' # user_2 user_2 = UserInfo() user_2.name = 'user_2' user_2.contact.email = '*****@*****.**' #user1 is a complete user self.subject = "/DC=org/DC=cilogon/C=US/O=ProtectNetwork/CN=Roger Unwin A254" actor_identity_obj = IonObject("ActorIdentity", {"name": self.subject}) actor_id = self.identcli.create_actor_identity(actor_identity_obj) user_credentials_obj = IonObject("UserCredentials", {"name": self.subject}) self.identcli.register_user_credentials(actor_id, user_credentials_obj) user_id_1 = self.identcli.create_user_info(actor_id, user_1) user_id_2, _ = self.rrclient.create(user_2) delivery_config1a = IonObject( OT.DeliveryConfiguration, email='*****@*****.**', mode=DeliveryModeEnum.EMAIL, frequency=NotificationFrequencyEnum.BATCH) delivery_config1b = IonObject( OT.DeliveryConfiguration, email='*****@*****.**', mode=DeliveryModeEnum.EMAIL, frequency=NotificationFrequencyEnum.BATCH) notification_request_1 = NotificationRequest( name="notification_1", origin=dp_id, origin_type="type_1", event_type=OT.ResourceLifecycleEvent, disabled_by_system=False, delivery_configurations=[delivery_config1a, delivery_config1b]) delivery_config2a = IonObject( OT.DeliveryConfiguration, email='*****@*****.**', mode=DeliveryModeEnum.EMAIL, frequency=NotificationFrequencyEnum.BATCH) delivery_config2b = IonObject( OT.DeliveryConfiguration, email='*****@*****.**', mode=DeliveryModeEnum.EMAIL, frequency=NotificationFrequencyEnum.BATCH) notification_request_2 = NotificationRequest( name="notification_2", origin=dp_id, origin_type="type_2", disabled_by_system=False, event_type=OT.DetectionEvent, delivery_configurations=[delivery_config2a, delivery_config2b]) notification_request_1_id = self.unsc.create_notification( notification=notification_request_1, user_id=user_id_1) notification_request_2_id = self.unsc.create_notification( notification=notification_request_2, user_id=user_id_2) self.unsc.delete_notification(notification_request_1_id) # test reading a non-existent data product log.debug('reading non-existent data product') with self.assertRaises(NotFound): dp_obj = self.dpsc_cli.read_data_product('some_fake_id') # update a data product (tests read also) log.debug('Updating data product') # first get the existing dp object dp_obj = self.dpsc_cli.read_data_product(dp_id) # now tweak the object dp_obj.description = 'the very first dp' dp_obj.geospatial_bounds.geospatial_latitude_limit_north = 20.0 dp_obj.geospatial_bounds.geospatial_latitude_limit_south = -20.0 dp_obj.geospatial_bounds.geospatial_longitude_limit_east = 20.0 dp_obj.geospatial_bounds.geospatial_longitude_limit_west = -20.0 # now write the dp back to the registry update_result = self.dpsc_cli.update_data_product(dp_obj) # now get the dp back to see if it was updated dp_obj = self.dpsc_cli.read_data_product(dp_id) self.assertEquals(dp_obj.description, 'the very first dp') self.assertEquals(dp_obj.geospatial_point_center.lat, 0.0) log.debug('Updated data product %s', dp_obj) #test extension extended_product = self.dpsc_cli.get_data_product_extension(dp_id) #validate that there is one active and one retired user notification for this data product self.assertEqual( 1, len(extended_product.computed.active_user_subscriptions.value)) self.assertEqual( 1, len(extended_product.computed.past_user_subscriptions.value)) self.assertEqual(dp_id, extended_product._id) self.assertEqual( ComputedValueAvailability.PROVIDED, extended_product.computed.product_download_size_estimated.status) self.assertEqual( 0, extended_product.computed.product_download_size_estimated.value) self.assertEqual(ComputedValueAvailability.PROVIDED, extended_product.computed.parameters.status) #log.debug("test_create_data_product: parameters %s" % extended_product.computed.parameters.value) def ion_object_encoder(obj): return obj.__dict__ #test prepare for create data_product_data = self.dpsc_cli.prepare_data_product_support() #print simplejson.dumps(data_product_data, default=ion_object_encoder, indent= 2) self.assertEqual(data_product_data._id, "") self.assertEqual(data_product_data.type_, OT.DataProductPrepareSupport) self.assertEqual( len(data_product_data.associations['StreamDefinition'].resources), 2) self.assertEqual( len(data_product_data.associations['Dataset'].resources), 0) self.assertEqual( len(data_product_data.associations['StreamDefinition']. associated_resources), 0) self.assertEqual( len(data_product_data.associations['Dataset'].associated_resources ), 0) #test prepare for update data_product_data = self.dpsc_cli.prepare_data_product_support(dp_id) #print simplejson.dumps(data_product_data, default=ion_object_encoder, indent= 2) self.assertEqual(data_product_data._id, dp_id) self.assertEqual(data_product_data.type_, OT.DataProductPrepareSupport) self.assertEqual( len(data_product_data.associations['StreamDefinition'].resources), 2) self.assertEqual( len(data_product_data.associations['Dataset'].resources), 1) self.assertEqual( len(data_product_data.associations['StreamDefinition']. associated_resources), 1) self.assertEqual( data_product_data.associations['StreamDefinition']. associated_resources[0].s, dp_id) self.assertEqual( len(data_product_data.associations['Dataset'].associated_resources ), 1) self.assertEqual( data_product_data.associations['Dataset'].associated_resources[0]. s, dp_id) # now 'delete' the data product log.debug("deleting data product: %s" % dp_id) self.dpsc_cli.delete_data_product(dp_id) # Assert that there are no associated streams leftover after deleting the data product stream_ids, assoc_ids = self.rrclient.find_objects( dp_id, PRED.hasStream, RT.Stream, True) self.assertEquals(len(stream_ids), 0) self.assertEquals(len(assoc_ids), 0) self.dpsc_cli.force_delete_data_product(dp_id) # now try to get the deleted dp object with self.assertRaises(NotFound): dp_obj = self.dpsc_cli.read_data_product(dp_id) # Get the events corresponding to the data product ret = self.unsc.get_recent_events(resource_id=dp_id) events = ret.value for event in events: log.debug("event time: %s" % event.ts_created) self.assertTrue(len(events) > 0)
def create_email(self, name='', description='', event_type='', event_subtype='', origin='', origin_type='', user_id='', email='', mode=None, message_header='', parser='', period=86400): ''' Creates a NotificationRequest object for the specified User Id. Associate the Notification resource with the user. Setup subscription and call back to send email @todo - is the user email automatically selected from the user id? ''' # assertions if not email: raise BadRequest("No email provided.") if not mode: raise BadRequest("No delivery mode provided.") #------------------------------------------------------------------------------------- # Build the email delivery config #------------------------------------------------------------------------------------- #@todo get the process_definition_id - Find it when the service starts... bootstrap #@todo Define a default for message header and parsing if not message_header: message_header = "Default message header" #@todo this has to be decided processing = {'message_header': message_header, 'parsing': parser} delivery = {'email': email, 'mode': mode, 'period': period} email_delivery_config = EmailDeliveryConfig(processing=processing, delivery=delivery) log.info("Email delivery config: %s" % str(email_delivery_config)) #------------------------------------------------------------------------------------- # Create a notification object #------------------------------------------------------------------------------------- notification_request = NotificationRequest( name=name, description=description, type=NotificationType.EMAIL, origin=origin, origin_type=origin_type, event_type=event_type, event_subtype=event_subtype, delivery_config=email_delivery_config) log.info("Notification Request: %s" % str(notification_request)) #------------------------------------------------------------------------------------- # Set up things so that the user gets notified for the particular notification request #------------------------------------------------------------------------------------- notification_id = self.create_notification( notification=notification_request, user_id=user_id) return notification_id