• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/opal/ZSI/test/wsdl2py/test_DerivedTypes.py

00001 #!/usr/bin/env python
00002 ############################################################################
00003 # Joshua R. Boverhof, LBNL
00004 # See LBNLCopyright for copyright notice!
00005 ###########################################################################
00006 import os, sys, unittest
00007 from ServiceTest import main, ServiceTestCase, ServiceTestSuite
00008 from ZSI import EvaluateException, FaultException
00009 from ZSI.writer import SoapWriter
00010 from ZSI.parse import ParsedSoap
00011 from ZSI.TC import _get_type_definition as GTD
00012 from ZSI.TC import _get_global_element_declaration as GED
00013 
00014 """
00015 Unittest 
00016 
00017 WSDL:  derivedTypes.
00018 """
00019 
00020 # General targets
00021 def dispatch():
00022     """Run all dispatch tests"""
00023     suite = ServiceTestSuite()
00024     suite.addTest(unittest.makeSuite(DTTestCase, 'test_dispatch'))
00025     return suite
00026 
00027 def local():
00028     """Run all local tests"""
00029     suite = ServiceTestSuite()
00030     suite.addTest(unittest.makeSuite(DTTestCase, 'test_local'))
00031     return suite
00032 
00033 def net():
00034     """Run all network tests"""
00035     suite = ServiceTestSuite()
00036     suite.addTest(unittest.makeSuite(DTTestCase, 'test_net'))
00037     return suite
00038     
00039 def all():
00040     """Run all tests"""
00041     suite = ServiceTestSuite()
00042     suite.addTest(unittest.makeSuite(DTTestCase, 'test_'))
00043     return suite
00044 
00045 
00046 class DTTestCase(ServiceTestCase):
00047     name = "test_DerivedTypes"
00048     client_file_name = None
00049     types_file_name  = "test_DerivedTypes_xsd_types.py"
00050     server_file_name = None
00051 
00052     def __init__(self, methodName):
00053         ServiceTestCase.__init__(self, methodName)
00054         self.wsdl2py_args.append('-x')
00055         self.wsdl2py_args.append('-b')
00056 
00057     def test_local_ged_substitution(self):
00058         """This test is designed to fail, trying to dump
00059         a GED in via type substitution.
00060         """
00061         self.types_module
00062         pyobj = GED('urn:test', 'test').pyclass()
00063         
00064         # use GED of a derived type
00065         pyobj.Actor = sub = GED('urn:test', 'MiddleActor').pyclass()
00066         sub.Element1 = 'foo'
00067         sub.Element2 = 'bar'
00068         
00069         sw = SoapWriter()
00070         self.failUnlessRaises(TypeError, sw.serialize, pyobj)
00071         
00072     def test_local_type_substitution_test2(self):
00073         """test extension of extension"""
00074 
00075         attr1 = 'aone'
00076         attr2 = 'atwo'
00077         attr3 = 'athree'
00078         self.types_module
00079         pyobj = GED('urn:test', 'test2').pyclass()
00080 
00081         # Test maxOccurs>1 for substitution 
00082         # 
00083         pyobj.Actor = [GTD('urn:test', 'TopActor')(None).pyclass()]
00084         sub1 = pyobj.Actor[0]
00085         sub1.Element1 = 'one'
00086         sub1.Element2 = 'two'
00087         sub1.Element3 = 'three'
00088         sub1.set_attribute_attr1(attr1)
00089         sub1.set_attribute_attr2(attr2)
00090         sub1.set_attribute_attr3(attr3)
00091         
00092         sw = SoapWriter()
00093         sw.serialize(pyobj)
00094         xml = str(sw)
00095         ps = ParsedSoap(xml)
00096         pyobj2 = ps.Parse(pyobj.typecode)
00097         sub2 = pyobj2.Actor[0]
00098 
00099         self.failUnless(sub2.get_attribute_attr1() == attr1, 'bad attribute 1')
00100         self.failUnless(sub2.get_attribute_attr2() == attr2, 'bad attribute 2')
00101         self.failUnless(sub2.get_attribute_attr3() == attr3, 'bad attribute 3')
00102 
00103         self.failUnless(sub2.Element1 == sub1.Element1, 'bad element 1')
00104         self.failUnless(sub2.Element2 == sub1.Element2, 'bad element 2')
00105         self.failUnless(sub2.Element3 == sub1.Element3, 'bad element 3')
00106                 
00107         # check parsed out correct type
00108         self.failUnless(isinstance(sub2.typecode, sub1.typecode.__class__), 
00109             'local element actor "%s" must be an instance of "%s"'%
00110                 (sub2.typecode, sub1.typecode.__class__))
00111         
00112         # check local element is derived from base
00113         base = GTD('urn:test', 'BaseActor')
00114         self.failUnless(isinstance(sub2.typecode, base), 
00115             'local element actor must be a derived type of "%s"'%
00116                 base)
00117 
00118         
00119     def test_local_type_substitution2(self):
00120         """test extension of extension"""
00121 
00122         attr1 = 'aone'
00123         attr2 = 'atwo'
00124         attr3 = 'athree'
00125         self.types_module
00126         pyobj = GED('urn:test', 'test').pyclass()
00127 
00128         # [ 1489129 ] Unexpected subsitution error message
00129         #  try to parse before type ever initialized
00130         """
00131         ps = ParsedSoap(MSG1)
00132         pyobj0 = ps.Parse(pyobj.typecode)
00133         sub0 = pyobj0.Actor
00134         self.failUnless(sub0.get_attribute_attr1() == attr1, 'bad attribute1')
00135         self.failUnless(sub0.get_attribute_attr2() == attr2, 'bad attribute2')
00136         """
00137 
00138         # [ 1489090 ] Derived type attributes don't populate the attr dictionary
00139         # [ 1489677 ] Derivation from derived type missing derived element
00140         # 
00141         pyobj.Actor = sub1 = GTD('urn:test', 'TopActor')(None).pyclass()
00142         sub1.Element1 = 'one'
00143         sub1.Element2 = 'two'
00144         sub1.Element3 = 'three'
00145         sub1.set_attribute_attr1(attr1)
00146         sub1.set_attribute_attr2(attr2)
00147         sub1.set_attribute_attr3(attr3)
00148         
00149         sw = SoapWriter()
00150         sw.serialize(pyobj)
00151         xml = str(sw)
00152         ps = ParsedSoap(xml)
00153         pyobj2 = ps.Parse(pyobj.typecode)
00154         sub2 = pyobj2.Actor
00155 
00156         self.failUnless(sub2.get_attribute_attr1() == attr1, 'bad attribute 1')
00157         self.failUnless(sub2.get_attribute_attr2() == attr2, 'bad attribute 2')
00158         self.failUnless(sub2.get_attribute_attr3() == attr3, 'bad attribute 3')
00159 
00160         self.failUnless(sub2.Element1 == sub1.Element1, 'bad element 1')
00161         self.failUnless(sub2.Element2 == sub1.Element2, 'bad element 2')
00162         self.failUnless(sub2.Element3 == sub1.Element3, 'bad element 3')
00163                 
00164         # check parsed out correct type
00165         self.failUnless(isinstance(sub2.typecode, sub1.typecode.__class__), 
00166             'local element actor "%s" must be an instance of "%s"'%
00167                 (sub2.typecode, sub1.typecode.__class__))
00168         
00169         # check local element is derived from base
00170         base = GTD('urn:test', 'BaseActor')
00171         self.failUnless(isinstance(sub2.typecode, base), 
00172             'local element actor must be a derived type of "%s"'%
00173                 base)
00174 
00175     def test_local_parse_missing_type_substitution(self):
00176         """attempt to substitute an unregistered/unknown type """
00177         attr1 = 'myclass'
00178         attr2 = 'whatever'
00179         self.types_module
00180         pyobj = GED('urn:test', 'test').pyclass()
00181 
00182         ps = ParsedSoap(NO_SUB_MSG)
00183         self.failUnlessRaises(EvaluateException, ps.Parse, pyobj.typecode)
00184 
00185     def test_local_type_substitution1(self):
00186         """test extension.   Parse known instance, serialize an equivalent, Parse it back. """
00187         attr1 = 'myclass'
00188         attr2 = 'whatever'
00189         self.types_module
00190         pyobj = GED('urn:test', 'test').pyclass()
00191 
00192         # [ 1489129 ] Unexpected subsitution error message
00193         #  try to parse before type ever initialized
00194         ps = ParsedSoap(MSG1)
00195         pyobj0 = ps.Parse(pyobj.typecode)
00196         sub0 = pyobj0.Actor
00197         self.failUnless(sub0.get_attribute_attr1() == attr1, 'bad attribute1')
00198         self.failUnless(sub0.get_attribute_attr2() == attr2, 'bad attribute2')
00199 
00200         # [ 1489090 ] Derived type attributes don't populate the attr dictionary
00201         # 
00202         pyobj.Actor = sub1 = GTD('urn:test', 'MiddleActor')(None).pyclass()
00203         sub1.Element1 = 'foo'
00204         sub1.Element2 = 'bar'
00205         sub1.set_attribute_attr1(attr1)
00206         sub1.set_attribute_attr2(attr2)
00207         
00208         sw = SoapWriter()
00209         sw.serialize(pyobj)
00210         xml = str(sw)
00211         ps = ParsedSoap(xml)
00212         pyobj2 = ps.Parse(pyobj.typecode)
00213         sub2 = pyobj2.Actor
00214 
00215         self.failUnless(sub2.get_attribute_attr1() == attr1, 'bad attribute class')
00216         self.failUnless(sub2.get_attribute_attr2() == attr2, 'bad attribute name')
00217                 
00218         # check parsed out correct type
00219         self.failUnless(isinstance(sub2.typecode, sub1.typecode.__class__), 
00220             'local element actor "%s" must be an instance of "%s"'%
00221                 (sub2.typecode, sub1.typecode.__class__))
00222         
00223         # check local element is derived from base
00224         base = GTD('urn:test', 'BaseActor')
00225         self.failUnless(isinstance(sub2.typecode, base), 
00226             'local element actor must be a derived type of "%s"'%
00227                 base)
00228         
00229 
00230 MSG1 = """<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ZSI="http://www.zolera.com/schemas/ZSI/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header></SOAP-ENV:Header><SOAP-ENV:Body xmlns:ns1="urn:test"><ns1:test><actor attr1="myclass" attr2="whatever" xsi:type="ns1:MiddleActor"><element1 xsi:type="xsd:string">foo</element1><element2 xsi:type="xsd:string">bar</element2></actor></ns1:test></SOAP-ENV:Body></SOAP-ENV:Envelope>"""
00231 
00232 NO_SUB_MSG = """<SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ZSI="http://www.zolera.com/schemas/ZSI/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><SOAP-ENV:Header></SOAP-ENV:Header><SOAP-ENV:Body xmlns:ns1="urn:test"><ns1:test><actor attr1="myclass" attr2="whatever" xsi:type="ns1:Bogus"><element1 xsi:type="xsd:string">foo</element1><element2 xsi:type="xsd:string">bar</element2></actor></ns1:test></SOAP-ENV:Body></SOAP-ENV:Envelope>"""
00233 
00234 if __name__ == "__main__" :
00235     main()
00236 

Generated on Wed Oct 20 2010 11:12:17 for APBS by  doxygen 1.7.2