Commit 7967704379ae6b6ebb534d9f55fad9f1a0aa1578

Authored by Alessandro Palmeira
2 parents 07e19481 f909e2e2

Merge branch 'update-client' into merging_configuration

plugins/mezuro/lib/kalibro/client/metric_configuration_client.rb 0 → 100644
... ... @@ -0,0 +1,28 @@
  1 +class Kalibro::Client::MetricConfigurationClient
  2 +
  3 + def initialize
  4 + @port = Kalibro::Client::Port.new('MetricConfiguration')
  5 + end
  6 +
  7 + def save(metric_configuration, configuration_name)
  8 + @port.request(:save_metric_configuration, {
  9 + :metric_configuration => metric_configuration.to_hash,
  10 + :configuration_name => configuration_name})
  11 + end
  12 +
  13 + def metric_configuration(configuration_name, metric_name)
  14 + hash = @port.request(:get_metric_configuration, {
  15 + :configuration_name => configuration_name,
  16 + :metric_name => metric_name
  17 + })[:metric_configuration]
  18 + Kalibro::Entities::MetricConfiguration.from_hash(hash)
  19 + end
  20 +
  21 + def remove (configuration_name, metric_name)
  22 + @port.request(:remove_metric_configuration, {
  23 + :configuration_name => configuration_name,
  24 + :metric_name=> metric_name
  25 + })
  26 + end
  27 +
  28 +end
0 29 \ No newline at end of file
... ...
plugins/mezuro/lib/kalibro/client/port.rb
... ... @@ -7,7 +7,8 @@ end
7 7 class Kalibro::Client::Port
8 8  
9 9 def initialize(endpoint)
10   - @client = Savon::Client.new("#{service_address}#{endpoint}Endpoint/?wsdl")
  10 + @endpoint = endpoint
  11 + initialize_client
11 12 end
12 13  
13 14 def service_address
... ... @@ -18,9 +19,20 @@ class Kalibro::Client::Port
18 19 @service_address
19 20 end
20 21  
  22 + def service_address=(address)
  23 + @service_address = address
  24 + initialize_client
  25 + end
  26 +
21 27 def request(action, request_body = nil)
22 28 response = @client.request(:kalibro, action) { soap.body = request_body }
23 29 response.to_hash["#{action}_response".to_sym]
24 30 end
25 31  
  32 + private
  33 +
  34 + def initialize_client
  35 + @client = Savon::Client.new("#{service_address}#{@endpoint}Endpoint/?wsdl")
  36 + end
  37 +
26 38 end
... ...
plugins/mezuro/lib/kalibro/entities/entity.rb
... ... @@ -2,7 +2,7 @@ class Kalibro::Entities::Entity
2 2  
3 3 def self.from_hash(hash)
4 4 entity = self.new
5   - hash.each { |field, value| entity.set(field, value) }
  5 + hash.each { |field, value| entity.set(field, value) if field != :attributes!}
6 6 entity
7 7 end
8 8  
... ... @@ -24,16 +24,16 @@ class Kalibro::Entities::Entity
24 24 fields.each do |field|
25 25 field_value = self.get(field)
26 26 hash[field] = convert_to_hash(field_value) if ! field_value.nil?
  27 + if need_xml_type?(field_value)
  28 + hash = {:attributes! => {}}.merge(hash)
  29 + hash[:attributes!][field.to_sym] = {
  30 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  31 + 'xsi:type' => 'kalibro:' + xml_class_name(field_value) }
  32 + end
27 33 end
28 34 hash
29 35 end
30 36  
31   - def convert_to_hash(value)
32   - return value.collect { |element| convert_to_hash(element) } if value.kind_of?(Array)
33   - return value.to_hash if value.kind_of?(Kalibro::Entities::Entity)
34   - value
35   - end
36   -
37 37 def ==(other)
38 38 begin
39 39 fields.each.inject(true) { |equal, field| equal && (self.get(field) == other.get(field)) }
... ... @@ -42,6 +42,8 @@ class Kalibro::Entities::Entity
42 42 end
43 43 end
44 44  
  45 + protected
  46 +
45 47 def fields
46 48 instance_variable_names.each.collect { |variable| variable.to_s.sub(/@/, '').to_sym }
47 49 end
... ... @@ -50,4 +52,23 @@ class Kalibro::Entities::Entity
50 52 send("#{field}")
51 53 end
52 54  
  55 + def convert_to_hash(value)
  56 + return value.collect { |element| convert_to_hash(element) } if value.is_a?(Array)
  57 + return value.to_hash if value.is_a?(Kalibro::Entities::Entity)
  58 + return 'INF' if value.is_a?(Float) and value.infinite? == 1
  59 + return '-INF' if value.is_a?(Float) and value.infinite? == -1
  60 + value
  61 + end
  62 +
  63 + def need_xml_type?(value)
  64 + value.is_a?(Kalibro::Entities::Entity) and value.class.superclass != Kalibro::Entities::Entity
  65 + end
  66 +
  67 + def xml_class_name(entity)
  68 + xml_name = entity.class.name
  69 + xml_name["Kalibro::Entities::"] = ""
  70 + xml_name[0..0] = xml_name[0..0].downcase
  71 + xml_name + "Xml"
  72 + end
  73 +
53 74 end
... ...
plugins/mezuro/lib/kalibro/entities/metric_configuration.rb
... ... @@ -11,6 +11,10 @@ class Kalibro::Entities::MetricConfiguration < Kalibro::Entities::Entity
11 11 end
12 12 end
13 13  
  14 + def weight=(value)
  15 + @weight = value.to_f
  16 + end
  17 +
14 18 def range=(value)
15 19 @range = to_entity_array(value, Kalibro::Entities::Range)
16 20 end
... ...
plugins/mezuro/lib/kalibro/entities/native_metric.rb
... ... @@ -2,4 +2,12 @@ class Kalibro::Entities::NativeMetric < Kalibro::Entities::Metric
2 2  
3 3 attr_accessor :origin, :language
4 4  
  5 + def languages
  6 + @language
  7 + end
  8 +
  9 + def languages=(languages)
  10 + @language = languages
  11 + end
  12 +
5 13 end
... ...
plugins/mezuro/lib/kalibro/entities/range.rb
... ... @@ -2,4 +2,14 @@ class Kalibro::Entities::Range < Kalibro::Entities::Entity
2 2  
3 3 attr_accessor :beginning, :end, :label, :grade, :color, :comments
4 4  
  5 + def beginning=(value)
  6 + @beginning = value.to_f
  7 + @beginning = -1.0/0.0 if value == "-INF"
  8 + end
  9 +
  10 + def end=(value)
  11 + @end = value.to_f
  12 + @end = 1.0/0.0 if value == "INF"
  13 + end
  14 +
5 15 end
6 16 \ No newline at end of file
... ...
plugins/mezuro/test/fixtures/compound_metric_with_error_fixtures.rb
... ... @@ -10,7 +10,10 @@ class CompoundMetricWithErrorFixtures
10 10 end
11 11  
12 12 def self.create_hash
13   - {:metric => CompoundMetricFixtures.sc_hash, :error => ErrorFixtures.create_hash}
  13 + {:metric => CompoundMetricFixtures.sc_hash, :error => ErrorFixtures.create_hash,
  14 + :attributes! => {:metric => {
  15 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  16 + 'xsi:type' => 'kalibro:compoundMetricXml' }}}
14 17 end
15 18  
16 19 end
... ...
plugins/mezuro/test/fixtures/metric_configuration_fixtures.rb
... ... @@ -25,12 +25,18 @@ class MetricConfigurationFixtures
25 25  
26 26 def self.amloc_configuration_hash
27 27 {:metric => NativeMetricFixtures.amloc_hash, :code => 'amloc', :weight => 1.0,
28   - :aggregation_form => 'AVERAGE', :range =>
29   - [RangeFixtures.amloc_excellent_hash, RangeFixtures.amloc_bad_hash]}
  28 + :aggregation_form => 'AVERAGE',
  29 + :range => [RangeFixtures.amloc_excellent_hash, RangeFixtures.amloc_bad_hash],
  30 + :attributes! => {:metric => {
  31 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  32 + 'xsi:type' => 'kalibro:nativeMetricXml' }}}
30 33 end
31 34  
32 35 def self.sc_configuration_hash
33   - {:metric => CompoundMetricFixtures.sc_hash, :code => 'sc', :weight => 1.0, :aggregation_form => 'AVERAGE'}
  36 + {:metric => CompoundMetricFixtures.sc_hash, :code => 'sc', :weight => 1.0, :aggregation_form => 'AVERAGE',
  37 + :attributes! => {:metric => {
  38 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  39 + 'xsi:type' => 'kalibro:compoundMetricXml' }}}
34 40 end
35 41  
36 42 end
... ...
plugins/mezuro/test/fixtures/metric_result_fixtures.rb
... ... @@ -23,11 +23,17 @@ class MetricResultFixtures
23 23  
24 24 def self.amloc_result_hash
25 25 {:metric => NativeMetricFixtures.amloc_hash, :value => 0.0, :descendent_result => [40.0, 42.0],
26   - :range => RangeFixtures.amloc_excellent_hash}
  26 + :range => RangeFixtures.amloc_excellent_hash,
  27 + :attributes! => {:metric => {
  28 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  29 + 'xsi:type' => 'kalibro:nativeMetricXml' }}}
27 30 end
28 31  
29 32 def self.sc_result_hash
30   - {:metric => CompoundMetricFixtures.sc_hash, :value => 1.0, :descendent_result => [2.0, 42.0]}
  33 + {:metric => CompoundMetricFixtures.sc_hash, :value => 1.0, :descendent_result => [2.0, 42.0],
  34 + :attributes! => {:metric => {
  35 + 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
  36 + 'xsi:type' => 'kalibro:compoundMetricXml' }}}
31 37 end
32 38  
33 39 end
... ...
plugins/mezuro/test/fixtures/native_metric_fixtures.rb
... ... @@ -5,12 +5,12 @@ class NativeMetricFixtures
5 5 total_cof.name = 'Total Coupling Factor'
6 6 total_cof.scope = 'APPLICATION'
7 7 total_cof.origin = 'Analizo'
8   - total_cof.language = 'JAVA'
  8 + total_cof.languages = ['JAVA']
9 9 total_cof
10 10 end
11 11  
12 12 def self.total_cof_hash
13   - {:name => 'Total Coupling Factor', :scope => 'APPLICATION', :origin => 'Analizo', :language => 'JAVA'}
  13 + {:name => 'Total Coupling Factor', :scope => 'APPLICATION', :origin => 'Analizo', :language => ['JAVA']}
14 14 end
15 15  
16 16 def self.amloc
... ... @@ -18,12 +18,12 @@ class NativeMetricFixtures
18 18 total_cof.name = 'Average Method LOC'
19 19 total_cof.scope = 'CLASS'
20 20 total_cof.origin = 'Analizo'
21   - total_cof.language = 'JAVA'
  21 + total_cof.languages = ['JAVA']
22 22 total_cof
23 23 end
24 24  
25 25 def self.amloc_hash
26   - {:name => 'Average Method LOC', :scope => 'CLASS', :origin => 'Analizo', :language => 'JAVA'}
  26 + {:name => 'Average Method LOC', :scope => 'CLASS', :origin => 'Analizo', :language => ['JAVA']}
27 27 end
28 28  
29 29 end
30 30 \ No newline at end of file
... ...
plugins/mezuro/test/functional/base_tool_client_test.rb 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +require "test_helper"
  2 +require File.dirname(__FILE__) + '/fake_port'
  3 +
  4 +class BaseToolClientTest < ActiveSupport::TestCase
  5 +
  6 + def setup
  7 + fake_port = FakePort.new('BaseTool')
  8 + Kalibro::Client::Port.expects(:new).with('BaseTool').returns(fake_port)
  9 + @client = Kalibro::Client::BaseToolClient.new
  10 + end
  11 +
  12 + should 'get base tool names' do
  13 + assert_equal ['Analizo', 'Checkstyle'], @client.base_tool_names
  14 + end
  15 +
  16 + should 'get base tool by name' do
  17 + analizo = @client.base_tool('Analizo')
  18 + assert_equal 'Analizo', analizo.name
  19 + assert_equal 'Analizo description', analizo.description
  20 + assert_equal 1, analizo.supported_metrics.size
  21 + metric = analizo.supported_metrics[0]
  22 + assert_equal 'Analizo', metric.origin
  23 + assert_equal 'Analizo metric', metric.name
  24 + assert_equal 'Analizo metric description', metric.description
  25 + assert_equal 'METHOD', metric.scope
  26 + assert_equal ['CPP', 'JAVA'], metric.languages
  27 + end
  28 +
  29 +end
0 30 \ No newline at end of file
... ...
plugins/mezuro/test/functional/fake_port.rb 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +class FakePort < Kalibro::Client::Port
  2 +
  3 + def initialize(endpoint)
  4 + super(endpoint)
  5 + self.service_address = 'http://localhost:8080/KalibroFake/'
  6 + end
  7 +end
... ...
plugins/mezuro/test/functional/metric_configuration_client_test.rb 0 → 100644
... ... @@ -0,0 +1,43 @@
  1 +require "test_helper"
  2 +require File.dirname(__FILE__) + '/fake_port'
  3 +
  4 +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/metric_configuration_fixtures"
  5 +
  6 +class MetricConfigurationClientTest < ActiveSupport::TestCase
  7 +
  8 + def setup
  9 + fake_port = FakePort.new('MetricConfiguration')
  10 + Kalibro::Client::Port.expects(:new).with('MetricConfiguration').returns(fake_port)
  11 + @client = Kalibro::Client::MetricConfigurationClient.new
  12 + end
  13 +
  14 + should 'save metric configuration' do
  15 + configuration = MetricConfigurationFixtures.amloc_configuration
  16 + @client.save(configuration, 'Configuration X')
  17 + end
  18 +
  19 + should 'get metric configuration by name' do
  20 + configuration = @client.metric_configuration('C', 'native')
  21 + assert_equal 'metricOfC', configuration.code
  22 + assert_equal 1.0, configuration.weight
  23 + assert_equal 'AVERAGE', configuration.aggregation_form
  24 + assert_equal 1, configuration.ranges.size
  25 +
  26 + range = configuration.ranges[0]
  27 + assert_equal -1.0/0.0, range.beginning
  28 + assert_equal 1.0/0.0, range.end
  29 +
  30 + metric = configuration.metric
  31 + puts metric
  32 + assert metric.is_a?(Kalibro::Entities::NativeMetric)
  33 + assert_equal 'Metric of C', metric.name
  34 + assert_equal 'METHOD', metric.scope
  35 + assert_equal ['JAVA'], metric.languages
  36 + assert_equal 'Metric of C description', metric.description
  37 + end
  38 +
  39 + should 'remove metric configuration by name' do
  40 + @client.remove('Configuration X', 'Metric X')
  41 + end
  42 +
  43 +end
0 44 \ No newline at end of file
... ...
plugins/mezuro/test/unit/kalibro/client/metric_configuration_client_test.rb 0 → 100644
... ... @@ -0,0 +1,44 @@
  1 +require "test_helper"
  2 +
  3 +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/metric_configuration_fixtures"
  4 +
  5 +class MetricConfigurationClientTest < ActiveSupport::TestCase
  6 +
  7 + def setup
  8 + @port = mock
  9 + Kalibro::Client::Port.expects(:new).with('MetricConfiguration').returns(@port)
  10 + @client = Kalibro::Client::MetricConfigurationClient.new
  11 + end
  12 +
  13 + should 'save metric configuration' do
  14 + configuration = MetricConfigurationFixtures.amloc_configuration
  15 + @port.expects(:request).with(:save_metric_configuration, {
  16 + :metric_configuration => configuration.to_hash,
  17 + :configuration_name => 'x'
  18 + })
  19 + @client.save(configuration, 'x')
  20 + end
  21 +
  22 + should 'get metric configuration by name' do
  23 + configuration = MetricConfigurationFixtures.amloc_configuration
  24 + request_body = {
  25 + :configuration_name => 'configuration.name',
  26 + :metric_name => configuration.metric.name
  27 + }
  28 + response_hash = {:metric_configuration => configuration.to_hash}
  29 + @port.expects(:request).with(:get_metric_configuration, request_body).returns(response_hash)
  30 + assert_equal configuration, @client.metric_configuration('configuration.name', configuration.metric.name)
  31 + end
  32 +
  33 + should 'remove metric configuration by name' do
  34 + metric_name = 'MetricConfigurationClientTest'
  35 + configuration_name = 'xxxx'
  36 + request_body = {
  37 + :configuration_name => configuration_name,
  38 + :metric_name => metric_name
  39 + }
  40 + @port.expects(:request).with(:remove_metric_configuration, request_body)
  41 + @client.remove(configuration_name, metric_name)
  42 + end
  43 +
  44 +end
0 45 \ No newline at end of file
... ...