You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/12/13 11:43:20 UTC
[PATCH core 4/4] Core: Added unit tests for JSON serialization
From: Michal Fojtik <mf...@redhat.com>
Signed-off-by: Michal fojtik <mf...@redhat.com>
---
.../collections/buckets_collection_test.rb | 3 ++
.../collections/drivers_collection_test.rb | 10 +++++
.../hardware_profiles_collection_test.rb | 4 ++
.../collections/images_collection_test.rb | 4 ++
.../collections/instances_collection_test.rb | 15 ++++++-
.../deltacloud/collections/keys_collection_test.rb | 4 ++
.../collections/realms_collection_test.rb | 47 ++++++++++++++++++++++
.../storage_snapshots_collection_test.rb | 47 ++++++++++++++++++++++
.../collections/storage_volumes_collection_test.rb | 47 ++++++++++++++++++++++
server/tests/deltacloud/common.rb | 15 +++++++
10 files changed, 195 insertions(+), 1 deletion(-)
create mode 100644 server/tests/deltacloud/collections/realms_collection_test.rb
create mode 100644 server/tests/deltacloud/collections/storage_snapshots_collection_test.rb
create mode 100644 server/tests/deltacloud/collections/storage_volumes_collection_test.rb
diff --git a/server/tests/deltacloud/collections/buckets_collection_test.rb b/server/tests/deltacloud/collections/buckets_collection_test.rb
index 80df01e..a30d221 100644
--- a/server/tests/deltacloud/collections/buckets_collection_test.rb
+++ b/server/tests/deltacloud/collections/buckets_collection_test.rb
@@ -61,5 +61,8 @@ describe Deltacloud::Collections::Buckets do
status.must_equal 204
end
+ it 'properly serialize attributes in JSON' do
+ check_json_serialization_for :bucket, 'bucket1'
+ end
end
diff --git a/server/tests/deltacloud/collections/drivers_collection_test.rb b/server/tests/deltacloud/collections/drivers_collection_test.rb
index 6e06a19..cd844d7 100644
--- a/server/tests/deltacloud/collections/drivers_collection_test.rb
+++ b/server/tests/deltacloud/collections/drivers_collection_test.rb
@@ -34,4 +34,14 @@ describe Deltacloud::Collections::Drivers do
end
end
+ it 'properly serialize attributes in JSON' do
+ header 'Accept', 'application/json'
+ get root_url + "/drivers/ec2"
+ status.must_equal 200
+ json['driver'].wont_be_empty
+ json['driver']['id'].must_equal 'ec2'
+ json['driver']['name'].must_equal 'EC2'
+ json['driver']['entrypoints'].wont_be_empty
+ end
+
end
diff --git a/server/tests/deltacloud/collections/hardware_profiles_collection_test.rb b/server/tests/deltacloud/collections/hardware_profiles_collection_test.rb
index 9c8df37..eb30dcc 100644
--- a/server/tests/deltacloud/collections/hardware_profiles_collection_test.rb
+++ b/server/tests/deltacloud/collections/hardware_profiles_collection_test.rb
@@ -50,4 +50,8 @@ describe Deltacloud::Collections::HardwareProfiles do
status.must_equal 404
end
+ it 'properly serialize attributes in JSON' do
+ check_json_serialization_for :hardware_profile, 'm1-small'
+ end
+
end
diff --git a/server/tests/deltacloud/collections/images_collection_test.rb b/server/tests/deltacloud/collections/images_collection_test.rb
index 7b04ba3..19cd261 100644
--- a/server/tests/deltacloud/collections/images_collection_test.rb
+++ b/server/tests/deltacloud/collections/images_collection_test.rb
@@ -56,4 +56,8 @@ describe Deltacloud::Collections::Images do
status.must_equal 404
end
+ it 'properly serialize attributes in JSON' do
+ check_json_serialization_for :image, 'img1'
+ end
+
end
diff --git a/server/tests/deltacloud/collections/instances_collection_test.rb b/server/tests/deltacloud/collections/instances_collection_test.rb
index 79c26ad..8db75e6 100644
--- a/server/tests/deltacloud/collections/instances_collection_test.rb
+++ b/server/tests/deltacloud/collections/instances_collection_test.rb
@@ -59,5 +59,18 @@ describe Deltacloud::Collections::Instances do
status.must_equal 204
end
-
+ it 'properly serialize attributes in JSON' do
+ header 'Accept', 'application/json'
+ get root_url + "/instances"
+ status.must_equal 200
+ json['instances'].wont_be_empty
+ get root_url + "/instances/inst1"
+ status.must_equal 200
+ json['instance'].wont_be_empty
+ Instance.attributes.each do |attr|
+ attr = attr.to_s.gsub(/_id$/,'') if attr.to_s =~ /_id$/
+ next if ['authn_error', 'firewalls', 'keyname', 'username', 'password'].include?(attr.to_s)
+ json['instance'].keys.must_include attr.to_s
+ end
+ end
end
diff --git a/server/tests/deltacloud/collections/keys_collection_test.rb b/server/tests/deltacloud/collections/keys_collection_test.rb
index 9751a33..e719553 100644
--- a/server/tests/deltacloud/collections/keys_collection_test.rb
+++ b/server/tests/deltacloud/collections/keys_collection_test.rb
@@ -57,4 +57,8 @@ describe Deltacloud::Collections::Keys do
status.must_equal 404
end
+ it 'properly serialize attributes in JSON' do
+ check_json_serialization_for :key, 'test-key'
+ end
+
end
diff --git a/server/tests/deltacloud/collections/realms_collection_test.rb b/server/tests/deltacloud/collections/realms_collection_test.rb
new file mode 100644
index 0000000..9401f8d
--- /dev/null
+++ b/server/tests/deltacloud/collections/realms_collection_test.rb
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+
+require_relative File.join('..', 'common.rb')
+
+describe Deltacloud::Collections::Realms do
+
+ before do
+ def app; run_frontend; end
+ authorize 'mockuser', 'mockpassword'
+ @collection = Deltacloud::Collections.collection(:realms)
+ end
+
+ it 'has index operation' do
+ @collection.operation(:index).must_equal Sinatra::Rabbit::RealmsCollection::IndexOperation
+ end
+
+ it 'has show operation' do
+ @collection.operation(:show).must_equal Sinatra::Rabbit::RealmsCollection::ShowOperation
+ end
+
+ it 'returns list of realms in various formats with index operation' do
+ formats.each do |format|
+ header 'Accept', format
+ get root_url + '/realms'
+ status.must_equal 200
+ end
+ end
+
+ it 'returns details about key in various formats with show operation' do
+ formats.each do |format|
+ header 'Accept', format
+ get root_url + '/realms/eu'
+ status.must_equal 200
+ end
+ end
+
+ it 'reports 404 when querying non-existing key' do
+ get root_url + '/realms/unknown'
+ status.must_equal 404
+ end
+
+ it 'properly serialize attributes in JSON' do
+ check_json_serialization_for :realm, 'us'
+ end
+
+end
diff --git a/server/tests/deltacloud/collections/storage_snapshots_collection_test.rb b/server/tests/deltacloud/collections/storage_snapshots_collection_test.rb
new file mode 100644
index 0000000..f2e4f1c
--- /dev/null
+++ b/server/tests/deltacloud/collections/storage_snapshots_collection_test.rb
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+
+require_relative File.join('..', 'common.rb')
+
+describe Deltacloud::Collections::StorageSnapshots do
+
+ before do
+ def app; run_frontend; end
+ authorize 'mockuser', 'mockpassword'
+ @collection = Deltacloud::Collections.collection(:storage_snapshots)
+ end
+
+ it 'has index operation' do
+ @collection.operation(:index).must_equal Sinatra::Rabbit::StorageSnapshotsCollection::IndexOperation
+ end
+
+ it 'has show operation' do
+ @collection.operation(:show).must_equal Sinatra::Rabbit::StorageSnapshotsCollection::ShowOperation
+ end
+
+ it 'returns list of storage_snapshots in various formats with index operation' do
+ formats.each do |format|
+ header 'Accept', format
+ get root_url + '/storage_snapshots'
+ status.must_equal 200
+ end
+ end
+
+ it 'returns details about storage_volume in various formats with show operation' do
+ formats.each do |format|
+ header 'Accept', format
+ get root_url + '/storage_snapshots/snap1'
+ status.must_equal 200
+ end
+ end
+
+ it 'reports 404 when querying non-existing key' do
+ get root_url + '/storage_snapshots/unknown'
+ status.must_equal 404
+ end
+
+ it 'properly serialize attributes in JSON' do
+ check_json_serialization_for :storage_snapshot, 'snap1'
+ end
+
+end
diff --git a/server/tests/deltacloud/collections/storage_volumes_collection_test.rb b/server/tests/deltacloud/collections/storage_volumes_collection_test.rb
new file mode 100644
index 0000000..06463cc
--- /dev/null
+++ b/server/tests/deltacloud/collections/storage_volumes_collection_test.rb
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+
+require_relative File.join('..', 'common.rb')
+
+describe Deltacloud::Collections::StorageVolumes do
+
+ before do
+ def app; run_frontend; end
+ authorize 'mockuser', 'mockpassword'
+ @collection = Deltacloud::Collections.collection(:storage_volumes)
+ end
+
+ it 'has index operation' do
+ @collection.operation(:index).must_equal Sinatra::Rabbit::StorageVolumesCollection::IndexOperation
+ end
+
+ it 'has show operation' do
+ @collection.operation(:show).must_equal Sinatra::Rabbit::StorageVolumesCollection::ShowOperation
+ end
+
+ it 'returns list of storage_volumes in various formats with index operation' do
+ formats.each do |format|
+ header 'Accept', format
+ get root_url + '/storage_volumes'
+ status.must_equal 200
+ end
+ end
+
+ it 'returns details about storage_volume in various formats with show operation' do
+ formats.each do |format|
+ header 'Accept', format
+ get root_url + '/storage_volumes/vol1'
+ status.must_equal 200
+ end
+ end
+
+ it 'reports 404 when querying non-existing key' do
+ get root_url + '/storage_volumes/unknown'
+ status.must_equal 404
+ end
+
+ it 'properly serialize attributes in JSON' do
+ check_json_serialization_for :storage_volume, 'vol1'
+ end
+
+end
diff --git a/server/tests/deltacloud/common.rb b/server/tests/deltacloud/common.rb
index 27e6a5b..e603a24 100644
--- a/server/tests/deltacloud/common.rb
+++ b/server/tests/deltacloud/common.rb
@@ -12,3 +12,18 @@ Deltacloud::configure do |server|
end
Deltacloud.require_frontend!
+
+def check_json_serialization_for(model, sample_id)
+ header 'Accept', 'application/json'
+ get root_url + "/#{model.to_s.pluralize}"
+ status.must_equal 200
+ json[model.to_s.pluralize].wont_be_empty
+ get root_url + "/#{model.to_s.pluralize}/#{sample_id}"
+ status.must_equal 200
+ json[model.to_s].wont_be_empty
+ klass = self.class.const_get(model.to_s.camelize)
+ klass.attributes.each do |attr|
+ attr = attr.to_s.gsub(/_id$/,'') if attr.to_s =~ /_id$/
+ json[model.to_s].keys.must_include attr.to_s
+ end
+end
--
1.8.0.2