#!/usr/bin/env python

# Contributors:
# Christopher P. Barnes <senrabc@gmail.com>
# Andrei Sura: github.com/indera
# Mohan Das Katragadda <mohan.das142@gmail.com>
# Philip Chase <philipbchase@gmail.com>
# Ruchi Vivek Desai <ruchivdesai@gmail.com>
# Taeber Rapczak <taeber@ufl.edu>
# Nicholas Rejack <nrejack@ufl.edu>
# Josh Hanna <josh@hanna.io>
# Copyright (c) 2014-2015, University of Florida
# All rights reserved.
#
# Distributed under the BSD 3-Clause License
# For full text of the BSD 3-Clause License see http://opensource.org/licenses/BSD-3-Clause

import unittest
import os
import logging
import shutil
import tempfile
import pysftp
from mock import patch
from redi.utils import GetEmrData
from redi.utils.GetEmrData import EmrFileAccessDetails

import time
from subprocess import Popen

# Try to silence extra info
logging.getLogger("paramico").addHandler(logging.NullHandler)
logging.getLogger("pysftp").addHandler(logging.NullHandler)

class TestGetEMRData(unittest.TestCase):

    def setUp(self):
        pass


    def _noop(*args, **kwargs):
        pass


    @patch.multiple(pysftp, Connection=_noop)
    @patch.multiple(GetEmrData, download_files=_noop)
    def test_get_emr_data(self):
        """
        This test verifies only that the csv file on the sftp server
        can be transformed to an xml file.
        Note: This test is not concerned with testing the sftp communication"""
        temp_folder = tempfile.mkdtemp('/')
        temp_txt_file = os.path.join(temp_folder, "raw.txt")
        temp_escaped_file = os.path.join(temp_folder, "rawEscaped.txt")
        temp_xml_file = os.path.join(temp_folder, "raw.xml")

        input_string = '''"NAME","COMPONENT_ID","RESULT","REFERENCE_UNIT","DATE_TIME_STAMP","STUDY_ID"
"RNA","1905","<5","IU/mL","1907-05-21 05:50:00","999-0059"
"EGFR","1740200","eGFR result is => 60 ml/min/1.73M2","ml/min/1.73M2","1903-11-27 15:13:00","999-0059"
"HEMATOCRIT","1534436",">27&<30","%","","999-0059"'''

        with open(temp_txt_file, 'w+') as f:
            f.write(input_string)

        props = EmrFileAccessDetails(
            emr_sftp_project_name='/',
            emr_download_list='raw.csv',
            emr_host='localhost',
            emr_username='admin',
            emr_password='admin',
            emr_port='7788',
            emr_private_key=None,
            emr_private_key_pass=None,
            )

        GetEmrData.get_emr_data(temp_folder, props)
        GetEmrData.data_preprocessing(temp_txt_file, temp_escaped_file)
        GetEmrData.generate_xml(temp_escaped_file, temp_xml_file)

        with open(temp_xml_file) as f:
            result = f.read()
        expected = '''<?xml version="1.0" encoding="utf8"?>
<study>
    <subject>
        <NAME>RNA</NAME>
        <COMPONENT_ID>1905</COMPONENT_ID>
        <RESULT>&lt;5</RESULT>
        <REFERENCE_UNIT>IU/mL</REFERENCE_UNIT>
        <DATE_TIME_STAMP>1907-05-21 05:50:00</DATE_TIME_STAMP>
        <STUDY_ID>999-0059</STUDY_ID>
    </subject>
    <subject>
        <NAME>EGFR</NAME>
        <COMPONENT_ID>1740200</COMPONENT_ID>
        <RESULT>eGFR result is =&gt; 60 ml/min/1.73M2</RESULT>
        <REFERENCE_UNIT>ml/min/1.73M2</REFERENCE_UNIT>
        <DATE_TIME_STAMP>1903-11-27 15:13:00</DATE_TIME_STAMP>
        <STUDY_ID>999-0059</STUDY_ID>
    </subject>
    <subject>
        <NAME>HEMATOCRIT</NAME>
        <COMPONENT_ID>1534436</COMPONENT_ID>
        <RESULT>&gt;27&amp;&lt;30</RESULT>
        <REFERENCE_UNIT>%</REFERENCE_UNIT>
        <DATE_TIME_STAMP></DATE_TIME_STAMP>
        <STUDY_ID>999-0059</STUDY_ID>
    </subject>
</study>
'''
        self.assertEqual(result, expected)
        shutil.rmtree(temp_folder)


    def test_pysftp_using_rsa_key(self):
        """
        Starts a sftp server and transfers a file
        to verify the connection using a private key.

        Notes: