Commit 70f57d8340ff506afc86dd02590f87616ca6325a
Committed by
João M. M. da Silva
1 parent
7ad294ee
Exists in
master
and in
23 other branches
[Mezuro] Fixing clone configuration feature and some refactoring
Showing
10 changed files
with
96 additions
and
93 deletions
Show diff stats
plugins/mezuro/lib/kalibro/configuration.rb
| @@ -19,24 +19,19 @@ class Kalibro::Configuration < Kalibro::Model | @@ -19,24 +19,19 @@ class Kalibro::Configuration < Kalibro::Model | ||
| 19 | end | 19 | end |
| 20 | 20 | ||
| 21 | def self.find_by_name(configuration_name) | 21 | def self.find_by_name(configuration_name) |
| 22 | - new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration] | ||
| 23 | - end | ||
| 24 | - | ||
| 25 | - def self.create(content, configuration) | ||
| 26 | - attributes = { | ||
| 27 | - :name => content.name, | ||
| 28 | - :description => content.description, | ||
| 29 | - :metric_configuration => configuration.to_hash[:metric_configuration] | ||
| 30 | - } | ||
| 31 | - super attributes | 22 | + begin |
| 23 | + new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration] | ||
| 24 | + rescue Exception => error | ||
| 25 | + nil | ||
| 26 | + end | ||
| 32 | end | 27 | end |
| 33 | 28 | ||
| 34 | def self.all_names | 29 | def self.all_names |
| 35 | - request("Configuration", :get_configuration_names)[:configuration_name] | ||
| 36 | - end | ||
| 37 | - | ||
| 38 | - def destroy | ||
| 39 | - self.class.request("Configuration", :remove_configuration, {:configuration_name => name}) | 30 | + begin |
| 31 | + request("Configuration", :get_configuration_names)[:configuration_name] | ||
| 32 | + rescue Exception | ||
| 33 | + [] | ||
| 34 | + end | ||
| 40 | end | 35 | end |
| 41 | 36 | ||
| 42 | def update_attributes(attributes={}) | 37 | def update_attributes(attributes={}) |
| @@ -44,4 +39,7 @@ class Kalibro::Configuration < Kalibro::Model | @@ -44,4 +39,7 @@ class Kalibro::Configuration < Kalibro::Model | ||
| 44 | save | 39 | save |
| 45 | end | 40 | end |
| 46 | 41 | ||
| 42 | + def metric_configurations_hash | ||
| 43 | + self.to_hash[:metric_configuration] | ||
| 44 | + end | ||
| 47 | end | 45 | end |
plugins/mezuro/lib/kalibro/model.rb
| @@ -37,7 +37,9 @@ class Kalibro::Model | @@ -37,7 +37,9 @@ class Kalibro::Model | ||
| 37 | end | 37 | end |
| 38 | 38 | ||
| 39 | def self.create(attributes={}) | 39 | def self.create(attributes={}) |
| 40 | - new(attributes).save | 40 | + new_model = new attributes |
| 41 | + new_model.save | ||
| 42 | + new_model | ||
| 41 | end | 43 | end |
| 42 | 44 | ||
| 43 | def save | 45 | def save |
| @@ -49,6 +51,13 @@ class Kalibro::Model | @@ -49,6 +51,13 @@ class Kalibro::Model | ||
| 49 | end | 51 | end |
| 50 | end | 52 | end |
| 51 | 53 | ||
| 54 | + def destroy | ||
| 55 | + begin | ||
| 56 | + self.class.request(destroy_endpoint, destroy_action, destroy_params) | ||
| 57 | + rescue Exception | ||
| 58 | + end | ||
| 59 | + end | ||
| 60 | + | ||
| 52 | protected | 61 | protected |
| 53 | 62 | ||
| 54 | def fields | 63 | def fields |
| @@ -101,4 +110,17 @@ class Kalibro::Model | @@ -101,4 +110,17 @@ class Kalibro::Model | ||
| 101 | def save_params | 110 | def save_params |
| 102 | {class_name.underscore.to_sym => self.to_hash} | 111 | {class_name.underscore.to_sym => self.to_hash} |
| 103 | end | 112 | end |
| 113 | + | ||
| 114 | + def destroy_endpoint | ||
| 115 | + class_name | ||
| 116 | + end | ||
| 117 | + | ||
| 118 | + def destroy_action | ||
| 119 | + "remove_#{class_name.underscore}".to_sym | ||
| 120 | + end | ||
| 121 | + | ||
| 122 | + def destroy_params | ||
| 123 | + {"#{class_name.underscore}_name".to_sym => self.name} | ||
| 124 | + end | ||
| 125 | + | ||
| 104 | end | 126 | end |
plugins/mezuro/lib/kalibro/project.rb
| @@ -10,24 +10,6 @@ class Kalibro::Project < Kalibro::Model | @@ -10,24 +10,6 @@ class Kalibro::Project < Kalibro::Model | ||
| 10 | new request("Project", :get_project, :project_name => project_name)[:project] | 10 | new request("Project", :get_project, :project_name => project_name)[:project] |
| 11 | end | 11 | end |
| 12 | 12 | ||
| 13 | - def self.create(content) | ||
| 14 | - attributes = { | ||
| 15 | - :name => content.name, | ||
| 16 | - :license => content.license, | ||
| 17 | - :description => content.description, | ||
| 18 | - :repository => { | ||
| 19 | - :type => content.repository_type, | ||
| 20 | - :address => content.repository_url | ||
| 21 | - }, | ||
| 22 | - :configuration_name => content.configuration_name | ||
| 23 | - } | ||
| 24 | - super attributes | ||
| 25 | - end | ||
| 26 | - | ||
| 27 | - def destroy | ||
| 28 | - self.class.request("Project", :remove_project, {:project_name => name}) | ||
| 29 | - end | ||
| 30 | - | ||
| 31 | def repository=(value) | 13 | def repository=(value) |
| 32 | @repository = Kalibro::Repository.to_object value | 14 | @repository = Kalibro::Repository.to_object value |
| 33 | end | 15 | end |
plugins/mezuro/lib/mezuro_plugin/configuration_content.rb
| @@ -19,12 +19,11 @@ class MezuroPlugin::ConfigurationContent < Article | @@ -19,12 +19,11 @@ class MezuroPlugin::ConfigurationContent < Article | ||
| 19 | end | 19 | end |
| 20 | 20 | ||
| 21 | def configuration | 21 | def configuration |
| 22 | - begin | ||
| 23 | - @configuration ||= Kalibro::Configuration.find_by_name(self.name) | ||
| 24 | - rescue Exception => error | ||
| 25 | - errors.add_to_base(error.message) | ||
| 26 | - nil | 22 | + @configuration ||= Kalibro::Configuration.find_by_name(self.name) |
| 23 | + if @configuration.nil? | ||
| 24 | + errors.add_to_base("Kalibro Configuration not found") | ||
| 27 | end | 25 | end |
| 26 | + @configuration | ||
| 28 | end | 27 | end |
| 29 | 28 | ||
| 30 | def metric_configurations | 29 | def metric_configurations |
| @@ -49,24 +48,35 @@ class MezuroPlugin::ConfigurationContent < Article | @@ -49,24 +48,35 @@ class MezuroPlugin::ConfigurationContent < Article | ||
| 49 | end | 48 | end |
| 50 | 49 | ||
| 51 | def send_configuration_to_service | 50 | def send_configuration_to_service |
| 52 | - if configuration.nil? | ||
| 53 | - begin | ||
| 54 | - clone_configuration = Kalibro::Configuration.find_by_name(self.clone_configuration_name) | ||
| 55 | - rescue Exception => error | ||
| 56 | - clone_configuration = nil | ||
| 57 | - end | ||
| 58 | - Kalibro::Configuration.create(self, clone_configuration) | ||
| 59 | - else | 51 | + if editing_configuration? |
| 60 | configuration.update_attributes({:description => description}) | 52 | configuration.update_attributes({:description => description}) |
| 53 | + else | ||
| 54 | + create_kalibro_configuration | ||
| 61 | end | 55 | end |
| 62 | end | 56 | end |
| 63 | 57 | ||
| 64 | def remove_configuration_from_service | 58 | def remove_configuration_from_service |
| 65 | - begin | ||
| 66 | - configuration.destroy | ||
| 67 | - rescue Exception => error | ||
| 68 | - errors.add_to_base(error.message) | 59 | + configuration.destroy |
| 60 | + end | ||
| 61 | + | ||
| 62 | + def create_kalibro_configuration | ||
| 63 | + attributes = {:name => name, :description => description} | ||
| 64 | + if cloning_configuration? | ||
| 65 | + attributes[:metric_configuration] = configuration_to_clone.metric_configurations_hash | ||
| 69 | end | 66 | end |
| 67 | + Kalibro::Configuration.create attributes | ||
| 68 | + end | ||
| 69 | + | ||
| 70 | + def editing_configuration? | ||
| 71 | + !configuration.nil? | ||
| 72 | + end | ||
| 73 | + | ||
| 74 | + def configuration_to_clone | ||
| 75 | + @configuration_to_clone ||= Kalibro::Configuration.find_by_name(self.clone_configuration_name) | ||
| 76 | + end | ||
| 77 | + | ||
| 78 | + def cloning_configuration? | ||
| 79 | + configuration_to_clone.nil? | ||
| 70 | end | 80 | end |
| 71 | 81 | ||
| 72 | end | 82 | end |
plugins/mezuro/lib/mezuro_plugin/project_content.rb
| @@ -87,20 +87,25 @@ Kalibro::ProjectResult.first_result_after(name, date) | @@ -87,20 +87,25 @@ Kalibro::ProjectResult.first_result_after(name, date) | ||
| 87 | end | 87 | end |
| 88 | 88 | ||
| 89 | def send_project_to_service | 89 | def send_project_to_service |
| 90 | - begin | ||
| 91 | - Kalibro::Project.create(self) | ||
| 92 | - project.process_project(periodicity_in_days) | ||
| 93 | - rescue Exception => error | ||
| 94 | - errors.add_to_base(error.message) | ||
| 95 | - end | 90 | + created_project = create_kalibro_project |
| 91 | + created_project.process_project(periodicity_in_days) | ||
| 92 | + end | ||
| 93 | + | ||
| 94 | + def create_kalibro_project | ||
| 95 | + Kalibro::Project.create( | ||
| 96 | + :name => name, | ||
| 97 | + :license => license, | ||
| 98 | + :description => description, | ||
| 99 | + :repository => { | ||
| 100 | + :type => repository_type, | ||
| 101 | + :address => repository_url | ||
| 102 | + }, | ||
| 103 | + :configuration_name => configuration_name | ||
| 104 | + ) | ||
| 96 | end | 105 | end |
| 97 | 106 | ||
| 98 | def destroy_project_from_service | 107 | def destroy_project_from_service |
| 99 | - begin | ||
| 100 | - project.destroy | ||
| 101 | - rescue Exception => error | ||
| 102 | - errors.add_to_base(error.message) | ||
| 103 | - end | 108 | + project.destroy |
| 104 | end | 109 | end |
| 105 | 110 | ||
| 106 | end | 111 | end |
plugins/mezuro/test/fixtures/configuration_fixtures.rb
| @@ -16,5 +16,13 @@ class ConfigurationFixtures | @@ -16,5 +16,13 @@ class ConfigurationFixtures | ||
| 16 | ] | 16 | ] |
| 17 | } | 17 | } |
| 18 | end | 18 | end |
| 19 | + | ||
| 20 | + def self.configuration_content(clone_configuration) | ||
| 21 | + MezuroPlugin::ConfigurationContent.new({ | ||
| 22 | + :name => 'Sample Configuration', | ||
| 23 | + :description => 'Kalibro configuration for Java projects.', | ||
| 24 | + :clone_configuration_name => clone_configuration | ||
| 25 | + }) | ||
| 26 | + end | ||
| 19 | 27 | ||
| 20 | end | 28 | end |
plugins/mezuro/test/unit/kalibro/configuration_test.rb
| @@ -7,6 +7,7 @@ class ConfigurationTest < ActiveSupport::TestCase | @@ -7,6 +7,7 @@ class ConfigurationTest < ActiveSupport::TestCase | ||
| 7 | def setup | 7 | def setup |
| 8 | @hash = ConfigurationFixtures.configuration_hash | 8 | @hash = ConfigurationFixtures.configuration_hash |
| 9 | @configuration = ConfigurationFixtures.configuration | 9 | @configuration = ConfigurationFixtures.configuration |
| 10 | + @configuration_content = ConfigurationFixtures.configuration_content([]) | ||
| 10 | end | 11 | end |
| 11 | 12 | ||
| 12 | should 'initialize configuration' do | 13 | should 'initialize configuration' do |
| @@ -26,7 +27,7 @@ class ConfigurationTest < ActiveSupport::TestCase | @@ -26,7 +27,7 @@ class ConfigurationTest < ActiveSupport::TestCase | ||
| 26 | Kalibro::Configuration.expects(:request).with("Configuration", :save_configuration, {:configuration => @configuration.to_hash}).raises(Exception.new) | 27 | Kalibro::Configuration.expects(:request).with("Configuration", :save_configuration, {:configuration => @configuration.to_hash}).raises(Exception.new) |
| 27 | assert !(@configuration.save) | 28 | assert !(@configuration.save) |
| 28 | end | 29 | end |
| 29 | - | 30 | + |
| 30 | should 'get all configuration names' do | 31 | should 'get all configuration names' do |
| 31 | names = ['Kalibro for Java', 'ConfigurationClientTest configuration'] | 32 | names = ['Kalibro for Java', 'ConfigurationClientTest configuration'] |
| 32 | Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration_names).returns({:configuration_name => names}) | 33 | Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration_names).returns({:configuration_name => names}) |
| @@ -40,19 +41,14 @@ class ConfigurationTest < ActiveSupport::TestCase | @@ -40,19 +41,14 @@ class ConfigurationTest < ActiveSupport::TestCase | ||
| 40 | assert_equal @configuration.name, Kalibro::Configuration.find_by_name(@configuration.name).name | 41 | assert_equal @configuration.name, Kalibro::Configuration.find_by_name(@configuration.name).name |
| 41 | end | 42 | end |
| 42 | 43 | ||
| 43 | - should 'raise error when configuration doesnt exist' do | 44 | + should 'return nil when configuration doesnt exist' do |
| 44 | request_body = {:configuration_name => @configuration.name} | 45 | request_body = {:configuration_name => @configuration.name} |
| 45 | Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration, request_body).raises(Exception.new) | 46 | Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration, request_body).raises(Exception.new) |
| 46 | - assert_raise Exception do Kalibro::Configuration.find_by_name(@configuration.name) end | 47 | + assert_nil Kalibro::Configuration.find_by_name(@configuration.name) |
| 47 | end | 48 | end |
| 48 | 49 | ||
| 49 | should 'destroy configuration by name' do | 50 | should 'destroy configuration by name' do |
| 50 | Kalibro::Configuration.expects(:request).with("Configuration", :remove_configuration, {:configuration_name => @configuration.name}) | 51 | Kalibro::Configuration.expects(:request).with("Configuration", :remove_configuration, {:configuration_name => @configuration.name}) |
| 51 | @configuration.destroy | 52 | @configuration.destroy |
| 52 | end | 53 | end |
| 53 | - | ||
| 54 | - should 'raise error when try to destroy inexistent configuration from service' do | ||
| 55 | - Kalibro::Configuration.expects(:request).with("Configuration", :remove_configuration, {:configuration_name => @configuration.name}).raises(Exception.new) | ||
| 56 | - assert_raise Exception do @configuration.destroy end | ||
| 57 | - end | ||
| 58 | end | 54 | end |
plugins/mezuro/test/unit/kalibro/project_test.rb
| @@ -44,11 +44,6 @@ class ProjectTest < ActiveSupport::TestCase | @@ -44,11 +44,6 @@ class ProjectTest < ActiveSupport::TestCase | ||
| 44 | Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}) | 44 | Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}) |
| 45 | @project.destroy | 45 | @project.destroy |
| 46 | end | 46 | end |
| 47 | - | ||
| 48 | - should 'raise error when try to remove inexistent project from service' do | ||
| 49 | - Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}).raises(Exception.new) | ||
| 50 | - assert_raise Exception do @project.destroy end | ||
| 51 | - end | ||
| 52 | 47 | ||
| 53 | should 'initialize new project from hash' do | 48 | should 'initialize new project from hash' do |
| 54 | project = Kalibro::Project.new @hash | 49 | project = Kalibro::Project.new @hash |
| @@ -56,21 +51,6 @@ class ProjectTest < ActiveSupport::TestCase | @@ -56,21 +51,6 @@ class ProjectTest < ActiveSupport::TestCase | ||
| 56 | assert_equal @project.repository.type, project.repository.type | 51 | assert_equal @project.repository.type, project.repository.type |
| 57 | end | 52 | end |
| 58 | 53 | ||
| 59 | - should 'create project' do | ||
| 60 | - project_hash = Kalibro::Project.new({ | ||
| 61 | - :name => @project_content.name, | ||
| 62 | - :license => @project_content.license, | ||
| 63 | - :description => @project_content.description, | ||
| 64 | - :repository => { | ||
| 65 | - :type => @project_content.repository_type, | ||
| 66 | - :address => @project_content.repository_url | ||
| 67 | - }, | ||
| 68 | - :configuration_name => @project_content.configuration_name | ||
| 69 | - }).to_hash | ||
| 70 | - Kalibro::Project.expects(:request).with("Project", :save_project, {:project => project_hash}) | ||
| 71 | - Kalibro::Project.create @project_content | ||
| 72 | - end | ||
| 73 | - | ||
| 74 | should 'convert project to hash' do | 54 | should 'convert project to hash' do |
| 75 | hash = @project.to_hash | 55 | hash = @project.to_hash |
| 76 | assert_equal @hash[:name], hash[:name] | 56 | assert_equal @hash[:name], hash[:name] |
plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb
| @@ -98,8 +98,10 @@ class ProjectContentTest < ActiveSupport::TestCase | @@ -98,8 +98,10 @@ class ProjectContentTest < ActiveSupport::TestCase | ||
| 98 | end | 98 | end |
| 99 | 99 | ||
| 100 | should 'send correct project to service' do | 100 | should 'send correct project to service' do |
| 101 | - Kalibro::Project.expects(:create).with(@content).returns(true) | ||
| 102 | - Kalibro::Project.expects(:find_by_name).with(@content.name).returns(@project) | 101 | + hash = ProjectFixtures.project_hash |
| 102 | + hash.delete(:attributes!) | ||
| 103 | + hash.delete(:state) | ||
| 104 | + Kalibro::Project.expects(:create).with(hash).returns(@project) | ||
| 103 | @project.expects(:process_project).with(@content.periodicity_in_days) | 105 | @project.expects(:process_project).with(@content.periodicity_in_days) |
| 104 | @content.send :send_project_to_service | 106 | @content.send :send_project_to_service |
| 105 | end | 107 | end |
plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb
| @@ -15,7 +15,7 @@ | @@ -15,7 +15,7 @@ | ||
| 15 | 15 | ||
| 16 | <% configuration_names = @article.configuration_names %> | 16 | <% configuration_names = @article.configuration_names %> |
| 17 | 17 | ||
| 18 | -<% selected = (configuration.nil? ? configuration_names[0] : @article.clone_configuration_name) %> | 18 | +<% selected = (configuration.nil? ? "None" : @article.clone_configuration_name) %> |
| 19 | 19 | ||
| 20 | <%= required_fields_message %> | 20 | <%= required_fields_message %> |
| 21 | 21 |