Commit 5e54605b7ac316d824483cd3156a797f6fb813b6
Committed by
Paulo Meireles
1 parent
8d1ac533
Exists in
master
and in
22 other branches
[Mezuro] Refactored base_tool and native_metric entities/clients to model
Showing
21 changed files
with
127 additions
and
275 deletions
Show diff stats
plugins/mezuro/controllers/mezuro_plugin_myprofile_controller.rb
... | ... | @@ -5,18 +5,18 @@ class MezuroPluginMyprofileController < ProfileController |
5 | 5 | |
6 | 6 | def choose_base_tool |
7 | 7 | @configuration_content = profile.articles.find(params[:id]) |
8 | - @base_tools = Kalibro::Client::BaseToolClient.base_tools | |
8 | + @base_tools = Kalibro::BaseTool.all_names | |
9 | 9 | end |
10 | 10 | |
11 | 11 | def choose_metric |
12 | 12 | @configuration_content = profile.articles.find(params[:id]) |
13 | 13 | @base_tool = params[:base_tool] |
14 | - @supported_metrics = Kalibro::Client::BaseToolClient.metrics @base_tool | |
14 | + @supported_metrics = Kalibro::BaseTool.find_by_name(@base_tool).supported_metrics | |
15 | 15 | end |
16 | 16 | |
17 | 17 | def new_metric_configuration |
18 | 18 | @configuration_content = profile.articles.find(params[:id]) |
19 | - @metric = Kalibro::Client::BaseToolClient.metric params[:metric_name], params[:base_tool] | |
19 | + @metric = Kalibro::BaseTool.find_by_name(params[:base_tool]).metric params[:metric_name] | |
20 | 20 | end |
21 | 21 | |
22 | 22 | def new_compound_metric_configuration |
... | ... | @@ -123,7 +123,7 @@ class MezuroPluginMyprofileController < ProfileController |
123 | 123 | |
124 | 124 | def new_metric_configuration_instance |
125 | 125 | metric_configuration = Kalibro::Entities::MetricConfiguration.new |
126 | - metric_configuration.metric = Kalibro::Entities::NativeMetric.new | |
126 | + metric_configuration.metric = Kalibro::NativeMetric.new | |
127 | 127 | assign_metric_configuration_instance(metric_configuration, Kalibro::Entities::MetricConfiguration::NATIVE_TYPE) |
128 | 128 | end |
129 | 129 | ... | ... |
... | ... | @@ -0,0 +1,29 @@ |
1 | +class Kalibro::BaseTool < Kalibro::Model | |
2 | + | |
3 | + attr_accessor :name, :description, :supported_metric | |
4 | + | |
5 | + def self.all_names | |
6 | + request("BaseTool", :get_base_tool_names)[:base_tool_name].to_a | |
7 | + end | |
8 | + | |
9 | + def self.find_by_name(name) | |
10 | + new request("BaseTool", :get_base_tool, {:base_tool_name => name})[:base_tool] | |
11 | + end | |
12 | + | |
13 | + def supported_metric=(value) | |
14 | + @supported_metric = to_objects_array(value, Kalibro::NativeMetric) | |
15 | + end | |
16 | + | |
17 | + def supported_metrics | |
18 | + @supported_metric | |
19 | + end | |
20 | + | |
21 | + def supported_metrics=(supported_metrics) | |
22 | + @supported_metric = supported_metrics | |
23 | + end | |
24 | + | |
25 | + def metric(name) | |
26 | + supported_metrics.find {|metric| metric.name == name} | |
27 | + end | |
28 | + | |
29 | +end | ... | ... |
plugins/mezuro/lib/kalibro/client/base_tool_client.rb
... | ... | @@ -1,28 +0,0 @@ |
1 | -class Kalibro::Client::BaseToolClient | |
2 | - | |
3 | - def self.base_tools | |
4 | - new.base_tool_names | |
5 | - end | |
6 | - | |
7 | - def self.metrics(base_tool) | |
8 | - new.base_tool(base_tool).supported_metrics | |
9 | - end | |
10 | - | |
11 | - def self.metric(metric_name, base_tool) | |
12 | - metrics(base_tool).find {|metric| metric.name == metric_name} | |
13 | - end | |
14 | - | |
15 | - def initialize | |
16 | - @port = Kalibro::Client::Port.new('BaseTool') | |
17 | - end | |
18 | - | |
19 | - def base_tool_names | |
20 | - @port.request(:get_base_tool_names)[:base_tool_name].to_a | |
21 | - end | |
22 | - | |
23 | - def base_tool(name) | |
24 | - hash = @port.request(:get_base_tool, {:base_tool_name => name})[:base_tool] | |
25 | - Kalibro::Entities::BaseTool.from_hash(hash) | |
26 | - end | |
27 | - | |
28 | -end |
plugins/mezuro/lib/kalibro/client/project_client.rb
... | ... | @@ -1,65 +0,0 @@ |
1 | -class Kalibro::Client::ProjectClient | |
2 | - | |
3 | - def self.project(project_name) | |
4 | - new.project(project_name) | |
5 | - end | |
6 | - | |
7 | - def self.save(project_content) | |
8 | - project = create_project(project_content) | |
9 | - new.save(project) | |
10 | - end | |
11 | - | |
12 | - def self.remove(project_name) | |
13 | - instance = new | |
14 | - if (instance.project_names.include?(project_name)) | |
15 | - instance.remove(project_name) | |
16 | - end | |
17 | - end | |
18 | - | |
19 | - def self.create_project (project_content) | |
20 | - project = Kalibro::Entities::Project.new | |
21 | - project.name = project_content.name | |
22 | - project.license = project_content.license | |
23 | - project.description = project_content.description | |
24 | - project.repository = create_repository(project_content) | |
25 | - project.configuration_name = project_content.configuration_name | |
26 | - project | |
27 | - end | |
28 | - | |
29 | - def self.create_repository(project_content) | |
30 | - repository = Kalibro::Entities::Repository.new | |
31 | - repository.type = project_content.repository_type | |
32 | - repository.address = project_content.repository_url | |
33 | - repository | |
34 | - end | |
35 | - | |
36 | - def initialize | |
37 | - @port = Kalibro::Client::Port.new('Project') | |
38 | - end | |
39 | - | |
40 | - def save(project) | |
41 | - @port.request(:save_project, {:project => project.to_hash}) | |
42 | - end | |
43 | - | |
44 | - def project_names | |
45 | - @port.request(:get_project_names)[:project_name].to_a | |
46 | - end | |
47 | - | |
48 | - def project(project_name) | |
49 | - begin | |
50 | - hash = @port.request(:get_project, {:project_name => project_name})[:project] | |
51 | - rescue Exception => error | |
52 | - unless (error.message =~ /There is no project named/).nil? | |
53 | - return nil | |
54 | - else | |
55 | - raise error | |
56 | - end | |
57 | - end | |
58 | - Kalibro::Entities::Project.from_hash(hash) | |
59 | - end | |
60 | - | |
61 | - def remove(project_name) | |
62 | - @port.request(:remove_project, {:project_name => project_name}) | |
63 | - end | |
64 | - | |
65 | -end |
plugins/mezuro/lib/kalibro/entities/base_tool.rb
... | ... | @@ -1,17 +0,0 @@ |
1 | -class Kalibro::Entities::BaseTool < Kalibro::Entities::Entity | |
2 | - | |
3 | - attr_accessor :name, :description, :supported_metric | |
4 | - | |
5 | - def supported_metric=(value) | |
6 | - @supported_metric = to_entity_array(value, Kalibro::Entities::NativeMetric) | |
7 | - end | |
8 | - | |
9 | - def supported_metrics | |
10 | - @supported_metric | |
11 | - end | |
12 | - | |
13 | - def supported_metrics=(supported_metrics) | |
14 | - @supported_metric = supported_metrics | |
15 | - end | |
16 | - | |
17 | -end | |
18 | 0 | \ No newline at end of file |
plugins/mezuro/lib/kalibro/entities/metric.rb
plugins/mezuro/lib/kalibro/entities/native_metric.rb
... | ... | @@ -1,17 +0,0 @@ |
1 | -class Kalibro::Entities::NativeMetric < Kalibro::Entities::Metric | |
2 | - | |
3 | - attr_accessor :origin, :language | |
4 | - | |
5 | - def languages | |
6 | - @language | |
7 | - end | |
8 | - | |
9 | - def languages=(languages) | |
10 | - @language = languages | |
11 | - end | |
12 | - | |
13 | - def language=(value) | |
14 | - @language = to_entity_array(value) | |
15 | - end | |
16 | - | |
17 | -end |
plugins/mezuro/lib/kalibro/entities/project.rb
... | ... | @@ -1,12 +0,0 @@ |
1 | -class Kalibro::Entities::Project < Kalibro::Entities::Entity | |
2 | - | |
3 | - attr_accessor :name, :license, :description, :repository, :configuration_name, :state, :error | |
4 | - | |
5 | - def repository=(value) | |
6 | - @repository = to_entity(value, Kalibro::Entities::Repository) | |
7 | - end | |
8 | - | |
9 | - def error=(value) | |
10 | - @error = to_entity(value, Kalibro::Entities::Error) | |
11 | - end | |
12 | -end |
plugins/mezuro/lib/kalibro/entities/repository.rb
plugins/mezuro/lib/kalibro/model.rb
... | ... | @@ -27,6 +27,7 @@ class Kalibro::Model |
27 | 27 | |
28 | 28 | def convert_to_hash(value) |
29 | 29 | return value if value.nil? |
30 | + return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array) | |
30 | 31 | return value.to_hash if value.is_a?(Kalibro::Model) |
31 | 32 | value |
32 | 33 | end |
... | ... | @@ -48,4 +49,13 @@ class Kalibro::Model |
48 | 49 | response.to_hash["#{action}_response".to_sym] |
49 | 50 | end |
50 | 51 | |
52 | + def to_objects_array(value, model_class = nil) | |
53 | + array = value.kind_of?(Array) ? value : [value] | |
54 | + array.each.collect { |element| to_object(element, model_class) } | |
55 | + end | |
56 | + | |
57 | + def to_object(value, model_class) | |
58 | + value.kind_of?(Hash) ? model_class.new(value) : value | |
59 | + end | |
60 | + | |
51 | 61 | end | ... | ... |
... | ... | @@ -0,0 +1,17 @@ |
1 | +class Kalibro::NativeMetric < Kalibro::Metric | |
2 | + | |
3 | + attr_accessor :origin, :language | |
4 | + | |
5 | + def languages | |
6 | + @language | |
7 | + end | |
8 | + | |
9 | + def languages=(languages) | |
10 | + @language = languages | |
11 | + end | |
12 | + | |
13 | + def language=(value) | |
14 | + @language = to_objects_array(value) | |
15 | + end | |
16 | + | |
17 | +end | ... | ... |
plugins/mezuro/lib/kalibro/project.rb
plugins/mezuro/test/fixtures/base_tool_fixtures.rb
... | ... | @@ -2,16 +2,11 @@ require File.dirname(__FILE__) + '/native_metric_fixtures' |
2 | 2 | |
3 | 3 | class BaseToolFixtures |
4 | 4 | |
5 | - def self.analizo | |
6 | - base_tool = Kalibro::Entities::BaseTool.new | |
7 | - base_tool.name = 'Analizo' | |
8 | - base_tool.supported_metrics = [ | |
9 | - NativeMetricFixtures.total_cof, | |
10 | - NativeMetricFixtures.amloc] | |
11 | - base_tool | |
5 | + def self.base_tool | |
6 | + Kalibro::BaseTool.new base_tool_hash | |
12 | 7 | end |
13 | 8 | |
14 | - def self.analizo_hash | |
9 | + def self.base_tool_hash | |
15 | 10 | {:name => 'Analizo', :supported_metric => [ |
16 | 11 | NativeMetricFixtures.total_cof_hash, |
17 | 12 | NativeMetricFixtures.amloc_hash]} | ... | ... |
plugins/mezuro/test/fixtures/native_metric_fixtures.rb
1 | 1 | class NativeMetricFixtures |
2 | 2 | |
3 | 3 | def self.total_cof |
4 | - total_cof = Kalibro::Entities::NativeMetric.new | |
5 | - total_cof.name = 'Total Coupling Factor' | |
6 | - total_cof.scope = 'APPLICATION' | |
7 | - total_cof.origin = 'Analizo' | |
8 | - total_cof.languages = ['JAVA'] | |
9 | - total_cof | |
4 | + Kalibro::NativeMetric.new total_cof_hash | |
10 | 5 | end |
11 | 6 | |
12 | 7 | def self.total_cof_hash |
... | ... | @@ -14,12 +9,7 @@ class NativeMetricFixtures |
14 | 9 | end |
15 | 10 | |
16 | 11 | def self.amloc |
17 | - total_cof = Kalibro::Entities::NativeMetric.new | |
18 | - total_cof.name = 'Average Method LOC' | |
19 | - total_cof.scope = 'CLASS' | |
20 | - total_cof.origin = 'Analizo' | |
21 | - total_cof.languages = ['JAVA'] | |
22 | - total_cof | |
12 | + Kalibro::NativeMetric.new amloc_hash | |
23 | 13 | end |
24 | 14 | |
25 | 15 | def self.amloc_hash | ... | ... |
... | ... | @@ -0,0 +1,36 @@ |
1 | +require "test_helper" | |
2 | + | |
3 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/base_tool_fixtures" | |
4 | + | |
5 | +class BaseToolTest < ActiveSupport::TestCase | |
6 | + | |
7 | + def setup | |
8 | + @hash = BaseToolFixtures.base_tool_hash | |
9 | + @base_tool = BaseToolFixtures.base_tool | |
10 | + end | |
11 | + | |
12 | + should 'create base tool from hash' do | |
13 | + assert_equal @base_tool.name, Kalibro::BaseTool.new(@hash).name | |
14 | + end | |
15 | + | |
16 | + should 'convert base tool to hash' do | |
17 | + assert_equal @hash, @base_tool.to_hash | |
18 | + end | |
19 | + | |
20 | + should 'get base tool names' do | |
21 | + names = ['Analizo', 'Checkstyle'] | |
22 | + Kalibro::BaseTool.expects(:request).with("BaseTool", :get_base_tool_names).returns({:base_tool_name => names}) | |
23 | + assert_equal names, Kalibro::BaseTool.all_names | |
24 | + end | |
25 | + | |
26 | + should 'get base tool by name' do | |
27 | + request_body = {:base_tool_name => @base_tool.name} | |
28 | + Kalibro::BaseTool.expects(:request).with("BaseTool", :get_base_tool, request_body).returns({:base_tool => @hash}) | |
29 | + assert_equal @base_tool.name, Kalibro::BaseTool.find_by_name(@base_tool.name).name | |
30 | + end | |
31 | + | |
32 | + should 'get base tool metrics' do | |
33 | + assert_equal @base_tool.supported_metrics[0].name, @base_tool.metric('Total Coupling Factor').name | |
34 | + end | |
35 | + | |
36 | +end | ... | ... |
plugins/mezuro/test/unit/kalibro/client/base_tool_client_test.rb
... | ... | @@ -1,37 +0,0 @@ |
1 | -require "test_helper" | |
2 | - | |
3 | -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/base_tool_fixtures" | |
4 | - | |
5 | -class BaseToolClientTest < ActiveSupport::TestCase | |
6 | - | |
7 | - def setup | |
8 | - @port = mock | |
9 | - Kalibro::Client::Port.expects(:new).with('BaseTool').returns(@port) | |
10 | - @client = Kalibro::Client::BaseToolClient.new | |
11 | - end | |
12 | - | |
13 | - should 'get base tool names (zero)' do | |
14 | - @port.expects(:request).with(:get_base_tool_names).returns({}) | |
15 | - assert_equal [], @client.base_tool_names | |
16 | - end | |
17 | - | |
18 | - should 'get base tool names (one)' do | |
19 | - name = 'Analizo' | |
20 | - @port.expects(:request).with(:get_base_tool_names).returns({:base_tool_name => name}) | |
21 | - assert_equal [name], @client.base_tool_names | |
22 | - end | |
23 | - | |
24 | - should 'get base tool names' do | |
25 | - names = ['Analizo', 'Checkstyle'] | |
26 | - @port.expects(:request).with(:get_base_tool_names).returns({:base_tool_name => names}) | |
27 | - assert_equal names, @client.base_tool_names | |
28 | - end | |
29 | - | |
30 | - should 'get base tool by name' do | |
31 | - analizo = BaseToolFixtures.analizo | |
32 | - request_body = {:base_tool_name => 'Analizo'} | |
33 | - @port.expects(:request).with(:get_base_tool, request_body).returns({:base_tool => analizo.to_hash}) | |
34 | - assert_equal analizo, @client.base_tool('Analizo') | |
35 | - end | |
36 | - | |
37 | -end | |
38 | 0 | \ No newline at end of file |
plugins/mezuro/test/unit/kalibro/entities/base_tool_test.rb
... | ... | @@ -1,20 +0,0 @@ |
1 | -require "test_helper" | |
2 | - | |
3 | -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/base_tool_fixtures" | |
4 | - | |
5 | -class BaseToolTest < ActiveSupport::TestCase | |
6 | - | |
7 | - def setup | |
8 | - @hash = BaseToolFixtures.analizo_hash | |
9 | - @base_tool = BaseToolFixtures.analizo | |
10 | - end | |
11 | - | |
12 | - should 'create base tool from hash' do | |
13 | - assert_equal @base_tool, Kalibro::Entities::BaseTool.from_hash(@hash) | |
14 | - end | |
15 | - | |
16 | - should 'convert base tool to hash' do | |
17 | - assert_equal @hash, @base_tool.to_hash | |
18 | - end | |
19 | - | |
20 | -end | |
21 | 0 | \ No newline at end of file |
plugins/mezuro/test/unit/kalibro/entities/metric_test.rb
... | ... | @@ -1,24 +0,0 @@ |
1 | -require "test_helper" | |
2 | - | |
3 | -class MetricTest < ActiveSupport::TestCase | |
4 | - | |
5 | - def setup | |
6 | - name = 'MetricTest metric' | |
7 | - scope = 'METHOD' | |
8 | - description = 'Metric created for testing' | |
9 | - @hash = {:name => name, :scope => scope, :description => description} | |
10 | - @metric = Kalibro::Entities::Metric.new | |
11 | - @metric.name = name | |
12 | - @metric.scope = scope | |
13 | - @metric.description = description | |
14 | - end | |
15 | - | |
16 | - should 'create metric from hash' do | |
17 | - assert_equal @metric, Kalibro::Entities::Metric.from_hash(@hash) | |
18 | - end | |
19 | - | |
20 | - should 'convert metric to hash' do | |
21 | - assert_equal @hash, @metric.to_hash | |
22 | - end | |
23 | - | |
24 | -end | |
25 | 0 | \ No newline at end of file |
plugins/mezuro/test/unit/kalibro/entities/native_metric_test.rb
... | ... | @@ -1,20 +0,0 @@ |
1 | -require "test_helper" | |
2 | - | |
3 | -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/native_metric_fixtures" | |
4 | - | |
5 | -class NativeMetricTest < ActiveSupport::TestCase | |
6 | - | |
7 | - def setup | |
8 | - @hash = NativeMetricFixtures.amloc_hash | |
9 | - @metric = NativeMetricFixtures.amloc | |
10 | - end | |
11 | - | |
12 | - should 'create native metric from hash' do | |
13 | - assert_equal @metric, Kalibro::Entities::NativeMetric.from_hash(@hash) | |
14 | - end | |
15 | - | |
16 | - should 'convert native metric to hash' do | |
17 | - assert_equal @hash, @metric.to_hash | |
18 | - end | |
19 | - | |
20 | -end | |
21 | 0 | \ No newline at end of file |
... | ... | @@ -0,0 +1,20 @@ |
1 | +require "test_helper" | |
2 | + | |
3 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/native_metric_fixtures" | |
4 | + | |
5 | +class NativeMetricTest < ActiveSupport::TestCase | |
6 | + | |
7 | + def setup | |
8 | + @hash = NativeMetricFixtures.amloc_hash | |
9 | + @metric = NativeMetricFixtures.amloc | |
10 | + end | |
11 | + | |
12 | + should 'create native metric from hash' do | |
13 | + assert_equal @hash[:name], Kalibro::NativeMetric.new(@hash).name | |
14 | + end | |
15 | + | |
16 | + should 'convert native metric to hash' do | |
17 | + assert_equal @hash, @metric.to_hash | |
18 | + end | |
19 | + | |
20 | +end | ... | ... |