From 352364638d05be77b44fc654fb8cf1328fc648b5 Mon Sep 17 00:00:00 2001 From: Paulo Meirelles Date: Fri, 20 Jul 2012 21:28:09 +0400 Subject: [PATCH] [Mezuro] refactoring kalibro entity to new design --- plugins/mezuro/lib/kalibro/client/port.rb | 38 -------------------------------------- plugins/mezuro/lib/kalibro/entities/entity.rb | 84 ------------------------------------------------------------------------------------ plugins/mezuro/lib/kalibro/kalibro.rb | 22 ---------------------- plugins/mezuro/lib/kalibro/metric_configuration.rb | 13 +++++++++---- plugins/mezuro/lib/kalibro/metric_result.rb | 12 +++++++----- plugins/mezuro/lib/kalibro/project.rb | 16 ++++++++++++++++ plugins/mezuro/lib/kalibro/repository.rb | 4 ++++ plugins/mezuro/lib/mezuro_plugin/project_content.rb | 3 +-- plugins/mezuro/test/unit/kalibro/kalibro_test.rb | 34 ---------------------------------- plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb | 52 ++++++++++++++++++++++++++-------------------------- plugins/mezuro/test/unit/kalibro/project_test.rb | 20 ++++++++++++++++++++ plugins/mezuro/test/unit/kalibro/repository_test.rb | 6 ++++++ plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb | 3 ++- plugins/mezuro/views/cms/mezuro_plugin/_project_content.html.erb | 9 ++++----- 14 files changed, 95 insertions(+), 221 deletions(-) delete mode 100644 plugins/mezuro/lib/kalibro/client/port.rb delete mode 100644 plugins/mezuro/lib/kalibro/entities/entity.rb delete mode 100644 plugins/mezuro/lib/kalibro/kalibro.rb delete mode 100644 plugins/mezuro/test/unit/kalibro/kalibro_test.rb diff --git a/plugins/mezuro/lib/kalibro/client/port.rb b/plugins/mezuro/lib/kalibro/client/port.rb deleted file mode 100644 index e5537f8..0000000 --- a/plugins/mezuro/lib/kalibro/client/port.rb +++ /dev/null @@ -1,38 +0,0 @@ -require 'savon' - -Savon.configure do |config| - config.log = HTTPI.log = (RAILS_ENV == 'development') -end - -class Kalibro::Client::Port - - def initialize(endpoint) - @endpoint = endpoint - initialize_client - end - - def service_address - if @service_address.nil? - service_file = "#{RAILS_ROOT}/plugins/mezuro/SERVICE" - File.open(service_file).each_line{ | line | @service_address = line } - end - @service_address - end - - def service_address=(address) - @service_address = address - initialize_client - end - - def request(action, request_body = nil) - response = @client.request(:kalibro, action) { soap.body = request_body } - response.to_hash["#{action}_response".to_sym] - end - - private - - def initialize_client - @client = Savon::Client.new("#{service_address}#{@endpoint}Endpoint/?wsdl") - end - -end diff --git a/plugins/mezuro/lib/kalibro/entities/entity.rb b/plugins/mezuro/lib/kalibro/entities/entity.rb deleted file mode 100644 index e486519..0000000 --- a/plugins/mezuro/lib/kalibro/entities/entity.rb +++ /dev/null @@ -1,84 +0,0 @@ -class Kalibro::Entities::Entity - - def self.from_hash(hash) - entity = self.new - hash.each { |field, value| entity.set(field, value) if is_valid?(field) } - entity - end - - def self.is_valid?(field) - field.to_s[0] != '@' and field != :attributes! - end - - def self.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 set(field, value) - send("#{field}=", value) if not field.to_s.start_with? '@' - end - - def to_entity_array(value, entity_class = nil) - array = value.kind_of?(Array) ? value : [value] - array.each.collect { |element| to_entity(element, entity_class) } - end - - def to_entity(value, entity_class) - value.kind_of?(Hash) ? entity_class.from_hash(value) : value - end - - def to_hash - hash = Hash.new - fields.each do |field| - field_value = self.get(field) - hash[field] = convert_to_hash(field_value) if ! field_value.nil? - if need_xml_type?(field_value) - hash = {:attributes! => {}}.merge(hash) - hash[:attributes!][field.to_sym] = { - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance', - 'xsi:type' => 'kalibro:' + xml_class_name(field_value) } - end - end - hash - end - - def ==(other) - begin - fields.each.inject(true) { |equal, field| equal && (self.get(field) == other.get(field)) } - rescue NoMethodError - false - end - end - - protected - - def fields - instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '').to_sym } - end - - def get(field) - send("#{field}") - end - - def convert_to_hash(value) - return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array) - return value.to_hash if value.is_a?(Kalibro::Entities::Entity) - return self.class.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 - end - - def need_xml_type?(value) - value.is_a?(Kalibro::Entities::Entity) and value.class.superclass != Kalibro::Entities::Entity - end - - def xml_class_name(entity) - xml_name = entity.class.name - xml_name["Kalibro::Entities::"] = "" - xml_name[0..0] = xml_name[0..0].downcase - xml_name + "Xml" - end - -end diff --git a/plugins/mezuro/lib/kalibro/kalibro.rb b/plugins/mezuro/lib/kalibro/kalibro.rb deleted file mode 100644 index 902ccb3..0000000 --- a/plugins/mezuro/lib/kalibro/kalibro.rb +++ /dev/null @@ -1,22 +0,0 @@ -class Kalibro::Kalibro < Kalibro::Model - - def self.repository_types - request("Kalibro", :get_supported_repository_types)[:repository_type].to_a - end - - def self.process_project(project_name, days = '0') - if days.to_i.zero? - request("Kalibro", :process_project, {:project_name => project_name}) - else - request("Kalibro", :process_periodically, {:project_name => project_name, :period_in_days => days}) - end - end - - def self.process_period(project_name) - request("Kalibro", :get_process_period, {:project_name => project_name})[:period] - end - - def self.cancel_periodic_process(project_name) - request("Kalibro", :cancel_periodic_process, {:project_name => project_name}) - end -end diff --git a/plugins/mezuro/lib/kalibro/metric_configuration.rb b/plugins/mezuro/lib/kalibro/metric_configuration.rb index cb79bdf..f38105f 100644 --- a/plugins/mezuro/lib/kalibro/metric_configuration.rb +++ b/plugins/mezuro/lib/kalibro/metric_configuration.rb @@ -7,8 +7,7 @@ class Kalibro::MetricConfiguration < Kalibro::Model def metric=(value) if value.kind_of?(Hash) - @metric = Kalibro::NativeMetric.to_object(value) if value.has_key?(:origin) - @metric = Kalibro::CompoundMetric.to_object(value) if value.has_key?(:script) + @metric = native?(value) ? Kalibro::NativeMetric.to_object(value) : Kalibro::CompoundMetric.to_object(value) else @metric = value end @@ -66,11 +65,11 @@ class Kalibro::MetricConfiguration < Kalibro::Model :metric_name=> metric.name }) end - + def to_hash hash = Hash.new fields.each do |field| - if !(field == :configuration_name) + if (field != :configuration_name) field_value = send(field) hash[field] = convert_to_hash(field_value) if field_value.is_a?(Kalibro::Model) @@ -84,4 +83,10 @@ class Kalibro::MetricConfiguration < Kalibro::Model hash end + private + + def native?(value) + value.has_key?(:origin) ? true : false + end + end diff --git a/plugins/mezuro/lib/kalibro/metric_result.rb b/plugins/mezuro/lib/kalibro/metric_result.rb index c5809ba..1ef5c09 100644 --- a/plugins/mezuro/lib/kalibro/metric_result.rb +++ b/plugins/mezuro/lib/kalibro/metric_result.rb @@ -4,16 +4,12 @@ class Kalibro::MetricResult < Kalibro::Model def metric=(value) if value.kind_of?(Hash) - compound?(value) ? @metric = Kalibro::CompoundMetric.to_object(value) : @metric = Kalibro::NativeMetric.to_object(value) + @metric = native?(value) ? Kalibro::NativeMetric.to_object(value) : Kalibro::CompoundMetric.to_object(value) else @metric = value end end - def compound?(metric) - metric.has_key?(:script) - end - def value=(value) @value = value.to_f end @@ -34,5 +30,11 @@ class Kalibro::MetricResult < Kalibro::Model def descendent_results=(descendent_results) @descendent_result = descendent_results end + + private + + def native?(value) + value.has_key?(:origin) ? true : false + end end diff --git a/plugins/mezuro/lib/kalibro/project.rb b/plugins/mezuro/lib/kalibro/project.rb index 0a75bbd..e580612 100644 --- a/plugins/mezuro/lib/kalibro/project.rb +++ b/plugins/mezuro/lib/kalibro/project.rb @@ -44,4 +44,20 @@ class Kalibro::Project < Kalibro::Model @error = Kalibro::Error.to_object value end + def process_project(days = '0') + if days.to_i.zero? + request("Kalibro", :process_project, {:project_name => name}) + else + request("Kalibro", :process_periodically, {:project_name => name, :period_in_days => days}) + end + end + + def process_period + request("Kalibro", :get_process_period, {:project_name => name})[:period] + end + + def cancel_periodic_process + request("Kalibro", :cancel_periodic_process, {:project_name => name}) + end + end diff --git a/plugins/mezuro/lib/kalibro/repository.rb b/plugins/mezuro/lib/kalibro/repository.rb index 649f046..26be3a7 100644 --- a/plugins/mezuro/lib/kalibro/repository.rb +++ b/plugins/mezuro/lib/kalibro/repository.rb @@ -2,4 +2,8 @@ class Kalibro::Repository < Kalibro::Model attr_accessor :type, :address, :username, :password + def self.repository_types + request("Kalibro", :get_supported_repository_types)[:repository_type].to_a + end + end diff --git a/plugins/mezuro/lib/mezuro_plugin/project_content.rb b/plugins/mezuro/lib/mezuro_plugin/project_content.rb index ba9d09c..1a27433 100644 --- a/plugins/mezuro/lib/mezuro_plugin/project_content.rb +++ b/plugins/mezuro/lib/mezuro_plugin/project_content.rb @@ -80,11 +80,10 @@ Kalibro::ProjectResult.first_result_after(name, date) def send_project_to_service begin Kalibro::Project.create(self) - Kalibro::Kalibro.process_project(name, periodicity_in_days) + project.process_project(periodicity_in_days) rescue Exception => error errors.add_to_base(error.message) end - end def destroy_project_from_service diff --git a/plugins/mezuro/test/unit/kalibro/kalibro_test.rb b/plugins/mezuro/test/unit/kalibro/kalibro_test.rb deleted file mode 100644 index 47d91ef..0000000 --- a/plugins/mezuro/test/unit/kalibro/kalibro_test.rb +++ /dev/null @@ -1,34 +0,0 @@ -require "test_helper" - -class KalibroClientTest < ActiveSupport::TestCase - - def setup - @name = 'KalibroTest' - end - - should 'get supported repository types' do - types = ['BAZAAR', 'GIT', 'SUBVERSION'] - Kalibro::Kalibro.expects(:request).with('Kalibro', :get_supported_repository_types).returns({:repository_type => types}) - assert_equal types, Kalibro::Kalibro.repository_types - end - - should 'process project without days' do - Kalibro::Kalibro.expects(:request).with('Kalibro', :process_project, {:project_name => @name}) - Kalibro::Kalibro.process_project(@name) - end - - should 'process project with days' do - Kalibro::Kalibro.expects(:request).with('Kalibro', :process_periodically, {:project_name => @name, :period_in_days => "1"}) - Kalibro::Kalibro.process_project(@name, "1") - end - - should 'process period' do - Kalibro::Kalibro.expects(:request).with('Kalibro', :get_process_period, {:project_name => @name}).returns({:period => "1"}) - assert_equal "1", Kalibro::Kalibro.process_period(@name) - end - - should 'cancel periodic process' do - Kalibro::Kalibro.expects(:request).with("Kalibro", :cancel_periodic_process, {:project_name => @name}) - Kalibro::Kalibro.cancel_periodic_process(@name) - end -end diff --git a/plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb b/plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb index bf05db8..1c620d6 100644 --- a/plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb +++ b/plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb @@ -6,65 +6,65 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/range_fixtures" class MetricConfigurationTest < ActiveSupport::TestCase def setup - @hash = MetricConfigurationFixtures.amloc_metric_configuration_hash - @metric_configuration1 = MetricConfigurationFixtures.amloc_metric_configuration - @metric_configuration2 = MetricConfigurationFixtures.sc_metric_configuration + @native_metric_configuration = MetricConfigurationFixtures.amloc_metric_configuration + @native_metric_configuration_hash = MetricConfigurationFixtures.amloc_metric_configuration_hash + @compound_metric_configuration = MetricConfigurationFixtures.sc_metric_configuration @metric_configuration_without_ranges = MetricConfigurationFixtures.metric_configuration_without_ranges - @range1 = RangeFixtures.range_excellent - @range2 = RangeFixtures.range_bad + @excellent_range = RangeFixtures.range_excellent + @bad_range = RangeFixtures.range_bad end should 'create metric configuration from hash' do - assert_equal @hash[:code], Kalibro::MetricConfiguration.new(@hash).code + assert_equal @native_metric_configuration_hash[:code], Kalibro::MetricConfiguration.new(@native_metric_configuration_hash).code end should 'convert metric configuration to hash' do - assert_equal @hash, @metric_configuration1.to_hash + assert_equal @native_metric_configuration_hash, @native_metric_configuration_hash end should 'create appropriate metric type' do - assert @metric_configuration1.metric.instance_of?(Kalibro::NativeMetric) - assert @metric_configuration2.metric.instance_of?(Kalibro::CompoundMetric) + assert @native_metric_configuration.metric.instance_of?(Kalibro::NativeMetric) + assert @compound_metric_configuration.metric.instance_of?(Kalibro::CompoundMetric) end should 'add a range to an empty range list' do - @metric_configuration_without_ranges.add_range @range1 - assert_equal @metric_configuration_without_ranges.ranges, [@range1] + @metric_configuration_without_ranges.add_range @excellent_range + assert_equal @metric_configuration_without_ranges.ranges, [@excellent_range] end should 'add a range to an non-empty range list' do - @metric_configuration_without_ranges.ranges = [@range1] - @metric_configuration_without_ranges.add_range @range2 - assert_equal @metric_configuration_without_ranges.ranges, [@range1, @range2] + @metric_configuration_without_ranges.ranges = [@excellent_range] + @metric_configuration_without_ranges.add_range @bad_range + assert_equal @metric_configuration_without_ranges.ranges, [@excellent_range, @bad_range] end should 'save metric configuration' do Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :save_metric_configuration, { - :metric_configuration => @metric_configuration1.to_hash, - :configuration_name => @metric_configuration1.configuration_name + :metric_configuration => @native_metric_configuration_hash, + :configuration_name => @native_metric_configuration.configuration_name }) - @metric_configuration1.save + @native_metric_configuration.save end should 'get metric configuration by name and configuration name' do request_body = { - :configuration_name => @metric_configuration1.configuration_name, - :metric_name => @metric_configuration1.metric.name + :configuration_name => @native_metric_configuration.configuration_name, + :metric_name => @native_metric_configuration.metric.name } - response_hash = {:metric_configuration => @metric_configuration1.to_hash} + response_hash = {:metric_configuration => @native_metric_configuration_hash} Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :get_metric_configuration, request_body).returns(response_hash) - metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(@metric_configuration1.configuration_name, - @metric_configuration1.metric.name) - assert_equal @metric_configuration1.code, metric_configuration.code + metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(@native_metric_configuration.configuration_name, + @native_metric_configuration.metric.name) + assert_equal @native_metric_configuration.code, metric_configuration.code end should 'destroy metric configuration by name' do request_body = { - :configuration_name => @metric_configuration1.configuration_name, - :metric_name => @metric_configuration1.metric.name + :configuration_name => @native_metric_configuration.configuration_name, + :metric_name => @native_metric_configuration.metric.name } Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :remove_metric_configuration, request_body) - @metric_configuration1.destroy + @native_metric_configuration.destroy end end diff --git a/plugins/mezuro/test/unit/kalibro/project_test.rb b/plugins/mezuro/test/unit/kalibro/project_test.rb index 6a3bc59..d704118 100644 --- a/plugins/mezuro/test/unit/kalibro/project_test.rb +++ b/plugins/mezuro/test/unit/kalibro/project_test.rb @@ -67,5 +67,25 @@ class ProjectTest < ActiveSupport::TestCase assert_equal @hash[:repository], hash[:repository] assert_equal @hash[:state], hash[:state] end + + should 'process project without days' do + @project.expects(:request).with('Kalibro', :process_project, {:project_name => @project.name}) + @project.process_project + end + + should 'process project with days' do + @project.expects(:request).with('Kalibro', :process_periodically, {:project_name => @project.name, :period_in_days => "1"}) + @project.process_project "1" + end + + should 'process period' do + @project.expects(:request).with('Kalibro', :get_process_period, {:project_name => @project.name}).returns({:period => "1"}) + assert_equal "1", @project.process_period + end + + should 'cancel periodic process' do + @project.expects(:request).with("Kalibro", :cancel_periodic_process, {:project_name => @project.name}) + @project.cancel_periodic_process + end end diff --git a/plugins/mezuro/test/unit/kalibro/repository_test.rb b/plugins/mezuro/test/unit/kalibro/repository_test.rb index ad87f4d..145c11a 100644 --- a/plugins/mezuro/test/unit/kalibro/repository_test.rb +++ b/plugins/mezuro/test/unit/kalibro/repository_test.rb @@ -17,4 +17,10 @@ class RepositoryTest < ActiveSupport::TestCase assert_equal @hash, @repository.to_hash end + should 'get supported repository types' do + types = ['BAZAAR', 'GIT', 'SUBVERSION'] + Kalibro::Repository.expects(:request).with('Kalibro', :get_supported_repository_types).returns({:repository_type => types}) + assert_equal types, Kalibro::Repository.repository_types + end + end diff --git a/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb b/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb index 50780b8..b3b066b 100644 --- a/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb +++ b/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb @@ -83,7 +83,8 @@ class ProjectContentTest < ActiveSupport::TestCase should 'send correct project to service' do Kalibro::Project.expects(:create).with(@content).returns(true) - Kalibro::Kalibro.expects(:process_project).with(@content.name, @content.periodicity_in_days) + Kalibro::Project.expects(:find_by_name).with(@content.name).returns(@project) + @project.expects(:process_project).with(@content.periodicity_in_days) @content.send :send_project_to_service end diff --git a/plugins/mezuro/views/cms/mezuro_plugin/_project_content.html.erb b/plugins/mezuro/views/cms/mezuro_plugin/_project_content.html.erb index e127b44..7cd4363 100644 --- a/plugins/mezuro/views/cms/mezuro_plugin/_project_content.html.erb +++ b/plugins/mezuro/views/cms/mezuro_plugin/_project_content.html.erb @@ -2,8 +2,7 @@ <% begin - @project = @article.title.nil? ? nil : Kalibro::Client::ProjectClient.project(@article.title) - @kalibro_client = Kalibro::Client::KalibroClient.new + @project = @article.title.nil? ? nil : Kalibro::Project.project(@article.title) rescue @project = nil end @@ -25,14 +24,14 @@ <%= f.text_field :description %>
-<% @repository_types = @kalibro_client.supported_repository_types.sort %> +<% @repository_types = Kalibro::Repository.repository_types.sort %> <% @selected = (@project.nil? ? @repository_types : @project.repository.type) %> <%= required labelled_form_field _('Repository type'), f.select(:repository_type, @repository_types, {:selected => @selected}) %>
<%= required f.text_field(:repository_url) %>
-<% @configuration_names = Kalibro::Client::ConfigurationClient.new.configuration_names.sort %> +<% @configuration_names = Kalibro::Configuration.all_names.sort %> <% @selected = (@project.nil? ? @configuration_names[0] : @project.configuration_name) %> <% if !@project.nil? && !@article.id.nil? %> @@ -43,6 +42,6 @@ f.select(:configuration_name, @configuration_names, {:selected => @selected}) %>
<% end %> -<% selected = (@project.nil? ? 0 : @kalibro_client.process_period(@article.title).to_i) %> +<% selected = (@project.nil? ? 0 : @project.process_period.to_i) %> <%= required labelled_form_field _('Periodic Avaliation'), f.select(:periodicity_in_days, MezuroPlugin::Helpers::ContentViewerHelper.create_periodicity_options ,{:selected => selected}) %>
-- libgit2 0.21.2