class TestEventState(unittest.TestCase):
    def setUp(self):
        self._log = logging.getLogger( "TestEventState" )
        self.router = None
        self.loader = None
        self.eventState = None
        return

    def tearDown(self):
        if self.eventState:
            self.eventState.stop( self.router )
            self.eventState = None

        if self.loader:
            self.loader.stop()  # all tasks
            self.loader = None
        self.router = None

        time.sleep(1)

    # Actual tests follow

    def TestEventState(self):
        # create testDespatch
        self.loader = EventRouterLoader()
        self.loader.loadHandlers( getDictFromXmlString(testConfigEvent) )
        self.loader.start()  # all tasks
        self.router = self.loader.getEventRouter()

        self.eventState = EventState()

        self.eventState.start( self.router );

        # request URL from .. 
        q1 = self.eventState.queryCache( 'webbrick/100/DO/0', 'state' )
        self.assertEqual( q1["stsval"], None )
        self.assertEqual( q1["stserr"], 'Not Known' )

        # send event
        self.router.publish( EventSource("TestEventState"), EventHandlers.tests.Events.evtDO_0_off )    # 0 Off
        # request good URL
        q2 = self.eventState.queryCache( 'webbrick/100/DO/0', 'state' )
        self.assertEqual( q2["stsval"], '0' )
        self.assertEqual( q2["stserr"], None )

        # request bad URL
        q3 = self.eventState.queryCache( 'webbrick/100/DO/1', 'state' )
        self.assertEqual( q3["stsval"], None )
        self.assertEqual( q3["stserr"], 'Not Known' )
    def TestEventState(self):
        # create testDespatch
        self.loader = EventRouterLoader()
        self.loader.loadHandlers( getDictFromXmlString(testConfigEvent) )
        self.loader.start()  # all tasks
        self.router = self.loader.getEventRouter()

        self.eventState = EventState()

        self.eventState.start( self.router );

        # request URL from .. 
        q1 = self.eventState.queryCache( 'webbrick/100/DO/0', 'state' )
        self.assertEqual( q1["stsval"], None )
        self.assertEqual( q1["stserr"], 'Not Known' )

        # send event
        self.router.publish( EventSource("TestEventState"), EventHandlers.tests.Events.evtDO_0_off )    # 0 Off
        # request good URL
        q2 = self.eventState.queryCache( 'webbrick/100/DO/0', 'state' )
        self.assertEqual( q2["stsval"], '0' )
        self.assertEqual( q2["stserr"], None )

        # request bad URL
        q3 = self.eventState.queryCache( 'webbrick/100/DO/1', 'state' )
        self.assertEqual( q3["stsval"], None )
        self.assertEqual( q3["stserr"], 'Not Known' )