#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Test Utilities Trapped Services
-------------------------------
"""

import unittest

from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.comm import ServiceAccessPoint, ApplicationServiceElement, bind

from ..trapped_classes import TrappedServiceAccessPoint, \
    TrappedApplicationServiceElement

# some debugging
_debug = 0
_log = ModuleLogger(globals())


@bacpypes_debugging
class EchoAccessPoint(ServiceAccessPoint):

    def sap_indication(self, pdu):
        if _debug: EchoAccessPoint._debug("sap_indication %r", pdu)
        self.sap_response(pdu)

    def sap_confirmation(self, pdu):
        if _debug: EchoAccessPoint._debug("sap_confirmation %r", pdu)
        pass


class TrappedEchoAccessPoint(TrappedServiceAccessPoint, EchoAccessPoint):
    pass


@bacpypes_debugging
class EchoServiceElement(ApplicationServiceElement):

    def indication(self, pdu):
        if _debug: EchoServiceElement._debug("indication %r", pdu)
        self.response(pdu)

    def confirmation(self, pdu):
        if _debug: EchoServiceElement._debug("confirmation %r", pdu)
        pass


class TrappedEchoServiceElement(TrappedApplicationServiceElement, EchoServiceElement):
    pass


@bacpypes_debugging
def setup_module():
    if _debug: setup_module._debug("setup_module")

    # verify the echo access point is trapped correctly
    assert TrappedEchoAccessPoint.__mro__ == (
        TrappedEchoAccessPoint,
        TrappedServiceAccessPoint,
        EchoAccessPoint,
        ServiceAccessPoint,
        object,
        )

    # verify the echo service element is trapped correctly
    assert TrappedEchoServiceElement.__mro__ == (
        TrappedEchoServiceElement,
        TrappedApplicationServiceElement,
        EchoServiceElement,
        ApplicationServiceElement,
        object,
        )


@bacpypes_debugging
class TestApplicationService(unittest.TestCase):

    def test_application_service(self):
        if _debug: TestApplicationService._debug("test_application_service")

        # create an access point and a service element and bind them together
        sap = TrappedEchoAccessPoint()
        ase = TrappedEchoServiceElement()
        bind(ase, sap)

        # make pdu object
        pdu = object()

        # service access point is going to request something
        sap.sap_request(pdu)

        # make sure the request was sent and received
        assert sap.sap_request_sent is pdu
        assert ase.indication_received is pdu

        # make sure the echo response was sent and received
        assert ase.response_sent is pdu
        assert sap.sap_confirmation_received is pdu

        # make another pdu object
        pdu = object()

        # service element is going to request something
        ase.request(pdu)

        # make sure the request was sent and received
        assert ase.request_sent is pdu
        assert sap.sap_indication_received is pdu

        # make sure the echo response was sent and received
        assert sap.sap_response_sent is pdu
        assert ase.confirmation_received is pdu

@@ -430,16 +430,48 @@
 als())%0A%0A
+# globals%0Asap = None%0Aase = None%0A
 %0A@bacpyp
@@ -1374,16 +1374,36 @@
 module%22)
+%0A    global sap, ase
 %0A%0A    # 
@@ -1646,16 +1646,81 @@
      )%0A%0A
+    # create an access point%0A    sap = TrappedEchoAccessPoint()%0A%0A
     # ve
@@ -1991,127 +1991,171 @@
  )%0A%0A
-%0A@bacpypes_debugging%0Aclass TestApplicationService(unittest.TestCase):%0A%0A    def test_application_service(self):%0A   
+    # create a service element%0A    ase = TrappedEchoServiceElement()%0A%0A    # bind them together%0A    bind(ase, sap)%0A%0A%0A@bacpypes_debugging%0Adef teardown_module():%0A
     
- 
 if _
@@ -2165,248 +2165,334 @@
 ug: 
-TestApplicationService._debug(%22test_application_servic
+setup_module._debug(%22teardown_modul
 e%22)%0A
-%0A
     
-    # create an access point and a service element and bind them together%0A        sap = TrappedEchoAccessPoint()%0A        ase = TrappedEchoServiceElement()%0A        bind(ase, sap)
+global sap, ase%0A%0A    # toss the objects into the garbage%0A    sap = None%0A    ase = None%0A%0A%0A@bacpypes_debugging%0Aclass TestApplicationService(unittest.TestCase):%0A%0A    def test_sap_request(self):%0A        if _debug: TestApplicationService._debug(%22test_sap_request%22)%0A        global sap, ase
 %0A%0A  
@@ -2504,16 +2504,18 @@
  # make 
+a 
 pdu obje
@@ -2921,32 +2921,158 @@
 eceived is pdu%0A%0A
+    def test_ase_request(self):%0A        if _debug: TestApplicationService._debug(%22test_ase_request%22)%0A        global sap, ase%0A%0A
         # make a
@@ -3075,14 +3075,8 @@
 ke a
-nother
  pdu

move the setup to the module, split the one big test into two