<s> from unittest import TestCase <EOL> from mock import MagicMock <EOL> from hipchat import HipChat <EOL> from graphitepager . description import Description <EOL> from graphitepager . notifiers . hipchat_notifier import HipChatNotifier <EOL> from graphitepager . redis_storage import RedisStorage <EOL> from graphitepager . alerts import Alert <EOL> from graphitepager . config import Config <EOL> from graphitepager . level import Level <EOL> class TestHipChatNotifier ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . alert_key = '<STR_LIT>' <EOL> self . description = MagicMock ( Description ) <EOL> self . mock_config = MagicMock ( Config ) <EOL> self . mock_redis_storage = MagicMock ( RedisStorage ) <EOL> self . mock_hipchat_client = MagicMock ( HipChat ) <EOL> self . mock_alert = MagicMock ( Alert ) <EOL> self . mock_config . get = self . mock_get <EOL> self . hcn = HipChatNotifier ( self . mock_redis_storage , self . mock_config ) <EOL> self . hcn . _client = self . mock_hipchat_client <EOL> def mock_get ( self , key , default = None ) : <EOL> if key == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> if key == '<STR_LIT>' : <EOL> return '<STR_LIT>' <EOL> return default <EOL> def test_should_not_notify_hipchat_if_no_rooms_have_been_added ( self ) : <EOL> self . hcn . notify ( <EOL> self . mock_alert , <EOL> self . alert_key , <EOL> Level . WARNING , <EOL> self . description ) <EOL> self . assertEqual ( self . mock_hipchat_client . message_room . mock_calls , [ ] ) <EOL> def test_should_not_notify_hipchat_if_warning_and_already_notified ( self ) : <EOL> self . mock_redis_storage . is_locked_for_domain_and_key . return_value = True <EOL> self . hcn . notify ( <EOL> self . mock_alert , <EOL> self . alert_key , <EOL> Level . WARNING , <EOL> self . description ) <EOL> self . assertEqual ( self . mock_hipchat_client . mock_calls , [ ] ) <EOL> def test_should_notify_hipchat_resolved_if_nominal_and_had_notified ( self ) : <EOL> room_name = '<STR_LIT>' <EOL> self . hcn . add_room ( room_name ) <EOL> self . mock_redis_storage . is_locked_for_domain_and_key . return_value = True <EOL> self . hcn . notify ( <EOL> self . mock_alert , <EOL> self . alert_key , <EOL> Level . NOMINAL , <EOL> self . description ) <EOL> self . mock_redis_storage . is_locked_for_domain_and_key . assert_called_once_with ( '<STR_LIT>' , self . alert_key ) <EOL> self . mock_hipchat_client . message_room . assert_called_once_with ( <EOL> room_name , <EOL> '<STR_LIT>' , <EOL> self . description . html ( ) , <EOL> message_format = '<STR_LIT:html>' , <EOL> color = '<STR_LIT>' , <EOL> ) <EOL> self . mock_redis_storage . remove_lock_for_domain_and_key . assert_called_once_with ( '<STR_LIT>' , self . alert_key ) <EOL> def test_should_notify_room_of_warning_if_had_not_notified_before ( self ) : <EOL> room_name = '<STR_LIT>' <EOL> self . hcn . add_room ( room_name ) <EOL> self . mock_redis_storage . is_locked_for_domain_and_key . return_value = False <EOL> self . hcn . notify ( <EOL> self . mock_alert , <EOL> self . alert_key , <EOL> Level . WARNING , <EOL> self . description ) <EOL> self . mock_hipchat_client . message_room . assert_called_once_with ( <EOL> room_name , <EOL> '<STR_LIT>' , <EOL> self . description . html ( ) , <EOL> message_format = '<STR_LIT:html>' , <EOL> color = '<STR_LIT>' <EOL> ) <EOL> self . mock_redis_storage . set_lock_for_domain_and_key . assert_called_once_with ( '<STR_LIT>' , self . alert_key ) <EOL> def test_should_notify_room_of_critical_if_had_not_notified_before ( self ) : <EOL> room_name = '<STR_LIT>' <EOL> self . hcn . add_room ( room_name ) <EOL> self . mock_redis_storage . is_locked_for_domain_and_key . return_value = False <EOL> self . hcn . notify ( <EOL> self . mock_alert , <EOL> self . alert_key , <EOL> Level . CRITICAL , <EOL> self . description ) <EOL> self . mock_hipchat_client . message_room . assert_called_once_with ( <EOL> room_name , <EOL> '<STR_LIT>' , <EOL> self . description . html ( ) , <EOL> message_format = '<STR_LIT:html>' , <EOL> color = '<STR_LIT>' <EOL> ) <EOL> self . mock_redis_storage . set_lock_for_domain_and_key . assert_called_once_with ( '<STR_LIT>' , self . alert_key ) <EOL> def test_should_notify_room_of_no_data_if_had_not_notified_before ( self ) : <EOL> room_name = '<STR_LIT>' <EOL> self . hcn . add_room ( room_name ) <EOL> self . mock_redis_storage . is_locked_for_domain_and_key . return_value = False <EOL> self . hcn . notify ( <EOL> self . mock_alert , <EOL> self . alert_key , <EOL> Level . NO_DATA , <EOL> self . description ) <EOL> self . mock_hipchat_client . message_room . assert_called_once_with ( <EOL> room_name , <EOL> '<STR_LIT>' , <EOL> self . description . html ( ) , <EOL> message_format = '<STR_LIT:html>' , <EOL> color = '<STR_LIT>' <EOL> ) <EOL> self . mock_redis_storage . set_lock_for_domain_and_key . assert_called_once_with ( '<STR_LIT>' , self . alert_key ) </s>
