deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 2/2] Added tests for exception DSL
Date Fri, 21 Sep 2012 22:11:29 GMT
From: Michal Fojtik <mfojtik@redhat.com>


Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 .../deltacloud/collections/keys_collection_test.rb |  3 -
 server/tests/drivers/base/exceptions_test.rb       | 64 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 3 deletions(-)
 create mode 100644 server/tests/drivers/base/exceptions_test.rb

diff --git a/server/tests/deltacloud/collections/keys_collection_test.rb b/server/tests/deltacloud/collections/keys_collection_test.rb
index c2e5635..9751a33 100644
--- a/server/tests/deltacloud/collections/keys_collection_test.rb
+++ b/server/tests/deltacloud/collections/keys_collection_test.rb
@@ -48,9 +48,6 @@ describe Deltacloud::Collections::Keys do
     xml.root.name.must_equal 'key'
     xml.root[:id].must_equal 'unit-test1'
     (xml/'key/pem').wont_be_empty
-    Proc.new {
-      post root_url + '/keys', { :name => 'unit-test1' }
-    }.must_raise Deltacloud::ExceptionHandler::ProviderError, 'keyExist'
     delete root_url + '/keys/unit-test1'
     status.must_equal 204
   end
diff --git a/server/tests/drivers/base/exceptions_test.rb b/server/tests/drivers/base/exceptions_test.rb
new file mode 100644
index 0000000..479627d
--- /dev/null
+++ b/server/tests/drivers/base/exceptions_test.rb
@@ -0,0 +1,64 @@
+require 'rubygems'
+require 'require_relative' if RUBY_VERSION < '1.9'
+require_relative './common'
+require_relative '../../../lib/deltacloud/drivers/exceptions'
+
+class TestException < StandardError; end
+
+class ExceptionTestClass
+  include Deltacloud::ExceptionHandler
+
+  def raise_exception(id)
+    case id
+      when 1 then safely { raise 'test1 exception' }
+      when 2 then safely { raise TestException }
+      when 3 then safely { raise 'not captured' }
+    end
+  end
+
+  exceptions do
+    on /test1/ do
+      status 500
+      message 'Test1ErrorMessage'
+    end
+    on TestException do
+      status 400
+      message 'StandardErrorTest'
+    end
+  end
+end
+
+def raise_error(id); ExceptionTestClass.new.raise_exception(id); end
+
+describe Deltacloud::ExceptionHandler do
+
+  it 'should capture exception when match the exception message' do
+    lambda { raise_error 1 }.must_raise Deltacloud::ExceptionHandler::BackendError
+
+    begin raise_error(1); rescue Deltacloud::ExceptionHandler::BackendError => e
+      e.code.must_equal 500
+      e.message.must_equal 'Test1ErrorMessage'
+      e.backtrace.wont_be_empty
+    end
+
+  end
+
+  it 'should capture exception when match the exception class' do
+    lambda { raise_error 2 }.must_raise Deltacloud::ExceptionHandler::ValidationFailure
+    begin raise_error(2); rescue Deltacloud::ExceptionHandler::ValidationFailure => e
+      e.code.must_equal 400
+      e.message.must_equal 'StandardErrorTest'
+      e.backtrace.wont_be_empty
+    end
+  end
+
+  it 'should capture exception when no match found' do
+    lambda { raise_error 3 }.must_raise Deltacloud::ExceptionHandler::BackendError
+    begin raise_error(3); rescue Deltacloud::ExceptionHandler::BackendError => e
+      e.code.must_equal 500
+      e.message.must_equal 'Unhandled exception or status code (not captured)'
+      e.backtrace.wont_be_empty
+    end
+  end
+
+end
-- 
1.7.12


Mime
View raw message