From 18ce4b7eca41ea75d45564466e4d44076128981e Mon Sep 17 00:00:00 2001 From: Kyle Rames Date: Wed, 3 Apr 2013 10:37:03 -0500 Subject: [PATCH] Added tests for Fog::JSON; Added Fog::JSON::LoadError class; Updated all MultiJson references to Fog::JSON --- lib/fog/core/json.rb | 19 +++++- lib/fog/hp.rb | 2 +- .../requests/compute/allocate_address.rb | 2 +- .../requests/compute/attach_volume.rb | 2 +- .../requests/compute/boot_from_snapshot.rb | 2 +- .../requests/compute/create_flavor.rb | 2 +- .../requests/compute/create_key_pair.rb | 2 +- .../requests/compute/create_security_group.rb | 2 +- .../compute/create_security_group_rule.rb | 2 +- .../requests/compute/create_volume.rb | 2 +- .../compute/create_volume_snapshot.rb | 2 +- .../requests/compute/list_tenants.rb | 2 +- .../requests/compute/update_quota.rb | 2 +- .../identity/create_ec2_credential.rb | 2 +- .../requests/identity/create_role.rb | 2 +- .../requests/identity/create_user.rb | 2 +- .../requests/identity/update_user.rb | 2 +- .../requests/image/update_image_members.rb | 2 +- .../requests/volume/create_volume.rb | 2 +- .../requests/volume/create_volume_snapshot.rb | 2 +- lib/fog/rackspace/compute_v2.rb | 2 +- lib/fog/riakcs.rb | 4 +- .../requests/provisioning/create_user.rb | 4 +- .../riakcs/requests/provisioning/get_user.rb | 2 +- .../requests/provisioning/list_users.rb | 2 +- lib/fog/riakcs/requests/usage/get_usage.rb | 2 +- tests/core/json_tests.rb | 62 +++++++++++++++++++ 27 files changed, 105 insertions(+), 30 deletions(-) create mode 100644 tests/core/json_tests.rb diff --git a/lib/fog/core/json.rb b/lib/fog/core/json.rb index 734dc1cdb..450afd917 100644 --- a/lib/fog/core/json.rb +++ b/lib/fog/core/json.rb @@ -5,6 +5,15 @@ module Fog class JSON include Singleton + class LoadError < StandardError + attr_reader :data + def initialize(message='', backtrace=[], data='') + super(message) + self.set_backtrace(backtrace) + @data = data + end + end + module LegacyJSON def encode(obj) ::JSON.generate(obj) @@ -12,23 +21,27 @@ module Fog def decode(obj) ::JSON.parse(obj) + rescue ::JSON::ParserError => e + raise LoadError.new(e.message, e.backtrace, obj) end end module NewJSON def encode(obj) - MultiJson.encode(obj) + ::MultiJson.encode(obj) end def decode(obj) - MultiJson.decode(obj) + ::MultiJson.decode(obj) + rescue MultiJson::LoadError => e + raise LoadError.new(e.message, e.backtrace, obj) end end begin require 'multi_json' include NewJSON - rescue LoadError + rescue Exception => e Fog::Logger.deprecation "Defaulting to json library for json parsing. Please consider using multi_json library for the greatest performance/flexibility." require 'json' include LegacyJSON diff --git a/lib/fog/hp.rb b/lib/fog/hp.rb index d90e2747a..26c51a2ab 100644 --- a/lib/fog/hp.rb +++ b/lib/fog/hp.rb @@ -25,7 +25,7 @@ module Fog if message.nil? and !data.values.first.nil? message = data.values.first['message'] end - rescue MultiJson::DecodeError + rescue Fog::JSON::LoadError message = error.response.body #### body is not in JSON format, so just return as is end end diff --git a/lib/fog/openstack/requests/compute/allocate_address.rb b/lib/fog/openstack/requests/compute/allocate_address.rb index a86f25662..5d1cb4085 100644 --- a/lib/fog/openstack/requests/compute/allocate_address.rb +++ b/lib/fog/openstack/requests/compute/allocate_address.rb @@ -6,7 +6,7 @@ module Fog def allocate_address(pool = nil) request( - :body => MultiJson.encode({'pool' => pool}), + :body => Fog::JSON.encode({'pool' => pool}), :expects => [200, 202], :method => 'POST', :path => 'os-floating-ips.json' diff --git a/lib/fog/openstack/requests/compute/attach_volume.rb b/lib/fog/openstack/requests/compute/attach_volume.rb index dcfd7ad91..37024577a 100644 --- a/lib/fog/openstack/requests/compute/attach_volume.rb +++ b/lib/fog/openstack/requests/compute/attach_volume.rb @@ -11,7 +11,7 @@ module Fog } } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => "servers/%s/os-volume_attachments" % [server_id] diff --git a/lib/fog/openstack/requests/compute/boot_from_snapshot.rb b/lib/fog/openstack/requests/compute/boot_from_snapshot.rb index 7911239d1..a79fb0d81 100644 --- a/lib/fog/openstack/requests/compute/boot_from_snapshot.rb +++ b/lib/fog/openstack/requests/compute/boot_from_snapshot.rb @@ -29,7 +29,7 @@ module Fog end request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => '/os-volumes_boot.json' diff --git a/lib/fog/openstack/requests/compute/create_flavor.rb b/lib/fog/openstack/requests/compute/create_flavor.rb index bd81c8394..8bf01e4e7 100644 --- a/lib/fog/openstack/requests/compute/create_flavor.rb +++ b/lib/fog/openstack/requests/compute/create_flavor.rb @@ -35,7 +35,7 @@ module Fog } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => 200, :method => 'POST', :path => 'flavors' diff --git a/lib/fog/openstack/requests/compute/create_key_pair.rb b/lib/fog/openstack/requests/compute/create_key_pair.rb index e8a601ab0..e53efb9cf 100644 --- a/lib/fog/openstack/requests/compute/create_key_pair.rb +++ b/lib/fog/openstack/requests/compute/create_key_pair.rb @@ -14,7 +14,7 @@ module Fog data['keypair']['public_key'] = public_key unless public_key.nil? request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => 200, :method => 'POST', :path => 'os-keypairs.json' diff --git a/lib/fog/openstack/requests/compute/create_security_group.rb b/lib/fog/openstack/requests/compute/create_security_group.rb index 10fd45915..1a266e12c 100644 --- a/lib/fog/openstack/requests/compute/create_security_group.rb +++ b/lib/fog/openstack/requests/compute/create_security_group.rb @@ -12,7 +12,7 @@ module Fog } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => 200, :method => 'POST', :path => 'os-security-groups.json' diff --git a/lib/fog/openstack/requests/compute/create_security_group_rule.rb b/lib/fog/openstack/requests/compute/create_security_group_rule.rb index 84d3e5f6a..1903902ab 100644 --- a/lib/fog/openstack/requests/compute/create_security_group_rule.rb +++ b/lib/fog/openstack/requests/compute/create_security_group_rule.rb @@ -18,7 +18,7 @@ module Fog request( :expects => 200, :method => 'POST', - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :path => 'os-security-group-rules.json' ) end diff --git a/lib/fog/openstack/requests/compute/create_volume.rb b/lib/fog/openstack/requests/compute/create_volume.rb index 1a8a2fced..d7c29c388 100644 --- a/lib/fog/openstack/requests/compute/create_volume.rb +++ b/lib/fog/openstack/requests/compute/create_volume.rb @@ -17,7 +17,7 @@ module Fog data['volume'][key] = options[key] end request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => "os-volumes" diff --git a/lib/fog/openstack/requests/compute/create_volume_snapshot.rb b/lib/fog/openstack/requests/compute/create_volume_snapshot.rb index c5f9a9e47..f2e1ced98 100644 --- a/lib/fog/openstack/requests/compute/create_volume_snapshot.rb +++ b/lib/fog/openstack/requests/compute/create_volume_snapshot.rb @@ -14,7 +14,7 @@ module Fog } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => "os-snapshots" diff --git a/lib/fog/openstack/requests/compute/list_tenants.rb b/lib/fog/openstack/requests/compute/list_tenants.rb index d840ebe57..ae37267ed 100644 --- a/lib/fog/openstack/requests/compute/list_tenants.rb +++ b/lib/fog/openstack/requests/compute/list_tenants.rb @@ -11,7 +11,7 @@ module Fog :method => 'GET', :path => '/v2.0/tenants' }) - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) response end end diff --git a/lib/fog/openstack/requests/compute/update_quota.rb b/lib/fog/openstack/requests/compute/update_quota.rb index f5217d490..51f59b296 100644 --- a/lib/fog/openstack/requests/compute/update_quota.rb +++ b/lib/fog/openstack/requests/compute/update_quota.rb @@ -6,7 +6,7 @@ module Fog def update_quota(tenant_id, options = {}) options['tenant_id'] = tenant_id request( - :body => MultiJson.encode({ 'quota_set' => options }), + :body => Fog::JSON.encode({ 'quota_set' => options }), :expects => 200, :method => 'PUT', :path => "/os-quota-sets/#{tenant_id}" diff --git a/lib/fog/openstack/requests/identity/create_ec2_credential.rb b/lib/fog/openstack/requests/identity/create_ec2_credential.rb index 2bf9e24a3..dc20e9896 100644 --- a/lib/fog/openstack/requests/identity/create_ec2_credential.rb +++ b/lib/fog/openstack/requests/identity/create_ec2_credential.rb @@ -26,7 +26,7 @@ module Fog data = { 'tenant_id' => tenant_id } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => "users/#{user_id}/credentials/OS-EC2" diff --git a/lib/fog/openstack/requests/identity/create_role.rb b/lib/fog/openstack/requests/identity/create_role.rb index 1223128e3..79c25a66c 100644 --- a/lib/fog/openstack/requests/identity/create_role.rb +++ b/lib/fog/openstack/requests/identity/create_role.rb @@ -10,7 +10,7 @@ module Fog } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => '/OS-KSADM/roles' diff --git a/lib/fog/openstack/requests/identity/create_user.rb b/lib/fog/openstack/requests/identity/create_user.rb index 1e0104898..c4996af8b 100644 --- a/lib/fog/openstack/requests/identity/create_user.rb +++ b/lib/fog/openstack/requests/identity/create_user.rb @@ -15,7 +15,7 @@ module Fog } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => '/users' diff --git a/lib/fog/openstack/requests/identity/update_user.rb b/lib/fog/openstack/requests/identity/update_user.rb index 4a11bd44d..a1f8fbab1 100644 --- a/lib/fog/openstack/requests/identity/update_user.rb +++ b/lib/fog/openstack/requests/identity/update_user.rb @@ -6,7 +6,7 @@ module Fog def update_user(user_id, options = {}) url = options.delete('url') || "/users/#{user_id}" request( - :body => MultiJson.encode({ 'user' => options }), + :body => Fog::JSON.encode({ 'user' => options }), :expects => 200, :method => 'PUT', :path => url diff --git a/lib/fog/openstack/requests/image/update_image_members.rb b/lib/fog/openstack/requests/image/update_image_members.rb index a7010fd61..741cf9ad1 100644 --- a/lib/fog/openstack/requests/image/update_image_members.rb +++ b/lib/fog/openstack/requests/image/update_image_members.rb @@ -11,7 +11,7 @@ module Fog data = { 'memberships' => members } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'PUT', :path => "images/#{image_id}/members" diff --git a/lib/fog/openstack/requests/volume/create_volume.rb b/lib/fog/openstack/requests/volume/create_volume.rb index 116aa8a56..4a71799d0 100644 --- a/lib/fog/openstack/requests/volume/create_volume.rb +++ b/lib/fog/openstack/requests/volume/create_volume.rb @@ -17,7 +17,7 @@ module Fog data['volume'][key] = options[key] end request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => "volumes" diff --git a/lib/fog/openstack/requests/volume/create_volume_snapshot.rb b/lib/fog/openstack/requests/volume/create_volume_snapshot.rb index 1d202eb04..ada07c8e9 100644 --- a/lib/fog/openstack/requests/volume/create_volume_snapshot.rb +++ b/lib/fog/openstack/requests/volume/create_volume_snapshot.rb @@ -14,7 +14,7 @@ module Fog } request( - :body => MultiJson.encode(data), + :body => Fog::JSON.encode(data), :expects => [200, 202], :method => 'POST', :path => "snapshots" diff --git a/lib/fog/rackspace/compute_v2.rb b/lib/fog/rackspace/compute_v2.rb index 1ca1f9c25..113765c09 100644 --- a/lib/fog/rackspace/compute_v2.rb +++ b/lib/fog/rackspace/compute_v2.rb @@ -160,7 +160,7 @@ module Fog unless response.body.empty? begin response.body = Fog::JSON.decode(response.body) - rescue MultiJson::DecodeError => e + rescue Fog::JSON::LoadError => e response.body = {} end end diff --git a/lib/fog/riakcs.rb b/lib/fog/riakcs.rb index a98305dd9..0f0d968ff 100644 --- a/lib/fog/riakcs.rb +++ b/lib/fog/riakcs.rb @@ -71,9 +71,9 @@ module Fog module UserUtils def update_riakcs_user(key_id, user) - response = @s3_connection.put_object('riak-cs', "user/#{key_id}", MultiJson.encode(user), { 'Content-Type' => 'application/json' }) + response = @s3_connection.put_object('riak-cs', "user/#{key_id}", Fog::JSON.encode(user), { 'Content-Type' => 'application/json' }) if !response.body.empty? - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end response end diff --git a/lib/fog/riakcs/requests/provisioning/create_user.rb b/lib/fog/riakcs/requests/provisioning/create_user.rb index b98fd9dc3..969331460 100644 --- a/lib/fog/riakcs/requests/provisioning/create_user.rb +++ b/lib/fog/riakcs/requests/provisioning/create_user.rb @@ -3,7 +3,7 @@ module Fog class Provisioning class Real def create_user(email, name, options = {}) - payload = MultiJson.encode({ :email => email, :name => name }) + payload = Fog::JSON.encode({ :email => email, :name => name }) headers = { 'Content-Type' => 'application/json' } if(options[:anonymous]) @@ -20,7 +20,7 @@ module Fog if !response.body.empty? case response.headers['Content-Type'] when 'application/json' - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end end response diff --git a/lib/fog/riakcs/requests/provisioning/get_user.rb b/lib/fog/riakcs/requests/provisioning/get_user.rb index 793dca95a..914861d7b 100644 --- a/lib/fog/riakcs/requests/provisioning/get_user.rb +++ b/lib/fog/riakcs/requests/provisioning/get_user.rb @@ -7,7 +7,7 @@ module Fog def get_user(key_id) response = @s3_connection.get_object('riak-cs', "user/#{key_id}", { 'Accept' => 'application/json' }) - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) response end end diff --git a/lib/fog/riakcs/requests/provisioning/list_users.rb b/lib/fog/riakcs/requests/provisioning/list_users.rb index c747b8b2d..cd3a2dba6 100644 --- a/lib/fog/riakcs/requests/provisioning/list_users.rb +++ b/lib/fog/riakcs/requests/provisioning/list_users.rb @@ -10,7 +10,7 @@ module Fog boundary = extract_boundary(response.headers['Content-Type']) parts = parse(response.body, boundary) - decoded = parts.map { |part| MultiJson.decode(part[:body]) } + decoded = parts.map { |part| Fog::JSON.decode(part[:body]) } response.body = decoded.flatten diff --git a/lib/fog/riakcs/requests/usage/get_usage.rb b/lib/fog/riakcs/requests/usage/get_usage.rb index ee35357ed..78be7f797 100644 --- a/lib/fog/riakcs/requests/usage/get_usage.rb +++ b/lib/fog/riakcs/requests/usage/get_usage.rb @@ -37,7 +37,7 @@ module Fog response = @connection.get_object('riak-cs', ["usage", request_uri(access_key_id, options)].join("/")) if !response.body.empty? - response.body = MultiJson.decode(response.body) + response.body = Fog::JSON.decode(response.body) end response end diff --git a/tests/core/json_tests.rb b/tests/core/json_tests.rb new file mode 100644 index 000000000..9db32da05 --- /dev/null +++ b/tests/core/json_tests.rb @@ -0,0 +1,62 @@ +Shindo.tests('Fog#JSON', 'core') do + +TEST_HASH = {'name' => 'fog-name', 'quantity' => 4} +VALID_JSON = "{\"name\":\"fog-name\",\"quantity\":4}" + + tests('LegacyJSON') do + + begin + require 'json' + rescue => e + returns('Failed to load JSON library', true) { false } + end + + pending? unless defined? ::JSON + + class LegacyJSONTester + include Fog::JSON::LegacyJSON + end + + @tester = LegacyJSONTester.new + tests('encode').returns(VALID_JSON) do + @tester.encode(TEST_HASH) + end + + tests('decode').returns(TEST_HASH) do + @tester.decode(VALID_JSON) + end + + tests('invalid decode').raises(Fog::JSON::LoadError) do + @tester.decode("I am not json") + end + end + + tests('NewJSON') do + + begin + require 'multi_json' + rescue => e + returns('Failed to load multi_json library', true) { false } + end + + pending unless defined? ::MultiJson + + class NewJSONTester + include Fog::JSON::NewJSON + end + + @tester = NewJSONTester.new + tests('encode').returns(VALID_JSON) do + @tester.encode(TEST_HASH) + end + + tests('decode').returns(TEST_HASH) do + @tester.decode(VALID_JSON) + end + + tests('invalid decode').raises(Fog::JSON::LoadError) do + @tester.decode("I am not json") + end + + end +end \ No newline at end of file