#!/usr/bin/env python

# -*- coding: utf-8 -*-
# Copyright (c) 2016, Mayo Clinic
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
#     list of conditions and the following disclaimer.
#
#     Redistributions in binary form must reproduce the above copyright notice,
#     this list of conditions and the following disclaimer in the documentation
#     and/or other materials provided with the distribution.
#
#     Neither the name of the <ORGANIZATION> nor the names of its contributors
#     may be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
# OF THE POSSIBILITY OF SUCH DAMAGE.
import sys
import random

import pyjxslt

gw_key = "_k" + str(random.randint(10000, 19999))

xsl1 = """<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xpath-default-namespace="http://example.org/test"
    exclude-result-prefixes="xs" version="2.0">
    <xsl:template match="/doc">
        <xsl:for-each select="entry"> ENTRY: <xsl:value-of select="@id"/>:<xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>"""

xml1 = """<?xml version="1.0" encoding="UTF-8"?>
<doc xmlns="http://example.org/test">
    <entry id='17'>FOO</entry>
    <entry id='42'>BAR</entry>
</doc>"""

xml1out = """<?xml version="1.0" encoding="UTF-8"?> ENTRY: 17:FOO ENTRY: 42:BAR"""

jsonout = """{
  "_xmlns": "http://example.org/test",
  "doc": {
    "entry": [
      {
        "id": "17",
        "_content": "FOO"
      },
      {
        "id": "42",
        "_content": "BAR"
      }
    ]
  }
}"""


def test_result(expected, actual):
    rval = expected.strip() == actual.strip()
    print("success!" if rval else "FAIL!")
    return rval


def main(argv):
    gw = pyjxslt.Gateway(int(argv[0] if argv else pyjxslt.DEFAULT_PORT))

    print("\tTesting add and process transformation...", end='')
    gw.add_transform(gw_key, xsl1)
    test_result(xml1out, gw.transform(gw_key, xml1))
    gw.drop_transform(gw_key)

    print("\tTesting XML to JSON transform...", end='')
    test_result(jsonout, gw.to_json(xml1))


if __name__ == '__main__':
    main(sys.argv[1:])


