import time

from Ft.Server.Client import FtServerClientException, Error
from Ft.Server.Common import ResourceTypes, Schema, ValidationInfo

import test_helper

def test_create(tester):


    tester.startTest("Create XML Document")

    #Create XML Document
    repo = test_helper.GetRepo(tester)
    test = repo.fetchResource("/test")

    doc = test.createDocument('doc1',DOC1,forcedType=ResourceTypes.ResourceType.XML_DOCUMENT)

    dMd = doc.getMetaDataResource().getContent()
    dContent = doc.getContent()


    tester.compare(ResourceTypes.ResourceType.XML_DOCUMENT,test.hasResource('doc1'))
    tester.compare(doc,test.fetchResource('doc1'))

    tester.compare(ResourceTypes.ResourceType.XML_DOCUMENT,doc.getResourceType())
    tester.compare(1,doc.isResourceType(ResourceTypes.ResourceType.RAW_FILE))
    tester.compare(1,doc.isResourceType(ResourceTypes.ResourceType.XML_DOCUMENT))
    tester.compare(0,doc.isResourceType(ResourceTypes.ResourceType.CONTAINER))
    repo.txCommit()

    repo = test_helper.GetRepo(tester)
    test = repo.fetchResource("/test")
    tester.compare(ResourceTypes.ResourceType.XML_DOCUMENT,test.hasResource('doc1'))
    repo.txRollback()

    repo = test_helper.GetRepo(tester)
    doc = repo.fetchResource("/test/doc1")
    test = repo.fetchResource('/test')
    tester.compare(test,doc.getParent())
    repo.txRollback()
                   
    tester.testDone()
    return




def test_access(tester):

    tester.startTest("XML Document Access")

    repo = test_helper.GetRepo(tester)
    doc = repo.fetchResource("/test/doc1")



    dMd = doc.getMetaDataResource().getContent()
    dContent = doc.getContent()


    tester.compare('text/xml',doc.getImt())
    tester.compare(DOC1,doc.getContent())
    test_helper.CompareValidationInfo(tester,ValidationInfo.NoValidation(),doc.getValidationInfo())
    #Just call, don't test
    doc.getCreationDate()
    doc.getLastModifiedDate()

    repo.txRollback()
    
    tester.testDone()


def test_modify(tester):

    tester.startTest("XML Document Modification")


    repo = test_helper.GetRepo(tester)
    doc = repo.fetchResource("/test/doc1")
    lmd = doc.getLastModifiedDate()
    time.sleep(1)
    doc.setContent(DOC2)

    dMd = doc.getMetaDataResource().getContent()
    dContent = doc.getContent()

    tester.compare(DOC2,doc.getContent())
    repo.txCommit()

    repo = test_helper.GetRepo(tester)
    doc = repo.fetchResource("/test/doc1")
    tester.compare(DOC2,doc.getContent())
    tester.compare(1,doc.getLastModifiedDate() > lmd)
    repo.txRollback()
    
    tester.testDone()


def test_delete(tester):

    tester.startTest("Delete XML Document")
    repo = test_helper.GetRepo(tester)
    doc = repo.fetchResource("/test/doc1")
    fp = doc.getAbsolutePath()
    test = doc.getParent()
    doc.delete()
    tester.compare(0,test.hasResource('doc1'))
    tester.compare(0,len(repo.getModel().complete(fp,None,None)))
    tester.testException(doc.getContent,(),FtServerClientException,{'code':Error.UNKNOWN_PATH})
    repo.txCommit()

    repo = test_helper.GetRepo(tester)
    test = repo.fetchResource("/test")
    tester.compare(0,test.hasResource('doc1'))
    tester.compare(0,len(repo.getModel().complete(fp,None,None)))
    repo.txRollback()

    tester.testDone()

def test_xml_interfaces(tester):

    tester.startTest("XML Document to DOM")
    repo = test_helper.GetRepo(tester)
    test = repo.fetchResource("/test")
    doc1 = test.createDocument('doc1',DOC1,forcedType=ResourceTypes.ResourceType.XML_DOCUMENT)

    dom = doc1.asDom()
    tester.compare('docelem',dom.documentElement.nodeName)
    repo.txCommit()
    tester.testDone()


def cleanup(tester):
    tester.startTest("Clean Up")
    repo = test_helper.GetRepo(tester)
    if repo.hasResource('/test'):
        t = repo.fetchResource('/test')
        t.delete()
    repo.createContainer("/test",1)
    repo.txCommit()
    tester.testDone()


DOC1="""<docelem v='foo'/>"""
DOC2="""<docelem w='foo'/>"""

SHEET1 = """<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
  Sheet 1
  </xsl:template>
</xsl:stylesheet>"""

SCHEMA1="""<schema xmlns='http://www.ascc.net/xml/schematron'>
    
  <pattern name='Root'>
    <rule context="/docelem">
      <assert test='@v'>
        VALIDATION ERROR: The root element must have a v attribute
      </assert>      
    </rule>
  </pattern>
  
</schema>"""


def Test(tester):

    cleanup(tester)
    test_create(tester)
    test_access(tester)
    test_modify(tester)
    test_delete(tester)
    test_xml_interfaces(tester)

