Commit 4110f11de28302ec170da3ee9c18b466cc9430b3

Authored by João M. M. da Silva + Paulo Meirelles
Committed by João M. M. da Silva
1 parent 243f282d

[Mezuro] Completed metric configuration model, fixtures and tests.

plugins/mezuro/lib/kalibro/metric_configuration.rb
1 1 class Kalibro::MetricConfiguration < Kalibro::Model
2 2  
3   - NATIVE_TYPE='native'
4   - COMPOUND_TYPE='compound'
5   -
6   - attr_accessor :metric, :code, :weight, :aggregation_form, :range, :configuration_name
  3 + attr_accessor :id, :code, :metric, :base_tool_name, :weight, :aggregation_form, :reading_group_id, :configuration_id
7 4  
8 5 def metric=(value)
9   - if value.kind_of?(Hash)
10   - @metric = native?(value) ? Kalibro::NativeMetric.to_object(value) : Kalibro::CompoundMetric.to_object(value)
11   - else
12   - @metric = value
13   - end
  6 + @metric = Kalibro::Metric.to_object(value)
14 7 end
15 8  
16 9 def weight=(value)
17 10 @weight = value.to_f
18 11 end
19 12  
20   - def range=(value)
21   - @range = Kalibro::Range.to_objects_array value
22   - end
23   -
24   - def add_range(new_range)
25   - @range = [] if @range.nil?
26   - @range << new_range
27   - end
28   -
29   - def ranges
30   - @range
31   - end
32   -
33   - def ranges=(ranges)
34   - @range = ranges
35   - end
36   -
37 13 def update_attributes(attributes={})
38 14 attributes.each { |field, value| send("#{field}=", value) if self.class.is_valid?(field) }
39 15 save
40 16 end
41 17  
42   - def self.find_by_configuration_name_and_metric_name(configuration_name, metric_name)
43   - metric_configuration = new request("MetricConfiguration", :get_metric_configuration, {
44   - :configuration_name => configuration_name,
45   - :metric_name => metric_name
46   - })[:metric_configuration]
47   - metric_configuration.configuration_name = configuration_name
48   - metric_configuration
  18 + def to_hash
  19 + super :except => [:configuration_id]
49 20 end
50 21  
51   - def destroy
52   - begin
53   - self.class.request("MetricConfiguration", :remove_metric_configuration, {
54   - :configuration_name => configuration_name,
55   - :metric_name=> metric.name
56   - })
57   - rescue Exception => exception
58   - add_error exception
59   - end
60   - end
61   -
62   - def to_hash
63   - super :except => [:configuration_name]
  22 + def self.metric_configurations_of(configuration_id)
  23 + hash = request("MetricConfiguration", :metric_configurations_of, {:configuration_id => configuration_id})
  24 + hash[:metric_configuration].to_a.map { |metric_configuration| new metric_configuration }
64 25 end
65 26  
66 27 private
67 28  
68   - def native?(value)
69   - value.has_key?(:origin) ? true : false
70   - end
71   -
72 29 def save_params
73   - {:metric_configuration => to_hash, :configuration_name => configuration_name}
  30 + {:metric_configuration => self.to_hash, :configuration_id => self.configuration_id}
74 31 end
75 32  
76 33 end
... ...
plugins/mezuro/test/fixtures/metric_configuration_fixtures.rb
1   -require File.dirname(__FILE__) + '/compound_metric_fixtures'
2   -require File.dirname(__FILE__) + '/native_metric_fixtures'
3   -require File.dirname(__FILE__) + '/range_fixtures'
  1 +require File.dirname(__FILE__) + '/metric_fixtures'
4 2  
5 3 class MetricConfigurationFixtures
6 4  
7 5 def self.amloc_metric_configuration
8 6 amloc = Kalibro::MetricConfiguration.new amloc_metric_configuration_hash
9   - amloc.configuration_name = "Sample Configuration"
10   - amloc
11   - end
12   -
13   - def self.metric_configuration_without_ranges
14   - amloc = Kalibro::MetricConfiguration.new
15   - {
16   - :metric => NativeMetricFixtures.amloc_hash,
17   - :code => 'amloc',
18   - :weight => 1.0,
19   - :aggregation_form => 'AVERAGE'
20   - }
21   - amloc.configuration_name = "Sample Configuration"
  7 + amloc.configuration_id = 13
22 8 amloc
23 9 end
24 10  
25 11 def self.sc_metric_configuration
26 12 sc = Kalibro::MetricConfiguration.new sc_metric_configuration_hash
27   - sc.configuration_name = "Sample Configuration"
  13 + sc.configuration_id = 13
28 14 sc
29 15 end
  16 +
  17 + def self.created_metric_configuration
  18 + Kalibro::MetricConfiguration.new({
  19 + :code => 'amloc',
  20 + :metric => MetricFixtures.amloc_hash,
  21 + :base_tool_name => MetricFixtures.amloc_hash[:origin],
  22 + :weight => 1.0,
  23 + :aggregation_form => 'AVERAGE',
  24 + :reading_group_id => 31,
  25 + :configuration_id => 13
  26 + })
  27 + end
30 28  
31 29 def self.amloc_metric_configuration_hash
32   - {:metric => NativeMetricFixtures.amloc_hash, :code => 'amloc', :weight => 1.0,
  30 + {
  31 + :id => 42,
  32 + :code => 'amloc',
  33 + :metric => MetricFixtures.amloc_hash,
  34 + :base_tool_name => MetricFixtures.amloc_hash[:origin],
  35 + :weight => 1.0,
33 36 :aggregation_form => 'AVERAGE',
34   - :range => [RangeFixtures.range_excellent_hash, RangeFixtures.range_bad_hash],
  37 + :reading_group_id => 31,
35 38 :attributes! => {:metric => {
36 39 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
37   - 'xsi:type' => 'kalibro:nativeMetricXml' }}}
  40 + 'xsi:type' => 'kalibro:metricXml' }}
  41 + }
38 42 end
39 43  
40 44 def self.sc_metric_configuration_hash
41   - {:metric => CompoundMetricFixtures.compound_metric_hash, :code => 'sc', :weight => 1.0, :aggregation_form => 'AVERAGE',
  45 + {
  46 + :id => 42,
  47 + :code => 'sc',
  48 + :metric => MetricFixtures.compound_metric_hash,
  49 + :weight => 1.0,
  50 + :aggregation_form => 'AVERAGE',
  51 + :reading_group_id => 31,
42 52 :attributes! => {:metric => {
43 53 'xmlns:xsi'=> 'http://www.w3.org/2001/XMLSchema-instance',
44   - 'xsi:type' => 'kalibro:compoundMetricXml' }}}
  54 + 'xsi:type' => 'kalibro:metricXml' }}
  55 + }
45 56 end
46 57  
47 58 end
... ...
plugins/mezuro/test/unit/kalibro/metric_configuration_test.rb
1 1 require "test_helper"
2 2  
3 3 require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/metric_configuration_fixtures"
4   -require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/range_fixtures"
5 4  
6 5 class MetricConfigurationTest < ActiveSupport::TestCase
7 6  
8 7 def setup
9 8 @native_metric_configuration = MetricConfigurationFixtures.amloc_metric_configuration
10 9 @native_metric_configuration_hash = MetricConfigurationFixtures.amloc_metric_configuration_hash
11   - @compound_metric_configuration = MetricConfigurationFixtures.sc_metric_configuration
12   - @metric_configuration_without_ranges = MetricConfigurationFixtures.metric_configuration_without_ranges
13   - @excellent_range = RangeFixtures.range_excellent
14   - @bad_range = RangeFixtures.range_bad
  10 + @created_metric_configuration = MetricConfigurationFixtures.created_metric_configuration
15 11 end
16 12  
17 13 should 'create metric configuration from hash' do
... ... @@ -19,51 +15,34 @@ class MetricConfigurationTest &lt; ActiveSupport::TestCase
19 15 end
20 16  
21 17 should 'convert metric configuration to hash' do
22   - assert_equal @native_metric_configuration_hash, @native_metric_configuration.to_hash()
  18 + assert_equal @native_metric_configuration_hash, @native_metric_configuration.to_hash
23 19 end
24 20  
25   - should 'create appropriate metric type' do
26   - assert @native_metric_configuration.metric.instance_of?(Kalibro::NativeMetric)
27   - assert @compound_metric_configuration.metric.instance_of?(Kalibro::CompoundMetric)
  21 + should 'get all metric configurations of a configuration' do
  22 + configuration_id = 13
  23 + request_body = { :configuration_id => configuration_id }
  24 + response_hash = {:metric_configuration => [@native_metric_configuration_hash]}
  25 + Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :metric_configurations_of, request_body).returns(response_hash)
  26 + assert_equal @native_metric_configuration.code, Kalibro::MetricConfiguration.metric_configurations_of(configuration_id).first.code
28 27 end
29 28  
30   - should 'add a range to an empty range list' do
31   - @metric_configuration_without_ranges.add_range @excellent_range
32   - assert_equal @metric_configuration_without_ranges.ranges, [@excellent_range]
33   - end
34   -
35   - should 'add a range to an non-empty range list' do
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   - end
40   -
41   - should 'save metric configuration' do
42   - Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :save_metric_configuration, {
43   - :metric_configuration => @native_metric_configuration_hash,
44   - :configuration_name => @native_metric_configuration.configuration_name
45   - })
46   - @native_metric_configuration.save
  29 + should 'return true when metric configuration is saved successfully' do
  30 + id_from_kalibro = 1
  31 + configuration_id = @created_metric_configuration.configuration_id
  32 + Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :save_metric_configuration, {:metric_configuration => @created_metric_configuration.to_hash, :configuration_id => configuration_id}).returns(:metric_configuration_id => id_from_kalibro)
  33 + assert @created_metric_configuration.save
  34 + assert_equal id_from_kalibro, @created_metric_configuration.id
47 35 end
48 36  
49   - should 'get metric configuration by name and configuration name' do
50   - request_body = {
51   - :configuration_name => @native_metric_configuration.configuration_name,
52   - :metric_name => @native_metric_configuration.metric.name
53   - }
54   - response_hash = {:metric_configuration => @native_metric_configuration_hash}
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(@native_metric_configuration.configuration_name,
57   - @native_metric_configuration.metric.name)
58   - assert_equal @native_metric_configuration.code, metric_configuration.code
  37 + should 'return false when metric configuration is not saved successfully' do
  38 + configuration_id = @created_metric_configuration.configuration_id
  39 + Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :save_metric_configuration, {:metric_configuration => @created_metric_configuration.to_hash, :configuration_id => configuration_id}).raises(Exception.new)
  40 + assert !(@created_metric_configuration.save)
  41 + assert_nil @created_metric_configuration.id
59 42 end
60 43  
61   - should 'destroy metric configuration by name' do
62   - request_body = {
63   - :configuration_name => @native_metric_configuration.configuration_name,
64   - :metric_name => @native_metric_configuration.metric.name
65   - }
66   - Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :remove_metric_configuration, request_body)
  44 + should 'destroy metric configuration' do
  45 + Kalibro::MetricConfiguration.expects(:request).with("MetricConfiguration", :delete_metric_configuration, :metric_configuration_id => @native_metric_configuration.id)
67 46 @native_metric_configuration.destroy
68 47 end
69 48  
... ...