Commit 352364638d05be77b44fc654fb8cf1328fc648b5

Authored by Paulo Meireles
1 parent 189563d8

[Mezuro] refactoring kalibro entity to new design

Conflicts:

	plugins/mezuro/lib/kalibro/client/port.rb
plugins/mezuro/lib/kalibro/client/port.rb
... ... @@ -1,38 +0,0 @@
1   -require 'savon'
2   -
3   -Savon.configure do |config|
4   - config.log = HTTPI.log = (RAILS_ENV == 'development')
5   -end
6   -
7   -class Kalibro::Client::Port
8   -
9   - def initialize(endpoint)
10   - @endpoint = endpoint
11   - initialize_client
12   - end
13   -
14   - def service_address
15   - if @service_address.nil?
16   - service_file = "#{RAILS_ROOT}/plugins/mezuro/SERVICE"
17   - File.open(service_file).each_line{ | line | @service_address = line }
18   - end
19   - @service_address
20   - end
21   -
22   - def service_address=(address)
23   - @service_address = address
24   - initialize_client
25   - end
26   -
27   - def request(action, request_body = nil)
28   - response = @client.request(:kalibro, action) { soap.body = request_body }
29   - response.to_hash["#{action}_response".to_sym]
30   - end
31   -
32   - private
33   -
34   - def initialize_client
35   - @client = Savon::Client.new("#{service_address}#{@endpoint}Endpoint/?wsdl")
36   - end
37   -
38   -end
plugins/mezuro/lib/kalibro/entities/entity.rb
... ... @@ -1,84 +0,0 @@
1   -class Kalibro::Entities::Entity
2   -
3   - def self.from_hash(hash)
4   - entity = self.new
5   - hash.each { |field, value| entity.set(field, value) if is_valid?(field) }
6   - entity
7   - end
8   -
9   - def self.is_valid?(field)
10   - field.to_s[0] != '@' and field != :attributes!
11   - end
12   -
13   - def self.date_with_milliseconds(date)
14   - milliseconds = "." + (date.sec_fraction * 60 * 60 * 24 * 1000).to_s
15   - date.to_s[0..18] + milliseconds + date.to_s[19..-1]
16   - end
17   -
18   - def set(field, value)
19   - send("#{field}=", value) if not field.to_s.start_with? '@'
20   - end
21   -
22   - def to_entity_array(value, entity_class = nil)
23   - array = value.kind_of?(Array) ? value : [value]
24   - array.each.collect { |element| to_entity(element, entity_class) }
25   - end
26   -
27   - def to_entity(value, entity_class)
28   - value.kind_of?(Hash) ? entity_class.from_hash(value) : value
29   - end
30   -
31   - def to_hash
32   - hash = Hash.new
33   - fields.each do |field|
34   - field_value = self.get(field)
35   - hash[field] = convert_to_hash(field_value) if ! field_value.nil?
36   - if need_xml_type?(field_value)
37   - hash = {:attributes! => {}}.merge(hash)
38   - hash[:attributes!][field.to_sym] = {
39   - 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
40   - 'xsi:type' => 'kalibro:' + xml_class_name(field_value) }
41   - end
42   - end
43   - hash
44   - end
45   -
46   - def ==(other)
47   - begin
48   - fields.each.inject(true) { |equal, field| equal && (self.get(field) == other.get(field)) }
49   - rescue NoMethodError
50   - false
51   - end
52   - end
53   -
54   - protected
55   -
56   - def fields
57   - instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '').to_sym }
58   - end
59   -
60   - def get(field)
61   - send("#{field}")
62   - end
63   -
64   - def convert_to_hash(value)
65   - return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array)
66   - return value.to_hash if value.is_a?(Kalibro::Entities::Entity)
67   - return self.class.date_with_milliseconds(value) if value.is_a?(DateTime)
68   - return 'INF' if value.is_a?(Float) and value.infinite? == 1
69   - return '-INF' if value.is_a?(Float) and value.infinite? == -1
70   - value
71   - end
72   -
73   - def need_xml_type?(value)
74   - value.is_a?(Kalibro::Entities::Entity) and value.class.superclass != Kalibro::Entities::Entity
75   - end
76   -
77   - def xml_class_name(entity)
78   - xml_name = entity.class.name
79   - xml_name["Kalibro::Entities::"] = ""
80   - xml_name[0..0] = xml_name[0..0].downcase
81   - xml_name + "Xml"
82   - end
83   -
84   -end
plugins/mezuro/lib/kalibro/kalibro.rb
... ... @@ -1,22 +0,0 @@
1   -class Kalibro::Kalibro < Kalibro::Model
2   -
3   - def self.repository_types
4   - request("Kalibro", :get_supported_repository_types)[:repository_type].to_a
5   - end
6   -
7   - def self.process_project(project_name, days = '0')
8   - if days.to_i.zero?
9   - request("Kalibro", :process_project, {:project_name => project_name})
10   - else
11   - request("Kalibro", :process_periodically, {:project_name => project_name, :period_in_days => days})
12   - end
13   - end
14   -
15   - def self.process_period(project_name)
16   - request("Kalibro", :get_process_period, {:project_name => project_name})[:period]
17   - end
18   -
19   - def self.cancel_periodic_process(project_name)
20   - request("Kalibro", :cancel_periodic_process, {:project_name => project_name})
21   - end
22   -end
plugins/mezuro/lib/kalibro/metric_configuration.rb
... ... @@ -7,8 +7,7 @@ class Kalibro::MetricConfiguration &lt; Kalibro::Model
7 7  
8 8 def metric=(value)
9 9 if value.kind_of?(Hash)
10   - @metric = Kalibro::NativeMetric.to_object(value) if value.has_key?(:origin)
11   - @metric = Kalibro::CompoundMetric.to_object(value) if value.has_key?(:script)
  10 + @metric = native?(value) ? Kalibro::NativeMetric.to_object(value) : Kalibro::CompoundMetric.to_object(value)
12 11 else
13 12 @metric = value
14 13 end
... ... @@ -66,11 +65,11 @@ class Kalibro::MetricConfiguration &lt; Kalibro::Model
66 65 :metric_name=> metric.name
67 66 })
68 67 end
69   -
  68 +
70 69 def to_hash
71 70 hash = Hash.new
72 71 fields.each do |field|
73   - if !(field == :configuration_name)
  72 + if (field != :configuration_name)
74 73 field_value = send(field)
75 74 hash[field] = convert_to_hash(field_value)
76 75 if field_value.is_a?(Kalibro::Model)
... ... @@ -84,4 +83,10 @@ class Kalibro::MetricConfiguration &lt; Kalibro::Model
84 83 hash
85 84 end
86 85  
  86 + private
  87 +
  88 + def native?(value)
  89 + value.has_key?(:origin) ? true : false
  90 + end
  91 +
87 92 end
... ...
plugins/mezuro/lib/kalibro/metric_result.rb
... ... @@ -4,16 +4,12 @@ class Kalibro::MetricResult &lt; Kalibro::Model
4 4  
5 5 def metric=(value)
6 6 if value.kind_of?(Hash)
7   - compound?(value) ? @metric = Kalibro::CompoundMetric.to_object(value) : @metric = Kalibro::NativeMetric.to_object(value)
  7 + @metric = native?(value) ? Kalibro::NativeMetric.to_object(value) : Kalibro::CompoundMetric.to_object(value)
8 8 else
9 9 @metric = value
10 10 end
11 11 end
12 12  
13   - def compound?(metric)
14   - metric.has_key?(:script)
15   - end
16   -
17 13 def value=(value)
18 14 @value = value.to_f
19 15 end
... ... @@ -34,5 +30,11 @@ class Kalibro::MetricResult &lt; Kalibro::Model
34 30 def descendent_results=(descendent_results)
35 31 @descendent_result = descendent_results
36 32 end
  33 +
  34 + private
  35 +
  36 + def native?(value)
  37 + value.has_key?(:origin) ? true : false
  38 + end
37 39  
38 40 end
... ...
plugins/mezuro/lib/kalibro/project.rb
... ... @@ -44,4 +44,20 @@ class Kalibro::Project &lt; Kalibro::Model
44 44 @error = Kalibro::Error.to_object value
45 45 end
46 46  
  47 + def process_project(days = '0')
  48 + if days.to_i.zero?
  49 + request("Kalibro", :process_project, {:project_name => name})
  50 + else
  51 + request("Kalibro", :process_periodically, {:project_name => name, :period_in_days => days})
  52 + end
  53 + end
  54 +
  55 + def process_period
  56 + request("Kalibro", :get_process_period, {:project_name => name})[:period]
  57 + end
  58 +
  59 + def cancel_periodic_process
  60 + request("Kalibro", :cancel_periodic_process, {:project_name => name})
  61 + end
  62 +
47 63 end
... ...
plugins/mezuro/lib/kalibro/repository.rb
... ... @@ -2,4 +2,8 @@ class Kalibro::Repository &lt; Kalibro::Model
2 2  
3 3 attr_accessor :type, :address, :username, :password
4 4  
  5 + def self.repository_types
  6 + request("Kalibro", :get_supported_repository_types)[:repository_type].to_a
  7 + end
  8 +
5 9 end
... ...
plugins/mezuro/lib/mezuro_plugin/project_content.rb
... ... @@ -80,11 +80,10 @@ Kalibro::ProjectResult.first_result_after(name, date)
80 80 def send_project_to_service
81 81 begin
82 82 Kalibro::Project.create(self)
83   - Kalibro::Kalibro.process_project(name, periodicity_in_days)
  83 + project.process_project(periodicity_in_days)
84 84 rescue Exception => error
85 85 errors.add_to_base(error.message)
86 86 end
87   -
88 87 end
89 88  
90 89 def destroy_project_from_service
... ...
plugins/mezuro/test/unit/kalibro/kalibro_test.rb
... ... @@ -1,34 +0,0 @@
1   -require "test_helper"
2   -
3   -class KalibroClientTest < ActiveSupport::TestCase
4   -
5   - def setup
6   - @name = 'KalibroTest'
7   - end
8   -
9   - should 'get supported repository types' do
10   - types = ['BAZAAR', 'GIT', 'SUBVERSION']
11   - Kalibro::Kalibro.expects(:request).with('Kalibro', :get_supported_repository_types).returns({:repository_type => types})
12   - assert_equal types, Kalibro::Kalibro.repository_types
13   - end
14   -
15   - should 'process project without days' do
16   - Kalibro::Kalibro.expects(:request).with('Kalibro', :process_project, {:project_name => @name})
17   - Kalibro::Kalibro.process_project(@name)
18   - end
19   -
20   - should 'process project with days' do
21   - Kalibro::Kalibro.expects(:request).with('Kalibro', :process_periodically, {:project_name => @name, :period_in_days => "1"})
22   - Kalibro::Kalibro.process_project(@name, "1")
23   - end
24   -
25   - should 'process period' do
26   - Kalibro::Kalibro.expects(:request).with('Kalibro', :get_process_period, {:project_name => @name}).returns({:period => "1"})
27   - assert_equal "1", Kalibro::Kalibro.process_period(@name)
28   - end
29   -
30   - should 'cancel periodic process' do
31   - Kalibro::Kalibro.expects(:request).with("Kalibro", :cancel_periodic_process, {:project_name => @name})
32   - Kalibro::Kalibro.cancel_periodic_process(@name)
33   - end
34   -end
plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb
... ... @@ -6,65 +6,65 @@ require &quot;#{RAILS_ROOT}/plugins/mezuro/test/fixtures/range_fixtures&quot;
6 6 class MetricConfigurationTest < ActiveSupport::TestCase
7 7  
8 8 def setup
9   - @hash = MetricConfigurationFixtures.amloc_metric_configuration_hash
10   - @metric_configuration1 = MetricConfigurationFixtures.amloc_metric_configuration
11   - @metric_configuration2 = MetricConfigurationFixtures.sc_metric_configuration
  9 + @native_metric_configuration = MetricConfigurationFixtures.amloc_metric_configuration
  10 + @native_metric_configuration_hash = MetricConfigurationFixtures.amloc_metric_configuration_hash
  11 + @compound_metric_configuration = MetricConfigurationFixtures.sc_metric_configuration
12 12 @metric_configuration_without_ranges = MetricConfigurationFixtures.metric_configuration_without_ranges
13   - @range1 = RangeFixtures.range_excellent
14   - @range2 = RangeFixtures.range_bad
  13 + @excellent_range = RangeFixtures.range_excellent
  14 + @bad_range = RangeFixtures.range_bad
15 15 end
16 16  
17 17 should 'create metric configuration from hash' do
18   - assert_equal @hash[:code], Kalibro::MetricConfiguration.new(@hash).code
  18 + assert_equal @native_metric_configuration_hash[:code], Kalibro::MetricConfiguration.new(@native_metric_configuration_hash).code
19 19 end
20 20  
21 21 should 'convert metric configuration to hash' do
22   - assert_equal @hash, @metric_configuration1.to_hash
  22 + assert_equal @native_metric_configuration_hash, @native_metric_configuration_hash
23 23 end
24 24  
25 25 should 'create appropriate metric type' do
26   - assert @metric_configuration1.metric.instance_of?(Kalibro::NativeMetric)
27   - assert @metric_configuration2.metric.instance_of?(Kalibro::CompoundMetric)
  26 + assert @native_metric_configuration.metric.instance_of?(Kalibro::NativeMetric)
  27 + assert @compound_metric_configuration.metric.instance_of?(Kalibro::CompoundMetric)
28 28 end
29 29  
30 30 should 'add a range to an empty range list' do
31   - @metric_configuration_without_ranges.add_range @range1
32   - assert_equal @metric_configuration_without_ranges.ranges, [@range1]
  31 + @metric_configuration_without_ranges.add_range @excellent_range
  32 + assert_equal @metric_configuration_without_ranges.ranges, [@excellent_range]
33 33 end
34 34  
35 35 should 'add a range to an non-empty range list' do
36   - @metric_configuration_without_ranges.ranges = [@range1]
37   - @metric_configuration_without_ranges.add_range @range2
38   - assert_equal @metric_configuration_without_ranges.ranges, [@range1, @range2]
  36 + @metric_configuration_without_ranges.ranges = [@excellent_range]
  37 + @metric_configuration_without_ranges.add_range @bad_range
  38 + assert_equal @metric_configuration_without_ranges.ranges, [@excellent_range, @bad_range]
39 39 end
40 40  
41 41 should 'save metric configuration' do
42 42 Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :save_metric_configuration, {
43   - :metric_configuration => @metric_configuration1.to_hash,
44   - :configuration_name => @metric_configuration1.configuration_name
  43 + :metric_configuration => @native_metric_configuration_hash,
  44 + :configuration_name => @native_metric_configuration.configuration_name
45 45 })
46   - @metric_configuration1.save
  46 + @native_metric_configuration.save
47 47 end
48 48  
49 49 should 'get metric configuration by name and configuration name' do
50 50 request_body = {
51   - :configuration_name => @metric_configuration1.configuration_name,
52   - :metric_name => @metric_configuration1.metric.name
  51 + :configuration_name => @native_metric_configuration.configuration_name,
  52 + :metric_name => @native_metric_configuration.metric.name
53 53 }
54   - response_hash = {:metric_configuration => @metric_configuration1.to_hash}
  54 + response_hash = {:metric_configuration => @native_metric_configuration_hash}
55 55 Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :get_metric_configuration, request_body).returns(response_hash)
56   - metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(@metric_configuration1.configuration_name,
57   - @metric_configuration1.metric.name)
58   - assert_equal @metric_configuration1.code, metric_configuration.code
  56 + metric_configuration = Kalibro::MetricConfiguration.find_by_configuration_name_and_metric_name(@native_metric_configuration.configuration_name,
  57 + @native_metric_configuration.metric.name)
  58 + assert_equal @native_metric_configuration.code, metric_configuration.code
59 59 end
60 60  
61 61 should 'destroy metric configuration by name' do
62 62 request_body = {
63   - :configuration_name => @metric_configuration1.configuration_name,
64   - :metric_name => @metric_configuration1.metric.name
  63 + :configuration_name => @native_metric_configuration.configuration_name,
  64 + :metric_name => @native_metric_configuration.metric.name
65 65 }
66 66 Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :remove_metric_configuration, request_body)
67   - @metric_configuration1.destroy
  67 + @native_metric_configuration.destroy
68 68 end
69 69  
70 70 end
... ...
plugins/mezuro/test/unit/kalibro/project_test.rb
... ... @@ -67,5 +67,25 @@ class ProjectTest &lt; ActiveSupport::TestCase
67 67 assert_equal @hash[:repository], hash[:repository]
68 68 assert_equal @hash[:state], hash[:state]
69 69 end
  70 +
  71 + should 'process project without days' do
  72 + @project.expects(:request).with('Kalibro', :process_project, {:project_name => @project.name})
  73 + @project.process_project
  74 + end
  75 +
  76 + should 'process project with days' do
  77 + @project.expects(:request).with('Kalibro', :process_periodically, {:project_name => @project.name, :period_in_days => "1"})
  78 + @project.process_project "1"
  79 + end
  80 +
  81 + should 'process period' do
  82 + @project.expects(:request).with('Kalibro', :get_process_period, {:project_name => @project.name}).returns({:period => "1"})
  83 + assert_equal "1", @project.process_period
  84 + end
  85 +
  86 + should 'cancel periodic process' do
  87 + @project.expects(:request).with("Kalibro", :cancel_periodic_process, {:project_name => @project.name})
  88 + @project.cancel_periodic_process
  89 + end
70 90  
71 91 end
... ...
plugins/mezuro/test/unit/kalibro/repository_test.rb
... ... @@ -17,4 +17,10 @@ class RepositoryTest &lt; ActiveSupport::TestCase
17 17 assert_equal @hash, @repository.to_hash
18 18 end
19 19  
  20 + should 'get supported repository types' do
  21 + types = ['BAZAAR', 'GIT', 'SUBVERSION']
  22 + Kalibro::Repository.expects(:request).with('Kalibro', :get_supported_repository_types).returns({:repository_type => types})
  23 + assert_equal types, Kalibro::Repository.repository_types
  24 + end
  25 +
20 26 end
... ...
plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb
... ... @@ -83,7 +83,8 @@ class ProjectContentTest &lt; ActiveSupport::TestCase
83 83  
84 84 should 'send correct project to service' do
85 85 Kalibro::Project.expects(:create).with(@content).returns(true)
86   - Kalibro::Kalibro.expects(:process_project).with(@content.name, @content.periodicity_in_days)
  86 + Kalibro::Project.expects(:find_by_name).with(@content.name).returns(@project)
  87 + @project.expects(:process_project).with(@content.periodicity_in_days)
87 88 @content.send :send_project_to_service
88 89 end
89 90  
... ...
plugins/mezuro/views/cms/mezuro_plugin/_project_content.html.erb
... ... @@ -2,8 +2,7 @@
2 2  
3 3 <%
4 4 begin
5   - @project = @article.title.nil? ? nil : Kalibro::Client::ProjectClient.project(@article.title)
6   - @kalibro_client = Kalibro::Client::KalibroClient.new
  5 + @project = @article.title.nil? ? nil : Kalibro::Project.project(@article.title)
7 6 rescue
8 7 @project = nil
9 8 end
... ... @@ -25,14 +24,14 @@
25 24  
26 25 <%= f.text_field :description %><br/>
27 26  
28   -<% @repository_types = @kalibro_client.supported_repository_types.sort %>
  27 +<% @repository_types = Kalibro::Repository.repository_types.sort %>
29 28 <% @selected = (@project.nil? ? @repository_types : @project.repository.type) %>
30 29 <%= required labelled_form_field _('Repository type'),
31 30 f.select(:repository_type, @repository_types, {:selected => @selected}) %><br/>
32 31  
33 32 <%= required f.text_field(:repository_url) %><br/>
34 33  
35   -<% @configuration_names = Kalibro::Client::ConfigurationClient.new.configuration_names.sort %>
  34 +<% @configuration_names = Kalibro::Configuration.all_names.sort %>
36 35 <% @selected = (@project.nil? ? @configuration_names[0] : @project.configuration_name) %>
37 36  
38 37 <% if !@project.nil? && !@article.id.nil? %>
... ... @@ -43,6 +42,6 @@
43 42 f.select(:configuration_name, @configuration_names, {:selected => @selected}) %><br/>
44 43 <% end %>
45 44  
46   -<% selected = (@project.nil? ? 0 : @kalibro_client.process_period(@article.title).to_i) %>
  45 +<% selected = (@project.nil? ? 0 : @project.process_period.to_i) %>
47 46 <%= required labelled_form_field _('Periodic Avaliation'),
48 47 f.select(:periodicity_in_days, MezuroPlugin::Helpers::ContentViewerHelper.create_periodicity_options ,{:selected => selected}) %><br/>
... ...