Commit 11820e8d authored by Mark Lapierre's avatar Mark Lapierre Committed by Ramya Authappan

Use predicate instead of exception

For clearer and simpler logic avoid using exceptions as flow control
parent 2c753f80
......@@ -7,11 +7,11 @@ module QA
module Resource
module ApiFabricator
include Capybara::DSL
include Support::API
include Errors
attr_reader :api_resource, :api_response
attr_writer :api_client
attr_accessor :api_user
attr_accessor :api_user, :api_resource, :api_response
def api_support?
respond_to?(:api_get_path) &&
......@@ -48,9 +48,6 @@ module QA
end
end
include Support::API
attr_writer :api_resource, :api_response
def api_put(body = api_put_body)
response = put(
Runtime::API::Request.new(api_client, api_put_path).url,
......@@ -67,6 +64,16 @@ module QA
@api_fabrication_http_method ||= :post
end
# Checks if a resource already exists
#
# @return [Boolean] true if the resource returns HTTP status code 200
def exists?
request = Runtime::API::Request.new(api_client, api_get_path)
response = get(request.url)
response.code == HTTP_STATUS_OK
end
private
def resource_web_url(resource)
......
......@@ -46,7 +46,9 @@ module QA
# @return [Hash<Symbol, QA::Resource>] the resources that were to be removed.
def remove_all_via_api!
resources.each do |reuse_as, resource|
QA::Runtime::Logger.debug("#{self.name} - removing #{reuse_as}")
QA::Runtime::Logger.debug("#{self.name} - removing resource reused as :#{reuse_as}")
next QA::Runtime::Logger.debug("#{self.name} reused as :#{reuse_as} has already been removed.") unless resource.exists?
resource.method(:remove_via_api!).super_method.call
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment