Package ndg :: Package xacml :: Package test :: Module test_functions
[hide private]

Source Code for Module ndg.xacml.test.test_functions

 1  #!/usr/bin/env python 
 2  """NDG XACML functions unit tests  
 3   
 4  NERC DataGrid 
 5  """ 
 6  __author__ = "P J Kershaw" 
 7  __date__ = "26/03/10" 
 8  __copyright__ = "(C) 2010 Science and Technology Facilities Council" 
 9  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
10  __license__ = "BSD - see LICENSE file in top-level directory" 
11  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
12  __revision__ = "$Id: test_functions.py 8020 2012-02-23 12:35:19Z pjkersha $" 
13  import unittest 
14  import logging 
15  logging.basicConfig(level=logging.DEBUG) 
16   
17  from ndg.xacml.core.functions import FunctionMap, AbstractFunction 
18  from ndg.xacml.core.functions.v2.regexp_match import RegexpMatchBase 
19  from ndg.xacml.core.context.exceptions import XacmlContextTypeError 
20   
21   
22 -def custom_function_factory(function_ns):
23 class CustomFunctionAbstractFunction(AbstractFunction): 24 FUNCTION_NS = 'urn:ndg:xacml:test:integer-square' 25 def evaluate(self, num): 26 """square an integer 27 28 @param num: number to square 29 @type num: int 30 @rtype: int 31 @raise TypeError: incorrect type for input 32 """ 33 if not isinstance(num, int): 34 raise XacmlContextTypeError('%r function expecting "int" type; ' 35 'got %r' % 36 (self.__class__.FUNCTION_NS, 37 type(num))) 38 39 return num * num
40 41 if function_ns == CustomFunctionAbstractFunction.FUNCTION_NS: 42 return CustomFunctionAbstractFunction 43 else: 44 return None 45 46
47 -class FunctionTestCase(unittest.TestCase):
48 """Test XACML functions implementation 49 50 The log output gives an indication of the XACML functions which are not 51 implemented yet""" 52
53 - def test01LoadMap(self):
54 funcMap = FunctionMap() 55 funcMap.loadAllCore() 56 anyUriMatchNs = \ 57 'urn:oasis:names:tc:xacml:2.0:function:anyURI-regexp-match' 58 59 self.assert_(issubclass(funcMap.get(anyUriMatchNs), RegexpMatchBase))
60
61 - def test01_add_custom_function(self):
62 func_map = FunctionMap() 63 func_map.loadAllCore() 64 func_map.load_custom_function('urn:ndg:xacml:test:integer-square', 65 custom_function_factory) 66 67 self.assertIn('urn:ndg:xacml:test:integer-square', func_map, 68 'custom function not added into map')
69 70 if __name__ == "__main__": 71 unittest.main() 72