def runtest():

    #
    # -------------------------------------------------------------------
    # Mocking setup: Provide overrides for some of the component methods
    # -------------------------------------------------------------------
    #

    class MyBaseCapabilities(BaseCapabilities):
        def accessResource(self, resource_uri, input=None, params=None, method=HTTP.GET):
            return RESOURCE_DICT[resource_uri]

    #
    # --------------------------------------------------------------------
    # Provide a mock for the request header. Not all components need this,
    # but one of the service methods of this component actually accesses
    # the request header.
    # --------------------------------------------------------------------
    #
    class MyRequest(RestxHttpRequest):
        def getRequestURI(self):
            return "http://foo.com"
        def getRequestHeaders(self):
            return { "foo" : [ "one", "two" ] }

    #
    # -------------------------------------------------------------------
    # The actual tests
    # -------------------------------------------------------------------
    #

    #
    # Test 1: Always prefix, two matches
    #
    rctp = dict(
        api_key         = "abcdefg",
        foo_1           = None,
        foo_2           = None,
        foo_list        = [],
        bar_list        = ParameterDefNumberList.listToArray([]),
    )

    # Create a component with our mock capabilities and request classes
    c = make_component(rctp, TestComponent, MyBaseCapabilities, MyRequest)

    res = c.foobar(HttpMethod.GET, None, "xyz", BigDecimal(10))

    # This is a Java component, so we can call the languageStructToPython() method
    # to convert the output data into a native Python structure.
    out = languageStructToPython(c, res.entity)
    data = [ u"Some text", 123,
            { u"foo" : u"This is a test", u"bar" : {
                                                      u"some ArrayList" : [ u"Blah", 12345 ],
                                                      u"some value" : 1,
                                                      u"another value" : u"Some text" } } ]

    test_evaluator("Test 1", compare_any(data, out))

    return get_test_result()
Ejemplo n.º 2
0
def __javaServiceMethodProxy(component, request, method, method_name, input, params, http_method, is_proxy_component):
    """
    Calls service methods in Java components.
    
    Prepares parameters, converts exceptions and results.
    
    """
    # We remove the resource creation time parameters from the map and
    # assign them directly to the component as new attributes. After that,
    # the pruned parameter map can be passed as keyword arg dict to the
    # service method.
    if is_proxy_component:
        proxy_component_param_map = dict()
    for name in component.componentDescriptor.getParamMap().keySet():
        if name in params:
            if type(component.componentDescriptor.getParamMap().get(name)) is ParameterDefNumber:
                # Why do we have this? The default type for numeric parameters is
                # BigDecimal. We can't just assign a float (or other numeric value)
                # to a BigDecimal variable. Instead, we need to create a new
                # instance of that type explicitly.
                if is_proxy_component:
                    proxy_component_param_map[name] = BigDecimal(params[name])
                else:
                    setattr(component, name, BigDecimal(params[name]))
            else:
                if is_proxy_component:
                    proxy_component_param_map[name] = params[name]
                else:
                    if type(component.componentDescriptor.getParamMap().get(name)) is ParameterDefNumberList:
                        setattr(component, name, ParameterDefNumberList.listToArray([ BigDecimal(x) for x in params[name] ]))
                    else:
                        setattr(component, name, params[name])
                
            del params[name]

    if is_proxy_component:
        proxy_param_func = getattr(component, "_setResourceParams")
        proxy_param_func(proxy_component_param_map)
    try:
        param_order = component.getParameterOrder()[method_name]
        param_types = component.getParameterTypes()[method_name]
        # Assemble the list of additional service method parameters. We need
        # to perform a case, so we combine the type list and parameter name list,
        # index the parameter map and perform the cast all in one swoop.
        if param_order and param_types:
            # Yes, the following CAN be written as a single line with list comprehension,
            # but then the 'reader comprehension' suffers. So, I wrote it out explicitly.
            arglist = list()
            for param_type, name in zip(param_types, param_order):
                param_value = params[name]
                if param_type != type(param_value):
                    # Some type conversion is required
                    arglist.append(param_type(param_value))
                else:
                    # Type is already in the right argument (often the case when a default
                    # value was specified).
                    arglist.append(param_value)
        else:
            arglist = list()

        # Importing at this odd place here avoids circular imports in other places
        from resource_accessor import ResourceAccessor

        # Provide a conversion methods specific to this component's language.
        # Passing them to ResourceAccessor means that I don't have to import those
        # symbols in the resource_accessor module.        
        component.resourceAccessor = ResourceAccessor(__javaStructToPython, __pythonStructToJava)
        if type(input) in [ str, unicode ]:
            input = String(input if input is not None else "")
        if not is_proxy_component:
            res = method(http_method, input, *arglist)
        else:
            proxy_arg_list = [ http_method, input ]
            proxy_arg_list.extend(arglist)
            res = method(method_name, proxy_arg_list)

    except RestxException, e:
        raise e