Пример #1
0
    def test_boolean(self):
        input_val = 'true'
        expected_val = BooleanValue(True)
        actual_val = DataValueConverter.convert_to_data_value(input_val)
        self.assertEqual(expected_val, actual_val)

        input_val = 'false'
        expected_val = BooleanValue(False)
        actual_val = DataValueConverter.convert_to_data_value(input_val)
        self.assertEqual(expected_val, actual_val)
Пример #2
0
    def test_integer(self):
        input_val = '10'
        expected_val = IntegerValue(10)
        actual_val = DataValueConverter.convert_to_data_value(input_val)
        self.assertEqual(expected_val, actual_val)

        input_val = '1000000000000000000000000000000'
        long_val = 1000000000000000000000000000000
        expected_val = IntegerValue(long_val)
        actual_val = DataValueConverter.convert_to_data_value(input_val)
        self.assertEqual(expected_val, actual_val)
Пример #3
0
    def test_optional_value(self):
        input_val = OptionalValue(StringValue('val1'))
        actual_val = DataValueConverter.convert_to_json(input_val)
        expected_val = '"val1"'
        self.assertEqual(expected_val, actual_val)
        # Verify json is valid
        json.loads(actual_val)

        input_val = OptionalValue()
        actual_val = DataValueConverter.convert_to_json(input_val)
        expected_val = 'null'
        self.assertEqual(expected_val, actual_val)
        # Verify json is valid
        json.loads(actual_val)
Пример #4
0
 def test_string_value(self):
     input_val = StringValue('val1')
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = '"val1"'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #5
0
    def test_double_value(self):
        input_val = DoubleValue(10.10)
        actual_val = DataValueConverter.convert_to_json(input_val)
        expected_val = '1.01E1'
        self.assertEqual(expected_val, actual_val)
        # Verify json is valid
        output = json.loads(actual_val, parse_float=decimal.Decimal)
        self.assertEqual(output, decimal.Decimal('10.10'))

        input_val = DoubleValue(decimal.Decimal('10.10'))
        actual_val = DataValueConverter.convert_to_json(input_val)
        expected_val = '1.01E1'
        self.assertEqual(expected_val, actual_val)
        # Verify json is valid
        output = json.loads(actual_val, parse_float=decimal.Decimal)
        self.assertEqual(output, decimal.Decimal('10.10'))
Пример #6
0
 def test_default(self):
     input_val = 'hello'
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = '"hello"'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #7
0
 def test_secret_value(self):
     input_val = SecretValue('password')
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = '"password"'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #8
0
 def test_void_value(self):
     input_val = VoidValue()
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = 'null'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #9
0
 def test_boolean_value(self):
     input_val = BooleanValue(True)
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = 'true'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #10
0
 def test_integer_value(self):
     input_val = IntegerValue(10)
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = '10'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #11
0
 def test_list(self):
     input_val = '["val1","val2"]'
     expected_val = ListValue(
         values=[StringValue('val1'),
                 StringValue('val2')])
     actual_val = DataValueConverter.convert_to_data_value(input_val)
     self.assertEqual(expected_val, actual_val)
Пример #12
0
    def test_binary_value(self):
        input_val = BlobValue(b'asdlkfu')
        actual_val = DataValueConverter.convert_to_json(input_val)
        expected_val = '"%s"' % base64.b64encode(b'asdlkfu').decode('utf-8')

        self.assertEqual(expected_val, actual_val)
        # Verify json is valid
        json.loads(actual_val)
Пример #13
0
 def test_dict(self):
     input_val = '{"int_field":10, "bool_field":true}'
     values = {
         'bool_field': BooleanValue(True),
         'int_field': IntegerValue(10)
     }
     expected_val = StructValue(values=values)
     actual_val = DataValueConverter.convert_to_data_value(input_val)
     self.assertEqual(expected_val, actual_val)
Пример #14
0
 def test_list_value(self):
     input_val = ListValue(
         values=[StringValue('val1'),
                 StringValue('val2')])
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = '["val1","val2"]'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #15
0
    def _serialize_output(self, request, service_id, operation_id,
                          method_result, use_cookies):
        """
        Serialize the MethodResult object

        :type  request: :class:`werkzeug.wrappers.Request`
        :param request: Request object
        :type  service_id: :class:`str`
        :param service_id: Identifier of the service to be invoked.
        :type  operation_id: :class:`str`
        :param operation_id: Identifier of the operation to be invoked.
        :type  method_result: :class:`vmware.vapi.core.MethodResult`
        :param method_result: MethodResult object to be serialized
        :type  use_cookies: :class:`bool`
        :param use_cookies: Whether cookies are to be sent in response
        :rtype: :class:`tuple` of :class:`int`, :class:`str`, :class:`dict`
        :return: HTTP status code, serialized json output of the operation and
            a dictionary of response cookies.
        """
        if method_result.success():
            if self.response_cookie_builder is None or not use_cookies:
                cookies = None
            else:
                cookies = self.response_cookie_builder.build(
                    service_id, operation_id, method_result)
            output = method_result.output
            json_output = None
            if not isinstance(output, VoidValue):
                if not isinstance(output, StructValue):
                    # If the output is not a struct or error value,
                    # box it with {'value':..} to make it a
                    # good json output
                    output = StructValue(name='output',
                                         values={'value': output})
                json_output = DataValueConverter.convert_to_json(output)
            status = 204 if request.method == 'DELETE' else 200
        else:
            cookies = None
            error = method_result.error
            json_output = DataValueConverter.convert_to_json(error)
            status = vapi_to_http_error_map.get(error.name, 500)
        return (status, json_output, cookies)
Пример #16
0
 def test_error_value(self):
     input_val = ErrorValue(name='name',
                            values={
                                'val1': StringValue('val1'),
                                'val2': StringValue('val2')
                            })
     actual_val = DataValueConverter.convert_to_json(input_val)
     self.assertTrue('"val1":"val1"' in actual_val)
     self.assertTrue('"val2":"val2"' in actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #17
0
 def test_struct_value_with_optional_unset(self):
     input_val = StructValue(name='name',
                             values={
                                 'val1': StringValue('val1'),
                                 'val2': OptionalValue()
                             })
     actual_val = DataValueConverter.convert_to_json(input_val)
     expected_val = '{"val1":"val1"}'
     self.assertEqual(expected_val, actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #18
0
 def test_struct_value_with_optional_set(self):
     input_val = StructValue(name='name',
                             values={
                                 'val1': StringValue('val1'),
                                 'val2': OptionalValue(StringValue('val2'))
                             })
     actual_val = DataValueConverter.convert_to_json(input_val)
     self.assertTrue('"val1":"val1"' in actual_val)
     self.assertTrue('"val2":"val2"' in actual_val)
     # Verify json is valid
     json.loads(actual_val)
Пример #19
0
    def __call__(self, request):
        """
        The implementation of WsgiApplication

        :type  request: :class:`werkzeug.wrappers.Request`
        :param request: Request object
        :rtype: :class:`werkzeug.wrappers.Response`
        :return: Response object
        """
        try:
            urls = self.rule_map.bind_to_environ(request.environ)
            endpoint, args = urls.match()
            if provider_wire_logger.isEnabledFor(logging.DEBUG):
                provider_wire_logger.debug('HTTP %s %s ', request.method,
                                           request.url)
                provider_wire_logger.debug('REQUEST HEADERS: %s',
                                           request.headers)
                provider_wire_logger.debug('REQUEST BODY: %s',
                                           request.get_data())
            if endpoint == JSONRPC:
                result, headers = self._handle_jsonrpc_call(request)
                response = Response(result)
                if headers:
                    response.headers = headers
            else:
                status, result, cookies, headers = self._handle_rest_call(
                    request, endpoint, args)
                response = Response(result)
                response.status_code = status
                if headers:
                    response.headers = headers
                if cookies:
                    path = self._rest_prefix
                    for k, v in six.iteritems(cookies):
                        response.set_cookie(k, v, path=path)
            response.content_type = JSON_CONTENT_TYPE
            if provider_wire_logger.isEnabledFor(logging.DEBUG):
                provider_wire_logger.debug('RESPONSE STATUS: %s',
                                           response.status_code)
                provider_wire_logger.debug('RESPONSE HEADERS: %s',
                                           response.headers)
                provider_wire_logger.debug('RESPONSE BODY: %s',
                                           response.response)
            return response
        except HTTPException as e:
            try:
                internal_server_error_def = make_std_error_def(
                    'com.vmware.vapi.std.errors.invalid_request')
                msg = message_factory.get_message(
                    'vapi.method.invoke.exception',  # pylint: disable=line-too-long
                    e.description)
                error_value = make_error_value_from_msgs(
                    internal_server_error_def, msg)
                json_output = DataValueConverter.convert_to_json(error_value)
                response = e.get_response()
                response.set_data(json_output)
                response.headers['Content-Type'] = 'application/json'
                return response
            except Exception:
                # if there is error in serialize the error,
                # just return the original raw exception
                return e
Пример #20
0
 def test_unicode_string(self):
     input_val = '"val??"'
     expected_val = StringValue(u'val??')
     actual_val = DataValueConverter.convert_to_data_value(input_val)
     self.assertEqual(expected_val, actual_val)
Пример #21
0
 def test_float(self):
     input_val = '1.01E1'
     expected_val = DoubleValue(decimal.Decimal('10.10'))
     actual_val = DataValueConverter.convert_to_data_value(input_val)
     self.assertEqual(expected_val, actual_val)
Пример #22
0
 def test_empty_list(self):
     input_val = '[]'
     expected_val = ListValue()
     actual_val = DataValueConverter.convert_to_data_value(input_val)
     self.assertEqual(expected_val, actual_val)
Пример #23
0
 def test_none(self):
     input_val = 'null'
     expected_val = OptionalValue()
     actual_val = DataValueConverter.convert_to_data_value(input_val)
     self.assertEqual(expected_val, actual_val)