Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added tests for Fog::JSON; Added Fog::JSON::LoadError class; Updated …

…all MultiJson references to Fog::JSON
  • Loading branch information...
commit 18ce4b7eca41ea75d45564466e4d44076128981e 1 parent 3f0314d
Kyle Rames authored
Showing with 105 additions and 30 deletions.
  1. +16 −3 lib/fog/core/json.rb
  2. +1 −1  lib/fog/hp.rb
  3. +1 −1  lib/fog/openstack/requests/compute/allocate_address.rb
  4. +1 −1  lib/fog/openstack/requests/compute/attach_volume.rb
  5. +1 −1  lib/fog/openstack/requests/compute/boot_from_snapshot.rb
  6. +1 −1  lib/fog/openstack/requests/compute/create_flavor.rb
  7. +1 −1  lib/fog/openstack/requests/compute/create_key_pair.rb
  8. +1 −1  lib/fog/openstack/requests/compute/create_security_group.rb
  9. +1 −1  lib/fog/openstack/requests/compute/create_security_group_rule.rb
  10. +1 −1  lib/fog/openstack/requests/compute/create_volume.rb
  11. +1 −1  lib/fog/openstack/requests/compute/create_volume_snapshot.rb
  12. +1 −1  lib/fog/openstack/requests/compute/list_tenants.rb
  13. +1 −1  lib/fog/openstack/requests/compute/update_quota.rb
  14. +1 −1  lib/fog/openstack/requests/identity/create_ec2_credential.rb
  15. +1 −1  lib/fog/openstack/requests/identity/create_role.rb
  16. +1 −1  lib/fog/openstack/requests/identity/create_user.rb
  17. +1 −1  lib/fog/openstack/requests/identity/update_user.rb
  18. +1 −1  lib/fog/openstack/requests/image/update_image_members.rb
  19. +1 −1  lib/fog/openstack/requests/volume/create_volume.rb
  20. +1 −1  lib/fog/openstack/requests/volume/create_volume_snapshot.rb
  21. +1 −1  lib/fog/rackspace/compute_v2.rb
  22. +2 −2 lib/fog/riakcs.rb
  23. +2 −2 lib/fog/riakcs/requests/provisioning/create_user.rb
  24. +1 −1  lib/fog/riakcs/requests/provisioning/get_user.rb
  25. +1 −1  lib/fog/riakcs/requests/provisioning/list_users.rb
  26. +1 −1  lib/fog/riakcs/requests/usage/get_usage.rb
  27. +62 −0 tests/core/json_tests.rb
View
19 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 @@ def encode(obj)
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
View
2  lib/fog/hp.rb
@@ -25,7 +25,7 @@ def self.slurp(error)
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
View
2  lib/fog/openstack/requests/compute/allocate_address.rb
@@ -6,7 +6,7 @@ class Real
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'
View
2  lib/fog/openstack/requests/compute/attach_volume.rb
@@ -11,7 +11,7 @@ def attach_volume(volume_id, server_id, device)
}
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "servers/%s/os-volume_attachments" % [server_id]
View
2  lib/fog/openstack/requests/compute/boot_from_snapshot.rb
@@ -29,7 +29,7 @@ def boot_from_snapshot(name, image_ref, flavor_ref, options={})
end
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/os-volumes_boot.json'
View
2  lib/fog/openstack/requests/compute/create_flavor.rb
@@ -35,7 +35,7 @@ def create_flavor(attributes)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'flavors'
View
2  lib/fog/openstack/requests/compute/create_key_pair.rb
@@ -14,7 +14,7 @@ def create_key_pair(key_name, public_key = nil)
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'
View
2  lib/fog/openstack/requests/compute/create_security_group.rb
@@ -12,7 +12,7 @@ def create_security_group(name, description)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => 200,
:method => 'POST',
:path => 'os-security-groups.json'
View
2  lib/fog/openstack/requests/compute/create_security_group_rule.rb
@@ -18,7 +18,7 @@ def create_security_group_rule(parent_group_id, ip_protocol, from_port, to_port,
request(
:expects => 200,
:method => 'POST',
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:path => 'os-security-group-rules.json'
)
end
View
2  lib/fog/openstack/requests/compute/create_volume.rb
@@ -17,7 +17,7 @@ def create_volume(name, description, size, options={})
data['volume'][key] = options[key]
end
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "os-volumes"
View
2  lib/fog/openstack/requests/compute/create_volume_snapshot.rb
@@ -14,7 +14,7 @@ def create_volume_snapshot(volume_id, name, description, force=false)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "os-snapshots"
View
2  lib/fog/openstack/requests/compute/list_tenants.rb
@@ -11,7 +11,7 @@ def list_tenants
:method => 'GET',
:path => '/v2.0/tenants'
})
- response.body = MultiJson.decode(response.body)
+ response.body = Fog::JSON.decode(response.body)
response
end
end
View
2  lib/fog/openstack/requests/compute/update_quota.rb
@@ -6,7 +6,7 @@ class Real
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}"
View
2  lib/fog/openstack/requests/identity/create_ec2_credential.rb
@@ -26,7 +26,7 @@ def create_ec2_credential(user_id, tenant_id)
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"
View
2  lib/fog/openstack/requests/identity/create_role.rb
@@ -10,7 +10,7 @@ def create_role(name)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/OS-KSADM/roles'
View
2  lib/fog/openstack/requests/identity/create_user.rb
@@ -15,7 +15,7 @@ def create_user(name, password, email, tenantId=nil, enabled=true)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => '/users'
View
2  lib/fog/openstack/requests/identity/update_user.rb
@@ -6,7 +6,7 @@ class Real
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
View
2  lib/fog/openstack/requests/image/update_image_members.rb
@@ -11,7 +11,7 @@ def update_image_members(image_id, members)
data = { 'memberships' => members }
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'PUT',
:path => "images/#{image_id}/members"
View
2  lib/fog/openstack/requests/volume/create_volume.rb
@@ -17,7 +17,7 @@ def create_volume(name, description, size, options={})
data['volume'][key] = options[key]
end
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "volumes"
View
2  lib/fog/openstack/requests/volume/create_volume_snapshot.rb
@@ -14,7 +14,7 @@ def create_volume_snapshot(volume_id, name, description, force=false)
}
request(
- :body => MultiJson.encode(data),
+ :body => Fog::JSON.encode(data),
:expects => [200, 202],
:method => 'POST',
:path => "snapshots"
View
2  lib/fog/rackspace/compute_v2.rb
@@ -160,7 +160,7 @@ def request(params)
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
View
4 lib/fog/riakcs.rb
@@ -71,9 +71,9 @@ def parse_multipart_section(part)
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
View
4 lib/fog/riakcs/requests/provisioning/create_user.rb
@@ -3,7 +3,7 @@ module RiakCS
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 @@ def create_user(email, name, options = {})
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
View
2  lib/fog/riakcs/requests/provisioning/get_user.rb
@@ -7,7 +7,7 @@ class Real
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
View
2  lib/fog/riakcs/requests/provisioning/list_users.rb
@@ -10,7 +10,7 @@ def list_users(options = {})
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
View
2  lib/fog/riakcs/requests/usage/get_usage.rb
@@ -37,7 +37,7 @@ def get_usage(access_key_id, options = {})
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
View
62 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
Please sign in to comment.
Something went wrong with that request. Please try again.