diff --git a/plugins/mezuro/lib/kalibro/configuration.rb b/plugins/mezuro/lib/kalibro/configuration.rb index 5183634..720c014 100644 --- a/plugins/mezuro/lib/kalibro/configuration.rb +++ b/plugins/mezuro/lib/kalibro/configuration.rb @@ -19,24 +19,19 @@ class Kalibro::Configuration < Kalibro::Model end def self.find_by_name(configuration_name) - new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration] - end - - def self.create(content, configuration) - attributes = { - :name => content.name, - :description => content.description, - :metric_configuration => configuration.to_hash[:metric_configuration] - } - super attributes + begin + new request("Configuration", :get_configuration, {:configuration_name => configuration_name})[:configuration] + rescue Exception => error + nil + end end def self.all_names - request("Configuration", :get_configuration_names)[:configuration_name] - end - - def destroy - self.class.request("Configuration", :remove_configuration, {:configuration_name => name}) + begin + request("Configuration", :get_configuration_names)[:configuration_name] + rescue Exception + [] + end end def update_attributes(attributes={}) @@ -44,4 +39,7 @@ class Kalibro::Configuration < Kalibro::Model save end + def metric_configurations_hash + self.to_hash[:metric_configuration] + end end diff --git a/plugins/mezuro/lib/kalibro/model.rb b/plugins/mezuro/lib/kalibro/model.rb index 56375e9..04c82bd 100644 --- a/plugins/mezuro/lib/kalibro/model.rb +++ b/plugins/mezuro/lib/kalibro/model.rb @@ -37,7 +37,9 @@ class Kalibro::Model end def self.create(attributes={}) - new(attributes).save + new_model = new attributes + new_model.save + new_model end def save @@ -49,6 +51,13 @@ class Kalibro::Model end end + def destroy + begin + self.class.request(destroy_endpoint, destroy_action, destroy_params) + rescue Exception + end + end + protected def fields @@ -101,4 +110,17 @@ class Kalibro::Model def save_params {class_name.underscore.to_sym => self.to_hash} end + + def destroy_endpoint + class_name + end + + def destroy_action + "remove_#{class_name.underscore}".to_sym + end + + def destroy_params + {"#{class_name.underscore}_name".to_sym => self.name} + end + end diff --git a/plugins/mezuro/lib/kalibro/project.rb b/plugins/mezuro/lib/kalibro/project.rb index 7ac187a..a67ccbf 100644 --- a/plugins/mezuro/lib/kalibro/project.rb +++ b/plugins/mezuro/lib/kalibro/project.rb @@ -10,24 +10,6 @@ class Kalibro::Project < Kalibro::Model new request("Project", :get_project, :project_name => project_name)[:project] end - def self.create(content) - attributes = { - :name => content.name, - :license => content.license, - :description => content.description, - :repository => { - :type => content.repository_type, - :address => content.repository_url - }, - :configuration_name => content.configuration_name - } - super attributes - end - - def destroy - self.class.request("Project", :remove_project, {:project_name => name}) - end - def repository=(value) @repository = Kalibro::Repository.to_object value end diff --git a/plugins/mezuro/lib/mezuro_plugin/configuration_content.rb b/plugins/mezuro/lib/mezuro_plugin/configuration_content.rb index 5145484..4a5d380 100644 --- a/plugins/mezuro/lib/mezuro_plugin/configuration_content.rb +++ b/plugins/mezuro/lib/mezuro_plugin/configuration_content.rb @@ -19,12 +19,11 @@ class MezuroPlugin::ConfigurationContent < Article end def configuration - begin - @configuration ||= Kalibro::Configuration.find_by_name(self.name) - rescue Exception => error - errors.add_to_base(error.message) - nil + @configuration ||= Kalibro::Configuration.find_by_name(self.name) + if @configuration.nil? + errors.add_to_base("Kalibro Configuration not found") end + @configuration end def metric_configurations @@ -49,24 +48,35 @@ class MezuroPlugin::ConfigurationContent < Article end def send_configuration_to_service - if configuration.nil? - begin - clone_configuration = Kalibro::Configuration.find_by_name(self.clone_configuration_name) - rescue Exception => error - clone_configuration = nil - end - Kalibro::Configuration.create(self, clone_configuration) - else + if editing_configuration? configuration.update_attributes({:description => description}) + else + create_kalibro_configuration end end def remove_configuration_from_service - begin - configuration.destroy - rescue Exception => error - errors.add_to_base(error.message) + configuration.destroy + end + + def create_kalibro_configuration + attributes = {:name => name, :description => description} + if cloning_configuration? + attributes[:metric_configuration] = configuration_to_clone.metric_configurations_hash end + Kalibro::Configuration.create attributes + end + + def editing_configuration? + !configuration.nil? + end + + def configuration_to_clone + @configuration_to_clone ||= Kalibro::Configuration.find_by_name(self.clone_configuration_name) + end + + def cloning_configuration? + configuration_to_clone.nil? end end diff --git a/plugins/mezuro/lib/mezuro_plugin/project_content.rb b/plugins/mezuro/lib/mezuro_plugin/project_content.rb index 4a8b67c..99dbc92 100644 --- a/plugins/mezuro/lib/mezuro_plugin/project_content.rb +++ b/plugins/mezuro/lib/mezuro_plugin/project_content.rb @@ -87,20 +87,25 @@ Kalibro::ProjectResult.first_result_after(name, date) end def send_project_to_service - begin - Kalibro::Project.create(self) - project.process_project(periodicity_in_days) - rescue Exception => error - errors.add_to_base(error.message) - end + created_project = create_kalibro_project + created_project.process_project(periodicity_in_days) + end + + def create_kalibro_project + Kalibro::Project.create( + :name => name, + :license => license, + :description => description, + :repository => { + :type => repository_type, + :address => repository_url + }, + :configuration_name => configuration_name + ) end def destroy_project_from_service - begin - project.destroy - rescue Exception => error - errors.add_to_base(error.message) - end + project.destroy end end diff --git a/plugins/mezuro/test/fixtures/configuration_fixtures.rb b/plugins/mezuro/test/fixtures/configuration_fixtures.rb index 6ffb18b..88ff63f 100644 --- a/plugins/mezuro/test/fixtures/configuration_fixtures.rb +++ b/plugins/mezuro/test/fixtures/configuration_fixtures.rb @@ -16,5 +16,13 @@ class ConfigurationFixtures ] } end + + def self.configuration_content(clone_configuration) + MezuroPlugin::ConfigurationContent.new({ + :name => 'Sample Configuration', + :description => 'Kalibro configuration for Java projects.', + :clone_configuration_name => clone_configuration + }) + end end diff --git a/plugins/mezuro/test/unit/kalibro/configuration_test.rb b/plugins/mezuro/test/unit/kalibro/configuration_test.rb index ba06720..15f9074 100644 --- a/plugins/mezuro/test/unit/kalibro/configuration_test.rb +++ b/plugins/mezuro/test/unit/kalibro/configuration_test.rb @@ -7,6 +7,7 @@ class ConfigurationTest < ActiveSupport::TestCase def setup @hash = ConfigurationFixtures.configuration_hash @configuration = ConfigurationFixtures.configuration + @configuration_content = ConfigurationFixtures.configuration_content([]) end should 'initialize configuration' do @@ -26,7 +27,7 @@ class ConfigurationTest < ActiveSupport::TestCase Kalibro::Configuration.expects(:request).with("Configuration", :save_configuration, {:configuration => @configuration.to_hash}).raises(Exception.new) assert !(@configuration.save) end - + should 'get all configuration names' do names = ['Kalibro for Java', 'ConfigurationClientTest configuration'] Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration_names).returns({:configuration_name => names}) @@ -40,19 +41,14 @@ class ConfigurationTest < ActiveSupport::TestCase assert_equal @configuration.name, Kalibro::Configuration.find_by_name(@configuration.name).name end - should 'raise error when configuration doesnt exist' do + should 'return nil when configuration doesnt exist' do request_body = {:configuration_name => @configuration.name} Kalibro::Configuration.expects(:request).with("Configuration", :get_configuration, request_body).raises(Exception.new) - assert_raise Exception do Kalibro::Configuration.find_by_name(@configuration.name) end + assert_nil Kalibro::Configuration.find_by_name(@configuration.name) end should 'destroy configuration by name' do Kalibro::Configuration.expects(:request).with("Configuration", :remove_configuration, {:configuration_name => @configuration.name}) @configuration.destroy end - - should 'raise error when try to destroy inexistent configuration from service' do - Kalibro::Configuration.expects(:request).with("Configuration", :remove_configuration, {:configuration_name => @configuration.name}).raises(Exception.new) - assert_raise Exception do @configuration.destroy end - end end diff --git a/plugins/mezuro/test/unit/kalibro/project_test.rb b/plugins/mezuro/test/unit/kalibro/project_test.rb index f53d15e..e61d080 100644 --- a/plugins/mezuro/test/unit/kalibro/project_test.rb +++ b/plugins/mezuro/test/unit/kalibro/project_test.rb @@ -44,11 +44,6 @@ class ProjectTest < ActiveSupport::TestCase Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}) @project.destroy end - - should 'raise error when try to remove inexistent project from service' do - Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}).raises(Exception.new) - assert_raise Exception do @project.destroy end - end should 'initialize new project from hash' do project = Kalibro::Project.new @hash @@ -56,21 +51,6 @@ class ProjectTest < ActiveSupport::TestCase assert_equal @project.repository.type, project.repository.type end - should 'create project' do - project_hash = Kalibro::Project.new({ - :name => @project_content.name, - :license => @project_content.license, - :description => @project_content.description, - :repository => { - :type => @project_content.repository_type, - :address => @project_content.repository_url - }, - :configuration_name => @project_content.configuration_name - }).to_hash - Kalibro::Project.expects(:request).with("Project", :save_project, {:project => project_hash}) - Kalibro::Project.create @project_content - end - should 'convert project to hash' do hash = @project.to_hash assert_equal @hash[:name], hash[:name] diff --git a/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb b/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb index dba3c00..28dbb75 100644 --- a/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb +++ b/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb @@ -98,8 +98,10 @@ class ProjectContentTest < ActiveSupport::TestCase end should 'send correct project to service' do - Kalibro::Project.expects(:create).with(@content).returns(true) - Kalibro::Project.expects(:find_by_name).with(@content.name).returns(@project) + hash = ProjectFixtures.project_hash + hash.delete(:attributes!) + hash.delete(:state) + Kalibro::Project.expects(:create).with(hash).returns(@project) @project.expects(:process_project).with(@content.periodicity_in_days) @content.send :send_project_to_service end diff --git a/plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb b/plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb index df94a30..1ec56b1 100644 --- a/plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb +++ b/plugins/mezuro/views/cms/mezuro_plugin/_configuration_content.html.erb @@ -15,7 +15,7 @@ <% configuration_names = @article.configuration_names %> -<% selected = (configuration.nil? ? configuration_names[0] : @article.clone_configuration_name) %> +<% selected = (configuration.nil? ? "None" : @article.clone_configuration_name) %> <%= required_fields_message %> -- libgit2 0.21.2