Commit a964974ab8ba43fc899f06543ba54cd63e9ab49d

Authored by Alessandro Palmeira + Eduardo Morais
Committed by João M. M. da Silva
1 parent 7fe4689a

[Mezuro] Refactoring project_content, some methods and tests missing.

plugins/mezuro/lib/kalibro/metric_result.rb
@@ -22,8 +22,8 @@ class Kalibro::MetricResult < Kalibro::Model @@ -22,8 +22,8 @@ class Kalibro::MetricResult < Kalibro::Model
22 request(:metric_results_of, {:module_result_id => module_result_id})[:metric_result].to_a.map {|metric_result| new metric_result} 22 request(:metric_results_of, {:module_result_id => module_result_id})[:metric_result].to_a.map {|metric_result| new metric_result}
23 end 23 end
24 24
25 - def history_of(module_id)  
26 - self.class.request(:history_of, {:metric_name => self.configuration.metric.name, :module_result_id => module_id})[:date_metric_result].to_a.map {|date_metric_result| Kalibro::DateMetricResult.new date_metric_result} 25 + def history_of(module_result_id)
  26 + self.class.request(:history_of, {:metric_name => self.configuration.metric.name, :module_result_id => module_result_id})[:date_metric_result].to_a.map {|date_metric_result| Kalibro::DateMetricResult.new date_metric_result}
27 end 27 end
28 28
29 end 29 end
plugins/mezuro/lib/mezuro_plugin/project_content.rb
@@ -42,6 +42,8 @@ class MezuroPlugin::ProjectContent < Article @@ -42,6 +42,8 @@ class MezuroPlugin::ProjectContent < Article
42 begin 42 begin
43 if Kalibro::Processing.has_ready_processing(repository_id) 43 if Kalibro::Processing.has_ready_processing(repository_id)
44 @processing ||= Kalibro::Processing.last_ready_processing_of(repository_id) 44 @processing ||= Kalibro::Processing.last_ready_processing_of(repository_id)
  45 + else
  46 + @processing = Kalibro::Processing.last_processing_of(repository_id)
45 end 47 end
46 rescue Exception => error 48 rescue Exception => error
47 errors.add_to_base(error.message) 49 errors.add_to_base(error.message)
@@ -51,10 +53,10 @@ class MezuroPlugin::ProjectContent < Article @@ -51,10 +53,10 @@ class MezuroPlugin::ProjectContent < Article
51 53
52 def processing_with_date(repository_id, date) 54 def processing_with_date(repository_id, date)
53 begin 55 begin
54 - if Kalibro::Processing.has_processing_before(repository_id, date) 56 + if Kalibro::Processing.has_processing_after(repository_id, date)
  57 + @processing ||= Kalibro::Processing.first_processing_after(repository_id, date)
  58 + elsif Kalibro::Processing.has_processing_before(repository_id, date)
55 @processing ||= Kalibro::Processing.last_processing_before(repository_id, date) 59 @processing ||= Kalibro::Processing.last_processing_before(repository_id, date)
56 - elsif Kalibro::Processing.has_processing_after(repository_id, date)  
57 - @processing ||= Kalibro::Processing.last_processing_after(repository_id, date)  
58 end 60 end
59 rescue Exception => error 61 rescue Exception => error
60 errors.add_to_base(error.message) 62 errors.add_to_base(error.message)
@@ -65,16 +67,16 @@ class MezuroPlugin::ProjectContent < Article @@ -65,16 +67,16 @@ class MezuroPlugin::ProjectContent < Article
65 def module_result(repository_id, date = nil) 67 def module_result(repository_id, date = nil)
66 @processing ||= date.nil? ? processing(repository_id) : processing_with_date(repository_id, date) 68 @processing ||= date.nil? ? processing(repository_id) : processing_with_date(repository_id, date)
67 begin 69 begin
68 - @module_result ||= Kalibro::ModuleResult.find(@procesing.results_root_id) 70 + @module_result ||= Kalibro::ModuleResult.find(@processing.results_root_id)
69 rescue Exception => error 71 rescue Exception => error
70 errors.add_to_base(error.message) 72 errors.add_to_base(error.message)
71 end 73 end
72 @module_result 74 @module_result
73 end 75 end
74 76
75 - def result_history(module_id) 77 + def result_history(module_result_id)
76 begin 78 begin
77 - @result_history ||= Kalibro::MetricResult.history_of(module_id) 79 + @result_history ||= Kalibro::MetricResult.history_of(module_result_id)
78 rescue Exception => error 80 rescue Exception => error
79 errors.add_to_base(error.message) 81 errors.add_to_base(error.message)
80 end 82 end
@@ -83,20 +85,24 @@ class MezuroPlugin::ProjectContent < Article @@ -83,20 +85,24 @@ class MezuroPlugin::ProjectContent < Article
83 def description=(value) 85 def description=(value)
84 @description=value 86 @description=value
85 end 87 end
  88 +
  89 + def description
  90 + @description
  91 + end
86 92
87 def repositories=(value) 93 def repositories=(value)
88 @repositories = value.kind_of?(Array) ? value : [value] 94 @repositories = value.kind_of?(Array) ? value : [value]
89 - @repositories = @repositories.map { |element| to_object(element) }  
90 - end  
91 -  
92 - def self.to_object value  
93 - value.kind_of?(Hash) ? Kalibro::Repository.new(value) : value 95 + @repositories = @repositories.map { |element| to_repository(element) }
94 end 96 end
95 97
96 after_save :send_project_to_service 98 after_save :send_project_to_service
97 after_destroy :destroy_project_from_service 99 after_destroy :destroy_project_from_service
98 100
99 private 101 private
  102 +
  103 + def self.to_repository value
  104 + value.kind_of?(Hash) ? Kalibro::Repository.new(value) : value
  105 + end
100 106
101 def validate_repository_url 107 def validate_repository_url
102 if(@repositories.nil? || repository_url == "") 108 if(@repositories.nil? || repository_url == "")
plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb
@@ -5,15 +5,19 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/project_content_fixtures" @@ -5,15 +5,19 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/project_content_fixtures"
5 require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/processing_fixtures" 5 require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/processing_fixtures"
6 require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/module_fixtures" 6 require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/module_fixtures"
7 require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/module_result_fixtures" 7 require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/module_result_fixtures"
  8 +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/date_metric_result_fixtures"
8 9
9 class ProjectContentTest < ActiveSupport::TestCase 10 class ProjectContentTest < ActiveSupport::TestCase
10 11
11 def setup 12 def setup
12 - @project = ProjectFixtures.project  
13 @project_content = ProjectContentFixtures.project_content 13 @project_content = ProjectContentFixtures.project_content
  14 + @project = ProjectFixtures.project
  15 + @repository = RepositoryFixtures.repository
14 @processing = ProcessingFixtures.processing 16 @processing = ProcessingFixtures.processing
  17 + @date = @processing.date
15 @module = ModuleFixtures.module 18 @module = ModuleFixtures.module
16 @module_result = ModuleResultFixtures.module_result 19 @module_result = ModuleResultFixtures.module_result
  20 + @date_metric_result = DateMetricResultFixtures.date_metric_result
17 end 21 end
18 22
19 should 'provide proper short description' do 23 should 'provide proper short description' do
@@ -33,64 +37,87 @@ class ProjectContentTest &lt; ActiveSupport::TestCase @@ -33,64 +37,87 @@ class ProjectContentTest &lt; ActiveSupport::TestCase
33 assert_equal @project, @project_content.project 37 assert_equal @project, @project_content.project
34 end 38 end
35 39
36 - should 'add error when the project does not exist' do 40 + should 'add error to base when the project does not exist' do
37 Kalibro::Project.expects(:find).with(@project.id).raises(Kalibro::Errors::RecordNotFound) 41 Kalibro::Project.expects(:find).with(@project.id).raises(Kalibro::Errors::RecordNotFound)
  42 + assert_nil @project_content.errors[:base]
38 @project_content.project 43 @project_content.project
39 -  
40 - assert_not_nil @project_content.errors 44 + assert_not_nil @project_content.errors[:base]
41 end 45 end
42 46
43 -=begin  
44 should 'get repositories of the project from service' do 47 should 'get repositories of the project from service' do
45 - 48 + Kalibro::Repository.expects(:repositories_of).with(@project.id).returns([@repository])
  49 + assert_equal [@repository], @project_content.repositories
  50 + end
  51 +
  52 + should 'add error to base when getting the repositories of a project that does not exist' do
  53 + Kalibro::Repository.expects(:repositories_of).with(@project.id).raises(Kalibro::Errors::RecordNotFound)
  54 + assert_nil @project_content.errors[:base]
  55 + @project_content.repositories
  56 + assert_not_nil @project_content.errors[:base]
  57 + end
  58 +
  59 + should 'get processing of a repository' do
  60 + Kalibro::Processing.expects(:has_ready_processing).with(@repository.id).returns(true)
  61 + Kalibro::Processing.expects(:last_ready_processing_of).with(@repository.id).returns(@processing)
  62 + assert_equal @processing, @project_content.processing(@repository.id)
46 end 63 end
  64 +
  65 + should 'get not ready processing of a repository' do
  66 + Kalibro::Processing.expects(:has_ready_processing).with(@repository.id).returns(false)
  67 + Kalibro::Processing.expects(:last_processing_of).with(@repository.id).returns(@processing)
  68 + assert_equal @processing, @project_content.processing(@repository.id)
  69 + end
  70 +
  71 + should 'get processing of a repository after date' do
  72 + Kalibro::Processing.expects(:has_processing_after).with(@repository.id, @date).returns(true)
  73 + Kalibro::Processing.expects(:first_processing_after).with(@repository.id, @date).returns(@processing)
  74 + assert_equal @processing, @project_content.processing_with_date(@repository.id, @date)
  75 + end
  76 +
  77 + should 'get processing of a repository before date' do
  78 + Kalibro::Processing.expects(:has_processing_after).with(@repository.id, @date).returns(false)
  79 + Kalibro::Processing.expects(:has_processing_before).with(@repository.id, @date).returns(true)
  80 + Kalibro::Processing.expects(:last_processing_before).with(@repository.id, @date).returns(@processing)
  81 + assert_equal @processing, @project_content.processing_with_date(@repository.id, @date)
  82 + end
  83 +
  84 + should 'get module result' do
  85 + @project_content.expects(:processing).with(@repository.id).returns(@processing)
  86 + Kalibro::ModuleResult.expects(:find).with(@processing.results_root_id).returns(@module_result)
  87 + assert_equal @module_result, @project_content.module_result(@repository.id)
47 88
48 - should 'get module result from service without date' do  
49 - date_with_milliseconds = Kalibro::ProjectResult.date_with_milliseconds(@project_result.date)  
50 - Kalibro::ProjectResult.expects(:request).with('ProjectResult', :get_last_result_of, {:project_name => @project.name}).returns({:project_result => @project_result.to_hash})  
51 - Kalibro::ModuleResult.expects(:request).with(  
52 - 'ModuleResult',  
53 - :get_module_result,  
54 - {  
55 - :project_name => @project.name,  
56 - :module_name => @module.name,  
57 - :date => date_with_milliseconds  
58 - }).returns({:module_result => @module_result.to_hash})  
59 - assert_equal @module_result.grade, @project_content.module_result({:module_name => @module.name}).grade  
60 - end  
61 -  
62 - should 'get module result from service with date' do  
63 - date_with_milliseconds = Kalibro::ProjectResult.date_with_milliseconds(@project_result.date)  
64 - request_body = {:project_name => @project.name, :date => @project_result.date}  
65 - Kalibro::ProjectResult.expects(:request).with("ProjectResult", :has_results_before, request_body).returns({:has_results => false})  
66 - Kalibro::ProjectResult.expects(:request).with("ProjectResult", :get_first_result_after, request_body).returns({:project_result => @project_result.to_hash})  
67 - Kalibro::ModuleResult.expects(:request).with(  
68 - 'ModuleResult',  
69 - :get_module_result,  
70 - {  
71 - :project_name => @project.name,  
72 - :module_name => @module.name,  
73 - :date => date_with_milliseconds  
74 - }).returns({:module_result => @module_result.to_hash})  
75 - assert_equal @module_result.grade, @project_content.module_result({:module_name => @module.name, :date => @project_result.date}).grade 89 + end
  90 +
  91 + should 'get module result with date' do
  92 + @project_content.expects(:processing_with_date).with(@repository.id,@date.to_s).returns(@processing)
  93 + Kalibro::ModuleResult.expects(:find).with(@processing.results_root_id).returns(@module_result)
  94 + assert_equal @module_result, @project_content.module_result(@repository.id, @date.to_s)
76 end 95 end
77 96
78 should 'get result history' do 97 should 'get result history' do
79 - Kalibro::ModuleResult.expects(:request).with(  
80 - 'ModuleResult',  
81 - :get_result_history,  
82 - {  
83 - :project_name => @project.name,  
84 - :module_name => @module.name  
85 - }).returns({:module_result => @module_result.to_hash})  
86 - @project_content.result_history(@module.name) 98 + Kalibro::MetricResult.expects(:history_of).with(@module_result.id).returns([@date_metric_result])
  99 + assert_equal [@date_metric_result], @project_content.result_history(@module_result.id)
  100 + end
  101 +
  102 + should 'add error to base when the module_result does not exist' do
  103 + Kalibro::MetricResult.expects(:history_of).with(@module_result.id).raises(Kalibro::Errors::RecordNotFound)
  104 + assert_nil @project_content.errors[:base]
  105 + @project_content.result_history(@module_result.id)
  106 + assert_not_nil @project_content.errors[:base]
87 end 107 end
88 108
  109 +=begin
89 should 'send project to service after saving' do 110 should 'send project to service after saving' do
90 @project_content.expects :send_project_to_service 111 @project_content.expects :send_project_to_service
91 @project_content.run_callbacks :after_save 112 @project_content.run_callbacks :after_save
92 end 113 end
93 114
  115 + should 'destroy project from service' do
  116 + Kalibro::Project.expects(:request).with("Project", :get_project, :project_name => @project.name).returns({:project => @project.to_hash})
  117 + Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name})
  118 + @project_content.send :destroy_project_from_service
  119 + end
  120 +
94 should 'send correct project to service' do 121 should 'send correct project to service' do
95 hash = ProjectFixtures.project_hash 122 hash = ProjectFixtures.project_hash
96 hash.delete(:attributes!) 123 hash.delete(:attributes!)
@@ -100,16 +127,5 @@ class ProjectContentTest &lt; ActiveSupport::TestCase @@ -100,16 +127,5 @@ class ProjectContentTest &lt; ActiveSupport::TestCase
100 @project_content.send :send_project_to_service 127 @project_content.send :send_project_to_service
101 end 128 end
102 129
103 - should 'destroy project from service' do  
104 - Kalibro::Project.expects(:request).with("Project", :get_project, :project_name => @project.name).returns({:project => @project.to_hash})  
105 - Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name})  
106 - @project_content.send :destroy_project_from_service  
107 - end  
108 -  
109 - should 'not save a project with an existing project name in kalibro' do  
110 - Kalibro::Project.expects(:all_names).returns([@project_content.name])  
111 - @project_content.send :validate_kalibro_project_name  
112 - assert_equal "Project name already exists in Kalibro", @project_content.errors.on_base  
113 - end  
114 =end 130 =end
115 end 131 end