# Path: rwslib/extras/audit_event/parser.py
# ODM_NS = '{http://www.cdisc.org/ns/odm/v1.3}'
# MDSOL_NS = '{http://www.mdsol.com/ns/odm/metadata}'
# DEFAULT_TRANSACTION_TYPE = u'Upsert'
# A_AUDIT_SUBCATEGORY_NAME = mdsol('AuditSubCategoryName')
# A_METADATA_VERSION_OID = 'MetaDataVersionOID'
# A_STUDY_OID = 'StudyOID'
# A_TRANSACTION_TYPE = 'TransactionType'
# A_SUBJECT_NAME = mdsol('SubjectName')
# A_SUBJECT_KEY = 'SubjectKey'
# A_USER_OID = 'UserOID'
# A_LOCATION_OID = 'LocationOID'
# A_ITEM_OID = 'ItemOID'
# A_VALUE = 'Value'
# A_STUDYEVENT_OID = 'StudyEventOID'
# A_STUDYEVENT_REPEAT_KEY = 'StudyEventRepeatKey'
# A_RECORD_ID = mdsol('RecordId')
# A_FORM_OID = 'FormOID'
# A_FORM_REPEAT_KEY = 'FormRepeatKey'
# A_ITEMGROUP_OID = 'ItemGroupOID'
# A_ITEMGROUP_REPEAT_KEY = 'ItemGroupRepeatKey'
# A_QUERY_REPEAT_KEY = 'QueryRepeatKey'
# A_STATUS = 'Status'
# A_RECIPIENT = 'Recipient'
# A_RESPONSE = 'Response'
# A_FREEZE = mdsol('Freeze')
# A_VERIFY = mdsol('Verify')
# A_LOCK = mdsol('Lock')
# A_SUBJECT_STATUS = mdsol('Status')
# A_PROTCOL_DEVIATION_REPEAT_KEY = 'ProtocolDeviationRepeatKey'
# A_CLASS = 'Class'  # PV
# A_CODE = 'Code'  # PV
# A_REVIEWED = 'Reviewed'  # Reviews
# A_GROUP_NAME = 'GroupName'
# A_COMMENT_REPEAT_KEY = 'CommentRepeatKey'
# A_INSTANCE_ID = mdsol('InstanceId')
# A_INSTANCE_NAME = mdsol('InstanceName')
# A_INSTANCE_OVERDUE = mdsol('InstanceOverdue')
# A_DATAPAGE_NAME = mdsol('DataPageName')
# A_DATAPAGE_ID = mdsol('DataPageId')
# A_SIGNATURE_OID = 'SignatureOID'
# A_SPECIFY_VALUE = mdsol('SpecifyValue')
# A_SIGNATURE_BROKEN = mdsol('SignatureBroken')
# E_CLINICAL_DATA = odm('ClinicalData')
# E_SUBJECT_DATA = odm('SubjectData')
# E_USER_REF = odm('UserRef')
# E_SOURCE_ID = odm('SourceID')
# E_DATE_TIME_STAMP_ = odm('DateTimeStamp')
# E_REASON_FOR_CHANGE = odm('ReasonForChange')
# E_LOCATION_REF = odm('LocationRef')
# E_STUDY_EVENT_DATA = odm('StudyEventData')
# E_FORM_DATA = odm('FormData')
# E_ITEM_GROUP_DATA = odm('ItemGroupData')
# E_ITEM_DATA = odm('ItemData')
# E_QUERY = mdsol('Query')
# E_PROTOCOL_DEVIATION = mdsol('ProtocolDeviation')
# E_REVIEW = mdsol('Review')
# E_COMMENT = mdsol('Comment')
# E_SIGNATURE = odm('Signature')
# E_SIGNATURE_REF = odm('SignatureRef')
# STATE_NONE = 0
# STATE_SOURCE_ID = 1
# STATE_DATETIME = 2
# STATE_REASON_FOR_CHANGE = 3
# AUDIT_REF_STATE = 0
# SIGNATURE_REF_STATE = 1
# def odm(value):
# def mdsol(value):
# def yes_no_none(value):
# def make_int(value, missing=-1):
#     def __init__(self, handler):
#     def emit(self):
#     def start(self, tag, attrib):
#     def end(self, tag):
#     def get_parent_element(self):
#     def data(self, data):
#     def close(self):
# def parse(data, eventer):
# class ODMTargetParser(object):


from rwslib.extras.audit_event import parser
import unittest
import os
class MockEventerEntered:
    """
    Mock Event Sink instance for the purposes of testing (capturing only 'Entered' ASC)
    """

    def __init__(self):
        self.__events = {}

    def get_audit_subcategory_events(self, audit_subcategory):
        return self.__events.get(audit_subcategory, [])

    def Entered(self, event):
        self.__events.setdefault(event.subcategory, []).append(event)

    @property
    def eventlist(self):
        return self.__events.keys()


class TestAuditEvent(unittest.TestCase):
    """
    Test Case for Audit Event Processor
    """
    def test_parses_audit_message(self):
        """parses an audit message from a CAR message"""
        with open(
            os.path.join(os.path.dirname(__file__), "fixtures", "car_message.xml")
        ) as fh:
            content = fh.read()
        eventer = MockEventer()

# Path: rwslib/extras/audit_event/parser.py
# ODM_NS = '{http://www.cdisc.org/ns/odm/v1.3}'
# MDSOL_NS = '{http://www.mdsol.com/ns/odm/metadata}'
# DEFAULT_TRANSACTION_TYPE = u'Upsert'
# A_AUDIT_SUBCATEGORY_NAME = mdsol('AuditSubCategoryName')
# A_METADATA_VERSION_OID = 'MetaDataVersionOID'
# A_STUDY_OID = 'StudyOID'
# A_TRANSACTION_TYPE = 'TransactionType'
# A_SUBJECT_NAME = mdsol('SubjectName')
# A_SUBJECT_KEY = 'SubjectKey'
# A_USER_OID = 'UserOID'
# A_LOCATION_OID = 'LocationOID'
# A_ITEM_OID = 'ItemOID'
# A_VALUE = 'Value'
# A_STUDYEVENT_OID = 'StudyEventOID'
# A_STUDYEVENT_REPEAT_KEY = 'StudyEventRepeatKey'
# A_RECORD_ID = mdsol('RecordId')
# A_FORM_OID = 'FormOID'
# A_FORM_REPEAT_KEY = 'FormRepeatKey'
# A_ITEMGROUP_OID = 'ItemGroupOID'
# A_ITEMGROUP_REPEAT_KEY = 'ItemGroupRepeatKey'
# A_QUERY_REPEAT_KEY = 'QueryRepeatKey'
# A_STATUS = 'Status'
# A_RECIPIENT = 'Recipient'
# A_RESPONSE = 'Response'
# A_FREEZE = mdsol('Freeze')
# A_VERIFY = mdsol('Verify')
# A_LOCK = mdsol('Lock')
# A_SUBJECT_STATUS = mdsol('Status')
# A_PROTCOL_DEVIATION_REPEAT_KEY = 'ProtocolDeviationRepeatKey'
# A_CLASS = 'Class'  # PV
# A_CODE = 'Code'  # PV
# A_REVIEWED = 'Reviewed'  # Reviews
# A_GROUP_NAME = 'GroupName'
# A_COMMENT_REPEAT_KEY = 'CommentRepeatKey'
# A_INSTANCE_ID = mdsol('InstanceId')
# A_INSTANCE_NAME = mdsol('InstanceName')
# A_INSTANCE_OVERDUE = mdsol('InstanceOverdue')
# A_DATAPAGE_NAME = mdsol('DataPageName')
# A_DATAPAGE_ID = mdsol('DataPageId')
# A_SIGNATURE_OID = 'SignatureOID'
# A_SPECIFY_VALUE = mdsol('SpecifyValue')
# A_SIGNATURE_BROKEN = mdsol('SignatureBroken')
# E_CLINICAL_DATA = odm('ClinicalData')
# E_SUBJECT_DATA = odm('SubjectData')
# E_USER_REF = odm('UserRef')
# E_SOURCE_ID = odm('SourceID')
# E_DATE_TIME_STAMP_ = odm('DateTimeStamp')
# E_REASON_FOR_CHANGE = odm('ReasonForChange')
# E_LOCATION_REF = odm('LocationRef')
# E_STUDY_EVENT_DATA = odm('StudyEventData')
# E_FORM_DATA = odm('FormData')
# E_ITEM_GROUP_DATA = odm('ItemGroupData')
# E_ITEM_DATA = odm('ItemData')
# E_QUERY = mdsol('Query')
# E_PROTOCOL_DEVIATION = mdsol('ProtocolDeviation')
# E_REVIEW = mdsol('Review')
# E_COMMENT = mdsol('Comment')
# E_SIGNATURE = odm('Signature')
# E_SIGNATURE_REF = odm('SignatureRef')
# STATE_NONE = 0
# STATE_SOURCE_ID = 1
# STATE_DATETIME = 2
# STATE_REASON_FOR_CHANGE = 3
# AUDIT_REF_STATE = 0
# SIGNATURE_REF_STATE = 1
# def odm(value):
# def mdsol(value):
# def yes_no_none(value):
# def make_int(value, missing=-1):
#     def __init__(self, handler):
#     def emit(self):
#     def start(self, tag, attrib):
#     def end(self, tag):
#     def get_parent_element(self):
#     def data(self, data):
#     def close(self):
# def parse(data, eventer):
# class ODMTargetParser(object):


# Path: rwslib/tests/test_audit_event.py
from rwslib.extras.audit_event import parser
import unittest
import os

class MockEventerEntered:
    """
    Mock Event Sink instance for the purposes of testing (capturing only 'Entered' ASC)
    """

    def __init__(self):
        self.__events = {}

    def get_audit_subcategory_events(self, audit_subcategory):
        return self.__events.get(audit_subcategory, [])

    def Entered(self, event):
        self.__events.setdefault(event.subcategory, []).append(event)

    @property
    def eventlist(self):
        return self.__events.keys()


class TestAuditEvent(unittest.TestCase):
    """
    Test Case for Audit Event Processor
    """
    def test_parses_audit_message(self):
        """parses an audit message from a CAR message"""
        with open(
            os.path.join(os.path.dirname(__file__), "fixtures", "car_message.xml")
        ) as fh:
            content = fh.read()
        eventer = MockEventer()

        message = parser.parse(content, eventer)