def test_load_triggers(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) trigger_file = os.path.dirname(__file__) + os.sep + "triggers.txt" self.assertTrue(os.path.exists(trigger_file)) config = FileStorageConfiguration() config._triggers_storage = FileStoreConfiguration(file=trigger_file, format="text", encoding="utf-8", delete_on_start=False) engine = FileStorageEngine(config) engine.initialise() storage_factory = StorageFactory() storage_factory._storage_engines[StorageFactory.TRIGGERS] = engine storage_factory._store_to_engine_map[StorageFactory.TRIGGERS] = engine mgr.load_triggers(storage_factory) triggers = mgr.get_triggers("SYSTEM_STARTUP") self.assertIsNotNone(triggers) self.assertEquals(2, len(triggers)) triggers = mgr.get_triggers("SYSTEM_SHUTDOWN") self.assertIsNotNone(triggers) self.assertEquals(1, len(triggers)) triggers = mgr.get_triggers("CONVERSATION_START") self.assertIsNotNone(triggers) self.assertEquals(1, len(triggers))
def test_trigger_triggers(self): config = TriggerConfiguration() config._manager = "programytest.triggers.test_rest.MockRestTriggerManager" config._additionals["url"] = "http://localhost/api/1.0/ask" mgr = TriggerManager.load_trigger_manager(config) client = TestClient() client_context = client.create_client_context("testid") triggered = mgr.trigger("SYSTEM_STARTUP") self.assertTrue(triggered) triggered = mgr.trigger("SYSTEM_STARTUP", additional={"key": "value"}) self.assertTrue(triggered) triggered = mgr.trigger("CONVERSATION_START", client_context) self.assertTrue(triggered) triggered = mgr.trigger("OTHER_EVENT") self.assertTrue(triggered) triggered = mgr.trigger("OTHER_EVENT", client_context) self.assertTrue(triggered) triggered = mgr.trigger("OTHER_EVENT", client_context, additional={"key": "value"}) self.assertTrue(triggered)
def test_to_yaml_with_defaults(self): triggers_config = TriggerConfiguration() triggers_config._manager = TriggerConfiguration.LOCAL_MANAGER data = {} triggers_config.to_yaml(data, defaults=True) self.assertEquals( {'manager': 'programy.triggers.local.LocalTriggerManager'}, data)
def test_to_yaml_no_defaults(self): triggers_config = TriggerConfiguration() triggers_config._manager = "programy.triggers.local.LocalTriggerManager2" data = {} triggers_config.to_yaml(data, defaults=False) self.assertEquals( {'manager': 'programy.triggers.local.LocalTriggerManager2'}, data)
def test_init_with_events(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) self.assertIsNotNone(mgr) self.assertIsInstance(mgr, TriggerManager)
def test_trigger_with_additionals_method_get(self): config = TriggerConfiguration() config._additionals['url'] = "http://some.service.com" config._additionals['method'] = "GET" mgr = MockRestTriggerManager(config) result = mgr.trigger("SYSTEM_STARTUP") self.assertFalse(result)
def test_add_trigger(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) self.assertIsNotNone(mgr) mgr.add_trigger("SYSTEM_STARTUP", "programytest.triggers.trigger1.Trigger1") self.assertTrue("SYSTEM_STARTUP" in mgr.triggers)
def __init__(self, name): BaseContainerConfigurationData.__init__(self, name) self._description = 'ProgramY AIML2.0 Client' self._bot_configs = [] self._bot_configs.append(BotConfiguration("bot")) self._bot_selector = None self._renderer = None self._scheduler = SchedulerConfiguration() self._storage = StorageConfiguration() self._email = EmailConfiguration() self._triggers = TriggerConfiguration()
def test_load_file_contents_with_exception(self): config = FileStorageConfiguration() engine = FileStorageEngine(config) engine.initialise() store = FileTriggersStore(engine) trigger_config = TriggerConfiguration() trigger_config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(trigger_config) self.assertFalse(store._load_file_contents(os.path.dirname(__file__) + os.sep + "data" + os.sep + "triggers" + os.sep + "triggers.txt", mgr))
def test_trigger_exception(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) self.assertIsNotNone(mgr) mgr.add_trigger("SYSTEM_STARTUP", "programy.triggers.excepter.ExceptionTrigger") triggered = mgr.trigger("SYSTEM_STARTUP") self.assertTrue(triggered)
def test_trigger_with_additionals_no_token(self): config = TriggerConfiguration() config._additionals['url'] = "http://some.service.com" config._additionals['method'] = "POST" mgr = MockRestTriggerManager(config) mgr.trigger("SYSTEM_STARTUP") self.assertEquals("http://some.service.com", mgr._api_url_base) self.assertEquals({'Content-Type': 'application/json'}, mgr._headers) self.assertEquals({'event': 'SYSTEM_STARTUP'}, mgr._payload)
def __init__(self, name): BaseContainerConfigurationData.__init__(self, name) self._description = 'ProgramY AIML2.0 Client' self._renderer = self._get_renderer_class() self._scheduler = SchedulerConfiguration() self._storage = StorageConfiguration() self._email = EmailConfiguration() self._triggers = TriggerConfiguration() self._responder = PingResponderConfig() self._bot_selector = self._get_bot_selector_class() bot_config = BotConfiguration('bot') self._bot_configs = [bot_config]
def test_process_line(self): config = FileStorageConfiguration() engine = FileStorageEngine(config) engine.initialise() store = FileTriggersStore(engine) trigger_config = TriggerConfiguration() trigger_config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(trigger_config) self.assertFalse(store._process_line("", mgr)) self.assertFalse(store._process_line("TRIGGER1", mgr)) self.assertFalse(store._process_line("#", mgr)) self.assertFalse(store._process_line("#TRIGGER1:programy.triggers.null.NullTrigger", mgr))
def test_with_no_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text(""" bot: """, ConsoleConfiguration(), ".") console_config = yaml.get_section("console") triggers_config = TriggerConfiguration() triggers_config.load_config_section(yaml, console_config, ".") self.assertEquals("programy.triggers.local.LocalTriggerManager", triggers_config.manager)
def test_add_triggers(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) self.assertIsNotNone(mgr) mgr.add_triggers({ "SYSTEM_STARTUP": "programytest.triggers.trigger1.Trigger1", "SYSTEM_SHUTDOWN": "programytest.triggers.trigger1.Trigger1", "CONVERSATION_START": "programytest.triggers.trigger1.Trigger1" }) self.assertTrue("SYSTEM_STARTUP" in mgr.triggers) self.assertTrue("SYSTEM_SHUTDOWN" in mgr.triggers) self.assertTrue("CONVERSATION_START" in mgr.triggers)
def test_trigger_triggers_exception(self): config = TriggerConfiguration() mgr = MockRestTriggerManager(config, doexcept=True) triggered = mgr.trigger("SYSTEM_STARTUP", client_context=None) self.assertFalse(triggered)
def test_trigger_triggers_status_code_500(self): config = TriggerConfiguration() mgr = MockRestTriggerManager(config, status_code=500) triggered = mgr.trigger("SYSTEM_STARTUP", client_context=None) self.assertFalse(triggered)
def test_load_triggers(self): config = FileStorageConfiguration() config._triggers_storage = FileStoreConfiguration(file=os.path.dirname(__file__) + os.sep + "data" + os.sep + "triggers" + os.sep + "triggers.txt", fileformat="text", encoding="utf-8", delete_on_start=False) engine = FileStorageEngine(config) engine.initialise() store = FileTriggersStore(engine) trigger_config = TriggerConfiguration() trigger_config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(trigger_config) store.load(mgr) self.assertTrue(mgr.has_trigger_event("TRIGGER1")) self.assertTrue(mgr.has_trigger_event("TRIGGER2")) self.assertTrue(mgr.has_trigger_event("TRIGGER3")) self.assertFalse(mgr.has_trigger_event("TRIGGER4"))
def test_trigger_triggers(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) trigger_file = os.path.dirname(__file__) + os.sep + "triggers.txt" config = FileStorageConfiguration() config._triggers_storage = FileStoreConfiguration( file=trigger_file, format="text", encoding="utf-8", delete_on_start=False) engine = FileStorageEngine(config) engine.initialise() storage_factory = StorageFactory() storage_factory._storage_engines[StorageFactory.TRIGGERS] = engine storage_factory._store_to_engine_map[StorageFactory.TRIGGERS] = engine mgr.load_triggers(storage_factory) client = TestClient() client_context = client.create_client_context("testid") triggered = mgr.trigger("SYSTEM_STARTUP") self.assertTrue(triggered) triggered = mgr.trigger("SYSTEM_STARTUP", additional={"key": "value"}) self.assertTrue(triggered) triggered = mgr.trigger("CONVERSATION_START", client_context) self.assertTrue(triggered) triggered = mgr.trigger("OTHER_EVENT") self.assertFalse(triggered) triggered = mgr.trigger("OTHER_EVENT", client_context) self.assertFalse(triggered) triggered = mgr.trigger("OTHER_EVENT", client_context, additional={"key": "value"}) self.assertFalse(triggered)
def assert_load(self, store): store.empty() store.upload_from_file(os.path.dirname(__file__) + os.sep + "data" + os.sep + "triggers" + os.sep + "triggers.txt") handler = LocalTriggerManager(TriggerConfiguration()) store.load(handler) self.assertEqual(3, len(handler.triggers)) self.assertTrue(handler.triggers.get("CONVERSATION_START"))
def test_load_triggers_bad_triggers(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) trigger_file = os.path.dirname(__file__) + os.sep + "bad_triggers.txt" self.assertTrue(os.path.exists(trigger_file)) config = FileStorageConfiguration() config._triggers_storage = FileStoreConfiguration(file=trigger_file, fileformat="text", encoding="utf-8", delete_on_start=False) engine = FileStorageEngine(config) engine.initialise() storage_factory = StorageFactory() storage_factory._storage_engines[StorageFactory.TRIGGERS] = engine storage_factory._store_to_engine_map[StorageFactory.TRIGGERS] = engine mgr.load_triggers(storage_factory)
def test_load_triggers_no_engine(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.LOCAL_MANAGER mgr = TriggerManager.load_trigger_manager(config) trigger_file = os.path.dirname(__file__) + os.sep + "triggers.txt" self.assertTrue(os.path.exists(trigger_file)) config = FileStorageConfiguration() config._triggers_storage = FileStoreConfiguration(file=trigger_file, fileformat="text", encoding="utf-8", delete_on_start=False) engine = FileStorageEngine(config) engine.initialise() storage_factory = StorageFactory() mgr.load_triggers(storage_factory) self.assertFalse("SYSTEM_STARTUP" in mgr.triggers) self.assertFalse("SYSTEM_SHUTDOWN" in mgr.triggers) self.assertFalse("CONVERSATION_START" in mgr.triggers)
def test_with_additional_data(self): yaml = YamlConfigurationFile() self.assertIsNotNone(yaml) yaml.load_from_text( """ console: triggers: manager: programy.triggers.rest.RestTriggerManager url: http://localhost:8989/api/v1.0/trigger method: POST token: 123BC4F3D """, ConsoleConfiguration(), ".") console_config = yaml.get_section("console") triggers_config = TriggerConfiguration() triggers_config.load_config_section(yaml, console_config, ".") self.assertEquals("programy.triggers.rest.RestTriggerManager", triggers_config.manager) self.assertEquals(triggers_config.value("url"), "http://localhost:8989/api/v1.0/trigger") self.assertEquals(triggers_config.value("method"), "POST") self.assertEquals(triggers_config.value("token"), "123BC4F3D")
def test_init(self): mgr = MockTriggerManager(TriggerConfiguration()) self.assertIsNotNone(mgr)
def test_load_trigger_manager_bad_manager(self): config = TriggerConfiguration() config._manager = "some.class.or.other" mgr = TriggerManager.load_trigger_manager(config) self.assertIsNone(mgr)
class ClientConfigurationData(BaseContainerConfigurationData): def __init__(self, name): BaseContainerConfigurationData.__init__(self, name) self._description = 'ProgramY AIML2.0 Client' self._renderer = self._get_renderer_class() self._scheduler = SchedulerConfiguration() self._storage = StorageConfiguration() self._email = EmailConfiguration() self._triggers = TriggerConfiguration() self._responder = PingResponderConfig() self._bot_selector = self._get_bot_selector_class() bot_config = BotConfiguration('bot') self._bot_configs = [bot_config] def _get_renderer_class(self): return "programy.clients.render.text.TextRenderer" def _get_bot_selector_class(self): return "programy.clients.botfactory.DefaultBotSelector" @property def description(self): return self._description @property def configurations(self): return self._bot_configs @property def bot_selector(self): return self._bot_selector @property def scheduler(self): return self._scheduler @property def storage(self): return self._storage @property def renderer(self): return self._renderer @property def email(self): return self._email @property def triggers(self): return self._triggers @property def responder(self): return self._responder def load_configuration(self, configuration_file, bot_root, subs: Substitutions = None): client = configuration_file.get_section(self.section_name) if client is None: YLogger.error( None, 'Client section named [%s] not found, trying "client"', self.section_name) client = configuration_file.get_section('client') if client is not None: self.load_configuration_section(configuration_file, client, bot_root, subs) else: YLogger.error(None, 'No client section not found!') def load_configuration_section(self, configuration_file, section, bot_root, subs: Substitutions = None): assert configuration_file is not None assert section is not None self._description = configuration_file.get_option( section, "description", missing_value='ProgramY AIML2.0 Client', subs=subs) self._scheduler.load_config_section(configuration_file, section, bot_root, subs=subs) self._storage.load_config_section(configuration_file, section, bot_root, subs=subs) self._renderer = configuration_file.get_option(section, "renderer", subs=subs) self._email.load_config_section(configuration_file, section, bot_root, subs=subs) self._triggers.load_config_section(configuration_file, section, bot_root, subs=subs) self._responder.load_config_section(configuration_file, section, bot_root, subs=subs) self._load_bot_configurations(configuration_file, section, bot_root, subs) def _load_bot_configurations(self, configuration_file, section, bot_root, subs): bots = configuration_file.get_section("bots", section) if bots is not None: bot_keys = configuration_file.get_keys(bots) first = True for name in bot_keys: if first is True: self._bot_configs.clear() first = False bot_config = configuration_file.get_section(name, bots) config = BotConfiguration(name) config.load_configuration_section(configuration_file, bot_config, bot_root, subs=subs) self._bot_configs.append(config) if len(self._bot_configs) == 0: YLogger.warning( self, "No bot name defined for client [%s], defaulting to 'bot'.", self.section_name) self._bot_configs.append(BotConfiguration("bot")) self._bot_configs[0].configurations.append( BrainConfiguration("brain")) self._bot_selector = configuration_file.get_option( section, "bot_selector", missing_value="programy.clients.botfactory.DefaultBotSelector", subs=subs) def to_yaml(self, data, defaults=True): assert data is not None if defaults is True: data['description'] = 'ProgramY AIML2.0 Client' data[ 'bot_selector'] = "programy.clients.botfactory.DefaultBotSelector" data['scheduler'] = {} self._scheduler.to_yaml(data['scheduler'], defaults) data['email'] = {} self._email.to_yaml(data['email'], defaults) data['triggers'] = {} self._triggers.to_yaml(data['triggers'], defaults) data['responder'] = {} self._responder.to_yaml(data['responder'], defaults) data['renderer'] = "programy.clients.render.text.TextRenderer" data['storage'] = {} self._storage.to_yaml(data['storage'], defaults) data['bots'] = {} data['bots']['bot'] = {} bot_config = BotConfiguration('bot') bot_config.to_yaml(data['bots']['bot'], defaults) else: data['description'] = self._description data['bot_selector'] = self.bot_selector data[self._scheduler.id] = {} self._scheduler.to_yaml(data[self._scheduler.id], defaults) data['email'] = {} self._email.to_yaml(data['email'], defaults) data['triggers'] = {} self._triggers.to_yaml(data['triggers'], defaults) data['responder'] = {} self._responder.to_yaml(data['responder'], defaults) data['renderer'] = self.renderer data['storage'] = {} self._storage.to_yaml(data['storage'], defaults) data['bots'] = {} for botconfig in self._bot_configs: data['bots'][botconfig.id] = {} botconfig.to_yaml(data['bots'][botconfig.id], defaults)
def test_create(self): config = TriggerConfiguration() config._manager = TriggerConfiguration.REST_MANAGER mgr = TriggerManager.load_trigger_manager(config) self.assertIsNotNone(mgr)
class ClientConfigurationData(BaseContainerConfigurationData): def __init__(self, name): BaseContainerConfigurationData.__init__(self, name) self._description = 'ProgramY AIML2.0 Client' self._bot_configs = [] self._bot_configs.append(BotConfiguration("bot")) self._bot_selector = None self._renderer = None self._scheduler = SchedulerConfiguration() self._storage = StorageConfiguration() self._email = EmailConfiguration() self._triggers = TriggerConfiguration() @property def description(self): return self._description @property def configurations(self): return self._bot_configs @property def bot_selector(self): return self._bot_selector @property def scheduler(self): return self._scheduler @property def storage(self): return self._storage @property def renderer(self): return self._renderer @property def email(self): return self._email @property def triggers(self): return self._triggers def check_for_license_keys(self, license_keys): BaseContainerConfigurationData.check_for_license_keys( self, license_keys) def load_configuration(self, configuration_file, bot_root, subs: Substitutions = None): client = configuration_file.get_section(self.section_name) if client is None: YLogger.error( None, 'Client section named [%s] not found, trying "client"', self.section_name) client = configuration_file.get_section('client') if client is not None: self.load_configuration_section(configuration_file, client, bot_root, subs) else: YLogger.error(None, 'No client section not found!') def load_configuration_section(self, configuration_file, section, bot_root, subs: Substitutions = None): assert (configuration_file is not None) if section is not None: self._description = configuration_file.get_option( section, "description", missing_value='ProgramY AIML2.0 Client', subs=subs) bot_names = configuration_file.get_multi_option( section, "bot", missing_value="bot", subs=subs) first = True for name in bot_names: if first is True: config = self._bot_configs[0] first = False else: config = BotConfiguration(name) self._bot_configs.append(config) config.load_configuration(configuration_file, bot_root, subs=subs) self._bot_selector = configuration_file.get_option( section, "bot_selector", missing_value="programy.clients.client.DefaultBotSelector", subs=subs) self._scheduler.load_config_section(configuration_file, section, bot_root, subs=subs) self._storage.load_config_section(configuration_file, section, bot_root, subs=subs) self._renderer = configuration_file.get_option(section, "renderer", subs=subs) self._email.load_config_section(configuration_file, section, bot_root, subs=subs) self._triggers.load_config_section(configuration_file, section, bot_root, subs=subs) else: YLogger.warning( self, "No bot name defined for client [%s], defaulting to 'bot'.", self.section_name) self._bot_configs[0]._section_name = "bot" self._bot_configs[0].load_configuration(configuration_file, bot_root, subs=subs) def to_yaml(self, data, defaults=True): assert (data is not None) if defaults is True: data['description'] = 'ProgramY AIML2.0 Client' data['bot'] = 'bot' data['bot_selector'] = "programy.clients.client.DefaultBotSelector" data['renderer'] = "programy.clients.render.text.TextRenderer" else: data['description'] = self._description data['bot'] = self._bot_configs[0].id data['bot_selector'] = self.bot_selector data['renderer'] = self.renderer data[self._scheduler.id] = {} self._scheduler.to_yaml(data[self._scheduler.id], defaults) data['email'] = {} self._email.to_yaml(data['email'], defaults) data['triggers'] = {} self._triggers.to_yaml(data['triggers'], defaults)
def test_defaults(self): triggers_config = TriggerConfiguration() data = {} triggers_config.to_yaml(data, True) TriggersConfigurationTests.assert_defaults(self, data)
def test_load_trigger_manager_no_config(self): config = TriggerConfiguration() config._manager = None mgr = TriggerManager.load_trigger_manager(config) self.assertIsNone(mgr)