From 3a862a40fa3100038ddc6a32140a2b9c8c8a8955 Mon Sep 17 00:00:00 2001 From: Daniel Miranda Date: Mon, 21 Mar 2016 13:52:45 -0300 Subject: [PATCH 01/27] Migrate configuration loading and Cucumber Helpers to Likeno --- kalibro_client.gemspec | 1 + lib/kalibro_client.rb | 43 +------ lib/kalibro_client/entities/base.rb | 4 +- .../kalibro_cucumber_helpers.rb | 43 ++----- .../kalibro_cucumber_helpers/cleaner.rb | 23 ++++ .../kalibro_cucumber_helpers/configuration.rb | 30 ----- spec/kalibro_cucumber_helpers/cleaner_spec.rb | 36 ++++++ spec/kalibro_entities_spec.rb | 112 ------------------ 8 files changed, 73 insertions(+), 219 deletions(-) create mode 100644 lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb delete mode 100644 lib/kalibro_client/kalibro_cucumber_helpers/configuration.rb create mode 100644 spec/kalibro_cucumber_helpers/cleaner_spec.rb delete mode 100644 spec/kalibro_entities_spec.rb diff --git a/kalibro_client.gemspec b/kalibro_client.gemspec index 78327ae..5a17dcb 100644 --- a/kalibro_client.gemspec +++ b/kalibro_client.gemspec @@ -47,4 +47,5 @@ Gem::Specification.new do |spec| spec.add_dependency "activesupport", ">= 2.2.1" #version in which underscore was introduced spec.add_dependency "faraday_middleware", "~> 0.9" + spec.add_dependency "likeno", "0.0.1.rc2" end diff --git a/lib/kalibro_client.rb b/lib/kalibro_client.rb index 3a384f2..a40bee1 100644 --- a/lib/kalibro_client.rb +++ b/lib/kalibro_client.rb @@ -14,49 +14,14 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -require 'yaml' -require 'logger' +require 'likeno' require "kalibro_client/version" require "kalibro_client/errors" require "kalibro_client/entities" module KalibroClient - @config = { - processor_address: "http://localhost:8082", - configurations_address: "http://localhost:8083" - } + # Apply default configuration to Likeno + Likeno.configure(processor_address: "http://localhost:8082", + configurations_address: "http://localhost:8083") - @valid_config_keys = @config.keys - - @logger = Logger.new(STDOUT) - - # Configure through hash - def KalibroClient.configure(opts = {}) - opts.each {|k,v| @config[k.to_sym] = v if @valid_config_keys.include? k.to_sym} - end - - # Configure through yaml file - def KalibroClient.configure_with(path_to_yaml_file) - begin - config = YAML::load(IO.read(path_to_yaml_file)) - rescue Errno::ENOENT - logger.warn("YAML configuration file couldn't be found. Using defaults."); return - rescue Psych::SyntaxError - logger.warn("YAML configuration file contains invalid syntax. Using defaults."); return - end - - configure(config) - end - - def KalibroClient.config - @config - end - - def KalibroClient.logger - @logger - end - - def KalibroClient.logger=(logger) - @logger = logger - end end diff --git a/lib/kalibro_client/entities/base.rb b/lib/kalibro_client/entities/base.rb index 75b786d..b21f0ad 100644 --- a/lib/kalibro_client/entities/base.rb +++ b/lib/kalibro_client/entities/base.rb @@ -1,4 +1,4 @@ -# This file is part of KalibroClient + # This file is part of KalibroClient # Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) # # This program is free software: you can redistribute it and/or modify @@ -166,7 +166,7 @@ def self.address # TODO: probably the connection could be a class static variable. def self.client - Faraday.new(:url => KalibroClient.config[address]) do |conn| + Faraday.new(:url => Likeno.config[address]) do |conn| conn.request :json conn.response :json, :content_type => /\bjson$/ conn.adapter Faraday.default_adapter # make requests with Net::HTTP diff --git a/lib/kalibro_client/kalibro_cucumber_helpers.rb b/lib/kalibro_client/kalibro_cucumber_helpers.rb index 053d7d1..0719f49 100644 --- a/lib/kalibro_client/kalibro_cucumber_helpers.rb +++ b/lib/kalibro_client/kalibro_cucumber_helpers.rb @@ -14,47 +14,18 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -require 'yaml' -require 'kalibro_client/kalibro_cucumber_helpers/configuration' +require 'kalibro_client/kalibro_cucumber_helpers/cleaner' module KalibroClient module KalibroCucumberHelpers - @configuration = KalibroClient::KalibroCucumberHelpers::Configuration.new - - def KalibroCucumberHelpers.configure(&config_block) - config_block.call(@configuration) - end - - def KalibroCucumberHelpers.configure_from_yml(file_path) - configuration = YAML.load(File.open("features/support/kalibro_cucumber_helpers.yml")) - - configuration["kalibro_cucumber_helpers"].each do |config, value| - @configuration.send("#{config}=", value) - end - end - - def KalibroCucumberHelpers.configuration - @configuration + def self.clean_processor + @processor_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:kalibro_processor_address) + @processor_cleaner.clean_database end - def KalibroCucumberHelpers.clean_processor - client = Faraday.new(:url => @configuration.kalibro_processor_address) do |conn| - conn.request :json - conn.response :json, :content_type => /\bjson$/ - conn.adapter Faraday.default_adapter # make requests with Net::HTTP - end - - client.send(:post, "/tests/clean_database", {}) - end - - def KalibroCucumberHelpers.clean_configurations - client = Faraday.new(:url => @configuration.kalibro_configurations_address) do |conn| - conn.request :json - conn.response :json, :content_type => /\bjson$/ - conn.adapter Faraday.default_adapter # make requests with Net::HTTP - end - - client.send(:post, "/tests/clean_database", {}) + def self.clean_configurations + @configurations_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:kalibro_configurations_address) + @configurations_cleaner.clean_database end end end diff --git a/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb b/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb new file mode 100644 index 0000000..249aa33 --- /dev/null +++ b/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb @@ -0,0 +1,23 @@ +require 'likeno/request_methods' + +module KalibroClient + module KalibroCucumberHelpers + class Cleaner + include Likeno::RequestMethods + + attr_reader :address + + def initialize(address) + @address = address + end + + def endpoint + 'tests' + end + + def clean_database + request('clean_database', {}, :post) + end + end + end +end diff --git a/lib/kalibro_client/kalibro_cucumber_helpers/configuration.rb b/lib/kalibro_client/kalibro_cucumber_helpers/configuration.rb deleted file mode 100644 index 7e467d8..0000000 --- a/lib/kalibro_client/kalibro_cucumber_helpers/configuration.rb +++ /dev/null @@ -1,30 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -module KalibroClient - module KalibroCucumberHelpers - class Configuration - attr_accessor :kalibro_processor_address, :kalibro_configurations_address - - def initialize(attributes={}) - self.kalibro_processor_address = "http://localhost:8082" - self.kalibro_configurations_address = "http://localhost:8083" - - attributes.each { |field, value| send("#{field}=", value) if respond_to?("#{field}=") } - end - end - end -end diff --git a/spec/kalibro_cucumber_helpers/cleaner_spec.rb b/spec/kalibro_cucumber_helpers/cleaner_spec.rb new file mode 100644 index 0000000..79b4b50 --- /dev/null +++ b/spec/kalibro_cucumber_helpers/cleaner_spec.rb @@ -0,0 +1,36 @@ +require 'rspec/mocks' +require 'faraday' +require 'likeno' +require 'kalibro_client/kalibro_cucumber_helpers' + +describe KalibroClient::KalibroCucumberHelpers::Cleaner do + let(:address_key) { :test_address } + let(:likeno_config) { {address_key => 'http://test'} } + + subject { described_class.new(address_key) } + + describe 'endpoint' do + it 'is expected to return "tests"' do + expect(subject.endpoint).to eq('tests') + end + end + + describe 'base_address' do + it 'is expected to find the address in Likeno' do + expect(subject.address).to eq(address_key) + end + end + + describe 'clean_database' do + let(:client) { mock("client") } + let(:request) { mock("request") } + let(:response) { mock("response", success?: true, body: '') } + let(:options) { stub_everything } + + it 'is expected to make a request to the correct address' do + subject.expects(:request).with('clean_database', {}, :post) + + subject.clean_database + end + end +end diff --git a/spec/kalibro_entities_spec.rb b/spec/kalibro_entities_spec.rb deleted file mode 100644 index 3dac586..0000000 --- a/spec/kalibro_entities_spec.rb +++ /dev/null @@ -1,112 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -require 'spec_helper' - -describe KalibroClient do - - context 'configuration' do - #FIXME: there should be a better way to keep the default values - let(:config) { { processor_address: "http://localhost:8082", - configurations_address: "http://localhost:8083" } } - - describe 'config' do - it 'should return the default configuration' do - expect(KalibroClient.config).to eq({ - processor_address: "http://localhost:8082", - configurations_address: "http://localhost:8083" - }) - end - end - - describe 'configure' do - after(:each) {KalibroClient.configure(config)} - - it 'should set the address' do - KalibroClient.configure({processor_address: 'http://test.test'}) - expect(KalibroClient.config[:processor_address]).to eq('http://test.test') - end - end - - describe 'configure_with' do - context 'with an existent YAML' do - after(:each) {KalibroClient.configure(config)} - - it 'should set the config' do - KalibroClient.configure_with('spec/fixtures/config.yml') - - expect(KalibroClient.config).to eq({ - processor_address: 'http://test1.test1', - configurations_address: 'http://test2.test2'}) - end - end - - context 'with an inexistent YAML' do - before :each do - @logger = Logger.new(File::NULL) - KalibroClient.expects(:logger).returns(@logger) - end - - it 'should keep the defaults' do - KalibroClient.configure_with('spec/fixtures/inexistent_file.yml') - expect(KalibroClient.config).to eq(config) - end - - it 'should log an warning' do - @logger.expects(:warn).with("YAML configuration file couldn't be found. Using defaults.") - - KalibroClient.configure_with('spec/fixtures/inexistent_file.yml') - end - end - - context 'with an invalid YAML' do - before :each do - @logger = Logger.new(File::NULL) - KalibroClient.expects(:logger).returns(@logger) - end - - it 'should keep the defaults' do - KalibroClient.configure_with('spec/fixtures/invalid_config.yml') - expect(KalibroClient.config).to eq(config) - end - - it 'should log an warning' do - @logger.expects(:warn).with("YAML configuration file contains invalid syntax. Using defaults.") - - KalibroClient.configure_with('spec/fixtures/invalid_config.yml') - end - end - end - end - - context 'Logger' do - describe 'logger' do - it 'should return the default logger' do - expect(KalibroClient.logger).to be_a(Logger) - end - end - - describe 'logger=' do - it 'should set the logger' do - logger = Logger.new(STDOUT) - - KalibroClient.logger = logger - - expect(KalibroClient.logger).to eq(logger) - end - end - end -end From 9f3b86d5c02ae0eec77407758cdad68aeab30f70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Silva=20Ara=C3=BAjo?= Date: Mon, 21 Mar 2016 17:11:29 -0300 Subject: [PATCH 02/27] Update Likeno to version 0.0.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Diego Araújo --- kalibro_client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kalibro_client.gemspec b/kalibro_client.gemspec index 5a17dcb..5c66c0e 100644 --- a/kalibro_client.gemspec +++ b/kalibro_client.gemspec @@ -47,5 +47,5 @@ Gem::Specification.new do |spec| spec.add_dependency "activesupport", ">= 2.2.1" #version in which underscore was introduced spec.add_dependency "faraday_middleware", "~> 0.9" - spec.add_dependency "likeno", "0.0.1.rc2" + spec.add_dependency "likeno", "0.0.1" end From 8bc552d71bf04bd9516217bd64862a11308d6a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Silva=20Ara=C3=BAjo?= Date: Mon, 21 Mar 2016 17:12:36 -0300 Subject: [PATCH 03/27] Fix KalibroCucumberHelpers::cleaner behaviour MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remove unnecessary initialization from env - Use Likeno to configure the paths to be used to clean external databases Signed-off-by: Diego Araújo --- features/support/env.rb | 3 --- lib/kalibro_client/kalibro_cucumber_helpers.rb | 4 ++-- .../kalibro_cucumber_helpers/cleaner.rb | 12 ++++++++---- spec/kalibro_cucumber_helpers/cleaner_spec.rb | 5 +++-- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/features/support/env.rb b/features/support/env.rb index d9a2e7f..3bfe490 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -15,9 +15,6 @@ # Kalibro hooks require 'kalibro_client/kalibro_cucumber_helpers/hooks' -# Configuring the right hooks -KalibroClient::KalibroCucumberHelpers.configure_from_yml("#{__dir__}/kalibro_cucumber_helpers.yml") - # The gem itself require 'kalibro_client' diff --git a/lib/kalibro_client/kalibro_cucumber_helpers.rb b/lib/kalibro_client/kalibro_cucumber_helpers.rb index 0719f49..4ec03c1 100644 --- a/lib/kalibro_client/kalibro_cucumber_helpers.rb +++ b/lib/kalibro_client/kalibro_cucumber_helpers.rb @@ -19,12 +19,12 @@ module KalibroClient module KalibroCucumberHelpers def self.clean_processor - @processor_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:kalibro_processor_address) + @processor_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:processor_address) @processor_cleaner.clean_database end def self.clean_configurations - @configurations_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:kalibro_configurations_address) + @configurations_cleaner ||= KalibroClient::KalibroCucumberHelpers::Cleaner.new(:configurations_address) @configurations_cleaner.clean_database end end diff --git a/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb b/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb index 249aa33..5f8078f 100644 --- a/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb +++ b/lib/kalibro_client/kalibro_cucumber_helpers/cleaner.rb @@ -1,14 +1,18 @@ -require 'likeno/request_methods' +require 'likeno' module KalibroClient module KalibroCucumberHelpers class Cleaner include Likeno::RequestMethods - attr_reader :address + attr_reader :address_key - def initialize(address) - @address = address + def initialize(address_key) + @address_key = address_key + end + + def address + Likeno.config[address_key] end def endpoint diff --git a/spec/kalibro_cucumber_helpers/cleaner_spec.rb b/spec/kalibro_cucumber_helpers/cleaner_spec.rb index 79b4b50..8e44ac1 100644 --- a/spec/kalibro_cucumber_helpers/cleaner_spec.rb +++ b/spec/kalibro_cucumber_helpers/cleaner_spec.rb @@ -15,9 +15,10 @@ end end - describe 'base_address' do + describe 'address' do it 'is expected to find the address in Likeno' do - expect(subject.address).to eq(address_key) + Likeno.expects(:config).returns likeno_config + expect(subject.address).to eq(likeno_config[address_key]) end end From ed8cdae1f769996203530cfe79e4a43f1706d4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Wed, 23 Mar 2016 16:30:19 -0300 Subject: [PATCH 04/27] Update likeno to v1.0.0 Signed off by: Rafael Reggiani Manzo --- kalibro_client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kalibro_client.gemspec b/kalibro_client.gemspec index 5c66c0e..2d04804 100644 --- a/kalibro_client.gemspec +++ b/kalibro_client.gemspec @@ -47,5 +47,5 @@ Gem::Specification.new do |spec| spec.add_dependency "activesupport", ">= 2.2.1" #version in which underscore was introduced spec.add_dependency "faraday_middleware", "~> 0.9" - spec.add_dependency "likeno", "0.0.1" + spec.add_dependency "likeno", "~> 1.0" end From 159a316e83cb95a30a29e7d05925d5c6d224c836 Mon Sep 17 00:00:00 2001 From: Rafael Reggiani Manzo Date: Wed, 23 Mar 2016 18:21:27 -0300 Subject: [PATCH 05/27] [ci skip] Adapt Base to use Likeno MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fails because Likeno hash conversion needs to get fixed first. Likeno adds new custom headers and prefixes functionality, which then required adaptations here. Base#convert_to_hash handles conversion from classes within Miscellaneous modules. Base#is_valid? has been renamed to Base#valid?. This may be a error of ours. If other applications require this method we will need to restore `is_valid?` as a deprecated alias to `valid?`. Just as `kalibro_errors`. Processor::Base and configurations::Base required adaptations as well to correctly pull the `address` from configuration files. Signed off by: Diego Araújo --- lib/kalibro_client/entities/base.rb | 217 +------- .../entities/configurations/base.rb | 6 +- .../configurations/metric_configuration.rb | 2 +- .../entities/miscellaneous/base.rb | 6 +- .../entities/miscellaneous/metric.rb | 2 - lib/kalibro_client/entities/processor/base.rb | 6 +- .../processor/hotspot_metric_result.rb | 2 +- .../entities/processor/tree_metric_result.rb | 2 +- lib/kalibro_client/helpers/date_attributes.rb | 11 - lib/kalibro_client/helpers/hash_converters.rb | 48 -- lib/kalibro_client/helpers/request_methods.rb | 64 --- spec/entities/base_spec.rb | 466 +----------------- .../configurations/kalibro_range_spec.rb | 6 +- .../metric_configuration_spec.rb | 2 +- spec/entities/configurations/reading_spec.rb | 6 +- spec/entities/processor/project_spec.rb | 2 +- spec/entities/processor/repository_spec.rb | 2 +- spec/helpers/date_attributes_spec.rb | 30 -- spec/helpers/hash_converters_spec.rb | 120 ----- 19 files changed, 52 insertions(+), 948 deletions(-) delete mode 100644 lib/kalibro_client/helpers/date_attributes.rb delete mode 100644 lib/kalibro_client/helpers/hash_converters.rb delete mode 100644 lib/kalibro_client/helpers/request_methods.rb delete mode 100644 spec/helpers/date_attributes_spec.rb delete mode 100644 spec/helpers/hash_converters_spec.rb diff --git a/lib/kalibro_client/entities/base.rb b/lib/kalibro_client/entities/base.rb index b21f0ad..34fffa9 100644 --- a/lib/kalibro_client/entities/base.rb +++ b/lib/kalibro_client/entities/base.rb @@ -14,218 +14,27 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -require 'faraday_middleware' -require 'kalibro_client/helpers/hash_converters' -require 'kalibro_client/helpers/request_methods' +require 'likeno' module KalibroClient module Entities - class Base - attr_accessor :kalibro_errors, :persisted - - def initialize(attributes={}, persisted=false) - attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } - @kalibro_errors = [] - @persisted = persisted - end - - def to_hash(options={}) - hash = Hash.new - excepts = options[:except].nil? ? [] : options[:except] - excepts << "kalibro_errors" - excepts << "persisted" - fields.each do |field| - hash = field_to_hash(field).merge(hash) if !excepts.include?(field) - end - hash - end - - def self.request(action, params = {}, method = :post, prefix="") - response = client.send(method) do |request| - url = "/#{endpoint}/#{action}".gsub(":id", params[:id].to_s) - url = "/#{prefix}#{url}" unless prefix.empty? - request.url url - request.body = params unless method == :get || params.empty? - request.options.timeout = 300 - request.options.open_timeout = 300 - end - - if response.success? - response.body - # FIXME This condition was added to preserve the deprecated error codes that are returned by kalibro processor - elsif response.status == 404 || (response.body.key?('errors') && /NotFound/ === response.body['errors']) - raise KalibroClient::Errors::RecordNotFound.new(response: response) - else - raise KalibroClient::Errors::RequestError.new(response: response) - end - end - - - def self.to_object value - value.kind_of?(Hash) ? new(value, true) : value - end - - def self.to_objects_array value - array = value.kind_of?(Array) ? value : [value] - array.each.map { |element| to_object(element) } - end - - def save - if persisted? - self.update - else - without_request_error? do - response = self.class.request(save_action, save_params, :post, save_prefix) - - self.id = response[instance_class_name]["id"] - self.created_at = response[instance_class_name]["created_at"] unless response[instance_class_name]["created_at"].nil? - self.updated_at = response[instance_class_name]["updated_at"] unless response[instance_class_name]["updated_at"].nil? - @persisted = true - end - end - end - - def save! - return true if save - raise KalibroClient::Errors::RecordInvalid.new(self) - end - - def self.create(attributes={}) - new_model = new attributes - new_model.save - new_model - end - - def update(attributes={}) - attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } - without_request_error? do - self.class.request(update_action, update_params, :put, update_prefix) - end - end - - def ==(another) - unless self.class == another.class - return false - end - - self.variable_names.each do |name| - next if name == "created_at" or name == "updated_at" or name == "persisted" - unless self.send("#{name}") == another.send("#{name}") then - return false - end - end - - return true - end - - def self.exists?(id) - request(exists_action, id_params(id), :get)['exists'] - end - - def self.find(id) - response = request(find_action, id_params(id), :get) - new(response[entity_name], true) - end - - def destroy - without_request_error? do - response = self.class.request(destroy_action, destroy_params, :delete, destroy_prefix) - @persisted = false - end + class Base < Likeno::Entity + # We do not track coverage here as it is deprecated and will be removed soon + # :nocov: + def kalibro_errors + STDERR.puts "DEPRECATED: use 'likeno_errors' instead of 'kalibro_errors'." + self.likeno_errors end + # :nocov: - def self.create_objects_array_from_hash (response) - create_array_from_hash(response[entity_name.pluralize]).map { |hash| new(hash, true) } + def self.module_name + 'KalibroClient' end - def self.create_array_from_hash (response) - response = [] if response.nil? - response = [response] if response.is_a?(Hash) - response + def convert_to_hash(value) + return value.to_hash if value.is_a?(KalibroClient::Entities::Miscellaneous::Base) + super(value) end - - alias_method :persisted?, :persisted - - protected - - def instance_variable_names - instance_variables.map { |var| var.to_s } - end - - def fields - instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '') } - end - - def variable_names - instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '') } - end - - def self.address - raise NotImplementedError - end - - # TODO: probably the connection could be a class static variable. - def self.client - Faraday.new(:url => Likeno.config[address]) do |conn| - conn.request :json - conn.response :json, :content_type => /\bjson$/ - conn.adapter Faraday.default_adapter # make requests with Net::HTTP - end - end - - def self.is_valid?(field) - field.to_s[0] != '@' and field != :attributes! and (field =~ /attributes!/).nil? and (field.to_s =~ /xsi/).nil? and (field.to_s =~ /errors/).nil? - end - - # TODO rename to instance_entity_name - def instance_class_name - self.class.entity_name - end - - include RequestMethods - extend RequestMethods::ClassMethods - - def add_error(exception) - @kalibro_errors << exception - end - - def self.endpoint - entity_name.pluralize - end - - def self.entity_name - # This loop is a generic way to make this work even when the children class has a different name - entity_class = self - until entity_class.name.include?("KalibroClient::Entities::") do - entity_class = entity_class.superclass - end - - return entity_class.name.split("::").last.underscore.downcase - end - - def without_request_error?(&block) - begin - block.call - true - rescue KalibroClient::Errors::RecordNotFound => error - raise error - rescue KalibroClient::Errors::RequestError => error - raise error if error.response.status.between?(500, 599) - - response_errors = error.response.body['errors'] - if response_errors.is_a?(Array) - response_errors.each { |error_msg| add_error(error_msg) } - elsif !response_errors.nil? - add_error response_errors - else - add_error error - end - - false - end - end - - include HashConverters end end end diff --git a/lib/kalibro_client/entities/configurations/base.rb b/lib/kalibro_client/entities/configurations/base.rb index fcb40ae..ee1fc40 100644 --- a/lib/kalibro_client/entities/configurations/base.rb +++ b/lib/kalibro_client/entities/configurations/base.rb @@ -1,13 +1,13 @@ -require 'kalibro_client/helpers/date_attributes' +require 'likeno/helpers/date_attributes' module KalibroClient module Entities module Configurations class Base < KalibroClient::Entities::Base - include DateAttributes + include Likeno::DateAttributes def self.address - :configurations_address + Likeno.config[:configurations_address] end end end diff --git a/lib/kalibro_client/entities/configurations/metric_configuration.rb b/lib/kalibro_client/entities/configurations/metric_configuration.rb index 2c59fd2..9ad7c44 100644 --- a/lib/kalibro_client/entities/configurations/metric_configuration.rb +++ b/lib/kalibro_client/entities/configurations/metric_configuration.rb @@ -56,7 +56,7 @@ def weight=(value) end def update_attributes(attributes={}) - attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } + attributes.each { |field, value| send("#{field}=", value) if self.class.valid?(field) } save end diff --git a/lib/kalibro_client/entities/miscellaneous/base.rb b/lib/kalibro_client/entities/miscellaneous/base.rb index d9dcfb4..21c7156 100644 --- a/lib/kalibro_client/entities/miscellaneous/base.rb +++ b/lib/kalibro_client/entities/miscellaneous/base.rb @@ -1,12 +1,14 @@ +require 'likeno/helpers/hash_converters' + module KalibroClient module Entities module Miscellaneous class Base def initialize(attributes={}) - attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) } + attributes.each { |field, value| send("#{field}=", value) if self.class.valid?(field) } end - include HashConverters + include Likeno::HashConverters def to_hash(options={}) hash = Hash.new excepts = options[:except].nil? ? [] : options[:except] diff --git a/lib/kalibro_client/entities/miscellaneous/metric.rb b/lib/kalibro_client/entities/miscellaneous/metric.rb index 310568e..b8fa2da 100644 --- a/lib/kalibro_client/entities/miscellaneous/metric.rb +++ b/lib/kalibro_client/entities/miscellaneous/metric.rb @@ -1,5 +1,3 @@ -require 'kalibro_client/helpers/hash_converters' - module KalibroClient module Entities module Miscellaneous diff --git a/lib/kalibro_client/entities/processor/base.rb b/lib/kalibro_client/entities/processor/base.rb index 6ef3fa2..8da6756 100644 --- a/lib/kalibro_client/entities/processor/base.rb +++ b/lib/kalibro_client/entities/processor/base.rb @@ -1,14 +1,14 @@ require 'json' -require 'kalibro_client/helpers/date_attributes' +require 'likeno/helpers/date_attributes' module KalibroClient module Entities module Processor class Base < KalibroClient::Entities::Base - include DateAttributes + include Likeno::DateAttributes def self.address - :processor_address + Likeno.config[:processor_address] end end end diff --git a/lib/kalibro_client/entities/processor/hotspot_metric_result.rb b/lib/kalibro_client/entities/processor/hotspot_metric_result.rb index d9cff33..0db74ff 100644 --- a/lib/kalibro_client/entities/processor/hotspot_metric_result.rb +++ b/lib/kalibro_client/entities/processor/hotspot_metric_result.rb @@ -24,7 +24,7 @@ class HotspotMetricResult < KalibroClient::Entities::Processor::MetricResult def initialize(attributes={}, persisted=false) @line_number = attributes["line_number"].to_i attributes.each do |field, value| - if field != "line_number" and self.class.is_valid?(field) + if field != "line_number" and self.class.valid?(field) send("#{field}=", value) end end diff --git a/lib/kalibro_client/entities/processor/tree_metric_result.rb b/lib/kalibro_client/entities/processor/tree_metric_result.rb index 9f942bf..7a30457 100644 --- a/lib/kalibro_client/entities/processor/tree_metric_result.rb +++ b/lib/kalibro_client/entities/processor/tree_metric_result.rb @@ -24,7 +24,7 @@ def initialize(attributes={}, persisted=false) value = attributes["value"] @value = (value == "NaN") ? attributes["aggregated_value"].to_f : value.to_f attributes.each do |field, value| - if field!= "value" and field!= "aggregated_value" and self.class.is_valid?(field) + if field!= "value" and field!= "aggregated_value" and self.class.valid?(field) send("#{field}=", value) end end diff --git a/lib/kalibro_client/helpers/date_attributes.rb b/lib/kalibro_client/helpers/date_attributes.rb deleted file mode 100644 index f4a01c9..0000000 --- a/lib/kalibro_client/helpers/date_attributes.rb +++ /dev/null @@ -1,11 +0,0 @@ -module DateAttributes - attr_reader :created_at, :updated_at - - def created_at=(value) - @created_at = DateTime.parse(value) - end - - def updated_at=(value) - @updated_at = DateTime.parse(value) - end -end \ No newline at end of file diff --git a/lib/kalibro_client/helpers/hash_converters.rb b/lib/kalibro_client/helpers/hash_converters.rb deleted file mode 100644 index cea5448..0000000 --- a/lib/kalibro_client/helpers/hash_converters.rb +++ /dev/null @@ -1,48 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -require 'date' -require 'kalibro_client/helpers/xml_converters' - -module HashConverters - include XMLConverters - - #FIXME: we can think about a better name. This method actually receives an DateTime and converts it to string - def date_with_milliseconds(date) - milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s - date.to_s[0..18] + milliseconds + date.to_s[19..-1] - end - - def convert_to_hash(value) - return value if value.nil? - return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array) - return value.to_hash if value.is_a?(KalibroClient::Entities::Base) || value.is_a?(KalibroClient::Entities::Miscellaneous::Base) - return date_with_milliseconds(value) if value.is_a?(DateTime) - return 'INF' if value.is_a?(Float) and value.infinite? == 1 - return '-INF' if value.is_a?(Float) and value.infinite? == -1 - value.to_s - end - - def field_to_hash(field) - hash = Hash.new - field_value = send(field) - if !field_value.nil? - hash[field] = convert_to_hash(field_value) - hash = get_xml(field, field_value).merge(hash) - end - hash - end -end diff --git a/lib/kalibro_client/helpers/request_methods.rb b/lib/kalibro_client/helpers/request_methods.rb deleted file mode 100644 index 6ae5d83..0000000 --- a/lib/kalibro_client/helpers/request_methods.rb +++ /dev/null @@ -1,64 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -module RequestMethods - def save_params - {instance_class_name.underscore.to_sym => self.to_hash} - end - - def save_action - "" - end - - def save_prefix - "" - end - - def destroy_action - ":id" - end - alias_method :update_action, :destroy_action - - def destroy_params - {id: self.id} - end - - def destroy_prefix - "" - end - - def update_params - {instance_class_name.underscore.to_sym => self.to_hash, :id => self.id} - end - - def update_prefix - "" - end - - module ClassMethods - def exists_action - ":id/exists" - end - - def id_params(id) - {id: id} - end - - def find_action - ":id" - end - end -end diff --git a/spec/entities/base_spec.rb b/spec/entities/base_spec.rb index fe61707..f207774 100644 --- a/spec/entities/base_spec.rb +++ b/spec/entities/base_spec.rb @@ -19,467 +19,35 @@ # Create a class that has the attribute assignment methods, since some methods expect they exist # (and usually the subclasses do that). +class MiscellaneousTest < KalibroClient::Entities::Miscellaneous::Base + attr_accessor :id +end + class BaseTest < KalibroClient::Entities::Base - attr_accessor :id, :created_at, :updated_at + attr_accessor :id, :miscellaneous end describe KalibroClient::Entities::Base do subject { BaseTest.new } - describe 'new' do - subject { described_class.new({}) } - - it 'is expected to create a model from an empty hash' do - expect(subject.kalibro_errors).to eq([]) - end - end - - describe 'entity_name' do - it 'is expected to be a String' do - expect(subject.class.entity_name).to be_a(String) - end - - it 'is expected to return Base' do - expect(subject.class.entity_name).to eq('base') - end - end - - describe 'endpoint' do - it 'is expected to return the entity_name' do - endpoint = 'tests' - described_class.expects(:entity_name).returns(endpoint) - expect(described_class.endpoint).to eq(endpoint) - end - end - - describe 'client' do - it 'returns a Faraday::Connection' do - expect { described_class.client }.to raise_error(NotImplementedError) - end - end - - describe 'request' do - context 'with successful responses' do - let(:exists_response) { { 'exists' => false } } - let(:bases_response) { { 'bases' => { 'id' => 1 } } } - let(:prefix_bases_response) { { 'bases' => { 'id' => 2 } } } - let(:faraday_stubs) { Faraday::Adapter::Test::Stubs.new } - let(:connection) { Faraday.new { |builder| builder.adapter :test, faraday_stubs } } - - before :each do - subject.class.expects(:client).at_least_once.returns(connection) - subject.class.expects(:endpoint).at_least_once.returns('bases') - end - - after :each do - faraday_stubs.verify_stubbed_calls - end - - context 'without an id parameter' do - context 'without a prefix' do - it 'is expected to make the request without the prefix' do - # stub.get receives arguments: path, headers, block - # The block should be a Array [status, headers, body] - faraday_stubs.get('/bases/') { [200, {}, bases_response] } - response = subject.class.request('', {}, :get) - expect(response).to eq(bases_response) - end - end - - context 'with a prefix' do - it 'is expected to make the request with the prefix' do - # stub.get receives arguments: path, headers, block - # The block should be a Array [status, headers, body] - faraday_stubs.get('/prefix/bases/') { [200, {}, prefix_bases_response] } - response = subject.class.request('', {}, :get, 'prefix') - expect(response).to eq(prefix_bases_response) - end - end - end - - context 'with an id parameter' do - it 'is expected to make the request with the id included' do - # stub.get receives arguments: path, headers, block - # The block should be a Array [status, headers, body] - faraday_stubs.get('/bases/1/exists') { [200, {}, exists_response] } - response = subject.class.request(':id/exists', { id: 1 }, :get) - expect(response).to eq(exists_response) - end - end - end - - context 'when the record was not found' do - context 'and or the status is 404' do - let!(:faraday_stubs) do - Faraday::Adapter::Test::Stubs.new do |stub| - # stub.get receives arguments: path, headers, block - # The block should be a Array [status, headers, body] - stub.get('/bases/1') { [404, {}, {}] } - end - end - let!(:connection) { Faraday.new { |builder| builder.adapter :test, faraday_stubs } } - - before :each do - described_class.expects(:client).at_least_once.returns(connection) - end - - it 'is expected to raise a RecordNotFound error' do - expect { described_class.request(':id', { id: 1 }, :get) }.to raise_error(KalibroClient::Errors::RecordNotFound) - faraday_stubs.verify_stubbed_calls - end - end - - context 'and or the response has a NotFound error message' do - let!(:faraday_stubs) do - Faraday::Adapter::Test::Stubs.new do |stub| - # stub.get receives arguments: path, headers, block - # The block should be a Array [status, headers, body] - stub.get('/bases/1') { [422, {}, { 'errors' => 'RecordNotFound' }] } - end - end - let!(:connection) { Faraday.new { |builder| builder.adapter :test, faraday_stubs } } - - before :each do - described_class.expects(:client).at_least_once.returns(connection) - end - - it 'is expected to raise a RecordNotFound error' do - expect { described_class.request(':id', { id: 1 }, :get) }.to raise_error(KalibroClient::Errors::RecordNotFound) - faraday_stubs.verify_stubbed_calls - end - end - end - - context 'with an unsuccessful request' do - let!(:stubs) { Faraday::Adapter::Test::Stubs.new { |stub| stub.get('/bases/1/exists') { [500, {}, {}] } } } - let(:connection) { Faraday.new { |builder| builder.adapter :test, stubs } } - - before :each do - subject.class.expects(:client).at_least_once.returns(connection) - end - - it 'is expected to raise a RequestError with the response' do - expect { subject.class.request(':id/exists', { id: 1 }, :get) }.to raise_error do |error| - expect(error).to be_a(KalibroClient::Errors::RequestError) - expect(error.response.status).to eq(500) - expect(error.response.body).to eq({}) - end - stubs.verify_stubbed_calls - end - end - end - - describe 'to_hash' do - it 'is expected to return an empty hash' do - expect(subject.to_hash).to be_empty - end + before do + subject.id = 24 + subject.miscellaneous = MiscellaneousTest.new + subject.miscellaneous.id = 42 end - describe 'to_object' do - it 'is expected to return an Object with an empty hash' do - expect(described_class.to_object({})).to eq(FactoryGirl.build(:model)) - end - - it "is expected to remain an object if it isn't a Hash" do - expect(described_class.to_object(Object.new)).to be_an(Object) - end - end - - describe 'to_objects_array' do - it 'is expected to convert [{}] to [Model]' do - expect(described_class.to_objects_array({})).to eq([FactoryGirl.build(:model)]) - end - - it 'is expected to remain an array if it already is one' do - object = Object.new - expect(described_class.to_objects_array([object])).to eq([object]) - end - end - - shared_examples 'persistence method' do |method_name, http_method, has_id = true| - before :each do - subject.id = 42 if has_id - end - - let(:url) { has_id ? ':id' : '' } - let(:params) { has_id ? has_entry(id: 42) : anything } - - context 'when a record does not exist with given id' do - before :each do - subject.class.expects(:request).with(url, params, http_method, ''). - raises(KalibroClient::Errors::RecordNotFound) - end - - it 'is expected to raise a RecordNotFound error' do - expect { subject.send(method_name) }.to raise_error(KalibroClient::Errors::RecordNotFound) - end - end - - context 'when a server error is returned' do - before :each do - error = KalibroClient::Errors::RequestError.new(response: mock(status: 500)) - - subject.class.expects(:request).with(url, params, http_method, '').raises(error) - end - - it 'is expected to raise a RequestError error' do - expect { subject.send(method_name) }.to raise_error(KalibroClient::Errors::RequestError) - end - end - - context 'when a regular kind of error is returned' do - before :each do - error = KalibroClient::Errors::RequestError.new(response: mock(status: 422, body: { 'errors' => errors })) - - subject.class.expects(:request).with(url, params, http_method, '').raises(error) - end - - context 'with a single error' do - let(:errors) { "error" } - - it 'is expected to set the kalibro_errors field' do - expect(subject.send(method_name)).to eq(false) - expect(subject.kalibro_errors).to eq([errors]) - end - end - - context 'with an array of errors' do - let(:errors) { ["error_1", "error_2"] } - - it 'is expected to set the kalibro_errors field' do - expect(subject.send(method_name)).to eq(false) - expect(subject.kalibro_errors).to eq(errors) - end - end - - context 'with no error message at all' do - let(:errors) { nil } - - it 'is expected to set the kalibro_errors field' do - expect(subject.send(method_name)).to eq(false) - expect(subject.kalibro_errors.first).to be_a(KalibroClient::Errors::RequestError) - end + describe 'class method' do + describe 'module_name' do + it 'is expected to return the camelized gem name' do + expect(described_class.module_name).to eq('KalibroClient') end end end - describe 'save' do - it_behaves_like 'persistence method', :save, :post, false # false means Don't use ids in URLs - - context 'with a successful response' do - context 'when it is not persisted' do - before :each do - subject.class.expects(:request).at_least_once.with('', anything, :post, ''). - returns({ "base" => { 'id' => 42, 'errors' => [] } }) - end - - it 'is expected to make a request to save model with id and return true without errors' do - expect(subject.save).to be(true) - expect(subject.id).to eq(42) - expect(subject.kalibro_errors).to be_empty - end - end - - context 'when it is persisted' do - before :each do - subject.expects(:persisted?).at_least_once.returns(true) - end - - it 'is expected to call the update method' do - subject.expects(:update).returns(true) - expect(subject.save).to eq(true) - end - end - end - end - - describe 'update' do - it_behaves_like 'persistence method', :update, :put - - context 'with valid parameters' do - before :each do - id = 42 - - subject.expects(:id).at_least_once.returns(id) - described_class.expects(:request).with(':id', has_entry(id: id), :put, ''). - returns({ "base" => { 'id' => id, 'errors' => [] }}) - end - - it 'is expected to return true' do - expect(subject.update).to eq(true) - end - end - end - - describe 'create' do - before :each do - subject.expects(:save) - described_class. - expects(:new). - with({}). - returns(subject) - end - - it 'is expected to instantiate and save the model' do - expect(described_class.create {}).to eq(subject) - end - end - - describe 'find' do - context 'with an inexistent id' do - before :each do - subject.class.expects(:request).at_least_once.with(':id', has_entry(id: 0), :get). - raises(KalibroClient::Errors::RecordNotFound) - end - - it 'is expected to raise a RecordNotFound error' do - expect { subject.class.find(0) }.to raise_error(KalibroClient::Errors::RecordNotFound) - end - end - - context 'with an existent id' do - before :each do - subject.class.expects(:request).with(':id', has_entry(id: 42), :get). - returns("base" => { 'id' => 42 }) - end - - it 'is expected to return an empty model' do - expect(subject.class.find(42).id).to eq(42) - end - end - end - - describe 'destroy' do - it_behaves_like 'persistence method', :destroy, :delete - - context 'when it gets successfully destroyed' do - before :each do - subject.expects(:id).at_least_once.returns(42) - described_class.expects(:request).with(':id', { id: subject.id }, :delete, '').returns({}) - end - - it 'is expected to remain with the errors array empty and not persisted' do - subject.destroy - expect(subject.kalibro_errors).to be_empty - expect(subject.persisted?).to eq(false) - end - end - end - - describe 'save!' do - it 'is expected to call save and not raise when saving works' do - subject.expects(:save).returns(true) - expect { subject.save! }.not_to raise_error - end - - it 'is expected to call save and raise RecordInvalid when saving fails' do - subject.expects(:kalibro_errors).returns(['test1', 'test2']) - subject.expects(:save).returns(false) - - expect { subject.save! }.to raise_error { |error| - expect(error).to be_a(KalibroClient::Errors::RecordInvalid) - expect(error.record).to be(subject) - expect(error.message).to eq('Record invalid: test1, test2') - } - end - end - - describe '==' do - subject { FactoryGirl.build(:model) } - - context 'comparing objects from different classes' do - it 'is expected to return false' do - expect(subject).not_to eq(Object.new) - end - end - - context 'with two models with different attribute values' do - let(:another_model) { FactoryGirl.build(:model) } - - before :each do - subject.expects(:variable_names).returns(["answer"]) - subject.expects(:send).with("answer").returns(42) - another_model.expects(:send).with("answer").returns(41) - end - - it 'is expected to return false' do - expect(subject).not_to eq(another_model) - end - end - - context 'with two empty models' do - it 'is expected to return true' do - expect(subject).to eq(FactoryGirl.build(:model)) - end - end - end - - describe 'exists?' do - context 'with an inexistent id' do - before :each do - described_class. - expects(:request). - with(':id/exists', { id: 0 }, :get). - returns({ 'exists' => false }) - end - - it 'is expected to return false' do - expect(described_class.exists?(0)).to eq(false) - end - end - - context 'with an existent id' do - before :each do - described_class. - expects(:request). - with(':id/exists', { id: 42 }, :get). - returns({ 'exists' => true }) - end - - it 'is expected to return false' do - expect(described_class.exists?(42)).to eq(true) - end - end - end - - describe 'create_objects_array_from_hash' do - subject { FactoryGirl.build(:model) } - - context 'with nil' do - it 'is expected to return an empty array' do - expect(described_class.create_objects_array_from_hash("bases" => [])).to eq([]) - end - end - - context 'with a Hash' do - it 'is expected to return the correspondent object to the given hash inside of an Array' do - expect(described_class.create_objects_array_from_hash("bases" => {})).to eq([subject]) - end - end - end - - describe 'is_valid?' do - context 'with a global var' do - it 'is expected to return false' do - expect(described_class.is_valid?('@test')).to be_falsey - end - end - - context 'with the attributes var' do - it 'is expected to return false' do - expect(described_class.is_valid?(:attributes!)).to be_falsey - end - end - - context 'with a xsi var' do - it 'is expected to return false' do - expect(described_class.is_valid?('test_xsi')).to be_falsey - end - end - - context 'with a valid var' do - it 'is expected to return true' do - expect(described_class.is_valid?('test')).to be_truthy + describe 'instance method' do + describe 'convert_to_hash' do + it 'is expected to convert miscellaneous nested objects' do + expect(subject.to_hash).to eq({'id' => "24", 'miscellaneous' => {'id' => "42"}}) end end end diff --git a/spec/entities/configurations/kalibro_range_spec.rb b/spec/entities/configurations/kalibro_range_spec.rb index aab6540..3a38115 100644 --- a/spec/entities/configurations/kalibro_range_spec.rb +++ b/spec/entities/configurations/kalibro_range_spec.rb @@ -123,7 +123,7 @@ before :each do KalibroClient::Entities::Configurations::KalibroRange. expects(:request). - with('', {:kalibro_range => subject.to_hash, :metric_configuration_id => subject.metric_configuration_id}, :post, "metric_configurations/#{subject.metric_configuration_id}"). + with('', {:kalibro_range => subject.to_hash, :metric_configuration_id => subject.metric_configuration_id}, :post, "metric_configurations/#{subject.metric_configuration_id}", {}). returns("kalibro_range" => { 'id' => 2, 'kalibro_errors' => []}) end @@ -140,7 +140,7 @@ before :each do KalibroClient::Entities::Configurations::KalibroRange. expects(:request). - with(":id", {id: subject.id}, :delete, "metric_configurations/#{subject.metric_configuration_id}").returns({}) + with(":id", {id: subject.id}, :delete, "metric_configurations/#{subject.metric_configuration_id}", {}).returns({}) end it 'should make a request to destroy' do @@ -156,7 +156,7 @@ subject.end = "555" KalibroClient::Entities::Configurations::KalibroRange. expects(:request). - with(':id', {:kalibro_range => subject.to_hash, :id => subject.id}, :put, "metric_configurations/#{subject.metric_configuration_id}"). + with(':id', {:kalibro_range => subject.to_hash, :id => subject.id}, :put, "metric_configurations/#{subject.metric_configuration_id}", {}). returns("errors" => nil) end diff --git a/spec/entities/configurations/metric_configuration_spec.rb b/spec/entities/configurations/metric_configuration_spec.rb index a49f7a7..831399e 100644 --- a/spec/entities/configurations/metric_configuration_spec.rb +++ b/spec/entities/configurations/metric_configuration_spec.rb @@ -159,7 +159,7 @@ before :each do KalibroClient::Entities::Configurations::MetricConfiguration. expects(:request). - with('', {:metric_configuration => subject.to_hash, :kalibro_configuration_id => subject.kalibro_configuration_id}, :post, ''). + with('', {:metric_configuration => subject.to_hash, :kalibro_configuration_id => subject.kalibro_configuration_id}, :post, '', {}). returns("metric_configuration" => {'id' => 1, 'kalibro_errors' => []}) end diff --git a/spec/entities/configurations/reading_spec.rb b/spec/entities/configurations/reading_spec.rb index 6e92970..2737ff9 100644 --- a/spec/entities/configurations/reading_spec.rb +++ b/spec/entities/configurations/reading_spec.rb @@ -52,7 +52,7 @@ before :each do KalibroClient::Entities::Configurations::Reading. expects(:request). - with('', {reading: reading.to_hash, reading_group_id: reading.reading_group_id}, :post, "reading_groups/#{reading.reading_group_id}"). + with('', {reading: reading.to_hash, reading_group_id: reading.reading_group_id}, :post, "reading_groups/#{reading.reading_group_id}", {}). returns("reading" => {'id' => reading_id, 'kalibro_errors' => []}) end @@ -70,7 +70,7 @@ before :each do KalibroClient::Entities::Configurations::Reading. expects(:request). - with(":id", {id: subject.id}, :delete, "reading_groups/#{subject.reading_group_id}").returns({}) + with(":id", {id: subject.id}, :delete, "reading_groups/#{subject.reading_group_id}", {}).returns({}) end it 'should make a request to destroy' do @@ -85,7 +85,7 @@ before :each do KalibroClient::Entities::Configurations::Reading. expects(:request). - with(":id", {reading: subject.to_hash, id: subject.id}, :put, "reading_groups/#{subject.reading_group_id}").returns({}) + with(":id", {reading: subject.to_hash, id: subject.id}, :put, "reading_groups/#{subject.reading_group_id}", {}).returns({}) end it 'should make a request to update' do diff --git a/spec/entities/processor/project_spec.rb b/spec/entities/processor/project_spec.rb index 836b258..6b8e7af 100644 --- a/spec/entities/processor/project_spec.rb +++ b/spec/entities/processor/project_spec.rb @@ -102,7 +102,7 @@ before :each do KalibroClient::Entities::Processor::Project. expects(:request). - with(':id', {project: {"name" => "Another Name", "description" => subject.description, "id" => subject.id.to_s}, id: subject.id}, :put, ''). + with(':id', {project: {"name" => "Another Name", "description" => subject.description, "id" => subject.id.to_s}, id: subject.id}, :put, '', {}). returns({"project" => {"id" => subject.id, "name" => "Another Name", "kalibro_errors" => []}}) end diff --git a/spec/entities/processor/repository_spec.rb b/spec/entities/processor/repository_spec.rb index a5b812a..a44459b 100644 --- a/spec/entities/processor/repository_spec.rb +++ b/spec/entities/processor/repository_spec.rb @@ -119,7 +119,7 @@ before :each do KalibroClient::Entities::Processor::Repository. expects(:request). - with('', {:repository => subject.to_hash, :project_id => 1}, :post, ''). + with('', {:repository => subject.to_hash, :project_id => 1}, :post, '', {}). returns("repository" => {'id' => 1, 'kalibro_errors' => []}) KalibroClient::Entities::Processor::Repository.any_instance. diff --git a/spec/helpers/date_attributes_spec.rb b/spec/helpers/date_attributes_spec.rb deleted file mode 100644 index 580a536..0000000 --- a/spec/helpers/date_attributes_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'spec_helper' -require 'kalibro_client/helpers/date_attributes' - -include DateAttributes - -class Klass - include DateAttributes -end - - -describe 'DateAttributes' do - subject { Klass.new } - let(:time){ "2015-02-04T15:53:18.452Z" } - - describe 'created_at=' do - it 'is expected to parse it to DateTime' do - DateTime.expects(:parse).with(time) - - subject.created_at = time - end - end - - describe 'updated_at=' do - it 'is expected to parse it to DateTime' do - DateTime.expects(:parse).with(time) - - subject.updated_at = time - end - end -end \ No newline at end of file diff --git a/spec/helpers/hash_converters_spec.rb b/spec/helpers/hash_converters_spec.rb deleted file mode 100644 index bffa649..0000000 --- a/spec/helpers/hash_converters_spec.rb +++ /dev/null @@ -1,120 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -require 'spec_helper' -require 'kalibro_client/helpers/hash_converters' - -include HashConverters - -describe HashConverters do - describe 'date_with_miliseconds' do - context 'with 21/12/1995 (first Ruby publication)' do - it 'should return 1995-12-21T00:00:00.0/1+00:00' do - expect(date_with_milliseconds(DateTime.parse("21/12/1995"))).to eq("1995-12-21T00:00:00.0/1+00:00") - end - end - end - - describe 'convert_to_hash' do - context 'with a nil value' do - it 'should return nil' do - expect(convert_to_hash(nil)).to be_nil - end - end - - context 'with an Array' do - before :each do - @array = [] - @element1 = :kalibro - - @array << @element1 - end - - it 'should return the Array wth its elements converted' do - expect(convert_to_hash(@array)[0]).to eq(@element1.to_s) - end - end - - context 'with a Base' do - before :each do - @model = KalibroClient::Entities::Base.new - end - - it "should return the Base's Hash" do - expect(convert_to_hash(@model)).to eq(@model.to_hash) - end - end - - context 'with a DateTime' do - before :each do - @date = DateTime.parse("21/12/1995") - end - - it 'should return th date with miliseconds' do - expect(convert_to_hash(@date)).to eq(date_with_milliseconds(@date)) - end - end - - context 'with an + infinite Float' do - it 'should return INF' do - expect(convert_to_hash(1.0/0.0)).to eq('INF') - end - end - - context 'with an - infinite Float' do - it 'should return -INF' do - expect(convert_to_hash(-1.0/0.0)).to eq('-INF') - end - end - - context 'with a granularity' do - let(:granularity) { FactoryGirl.build(:granularity) } - - it 'is expected to convert the granularity to a hash' do - expect(convert_to_hash(granularity)).to be_a Hash - expect(convert_to_hash(granularity)).to include 'type' - end - end - end - - describe 'field_to_hash' do - context 'with a nil field value' do - before do - @model = KalibroClient::Entities::Base.new - @model.expects(:send).with(:field_getter).returns(nil) - end - - it 'should return an instance of Hash' do - expect(@model.field_to_hash(:field_getter)).to be_a(Hash) - end - - it 'should return an empty Hash' do - expect(@model.field_to_hash(:field_getter)).to eq({}) - end - end - - context 'with a Float field value' do - before do - @model = KalibroClient::Entities::Base.new - @model.expects(:send).with(:field_getter).returns(1.0) - end - - it 'should return an instance of Hash' do - expect(@model.field_to_hash(:field_getter)).to be_a(Hash) - end - end - end -end From bb909a70d0aa2880694e81e13478a392e6c45970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Wed, 23 Mar 2016 18:57:12 -0300 Subject: [PATCH 06/27] Update likeno to v1.0.1 This fixes the hash conversion bug so now we expect all tests to pass. Signed off by: Rafael Reggiani Manzo --- kalibro_client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kalibro_client.gemspec b/kalibro_client.gemspec index 2d04804..c5a6ca5 100644 --- a/kalibro_client.gemspec +++ b/kalibro_client.gemspec @@ -47,5 +47,5 @@ Gem::Specification.new do |spec| spec.add_dependency "activesupport", ">= 2.2.1" #version in which underscore was introduced spec.add_dependency "faraday_middleware", "~> 0.9" - spec.add_dependency "likeno", "~> 1.0" + spec.add_dependency "likeno", ">= 1.0.1", "< 2" end From d150012dcbde913de2d826b11e1308d4db9c41d1 Mon Sep 17 00:00:00 2001 From: Rafael Reggiani Manzo Date: Wed, 23 Mar 2016 19:56:20 -0300 Subject: [PATCH 07/27] Bump likeno version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This adds more complete hash conversion which fixes Miscellaneous module classes conversion to hash and enables us to remove one workaround on Base. Signed off by: Diego Araújo --- kalibro_client.gemspec | 2 +- lib/kalibro_client/entities/base.rb | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/kalibro_client.gemspec b/kalibro_client.gemspec index c5a6ca5..c5ca792 100644 --- a/kalibro_client.gemspec +++ b/kalibro_client.gemspec @@ -47,5 +47,5 @@ Gem::Specification.new do |spec| spec.add_dependency "activesupport", ">= 2.2.1" #version in which underscore was introduced spec.add_dependency "faraday_middleware", "~> 0.9" - spec.add_dependency "likeno", ">= 1.0.1", "< 2" + spec.add_dependency "likeno", ">= 1.1", "< 2" end diff --git a/lib/kalibro_client/entities/base.rb b/lib/kalibro_client/entities/base.rb index 34fffa9..12a8fa0 100644 --- a/lib/kalibro_client/entities/base.rb +++ b/lib/kalibro_client/entities/base.rb @@ -30,11 +30,6 @@ def kalibro_errors def self.module_name 'KalibroClient' end - - def convert_to_hash(value) - return value.to_hash if value.is_a?(KalibroClient::Entities::Miscellaneous::Base) - super(value) - end end end end From 367c9f84d0b6210770daed6151e81c4452f7de32 Mon Sep 17 00:00:00 2001 From: Rafael Reggiani Manzo Date: Thu, 24 Mar 2016 08:28:06 -0300 Subject: [PATCH 08/27] Fix cucumber steps relying on old error classes After the introduction of Likeno, `KalibroClient::Errors` has become `Likeno::Errors`. --- features/step_definitions/metric_configuration_steps.rb | 4 ++-- features/step_definitions/module_result_steps.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/features/step_definitions/metric_configuration_steps.rb b/features/step_definitions/metric_configuration_steps.rb index cfc9e33..dfe191e 100644 --- a/features/step_definitions/metric_configuration_steps.rb +++ b/features/step_definitions/metric_configuration_steps.rb @@ -38,7 +38,7 @@ inexistent_id = rand(Time.now.to_i) begin KalibroClient::Entities::Configurations::MetricConfiguration.find(inexistent_id) - rescue KalibroClient::Errors::RecordNotFound + rescue Likeno::Errors::RecordNotFound @is_error = true end end @@ -81,7 +81,7 @@ end Then(/^the metric configuration should no longer exist$/) do - expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id)}.to raise_error(KalibroClient::Errors::RecordNotFound) + expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id)}.to raise_error(Likeno::Errors::RecordNotFound) end Then(/^the metric configuration should exist$/) do diff --git a/features/step_definitions/module_result_steps.rb b/features/step_definitions/module_result_steps.rb index 3c44427..83bf598 100644 --- a/features/step_definitions/module_result_steps.rb +++ b/features/step_definitions/module_result_steps.rb @@ -6,7 +6,7 @@ @is_error = false begin KalibroClient::Entities::Processor::ModuleResult.find(-1) - rescue KalibroClient::Errors::RecordNotFound + rescue Likeno::Errors::RecordNotFound @is_error = true end end From 91777fcba4210b8ea5f7c4435d9a219a98163fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 14:03:45 -0300 Subject: [PATCH 09/27] Use Likeno::Entity#all implementation on KalibroConfigurations --- .../configurations/kalibro_configuration.rb | 4 --- .../kalibro_configuration_spec.rb | 35 ------------------- 2 files changed, 39 deletions(-) diff --git a/lib/kalibro_client/entities/configurations/kalibro_configuration.rb b/lib/kalibro_client/entities/configurations/kalibro_configuration.rb index dac73fd..4e7c42c 100644 --- a/lib/kalibro_client/entities/configurations/kalibro_configuration.rb +++ b/lib/kalibro_client/entities/configurations/kalibro_configuration.rb @@ -24,10 +24,6 @@ def id=(value) @id = value.to_i end - def self.all - create_objects_array_from_hash(request('', {}, :get)) - end - def metric_configurations KalibroClient::Entities::Configurations::MetricConfiguration.create_objects_array_from_hash(self.class.request(':id/metric_configurations', {id: id}, :get)) end diff --git a/spec/entities/configurations/kalibro_configuration_spec.rb b/spec/entities/configurations/kalibro_configuration_spec.rb index 53363dc..86f52aa 100644 --- a/spec/entities/configurations/kalibro_configuration_spec.rb +++ b/spec/entities/configurations/kalibro_configuration_spec.rb @@ -24,41 +24,6 @@ end end - describe 'all' do - context 'with no configurations' do - before :each do - KalibroClient::Entities::Configurations::KalibroConfiguration. - expects(:request). - with('', {}, :get). - returns({'kalibro_configurations' => []}) - end - - it 'is expected to return nil' do - expect(KalibroClient::Entities::Configurations::KalibroConfiguration.all).to be_empty - end - end - - context 'with many configurations' do - let(:kalibro_configuration) { FactoryGirl.build(:kalibro_configuration_with_id) } - let(:another_kalibro_configuration) { FactoryGirl.build(:another_kalibro_configuration) } - - before :each do - KalibroClient::Entities::Configurations::KalibroConfiguration. - expects(:request). - with('', {}, :get). - returns({'kalibro_configurations' => [kalibro_configuration.to_hash, another_kalibro_configuration.to_hash]}) - end - - it 'is expected to return the two elements' do - kalibro_configurations = KalibroClient::Entities::Configurations::KalibroConfiguration.all - - expect(kalibro_configurations.size).to eq(2) - expect(kalibro_configurations.first.name).to eq(kalibro_configuration.name) - expect(kalibro_configurations.last.name).to eq(another_kalibro_configuration.name) - end - end - end - describe 'metric_configurations' do context 'with no metric configurations' do before :each do From eb46db3d50ecfc07f775afc48dd96ee54f11534e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 14:06:18 -0300 Subject: [PATCH 10/27] Use Likeno::Entity#all implementation on ReadingGroups --- .../entities/configurations/reading_group.rb | 6 +--- .../configurations/reading_group_spec.rb | 32 ------------------- 2 files changed, 1 insertion(+), 37 deletions(-) diff --git a/lib/kalibro_client/entities/configurations/reading_group.rb b/lib/kalibro_client/entities/configurations/reading_group.rb index 5e57fb8..f38be7c 100644 --- a/lib/kalibro_client/entities/configurations/reading_group.rb +++ b/lib/kalibro_client/entities/configurations/reading_group.rb @@ -5,7 +5,7 @@ # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. -# +# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -25,10 +25,6 @@ def id=(value) @id = value.to_i end - def self.all - create_objects_array_from_hash(request('', {}, :get)) - end - def readings KalibroClient::Entities::Configurations::Reading.create_objects_array_from_hash(self.class.request(':id/readings', {id: @id}, :get)) end diff --git a/spec/entities/configurations/reading_group_spec.rb b/spec/entities/configurations/reading_group_spec.rb index c85f1df..8380bb9 100644 --- a/spec/entities/configurations/reading_group_spec.rb +++ b/spec/entities/configurations/reading_group_spec.rb @@ -26,38 +26,6 @@ end end - describe 'all' do - context 'with no reading_groups' do - before :each do - KalibroClient::Entities::Configurations::ReadingGroup. - expects(:request). - with('', {}, :get). - returns({'reading_groups' => []}) - end - - it 'should return nil' do - expect(KalibroClient::Entities::Configurations::ReadingGroup.all).to be_empty - end - end - - context 'with many reading_groups' do - let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } - before :each do - KalibroClient::Entities::Configurations::ReadingGroup. - expects(:request). - with('', {}, :get). - returns({'reading_groups' => [reading_group.to_hash, reading_group.to_hash]}) - end - - it 'should return nil' do - reading_groups = KalibroClient::Entities::Configurations::ReadingGroup.all - - expect(reading_groups.first.name).to eq(reading_group.name) - expect(reading_groups.last.name).to eq(reading_group.name) - end - end - end - describe 'readings' do let(:reading_group) { FactoryGirl.build(:reading_group_with_id) } From 42bba5a92d2c06cc9618566032b3eeb4e0cd2022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 14:07:53 -0300 Subject: [PATCH 11/27] Use Likeno::Entity#all implementation on MetricCollectorDetails --- .../processor/metric_collector_details.rb | 4 --- .../metric_collector_details_spec.rb | 35 ------------------- 2 files changed, 39 deletions(-) diff --git a/lib/kalibro_client/entities/processor/metric_collector_details.rb b/lib/kalibro_client/entities/processor/metric_collector_details.rb index 8d8ba35..4686eff 100644 --- a/lib/kalibro_client/entities/processor/metric_collector_details.rb +++ b/lib/kalibro_client/entities/processor/metric_collector_details.rb @@ -67,10 +67,6 @@ def self.find_by_name!(metric_collector_name) def self.all_names request(:names, {}, :get)['metric_collector_names'].to_a end - - def self.all - create_objects_array_from_hash(request('', {}, :get)) - end end end end diff --git a/spec/entities/processor/metric_collector_details_spec.rb b/spec/entities/processor/metric_collector_details_spec.rb index 6e6b842..248f30d 100644 --- a/spec/entities/processor/metric_collector_details_spec.rb +++ b/spec/entities/processor/metric_collector_details_spec.rb @@ -52,41 +52,6 @@ end end - describe 'all' do - context 'with no metric collector details' do - before :each do - KalibroClient::Entities::Processor::MetricCollectorDetails. - expects(:request). - with('', {}, :get). - returns({"metric_collector_details" => []}) - end - - it 'should return empty array' do - expect(KalibroClient::Entities::Processor::MetricCollectorDetails.all).to be_empty - end - end - - context 'with many metric collector details' do - let!(:metric_collector_details_hash) { FactoryGirl.attributes_for(:metric_collector_details) } - let!(:another_metric_collector_details_hash) { FactoryGirl.attributes_for(:another_metric_collector_details) } - - before :each do - KalibroClient::Entities::Processor::MetricCollectorDetails. - expects(:request). - with('', {}, :get). - returns({"metric_collector_details" => [metric_collector_details_hash, another_metric_collector_details_hash]}) - end - - it 'should return the two elements' do - metric_collector_details = KalibroClient::Entities::Processor::MetricCollectorDetails.all - - expect(metric_collector_details.size).to eq(2) - expect(metric_collector_details.first.name).to eq(metric_collector_details_hash[:name]) - expect(metric_collector_details.last.name).to eq(another_metric_collector_details_hash[:name]) - end - end - end - describe 'find_by_name' do subject { FactoryGirl.build(:metric_collector_details) } From 9558cac64b8e0be8ca92d6703ea8dd5002accb6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 14:09:04 -0300 Subject: [PATCH 12/27] Use Likeno::Entity#all implementation on Projects --- .../entities/processor/project.rb | 4 --- spec/entities/processor/project_spec.rb | 34 ------------------- 2 files changed, 38 deletions(-) diff --git a/lib/kalibro_client/entities/processor/project.rb b/lib/kalibro_client/entities/processor/project.rb index 0333667..40fb33b 100644 --- a/lib/kalibro_client/entities/processor/project.rb +++ b/lib/kalibro_client/entities/processor/project.rb @@ -25,10 +25,6 @@ def id=(value) @id = value.to_i end - def self.all - create_objects_array_from_hash(request('', {}, :get)) - end - def repositories Repository.create_objects_array_from_hash(self.class.request(':id/repositories', {id: id}, :get)) end diff --git a/spec/entities/processor/project_spec.rb b/spec/entities/processor/project_spec.rb index 6b8e7af..8f36311 100644 --- a/spec/entities/processor/project_spec.rb +++ b/spec/entities/processor/project_spec.rb @@ -31,40 +31,6 @@ end end - describe 'all' do - context 'with no projects' do - before :each do - KalibroClient::Entities::Processor::Project. - expects(:request). - with('', {}, :get). - returns({"projects" => []}) - end - - it 'should return nil' do - expect(KalibroClient::Entities::Processor::Project.all).to be_empty - end - end - - context 'with many projects' do - let(:project) { FactoryGirl.build(:project) } - let(:another_project) { FactoryGirl.build(:another_project) } - - before :each do - KalibroClient::Entities::Processor::Project. - expects(:request). - with('', {}, :get). - returns({"projects" => [project.to_hash, another_project.to_hash]}) - end - - it 'should return a list with projects' do - projects = KalibroClient::Entities::Processor::Project.all - - expect(projects.first.name).to eq(project.name) - expect(projects.last.name).to eq(another_project.name) - end - end - end - describe 'repositories' do let(:project) { FactoryGirl.build(:project) } let(:repository) { FactoryGirl.build(:repository) } From 877a5bfcf0c206d4aa84c1d128aec6f70c09a157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 14:15:02 -0300 Subject: [PATCH 13/27] Use Likeno::Entity#all implementation on Repositories --- lib/kalibro_client/entities/processor/repository.rb | 4 ---- spec/entities/processor/repository_spec.rb | 13 ------------- 2 files changed, 17 deletions(-) diff --git a/lib/kalibro_client/entities/processor/repository.rb b/lib/kalibro_client/entities/processor/repository.rb index a728397..ef1bd55 100644 --- a/lib/kalibro_client/entities/processor/repository.rb +++ b/lib/kalibro_client/entities/processor/repository.rb @@ -112,10 +112,6 @@ def last_processing_before(date) Processing.new(self.class.request("#{self.id}/last_processing/before", {:date => date})['processing']) end - def self.all - create_objects_array_from_hash(request("", {}, :get)) - end - def self.branches(url, scm_type) request("/branches", {url: url, scm_type: scm_type}) end diff --git a/spec/entities/processor/repository_spec.rb b/spec/entities/processor/repository_spec.rb index a44459b..9f629c1 100644 --- a/spec/entities/processor/repository_spec.rb +++ b/spec/entities/processor/repository_spec.rb @@ -99,19 +99,6 @@ end end - describe 'all' do - before :each do - KalibroClient::Entities::Processor::Repository. - expects(:request). - with("", {}, :get). - returns({'repositories' => [subject.to_hash]}) - end - - it 'should list all the repositories' do - expect(KalibroClient::Entities::Processor::Repository.all).to include(subject) - end - end - # The only purpose of this test is to cover the overrided save_params method describe 'save' do subject {FactoryGirl.build(:repository)} From 69d4ee0a2fe17cf36b239299efc260bc6fceeeaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 14:54:25 -0300 Subject: [PATCH 14/27] Rescue Likeno errors on MetricCollectorDetails --- features/step_definitions/metric_collector_details_steps.rb | 2 +- .../entities/processor/metric_collector_details.rb | 2 +- spec/entities/processor/metric_collector_details_spec.rb | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/features/step_definitions/metric_collector_details_steps.rb b/features/step_definitions/metric_collector_details_steps.rb index 1c5f52b..7dfd028 100644 --- a/features/step_definitions/metric_collector_details_steps.rb +++ b/features/step_definitions/metric_collector_details_steps.rb @@ -10,7 +10,7 @@ @is_error = false begin KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name!("Avalio") - rescue KalibroClient::Errors::RecordNotFound + rescue Likeno::Errors::RecordNotFound @is_error = true end end diff --git a/lib/kalibro_client/entities/processor/metric_collector_details.rb b/lib/kalibro_client/entities/processor/metric_collector_details.rb index 4686eff..db26801 100644 --- a/lib/kalibro_client/entities/processor/metric_collector_details.rb +++ b/lib/kalibro_client/entities/processor/metric_collector_details.rb @@ -55,7 +55,7 @@ def find_metric_by_code!(metric_code) def self.find_by_name(metric_collector_name) begin self.find_by_name!(metric_collector_name) - rescue KalibroClient::Errors::RecordNotFound + rescue Likeno::Errors::RecordNotFound nil end end diff --git a/spec/entities/processor/metric_collector_details_spec.rb b/spec/entities/processor/metric_collector_details_spec.rb index 248f30d..9cdbf33 100644 --- a/spec/entities/processor/metric_collector_details_spec.rb +++ b/spec/entities/processor/metric_collector_details_spec.rb @@ -60,7 +60,7 @@ KalibroClient::Entities::Processor::MetricCollectorDetails. expects(:find_by_name!). with(subject.name). - raises(KalibroClient::Errors::RecordNotFound) + raises(Likeno::Errors::RecordNotFound) end it 'is expected to return nil' do @@ -90,12 +90,12 @@ KalibroClient::Entities::Processor::MetricCollectorDetails. expects(:request). with(:find, {name: subject.name}). - raises(KalibroClient::Errors::RecordNotFound) + raises(Likeno::Errors::RecordNotFound) end it 'should raise a RecordNotFound error' do expect { KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name!(subject.name)}. - to raise_error(KalibroClient::Errors::RecordNotFound) + to raise_error(Likeno::Errors::RecordNotFound) end end From 584fc8bf74206a04a50458b0b6b590c3e6e37318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:08:59 -0300 Subject: [PATCH 15/27] Use the default Likeno standard error class --- lib/kalibro_client/errors.rb | 1 - lib/kalibro_client/errors/record_invalid.rb | 2 +- lib/kalibro_client/errors/request_error.rb | 4 ++-- lib/kalibro_client/errors/standard.rb | 24 --------------------- 4 files changed, 3 insertions(+), 28 deletions(-) delete mode 100644 lib/kalibro_client/errors/standard.rb diff --git a/lib/kalibro_client/errors.rb b/lib/kalibro_client/errors.rb index 25150f3..c74f699 100644 --- a/lib/kalibro_client/errors.rb +++ b/lib/kalibro_client/errors.rb @@ -14,7 +14,6 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -require 'kalibro_client/errors/standard' require 'kalibro_client/errors/request_error' require 'kalibro_client/errors/record_not_found' require 'kalibro_client/errors/record_invalid' diff --git a/lib/kalibro_client/errors/record_invalid.rb b/lib/kalibro_client/errors/record_invalid.rb index fdf2dea..94d1988 100644 --- a/lib/kalibro_client/errors/record_invalid.rb +++ b/lib/kalibro_client/errors/record_invalid.rb @@ -1,6 +1,6 @@ module KalibroClient module Errors - class RecordInvalid < Standard + class RecordInvalid < Likeno::Errors::Standard attr_reader :record def initialize(record = nil) diff --git a/lib/kalibro_client/errors/request_error.rb b/lib/kalibro_client/errors/request_error.rb index 138ef70..a6a99c9 100644 --- a/lib/kalibro_client/errors/request_error.rb +++ b/lib/kalibro_client/errors/request_error.rb @@ -5,7 +5,7 @@ # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. -# +# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the @@ -16,7 +16,7 @@ module KalibroClient module Errors - class RequestError < Standard + class RequestError < Likeno::Errors::Standard attr_reader :response def initialize(attributes={}) diff --git a/lib/kalibro_client/errors/standard.rb b/lib/kalibro_client/errors/standard.rb deleted file mode 100644 index f84b1c0..0000000 --- a/lib/kalibro_client/errors/standard.rb +++ /dev/null @@ -1,24 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -#Inspired on: -#https://github.com/rails/rails/blob/master/activerecord/lib/active_record/errors.rb -module KalibroClient - module Errors - class Standard < StandardError - end - end -end From 682d480cb3259cef20a8613eeaddc90c701caebb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:15:18 -0300 Subject: [PATCH 16/27] Use Likeno's implementation of RequestErrors --- lib/kalibro_client/entities/processor/module_result.rb | 2 +- lib/kalibro_client/errors/request_error.rb | 8 +------- spec/entities/processor/module_result_spec.rb | 6 +++--- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/kalibro_client/entities/processor/module_result.rb b/lib/kalibro_client/entities/processor/module_result.rb index ff64138..194c232 100644 --- a/lib/kalibro_client/entities/processor/module_result.rb +++ b/lib/kalibro_client/entities/processor/module_result.rb @@ -94,7 +94,7 @@ def hotspot_metric_results def self.find(id) begin super - rescue KalibroClient::Errors::RequestError => e + rescue Likeno::Errors::RequestError => e if(e.response.status == 422) raise KalibroClient::Errors::RecordNotFound.new(response: e.response) else diff --git a/lib/kalibro_client/errors/request_error.rb b/lib/kalibro_client/errors/request_error.rb index a6a99c9..e55ffad 100644 --- a/lib/kalibro_client/errors/request_error.rb +++ b/lib/kalibro_client/errors/request_error.rb @@ -16,12 +16,6 @@ module KalibroClient module Errors - class RequestError < Likeno::Errors::Standard - attr_reader :response - - def initialize(attributes={}) - @response = attributes[:response] - end - end + class RequestError < Likeno::Errors::Standard; end end end diff --git a/spec/entities/processor/module_result_spec.rb b/spec/entities/processor/module_result_spec.rb index cf566ff..b8219a5 100644 --- a/spec/entities/processor/module_result_spec.rb +++ b/spec/entities/processor/module_result_spec.rb @@ -243,7 +243,7 @@ response = mock('response') response.expects(:status).at_least_once.returns(422) - KalibroClient::Entities::Base.expects(:find).with(id).raises(KalibroClient::Errors::RequestError.new(response: response)) + KalibroClient::Entities::Base.expects(:find).with(id).raises(Likeno::Errors::RequestError.new(response: response)) end it 'is expected to raise a RecordNotFound error' do @@ -256,11 +256,11 @@ response = mock('response') response.expects(:status).at_least_once.returns(500) - KalibroClient::Entities::Base.expects(:find).with(id).raises(KalibroClient::Errors::RequestError.new(response: response)) + KalibroClient::Entities::Base.expects(:find).with(id).raises(Likeno::Errors::RequestError.new(response: response)) end it 'is expected to raise a RequestError' do - expect { described_class.find(id) }.to raise_error(KalibroClient::Errors::RequestError) + expect { described_class.find(id) }.to raise_error(Likeno::Errors::RequestError) end end end From f10e0083386cc89d6671668108a459dfada10757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:17:40 -0300 Subject: [PATCH 17/27] Use Likeno's implementation of RecordInvalid errors --- lib/kalibro_client/errors/record_invalid.rb | 16 +------- spec/errors/record_invalid_spec.rb | 45 --------------------- 2 files changed, 1 insertion(+), 60 deletions(-) delete mode 100644 spec/errors/record_invalid_spec.rb diff --git a/lib/kalibro_client/errors/record_invalid.rb b/lib/kalibro_client/errors/record_invalid.rb index 94d1988..0c223b1 100644 --- a/lib/kalibro_client/errors/record_invalid.rb +++ b/lib/kalibro_client/errors/record_invalid.rb @@ -1,19 +1,5 @@ module KalibroClient module Errors - class RecordInvalid < Likeno::Errors::Standard - attr_reader :record - - def initialize(record = nil) - if record - @record = record - errors = @record.kalibro_errors.join(', ') - message = "Record invalid: #{errors}" - else - message = 'Record invalid' - end - - super(message) - end - end + class RecordInvalid < Likeno::Errors::Standard; end end end diff --git a/spec/errors/record_invalid_spec.rb b/spec/errors/record_invalid_spec.rb deleted file mode 100644 index ffcef3a..0000000 --- a/spec/errors/record_invalid_spec.rb +++ /dev/null @@ -1,45 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -require 'spec_helper' - -describe KalibroClient::Errors::RecordInvalid do - describe 'initialize' do - let(:default_message){ "Record invalid" } - - context 'without a record' do - subject { KalibroClient::Errors::RecordInvalid.new } - - it 'is expected to have the default message' do - expect(subject.message).to eq(default_message) - end - end - - context 'with a given record' do - let!(:record) { mock('record') } - let!(:kalibro_errors) { ['Kalibro', 'Error'] } - subject { KalibroClient::Errors::RecordInvalid.new(record) } - - before :each do - record.expects(:kalibro_errors).returns(kalibro_errors) - end - - it 'is expected to return the default message concatenated with the errors' do - expect(subject.message).to eq("#{default_message}: #{kalibro_errors.join(', ')}") - end - end - end -end \ No newline at end of file From 215c82309fe764f3ba946a25e6848249420374f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:19:54 -0300 Subject: [PATCH 18/27] Rescue Likeno RecordNotFound errors on MetricConfiguration --- .../entities/configurations/metric_configuration.rb | 2 +- spec/entities/configurations/metric_configuration_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kalibro_client/entities/configurations/metric_configuration.rb b/lib/kalibro_client/entities/configurations/metric_configuration.rb index 9ad7c44..50f3db0 100644 --- a/lib/kalibro_client/entities/configurations/metric_configuration.rb +++ b/lib/kalibro_client/entities/configurations/metric_configuration.rb @@ -81,7 +81,7 @@ def self.find(id) def self.exists?(id) begin return true unless find(id).nil? - rescue KalibroClient::Errors::RecordNotFound + rescue Likeno::Errors::RecordNotFound return false end end diff --git a/spec/entities/configurations/metric_configuration_spec.rb b/spec/entities/configurations/metric_configuration_spec.rb index 831399e..a5125ee 100644 --- a/spec/entities/configurations/metric_configuration_spec.rb +++ b/spec/entities/configurations/metric_configuration_spec.rb @@ -184,7 +184,7 @@ context 'when the metric configuration does not exist' do before :each do - KalibroClient::Entities::Configurations::MetricConfiguration.expects(:find).with(subject.id).raises(KalibroClient::Errors::RecordNotFound) + KalibroClient::Entities::Configurations::MetricConfiguration.expects(:find).with(subject.id).raises(Likeno::Errors::RecordNotFound) end it 'should return false' do From 76aaa5622ce62d69cc2eb57c5208da1cd04d07df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:21:20 -0300 Subject: [PATCH 19/27] Rescue Likeno request errors on MetricConfiguration --- .../entities/configurations/metric_configuration.rb | 2 +- spec/entities/configurations/metric_configuration_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/kalibro_client/entities/configurations/metric_configuration.rb b/lib/kalibro_client/entities/configurations/metric_configuration.rb index 50f3db0..4390703 100644 --- a/lib/kalibro_client/entities/configurations/metric_configuration.rb +++ b/lib/kalibro_client/entities/configurations/metric_configuration.rb @@ -73,7 +73,7 @@ def self.find(id) metric_configuration = request(':id', {id: id}, :get) return new(metric_configuration['metric_configuration'], true) #FIXME Temporary until KalibroProcessor returns proper http statuses - rescue KalibroClient::Errors::RequestError + rescue Likeno::Errors::RequestError raise KalibroClient::Errors::RecordNotFound end end diff --git a/spec/entities/configurations/metric_configuration_spec.rb b/spec/entities/configurations/metric_configuration_spec.rb index a5125ee..b1e24d0 100644 --- a/spec/entities/configurations/metric_configuration_spec.rb +++ b/spec/entities/configurations/metric_configuration_spec.rb @@ -216,7 +216,7 @@ KalibroClient::Entities::Configurations::MetricConfiguration. expects(:request). with(':id', {id: metric_configuration.id}, :get). - raises(KalibroClient::Errors::RequestError) + raises(Likeno::Errors::RequestError) end it 'should raise the RecordNotFound error' do From 69e5153d373525e260f4daa968a716275d7d8795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:40:28 -0300 Subject: [PATCH 20/27] Add deprecation warnings on error classes --- lib/kalibro_client/errors/record_invalid.rb | 7 ++++++- lib/kalibro_client/errors/record_not_found.rb | 4 ++++ lib/kalibro_client/errors/request_error.rb | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/kalibro_client/errors/record_invalid.rb b/lib/kalibro_client/errors/record_invalid.rb index 0c223b1..f9f4a7e 100644 --- a/lib/kalibro_client/errors/record_invalid.rb +++ b/lib/kalibro_client/errors/record_invalid.rb @@ -1,5 +1,10 @@ module KalibroClient module Errors - class RecordInvalid < Likeno::Errors::Standard; end + class RecordInvalid < Likeno::Errors::Standard + def initialize(record = nil) + STDERR.puts 'DEPRECATED: KalibroClient::Errors::RecordInvalid will be replaced by Likeno::Errors::RecordInvalid.' + super(record) + end + end end end diff --git a/lib/kalibro_client/errors/record_not_found.rb b/lib/kalibro_client/errors/record_not_found.rb index c78945f..d1b0097 100644 --- a/lib/kalibro_client/errors/record_not_found.rb +++ b/lib/kalibro_client/errors/record_not_found.rb @@ -17,6 +17,10 @@ module KalibroClient module Errors class RecordNotFound < RequestError + def initialize(attributes = {}) + STDERR.puts 'DEPRECATED: KalibroClient::Errors::RecordNotFound will be replaced by Likeno::Errors::RecordNotFound.' + super(attributes) + end end end end diff --git a/lib/kalibro_client/errors/request_error.rb b/lib/kalibro_client/errors/request_error.rb index e55ffad..64a9555 100644 --- a/lib/kalibro_client/errors/request_error.rb +++ b/lib/kalibro_client/errors/request_error.rb @@ -16,6 +16,11 @@ module KalibroClient module Errors - class RequestError < Likeno::Errors::Standard; end + class RequestError < Likeno::Errors::Standard + def initialize(attributes = {}) + STDERR.puts "DEPRECATED: KalibroClient::Errors::RequestError will be replaced by Likeno::Errors::RequestError." + super(attributes) + end + end end end From feefd017e3e08ed0867164a86e59d5a24a1b779e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:41:50 -0300 Subject: [PATCH 21/27] Remove unnecessary mocks on Cleaner spec --- spec/kalibro_cucumber_helpers/cleaner_spec.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/spec/kalibro_cucumber_helpers/cleaner_spec.rb b/spec/kalibro_cucumber_helpers/cleaner_spec.rb index 8e44ac1..a1fd9f7 100644 --- a/spec/kalibro_cucumber_helpers/cleaner_spec.rb +++ b/spec/kalibro_cucumber_helpers/cleaner_spec.rb @@ -23,11 +23,6 @@ end describe 'clean_database' do - let(:client) { mock("client") } - let(:request) { mock("request") } - let(:response) { mock("response", success?: true, body: '') } - let(:options) { stub_everything } - it 'is expected to make a request to the correct address' do subject.expects(:request).with('clean_database', {}, :post) From 7e7cebaa43233b8a9b9006994985b635a2d427d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 15:43:02 -0300 Subject: [PATCH 22/27] Remove unused XML helpers This was legacy code from distant times where Kalibro used SOAP for communication. --- lib/kalibro_client/helpers/xml_converters.rb | 20 ------ spec/helpers/xml_converters_spec.rb | 64 -------------------- 2 files changed, 84 deletions(-) delete mode 100644 lib/kalibro_client/helpers/xml_converters.rb delete mode 100644 spec/helpers/xml_converters_spec.rb diff --git a/lib/kalibro_client/helpers/xml_converters.rb b/lib/kalibro_client/helpers/xml_converters.rb deleted file mode 100644 index 4a36d89..0000000 --- a/lib/kalibro_client/helpers/xml_converters.rb +++ /dev/null @@ -1,20 +0,0 @@ -module XMLConverters - def xml_instance_class_name(object) - xml_name = object.class.name - xml_name["KalibroClient::Entities::"] = "" if xml_name.start_with?("KalibroClient::Entities::") - xml_name[0..0] = xml_name[0..0].downcase - xml_name + "Xml" - end - - def get_xml(field, field_value) - hash = Hash.new - if field_value.is_a?(KalibroClient::Entities::Base) - hash = {:attributes! => {}} - hash[:attributes!][field.to_sym] = { - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', - 'xsi:type' => 'kalibro:' + xml_instance_class_name(field_value) - } - end - hash - end -end diff --git a/spec/helpers/xml_converters_spec.rb b/spec/helpers/xml_converters_spec.rb deleted file mode 100644 index 64b7d0a..0000000 --- a/spec/helpers/xml_converters_spec.rb +++ /dev/null @@ -1,64 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -require 'spec_helper' -require 'kalibro_client/helpers/xml_converters' - -include XMLConverters - -describe XMLConverters do - describe 'xml_instance_class_name' do - before { @model = KalibroClient::Entities::Base.new } - - it 'should return modelXml' do - expect(xml_instance_class_name(@model)).to eq('baseXml') - end - end - - describe 'get_xml' do - context 'with an object that is not an instance of Base' do - before { @object = "kalibro" } - - it 'should return a Hash' do - expect(get_xml("field", @object)).to be_a(Hash) - end - - it 'should return an empty Hash' do - expect(get_xml("field", @object)).to eq({}) - end - end - - context 'with an instance of Base' do - before { @object = KalibroClient::Entities::Base.new } - - it 'should return a Hash' do - expect(get_xml("field", @object)).to be_a(Hash) - end - - it 'should return the XML Hash' do - field_xml_hash = {:attributes! => - {:field => - {"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance", - "xsi:type"=>"kalibro:baseXml" - } - } - } - - expect(get_xml("field", @object)).to eq(field_xml_hash) - end - end - end -end From b4ef614e8e576213b99e729141cd2dd64b880a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Mon, 28 Mar 2016 16:12:21 -0300 Subject: [PATCH 23/27] Fix MetricConfiguration steps to correct handle errors --- features/step_definitions/metric_configuration_steps.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/step_definitions/metric_configuration_steps.rb b/features/step_definitions/metric_configuration_steps.rb index dfe191e..cfc9e33 100644 --- a/features/step_definitions/metric_configuration_steps.rb +++ b/features/step_definitions/metric_configuration_steps.rb @@ -38,7 +38,7 @@ inexistent_id = rand(Time.now.to_i) begin KalibroClient::Entities::Configurations::MetricConfiguration.find(inexistent_id) - rescue Likeno::Errors::RecordNotFound + rescue KalibroClient::Errors::RecordNotFound @is_error = true end end @@ -81,7 +81,7 @@ end Then(/^the metric configuration should no longer exist$/) do - expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id)}.to raise_error(Likeno::Errors::RecordNotFound) + expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id)}.to raise_error(KalibroClient::Errors::RecordNotFound) end Then(/^the metric configuration should exist$/) do From 49cccf1816351adb1d9095513faf13d8ed200cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Wed, 30 Mar 2016 14:51:28 -0300 Subject: [PATCH 24/27] Use Likeno errors instead of KalibroClient errors Since the same errors are raised, we do not need to duplicate the code. Notice that this breaks the KalibroClient API. Signed-off-by: Daniel Miranda --- .../metric_configuration_steps.rb | 4 +- lib/kalibro_client/entities/base.rb | 1 + .../configurations/metric_configuration.rb | 18 ------ .../processor/metric_collector_details.rb | 4 +- .../entities/processor/module_result.rb | 13 ----- lib/kalibro_client/errors.rb | 4 +- lib/kalibro_client/errors/record_invalid.rb | 10 ---- lib/kalibro_client/errors/record_not_found.rb | 26 --------- lib/kalibro_client/errors/request_error.rb | 26 --------- .../metric_configuration_spec.rb | 57 ------------------- .../metric_collector_details_spec.rb | 4 +- spec/entities/processor/module_result_spec.rb | 41 ------------- 12 files changed, 8 insertions(+), 200 deletions(-) delete mode 100644 lib/kalibro_client/errors/record_invalid.rb delete mode 100644 lib/kalibro_client/errors/record_not_found.rb delete mode 100644 lib/kalibro_client/errors/request_error.rb diff --git a/features/step_definitions/metric_configuration_steps.rb b/features/step_definitions/metric_configuration_steps.rb index cfc9e33..dfe191e 100644 --- a/features/step_definitions/metric_configuration_steps.rb +++ b/features/step_definitions/metric_configuration_steps.rb @@ -38,7 +38,7 @@ inexistent_id = rand(Time.now.to_i) begin KalibroClient::Entities::Configurations::MetricConfiguration.find(inexistent_id) - rescue KalibroClient::Errors::RecordNotFound + rescue Likeno::Errors::RecordNotFound @is_error = true end end @@ -81,7 +81,7 @@ end Then(/^the metric configuration should no longer exist$/) do - expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id)}.to raise_error(KalibroClient::Errors::RecordNotFound) + expect { KalibroClient::Entities::Configurations::MetricConfiguration.find(@metric_configuration.id)}.to raise_error(Likeno::Errors::RecordNotFound) end Then(/^the metric configuration should exist$/) do diff --git a/lib/kalibro_client/entities/base.rb b/lib/kalibro_client/entities/base.rb index 12a8fa0..973e1ed 100644 --- a/lib/kalibro_client/entities/base.rb +++ b/lib/kalibro_client/entities/base.rb @@ -15,6 +15,7 @@ # along with this program. If not, see . require 'likeno' +require 'kalibro_client/errors' module KalibroClient module Entities diff --git a/lib/kalibro_client/entities/configurations/metric_configuration.rb b/lib/kalibro_client/entities/configurations/metric_configuration.rb index 4390703..686af4a 100644 --- a/lib/kalibro_client/entities/configurations/metric_configuration.rb +++ b/lib/kalibro_client/entities/configurations/metric_configuration.rb @@ -68,24 +68,6 @@ def self.metric_configurations_of(configuration_id) create_objects_array_from_hash(request('', {}, :get, "kalibro_configurations/#{configuration_id}")) end - def self.find(id) - begin - metric_configuration = request(':id', {id: id}, :get) - return new(metric_configuration['metric_configuration'], true) - #FIXME Temporary until KalibroProcessor returns proper http statuses - rescue Likeno::Errors::RequestError - raise KalibroClient::Errors::RecordNotFound - end - end - - def self.exists?(id) - begin - return true unless find(id).nil? - rescue Likeno::Errors::RecordNotFound - return false - end - end - def kalibro_ranges KalibroClient::Entities::Configurations::KalibroRange.create_objects_array_from_hash(self.class.request(':id/kalibro_ranges', {id: id}, :get)) end diff --git a/lib/kalibro_client/entities/processor/metric_collector_details.rb b/lib/kalibro_client/entities/processor/metric_collector_details.rb index db26801..e544ba0 100644 --- a/lib/kalibro_client/entities/processor/metric_collector_details.rb +++ b/lib/kalibro_client/entities/processor/metric_collector_details.rb @@ -38,7 +38,7 @@ def find_metric_by_name(name) def find_metric_by_name!(name) metric = self.find_metric_by_name(name) - raise KalibroClient::Errors::RecordNotFound if metric.nil? + raise Likeno::Errors::RecordNotFound if metric.nil? metric end @@ -48,7 +48,7 @@ def find_metric_by_code(metric_code) def find_metric_by_code!(metric_code) metric = self.find_metric_by_code(metric_code) - raise KalibroClient::Errors::RecordNotFound if metric.nil? + raise Likeno::Errors::RecordNotFound if metric.nil? metric end diff --git a/lib/kalibro_client/entities/processor/module_result.rb b/lib/kalibro_client/entities/processor/module_result.rb index 194c232..7fb47e0 100644 --- a/lib/kalibro_client/entities/processor/module_result.rb +++ b/lib/kalibro_client/entities/processor/module_result.rb @@ -89,19 +89,6 @@ def hotspot_metric_results HotspotMetricResult.create_objects_array_from_hash(self.class.request(":id/hotspot_metric_results", {id: self.id}, :get)) end - - # FIXME: KalibroProcessor should return a 404 if the object does not exist instead of 422 - def self.find(id) - begin - super - rescue Likeno::Errors::RequestError => e - if(e.response.status == 422) - raise KalibroClient::Errors::RecordNotFound.new(response: e.response) - else - raise e - end - end - end end end end diff --git a/lib/kalibro_client/errors.rb b/lib/kalibro_client/errors.rb index c74f699..a91a813 100644 --- a/lib/kalibro_client/errors.rb +++ b/lib/kalibro_client/errors.rb @@ -14,6 +14,4 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -require 'kalibro_client/errors/request_error' -require 'kalibro_client/errors/record_not_found' -require 'kalibro_client/errors/record_invalid' +require 'likeno/errors' diff --git a/lib/kalibro_client/errors/record_invalid.rb b/lib/kalibro_client/errors/record_invalid.rb deleted file mode 100644 index f9f4a7e..0000000 --- a/lib/kalibro_client/errors/record_invalid.rb +++ /dev/null @@ -1,10 +0,0 @@ -module KalibroClient - module Errors - class RecordInvalid < Likeno::Errors::Standard - def initialize(record = nil) - STDERR.puts 'DEPRECATED: KalibroClient::Errors::RecordInvalid will be replaced by Likeno::Errors::RecordInvalid.' - super(record) - end - end - end -end diff --git a/lib/kalibro_client/errors/record_not_found.rb b/lib/kalibro_client/errors/record_not_found.rb deleted file mode 100644 index d1b0097..0000000 --- a/lib/kalibro_client/errors/record_not_found.rb +++ /dev/null @@ -1,26 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -module KalibroClient - module Errors - class RecordNotFound < RequestError - def initialize(attributes = {}) - STDERR.puts 'DEPRECATED: KalibroClient::Errors::RecordNotFound will be replaced by Likeno::Errors::RecordNotFound.' - super(attributes) - end - end - end -end diff --git a/lib/kalibro_client/errors/request_error.rb b/lib/kalibro_client/errors/request_error.rb deleted file mode 100644 index 64a9555..0000000 --- a/lib/kalibro_client/errors/request_error.rb +++ /dev/null @@ -1,26 +0,0 @@ -# This file is part of KalibroClient -# Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. - -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -module KalibroClient - module Errors - class RequestError < Likeno::Errors::Standard - def initialize(attributes = {}) - STDERR.puts "DEPRECATED: KalibroClient::Errors::RequestError will be replaced by Likeno::Errors::RequestError." - super(attributes) - end - end - end -end diff --git a/spec/entities/configurations/metric_configuration_spec.rb b/spec/entities/configurations/metric_configuration_spec.rb index b1e24d0..406febe 100644 --- a/spec/entities/configurations/metric_configuration_spec.rb +++ b/spec/entities/configurations/metric_configuration_spec.rb @@ -169,63 +169,6 @@ end end - describe 'exists?' do - subject {FactoryGirl.build(:metric_configuration_with_id)} - - context 'when the metric configuration exists' do - before :each do - KalibroClient::Entities::Configurations::MetricConfiguration.expects(:find).with(subject.id).returns(subject) - end - - it 'should return true' do - expect(KalibroClient::Entities::Configurations::MetricConfiguration.exists?(subject.id)).to be_truthy - end - end - - context 'when the metric configuration does not exist' do - before :each do - KalibroClient::Entities::Configurations::MetricConfiguration.expects(:find).with(subject.id).raises(Likeno::Errors::RecordNotFound) - end - - it 'should return false' do - expect(KalibroClient::Entities::Configurations::MetricConfiguration.exists?(subject.id)).to be_falsey - end - end - end - - describe 'find' do - let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } - - context 'with an existant MetricConfiguration' do - before :each do - KalibroClient::Entities::Configurations::MetricConfiguration. - expects(:request). - with(':id', {id: metric_configuration.id}, :get). - returns({'metric_configuration' => metric_configuration.to_hash}) - end - - it 'should return the metric_configuration' do - found_metric_configuration = KalibroClient::Entities::Configurations::MetricConfiguration.find(metric_configuration.id) - expect(found_metric_configuration.id).to eq(metric_configuration.id) - expect(found_metric_configuration.persisted).to be_truthy - end - end - - context 'with an inexistant MetricConfiguration' do - before :each do - KalibroClient::Entities::Configurations::MetricConfiguration. - expects(:request). - with(':id', {id: metric_configuration.id}, :get). - raises(Likeno::Errors::RequestError) - end - - it 'should raise the RecordNotFound error' do - expect {KalibroClient::Entities::Configurations::MetricConfiguration.find(metric_configuration.id)}. - to raise_error(KalibroClient::Errors::RecordNotFound) - end - end - end - describe 'kalibro ranges' do let(:metric_configuration) { FactoryGirl.build(:metric_configuration_with_id) } let(:kalibro_range_1) { FactoryGirl.build(:range, metric_configuration_id: metric_configuration.id) } diff --git a/spec/entities/processor/metric_collector_details_spec.rb b/spec/entities/processor/metric_collector_details_spec.rb index 9cdbf33..aff5708 100644 --- a/spec/entities/processor/metric_collector_details_spec.rb +++ b/spec/entities/processor/metric_collector_details_spec.rb @@ -146,7 +146,7 @@ let(:metric) { subject.supported_metrics["loc"] } it 'should return nil with an inexistent name' do - expect { subject.find_metric_by_name!("fake name") }.to raise_error(KalibroClient::Errors::RecordNotFound) + expect { subject.find_metric_by_name!("fake name") }.to raise_error(Likeno::Errors::RecordNotFound) end it 'should return a metric with an existent name' do @@ -172,7 +172,7 @@ let(:metric) { subject.supported_metrics["loc"] } it 'should return nil with an inexistent code' do - expect{subject.find_metric_by_code!("fake code")}.to raise_error(KalibroClient::Errors::RecordNotFound) + expect{subject.find_metric_by_code!("fake code")}.to raise_error(Likeno::Errors::RecordNotFound) end it 'should return a metric with an existent code' do diff --git a/spec/entities/processor/module_result_spec.rb b/spec/entities/processor/module_result_spec.rb index b8219a5..5d3aca8 100644 --- a/spec/entities/processor/module_result_spec.rb +++ b/spec/entities/processor/module_result_spec.rb @@ -223,45 +223,4 @@ expect(subject.processing).to eq(processing) end end - - describe 'find' do - let(:id) { 1 } - - context 'when the ModuleResult exists' do - let!(:module_result) { FactoryGirl.build(:module_result) } - before :each do - KalibroClient::Entities::Base.expects(:find).with(id).returns(module_result) - end - - it 'is expected to return the found module result' do - expect(described_class.find(id)).to eq(module_result) - end - end - - context 'when the ModuleResult does not exist' do - before :each do - response = mock('response') - response.expects(:status).at_least_once.returns(422) - - KalibroClient::Entities::Base.expects(:find).with(id).raises(Likeno::Errors::RequestError.new(response: response)) - end - - it 'is expected to raise a RecordNotFound error' do - expect { described_class.find(id) }.to raise_error(KalibroClient::Errors::RecordNotFound) - end - end - - context 'when there is an unexpected server error' do - before :each do - response = mock('response') - response.expects(:status).at_least_once.returns(500) - - KalibroClient::Entities::Base.expects(:find).with(id).raises(Likeno::Errors::RequestError.new(response: response)) - end - - it 'is expected to raise a RequestError' do - expect { described_class.find(id) }.to raise_error(Likeno::Errors::RequestError) - end - end - end end From f1ed1337c469eda6c792cf7d9eb6d92c3d97a64b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Wed, 30 Mar 2016 15:09:37 -0300 Subject: [PATCH 25/27] Use a more concise syntax to define kalibro_client's version Signed-off-by: Daniel Miranda --- kalibro_client.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kalibro_client.gemspec b/kalibro_client.gemspec index c5ca792..3f0d29d 100644 --- a/kalibro_client.gemspec +++ b/kalibro_client.gemspec @@ -47,5 +47,5 @@ Gem::Specification.new do |spec| spec.add_dependency "activesupport", ">= 2.2.1" #version in which underscore was introduced spec.add_dependency "faraday_middleware", "~> 0.9" - spec.add_dependency "likeno", ">= 1.1", "< 2" + spec.add_dependency "likeno", "~> 1.1" end From fc95371a5d972b9929156b46d70608f56a1f4e07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Ara=C3=BAjo?= Date: Wed, 30 Mar 2016 15:32:37 -0300 Subject: [PATCH 26/27] Release version v4.0.0.alpha1 --- lib/kalibro_client/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kalibro_client/version.rb b/lib/kalibro_client/version.rb index ab80568..4d0d995 100644 --- a/lib/kalibro_client/version.rb +++ b/lib/kalibro_client/version.rb @@ -15,5 +15,5 @@ # along with this program. If not, see . module KalibroClient - VERSION = "3.0.1" + VERSION = "4.0.0.alpha1" end From e0aacac680cacd31f1017df025e4ffacc6947723 Mon Sep 17 00:00:00 2001 From: Daniel Miranda Date: Wed, 30 Mar 2016 16:10:58 -0300 Subject: [PATCH 27/27] Fix excessive spaces in comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Diego Araújo --- lib/kalibro_client/entities/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/kalibro_client/entities/base.rb b/lib/kalibro_client/entities/base.rb index 973e1ed..3923ecb 100644 --- a/lib/kalibro_client/entities/base.rb +++ b/lib/kalibro_client/entities/base.rb @@ -1,4 +1,4 @@ - # This file is part of KalibroClient +# This file is part of KalibroClient # Copyright (C) 2013 it's respectives authors (please see the AUTHORS file) # # This program is free software: you can redistribute it and/or modify