From 5e54605b7ac316d824483cd3156a797f6fb813b6 Mon Sep 17 00:00:00 2001 From: João M. M. da Silva + Diego Araújo Date: Thu, 12 Jul 2012 16:16:35 -0300 Subject: [PATCH] [Mezuro] Refactored base_tool and native_metric entities/clients to model --- plugins/mezuro/controllers/mezuro_plugin_myprofile_controller.rb | 8 ++++---- plugins/mezuro/lib/kalibro/base_tool.rb | 29 +++++++++++++++++++++++++++++ plugins/mezuro/lib/kalibro/client/base_tool_client.rb | 28 ---------------------------- plugins/mezuro/lib/kalibro/client/project_client.rb | 65 ----------------------------------------------------------------- plugins/mezuro/lib/kalibro/entities/base_tool.rb | 17 ----------------- plugins/mezuro/lib/kalibro/entities/metric.rb | 5 ----- plugins/mezuro/lib/kalibro/entities/native_metric.rb | 17 ----------------- plugins/mezuro/lib/kalibro/entities/project.rb | 12 ------------ plugins/mezuro/lib/kalibro/entities/repository.rb | 5 ----- plugins/mezuro/lib/kalibro/metric.rb | 5 +++++ plugins/mezuro/lib/kalibro/model.rb | 10 ++++++++++ plugins/mezuro/lib/kalibro/native_metric.rb | 17 +++++++++++++++++ plugins/mezuro/lib/kalibro/project.rb | 2 +- plugins/mezuro/test/fixtures/base_tool_fixtures.rb | 11 +++-------- plugins/mezuro/test/fixtures/native_metric_fixtures.rb | 14 ++------------ plugins/mezuro/test/unit/kalibro/base_tool_test.rb | 36 ++++++++++++++++++++++++++++++++++++ plugins/mezuro/test/unit/kalibro/client/base_tool_client_test.rb | 37 ------------------------------------- plugins/mezuro/test/unit/kalibro/entities/base_tool_test.rb | 20 -------------------- plugins/mezuro/test/unit/kalibro/entities/metric_test.rb | 24 ------------------------ plugins/mezuro/test/unit/kalibro/entities/native_metric_test.rb | 20 -------------------- plugins/mezuro/test/unit/kalibro/native_metric_test.rb | 20 ++++++++++++++++++++ 21 files changed, 127 insertions(+), 275 deletions(-) create mode 100644 plugins/mezuro/lib/kalibro/base_tool.rb delete mode 100644 plugins/mezuro/lib/kalibro/client/base_tool_client.rb delete mode 100644 plugins/mezuro/lib/kalibro/client/project_client.rb delete mode 100644 plugins/mezuro/lib/kalibro/entities/base_tool.rb delete mode 100644 plugins/mezuro/lib/kalibro/entities/metric.rb delete mode 100644 plugins/mezuro/lib/kalibro/entities/native_metric.rb delete mode 100644 plugins/mezuro/lib/kalibro/entities/project.rb delete mode 100644 plugins/mezuro/lib/kalibro/entities/repository.rb create mode 100644 plugins/mezuro/lib/kalibro/metric.rb create mode 100644 plugins/mezuro/lib/kalibro/native_metric.rb create mode 100644 plugins/mezuro/test/unit/kalibro/base_tool_test.rb delete mode 100644 plugins/mezuro/test/unit/kalibro/client/base_tool_client_test.rb delete mode 100644 plugins/mezuro/test/unit/kalibro/entities/base_tool_test.rb delete mode 100644 plugins/mezuro/test/unit/kalibro/entities/metric_test.rb delete mode 100644 plugins/mezuro/test/unit/kalibro/entities/native_metric_test.rb create mode 100644 plugins/mezuro/test/unit/kalibro/native_metric_test.rb diff --git a/plugins/mezuro/controllers/mezuro_plugin_myprofile_controller.rb b/plugins/mezuro/controllers/mezuro_plugin_myprofile_controller.rb index 5f090fa..74ef09f 100644 --- a/plugins/mezuro/controllers/mezuro_plugin_myprofile_controller.rb +++ b/plugins/mezuro/controllers/mezuro_plugin_myprofile_controller.rb @@ -5,18 +5,18 @@ class MezuroPluginMyprofileController < ProfileController def choose_base_tool @configuration_content = profile.articles.find(params[:id]) - @base_tools = Kalibro::Client::BaseToolClient.base_tools + @base_tools = Kalibro::BaseTool.all_names end def choose_metric @configuration_content = profile.articles.find(params[:id]) @base_tool = params[:base_tool] - @supported_metrics = Kalibro::Client::BaseToolClient.metrics @base_tool + @supported_metrics = Kalibro::BaseTool.find_by_name(@base_tool).supported_metrics end def new_metric_configuration @configuration_content = profile.articles.find(params[:id]) - @metric = Kalibro::Client::BaseToolClient.metric params[:metric_name], params[:base_tool] + @metric = Kalibro::BaseTool.find_by_name(params[:base_tool]).metric params[:metric_name] end def new_compound_metric_configuration @@ -123,7 +123,7 @@ class MezuroPluginMyprofileController < ProfileController def new_metric_configuration_instance metric_configuration = Kalibro::Entities::MetricConfiguration.new - metric_configuration.metric = Kalibro::Entities::NativeMetric.new + metric_configuration.metric = Kalibro::NativeMetric.new assign_metric_configuration_instance(metric_configuration, Kalibro::Entities::MetricConfiguration::NATIVE_TYPE) end diff --git a/plugins/mezuro/lib/kalibro/base_tool.rb b/plugins/mezuro/lib/kalibro/base_tool.rb new file mode 100644 index 0000000..e0b1838 --- /dev/null +++ b/plugins/mezuro/lib/kalibro/base_tool.rb @@ -0,0 +1,29 @@ +class Kalibro::BaseTool < Kalibro::Model + + attr_accessor :name, :description, :supported_metric + + def self.all_names + request("BaseTool", :get_base_tool_names)[:base_tool_name].to_a + end + + def self.find_by_name(name) + new request("BaseTool", :get_base_tool, {:base_tool_name => name})[:base_tool] + end + + def supported_metric=(value) + @supported_metric = to_objects_array(value, Kalibro::NativeMetric) + end + + def supported_metrics + @supported_metric + end + + def supported_metrics=(supported_metrics) + @supported_metric = supported_metrics + end + + def metric(name) + supported_metrics.find {|metric| metric.name == name} + end + +end diff --git a/plugins/mezuro/lib/kalibro/client/base_tool_client.rb b/plugins/mezuro/lib/kalibro/client/base_tool_client.rb deleted file mode 100644 index c2240b6..0000000 --- a/plugins/mezuro/lib/kalibro/client/base_tool_client.rb +++ /dev/null @@ -1,28 +0,0 @@ -class Kalibro::Client::BaseToolClient - - def self.base_tools - new.base_tool_names - end - - def self.metrics(base_tool) - new.base_tool(base_tool).supported_metrics - end - - def self.metric(metric_name, base_tool) - metrics(base_tool).find {|metric| metric.name == metric_name} - end - - def initialize - @port = Kalibro::Client::Port.new('BaseTool') - end - - def base_tool_names - @port.request(:get_base_tool_names)[:base_tool_name].to_a - end - - def base_tool(name) - hash = @port.request(:get_base_tool, {:base_tool_name => name})[:base_tool] - Kalibro::Entities::BaseTool.from_hash(hash) - end - -end diff --git a/plugins/mezuro/lib/kalibro/client/project_client.rb b/plugins/mezuro/lib/kalibro/client/project_client.rb deleted file mode 100644 index c9018e9..0000000 --- a/plugins/mezuro/lib/kalibro/client/project_client.rb +++ /dev/null @@ -1,65 +0,0 @@ -class Kalibro::Client::ProjectClient - - def self.project(project_name) - new.project(project_name) - end - - def self.save(project_content) - project = create_project(project_content) - new.save(project) - end - - def self.remove(project_name) - instance = new - if (instance.project_names.include?(project_name)) - instance.remove(project_name) - end - end - - def self.create_project (project_content) - project = Kalibro::Entities::Project.new - project.name = project_content.name - project.license = project_content.license - project.description = project_content.description - project.repository = create_repository(project_content) - project.configuration_name = project_content.configuration_name - project - end - - def self.create_repository(project_content) - repository = Kalibro::Entities::Repository.new - repository.type = project_content.repository_type - repository.address = project_content.repository_url - repository - end - - def initialize - @port = Kalibro::Client::Port.new('Project') - end - - def save(project) - @port.request(:save_project, {:project => project.to_hash}) - end - - def project_names - @port.request(:get_project_names)[:project_name].to_a - end - - def project(project_name) - begin - hash = @port.request(:get_project, {:project_name => project_name})[:project] - rescue Exception => error - unless (error.message =~ /There is no project named/).nil? - return nil - else - raise error - end - end - Kalibro::Entities::Project.from_hash(hash) - end - - def remove(project_name) - @port.request(:remove_project, {:project_name => project_name}) - end - -end diff --git a/plugins/mezuro/lib/kalibro/entities/base_tool.rb b/plugins/mezuro/lib/kalibro/entities/base_tool.rb deleted file mode 100644 index 334e651..0000000 --- a/plugins/mezuro/lib/kalibro/entities/base_tool.rb +++ /dev/null @@ -1,17 +0,0 @@ -class Kalibro::Entities::BaseTool < Kalibro::Entities::Entity - - attr_accessor :name, :description, :supported_metric - - def supported_metric=(value) - @supported_metric = to_entity_array(value, Kalibro::Entities::NativeMetric) - end - - def supported_metrics - @supported_metric - end - - def supported_metrics=(supported_metrics) - @supported_metric = supported_metrics - end - -end \ No newline at end of file diff --git a/plugins/mezuro/lib/kalibro/entities/metric.rb b/plugins/mezuro/lib/kalibro/entities/metric.rb deleted file mode 100644 index e369fc8..0000000 --- a/plugins/mezuro/lib/kalibro/entities/metric.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Kalibro::Entities::Metric < Kalibro::Entities::Entity - - attr_accessor :name, :scope, :description - -end diff --git a/plugins/mezuro/lib/kalibro/entities/native_metric.rb b/plugins/mezuro/lib/kalibro/entities/native_metric.rb deleted file mode 100644 index 92f8ef4..0000000 --- a/plugins/mezuro/lib/kalibro/entities/native_metric.rb +++ /dev/null @@ -1,17 +0,0 @@ -class Kalibro::Entities::NativeMetric < Kalibro::Entities::Metric - - attr_accessor :origin, :language - - def languages - @language - end - - def languages=(languages) - @language = languages - end - - def language=(value) - @language = to_entity_array(value) - end - -end diff --git a/plugins/mezuro/lib/kalibro/entities/project.rb b/plugins/mezuro/lib/kalibro/entities/project.rb deleted file mode 100644 index f52c8d2..0000000 --- a/plugins/mezuro/lib/kalibro/entities/project.rb +++ /dev/null @@ -1,12 +0,0 @@ -class Kalibro::Entities::Project < Kalibro::Entities::Entity - - attr_accessor :name, :license, :description, :repository, :configuration_name, :state, :error - - def repository=(value) - @repository = to_entity(value, Kalibro::Entities::Repository) - end - - def error=(value) - @error = to_entity(value, Kalibro::Entities::Error) - end -end diff --git a/plugins/mezuro/lib/kalibro/entities/repository.rb b/plugins/mezuro/lib/kalibro/entities/repository.rb deleted file mode 100644 index 22ee062..0000000 --- a/plugins/mezuro/lib/kalibro/entities/repository.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Kalibro::Entities::Repository < Kalibro::Entities::Entity - - attr_accessor :type, :address, :username, :password - -end \ No newline at end of file diff --git a/plugins/mezuro/lib/kalibro/metric.rb b/plugins/mezuro/lib/kalibro/metric.rb new file mode 100644 index 0000000..3390e73 --- /dev/null +++ b/plugins/mezuro/lib/kalibro/metric.rb @@ -0,0 +1,5 @@ +class Kalibro::Metric < Kalibro::Model + + attr_accessor :name, :scope, :description + +end diff --git a/plugins/mezuro/lib/kalibro/model.rb b/plugins/mezuro/lib/kalibro/model.rb index f276c11..71d16ef 100644 --- a/plugins/mezuro/lib/kalibro/model.rb +++ b/plugins/mezuro/lib/kalibro/model.rb @@ -27,6 +27,7 @@ class Kalibro::Model 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?(Kalibro::Model) value end @@ -48,4 +49,13 @@ class Kalibro::Model response.to_hash["#{action}_response".to_sym] end + def to_objects_array(value, model_class = nil) + array = value.kind_of?(Array) ? value : [value] + array.each.collect { |element| to_object(element, model_class) } + end + + def to_object(value, model_class) + value.kind_of?(Hash) ? model_class.new(value) : value + end + end diff --git a/plugins/mezuro/lib/kalibro/native_metric.rb b/plugins/mezuro/lib/kalibro/native_metric.rb new file mode 100644 index 0000000..5fa55cd --- /dev/null +++ b/plugins/mezuro/lib/kalibro/native_metric.rb @@ -0,0 +1,17 @@ +class Kalibro::NativeMetric < Kalibro::Metric + + attr_accessor :origin, :language + + def languages + @language + end + + def languages=(languages) + @language = languages + end + + def language=(value) + @language = to_objects_array(value) + end + +end diff --git a/plugins/mezuro/lib/kalibro/project.rb b/plugins/mezuro/lib/kalibro/project.rb index 8d4d3d3..8ac0fa3 100644 --- a/plugins/mezuro/lib/kalibro/project.rb +++ b/plugins/mezuro/lib/kalibro/project.rb @@ -42,7 +42,7 @@ class Kalibro::Project < Kalibro::Model end def repository=(value) - @repository = (value.kind_of?(Hash)) ? Kalibro::Repository.new(value) : value + @repository = to_object(value, Kalibro::Repository) end end diff --git a/plugins/mezuro/test/fixtures/base_tool_fixtures.rb b/plugins/mezuro/test/fixtures/base_tool_fixtures.rb index 4824d90..05ca51a 100644 --- a/plugins/mezuro/test/fixtures/base_tool_fixtures.rb +++ b/plugins/mezuro/test/fixtures/base_tool_fixtures.rb @@ -2,16 +2,11 @@ require File.dirname(__FILE__) + '/native_metric_fixtures' class BaseToolFixtures - def self.analizo - base_tool = Kalibro::Entities::BaseTool.new - base_tool.name = 'Analizo' - base_tool.supported_metrics = [ - NativeMetricFixtures.total_cof, - NativeMetricFixtures.amloc] - base_tool + def self.base_tool + Kalibro::BaseTool.new base_tool_hash end - def self.analizo_hash + def self.base_tool_hash {:name => 'Analizo', :supported_metric => [ NativeMetricFixtures.total_cof_hash, NativeMetricFixtures.amloc_hash]} diff --git a/plugins/mezuro/test/fixtures/native_metric_fixtures.rb b/plugins/mezuro/test/fixtures/native_metric_fixtures.rb index be6496c..809bcc1 100644 --- a/plugins/mezuro/test/fixtures/native_metric_fixtures.rb +++ b/plugins/mezuro/test/fixtures/native_metric_fixtures.rb @@ -1,12 +1,7 @@ class NativeMetricFixtures def self.total_cof - total_cof = Kalibro::Entities::NativeMetric.new - total_cof.name = 'Total Coupling Factor' - total_cof.scope = 'APPLICATION' - total_cof.origin = 'Analizo' - total_cof.languages = ['JAVA'] - total_cof + Kalibro::NativeMetric.new total_cof_hash end def self.total_cof_hash @@ -14,12 +9,7 @@ class NativeMetricFixtures end def self.amloc - total_cof = Kalibro::Entities::NativeMetric.new - total_cof.name = 'Average Method LOC' - total_cof.scope = 'CLASS' - total_cof.origin = 'Analizo' - total_cof.languages = ['JAVA'] - total_cof + Kalibro::NativeMetric.new amloc_hash end def self.amloc_hash diff --git a/plugins/mezuro/test/unit/kalibro/base_tool_test.rb b/plugins/mezuro/test/unit/kalibro/base_tool_test.rb new file mode 100644 index 0000000..f4a00f2 --- /dev/null +++ b/plugins/mezuro/test/unit/kalibro/base_tool_test.rb @@ -0,0 +1,36 @@ +require "test_helper" + +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/base_tool_fixtures" + +class BaseToolTest < ActiveSupport::TestCase + + def setup + @hash = BaseToolFixtures.base_tool_hash + @base_tool = BaseToolFixtures.base_tool + end + + should 'create base tool from hash' do + assert_equal @base_tool.name, Kalibro::BaseTool.new(@hash).name + end + + should 'convert base tool to hash' do + assert_equal @hash, @base_tool.to_hash + end + + should 'get base tool names' do + names = ['Analizo', 'Checkstyle'] + Kalibro::BaseTool.expects(:request).with("BaseTool", :get_base_tool_names).returns({:base_tool_name => names}) + assert_equal names, Kalibro::BaseTool.all_names + end + + should 'get base tool by name' do + request_body = {:base_tool_name => @base_tool.name} + Kalibro::BaseTool.expects(:request).with("BaseTool", :get_base_tool, request_body).returns({:base_tool => @hash}) + assert_equal @base_tool.name, Kalibro::BaseTool.find_by_name(@base_tool.name).name + end + + should 'get base tool metrics' do + assert_equal @base_tool.supported_metrics[0].name, @base_tool.metric('Total Coupling Factor').name + end + +end diff --git a/plugins/mezuro/test/unit/kalibro/client/base_tool_client_test.rb b/plugins/mezuro/test/unit/kalibro/client/base_tool_client_test.rb deleted file mode 100644 index db4c05e..0000000 --- a/plugins/mezuro/test/unit/kalibro/client/base_tool_client_test.rb +++ /dev/null @@ -1,37 +0,0 @@ -require "test_helper" - -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/base_tool_fixtures" - -class BaseToolClientTest < ActiveSupport::TestCase - - def setup - @port = mock - Kalibro::Client::Port.expects(:new).with('BaseTool').returns(@port) - @client = Kalibro::Client::BaseToolClient.new - end - - should 'get base tool names (zero)' do - @port.expects(:request).with(:get_base_tool_names).returns({}) - assert_equal [], @client.base_tool_names - end - - should 'get base tool names (one)' do - name = 'Analizo' - @port.expects(:request).with(:get_base_tool_names).returns({:base_tool_name => name}) - assert_equal [name], @client.base_tool_names - end - - should 'get base tool names' do - names = ['Analizo', 'Checkstyle'] - @port.expects(:request).with(:get_base_tool_names).returns({:base_tool_name => names}) - assert_equal names, @client.base_tool_names - end - - should 'get base tool by name' do - analizo = BaseToolFixtures.analizo - request_body = {:base_tool_name => 'Analizo'} - @port.expects(:request).with(:get_base_tool, request_body).returns({:base_tool => analizo.to_hash}) - assert_equal analizo, @client.base_tool('Analizo') - end - -end \ No newline at end of file diff --git a/plugins/mezuro/test/unit/kalibro/entities/base_tool_test.rb b/plugins/mezuro/test/unit/kalibro/entities/base_tool_test.rb deleted file mode 100644 index 1df692e..0000000 --- a/plugins/mezuro/test/unit/kalibro/entities/base_tool_test.rb +++ /dev/null @@ -1,20 +0,0 @@ -require "test_helper" - -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/base_tool_fixtures" - -class BaseToolTest < ActiveSupport::TestCase - - def setup - @hash = BaseToolFixtures.analizo_hash - @base_tool = BaseToolFixtures.analizo - end - - should 'create base tool from hash' do - assert_equal @base_tool, Kalibro::Entities::BaseTool.from_hash(@hash) - end - - should 'convert base tool to hash' do - assert_equal @hash, @base_tool.to_hash - end - -end \ No newline at end of file diff --git a/plugins/mezuro/test/unit/kalibro/entities/metric_test.rb b/plugins/mezuro/test/unit/kalibro/entities/metric_test.rb deleted file mode 100644 index d088c69..0000000 --- a/plugins/mezuro/test/unit/kalibro/entities/metric_test.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "test_helper" - -class MetricTest < ActiveSupport::TestCase - - def setup - name = 'MetricTest metric' - scope = 'METHOD' - description = 'Metric created for testing' - @hash = {:name => name, :scope => scope, :description => description} - @metric = Kalibro::Entities::Metric.new - @metric.name = name - @metric.scope = scope - @metric.description = description - end - - should 'create metric from hash' do - assert_equal @metric, Kalibro::Entities::Metric.from_hash(@hash) - end - - should 'convert metric to hash' do - assert_equal @hash, @metric.to_hash - end - -end \ No newline at end of file diff --git a/plugins/mezuro/test/unit/kalibro/entities/native_metric_test.rb b/plugins/mezuro/test/unit/kalibro/entities/native_metric_test.rb deleted file mode 100644 index 8625d76..0000000 --- a/plugins/mezuro/test/unit/kalibro/entities/native_metric_test.rb +++ /dev/null @@ -1,20 +0,0 @@ -require "test_helper" - -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/native_metric_fixtures" - -class NativeMetricTest < ActiveSupport::TestCase - - def setup - @hash = NativeMetricFixtures.amloc_hash - @metric = NativeMetricFixtures.amloc - end - - should 'create native metric from hash' do - assert_equal @metric, Kalibro::Entities::NativeMetric.from_hash(@hash) - end - - should 'convert native metric to hash' do - assert_equal @hash, @metric.to_hash - end - -end \ No newline at end of file diff --git a/plugins/mezuro/test/unit/kalibro/native_metric_test.rb b/plugins/mezuro/test/unit/kalibro/native_metric_test.rb new file mode 100644 index 0000000..616aaf7 --- /dev/null +++ b/plugins/mezuro/test/unit/kalibro/native_metric_test.rb @@ -0,0 +1,20 @@ +require "test_helper" + +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/native_metric_fixtures" + +class NativeMetricTest < ActiveSupport::TestCase + + def setup + @hash = NativeMetricFixtures.amloc_hash + @metric = NativeMetricFixtures.amloc + end + + should 'create native metric from hash' do + assert_equal @hash[:name], Kalibro::NativeMetric.new(@hash).name + end + + should 'convert native metric to hash' do + assert_equal @hash, @metric.to_hash + end + +end -- libgit2 0.21.2