import mock
import unittest2

from st2common.persistence.reactor import Trigger, TriggerType
from st2common.models.db.reactor import TriggerDB, TriggerTypeDB
import st2reactor.container.utils as container_utils

MOCK_TRIGGER_TYPE = TriggerTypeDB()
MOCK_TRIGGER_TYPE.id = 'trigger-type-test.id'
MOCK_TRIGGER_TYPE.name = 'trigger-type-test.name'
MOCK_TRIGGER_TYPE.parameters_schema = {}
MOCK_TRIGGER_TYPE.payload_info = {}

MOCK_TRIGGER = TriggerDB()
MOCK_TRIGGER.id = 'trigger-test.id'
MOCK_TRIGGER.name = 'trigger-test.name'
MOCK_TRIGGER.parameters = {}
MOCK_TRIGGER.type = {'name': MOCK_TRIGGER_TYPE.name, 'id': MOCK_TRIGGER_TYPE.id}


class ContainerUtilsTest(unittest2.TestCase):
    @mock.patch.object(TriggerType, 'query', mock.MagicMock(
        return_value=[MOCK_TRIGGER_TYPE]))
    @mock.patch.object(Trigger, 'get_by_name', mock.MagicMock(return_value=MOCK_TRIGGER))
    @mock.patch.object(TriggerType, 'add_or_update')
    def test_add_trigger(self, mock_add_handler):
        mock_add_handler.return_value = MOCK_TRIGGER_TYPE
        container_utils.add_trigger_models([MOCK_TRIGGER_TYPE])
        self.assertTrue(mock_add_handler.called, 'trigger not added.')

    def test_add_trigger_type(self):
        """
        This sensor has misconfigured trigger type. We shouldn't explode.
        """
        class FailTestSensor(object):
            started = False

            def setup(self):
                pass

            def start(self):
                FailTestSensor.started = True

            def stop(self):
                pass

            def get_trigger_types(self):
                return [
                    {'description': 'Ain\'t got no name'}
                ]

        try:
            container_utils.add_trigger_models(FailTestSensor().get_trigger_types())
            self.assertTrue(False, 'Trigger type doesn\'t have \'name\' field. Should have thrown.')
        except Exception:
            self.assertTrue(True)

@@ -8,25 +8,8 @@
 mock
-%0Aimport unittest2
 %0A%0Afr
@@ -134,16 +134,73 @@
 rTypeDB%0A
+from st2common.transport.publishers import PoolPublisher%0A
 import s
@@ -243,16 +243,53 @@
 er_utils
+%0Afrom st2tests.base import DbTestCase
 %0A%0AMOCK_T
@@ -707,16 +707,79 @@
 E.id%7D%0A%0A%0A
+@mock.patch.object(PoolPublisher, 'publish', mock.MagicMock())%0A
 class Co
@@ -799,18 +799,10 @@
 est(
-unittest2.
+Db
 Test
@@ -2046,16 +2046,16 @@
 eption:%0A
-
         
@@ -2080,8 +2080,1772 @@
 e(True)%0A
+%0A    def test_create_trigger_instance_invalid_trigger(self):%0A        instance = container_utils.create_trigger_instance(%7B'name': 'footrigger'%7D, %7B%7D, None)%0A        self.assertTrue(instance is None)%0A%0A    def test_add_trigger_type_no_params(self):%0A        # Trigger type with no params should create a trigger with same name as trigger type.%0A        trig_type = %7B%0A            'name': 'myawesometriggertype',%0A            'description': 'Words cannot describe how awesome I am.',%0A            'parameters_schema': %7B%7D,%0A            'payload_schema': %7B%7D%0A        %7D%0A        trigtype_dbs = container_utils.add_trigger_models(%5Btrig_type%5D)%0A        trigger_type, trigger = trigtype_dbs%5B0%5D%0A        trigtype_db = TriggerType.get_by_id(trigger_type.id)%0A        self.assertEqual(trigtype_db.name, trig_type.get('name'))%0A        self.assertTrue(trigger is not None)%0A        self.assertEqual(trigger.name, trigtype_db.name)%0A%0A    def test_add_trigger_type_with_params(self):%0A        # Trigger type with params should not create a trigger.%0A        PARAMETERS_SCHEMA = %7B%0A            %22type%22: %22object%22,%0A            %22properties%22: %7B%0A                %22url%22: %7B%22type%22: %22string%22%7D%0A            %7D,%0A            %22required%22: %5B'url'%5D,%0A            %22additionalProperties%22: False%0A        %7D%0A        trig_type = %7B%0A            'name': 'myawesometriggertype2',%0A            'description': 'Words cannot describe how awesome I am.',%0A            'parameters_schema': PARAMETERS_SCHEMA,%0A            'payload_schema': %7B%7D%0A        %7D%0A        trigtype_dbs = container_utils.add_trigger_models(%5Btrig_type%5D)%0A        trigger_type, trigger = trigtype_dbs%5B0%5D%0A        trigtype_db = TriggerType.get_by_id(trigger_type.id)%0A        self.assertEqual(trigtype_db.name, trig_type.get('name'))%0A        self.assertEqual(trigger, None)%0A

Test cases for reactor container utils