From d056407511404579f6c2d086d55e459feac01407 Mon Sep 17 00:00:00 2001 From: Diego Araújo + João M. M. da Silva Date: Wed, 11 Jul 2012 15:21:26 -0300 Subject: [PATCH] [Mezuro] Finished refactoring project model design. --- plugins/mezuro/lib/mezuro_plugin/project_content.rb | 13 ++++++------- plugins/mezuro/test/fixtures/project_fixtures.rb | 20 ++++++++++++++++---- plugins/mezuro/test/fixtures/project_result_fixtures.rb | 4 ++-- plugins/mezuro/test/fixtures/repository_fixtures.rb | 6 +++--- plugins/mezuro/test/unit/kalibro/project_test.rb | 48 ++++++++++++++++++++++++------------------------ plugins/mezuro/test/unit/kalibro/repository_test.rb | 4 ++-- plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb | 29 +++++++++++------------------ 7 files changed, 64 insertions(+), 60 deletions(-) diff --git a/plugins/mezuro/lib/mezuro_plugin/project_content.rb b/plugins/mezuro/lib/mezuro_plugin/project_content.rb index 7ad9919..d0d5bfa 100644 --- a/plugins/mezuro/lib/mezuro_plugin/project_content.rb +++ b/plugins/mezuro/lib/mezuro_plugin/project_content.rb @@ -21,7 +21,7 @@ class MezuroPlugin::ProjectContent < Article def project begin - @project ||= Kalibro::Client::ProjectClient.project(name) + @project ||= Kalibro::Project.find_by_name(name) rescue Exception => error errors.add_to_base(error.message) end @@ -55,13 +55,13 @@ client.first_result_after(name, date) end after_save :send_project_to_service - after_destroy :remove_project_from_service + after_destroy :destroy_project_from_service private def validate_kalibro_project_name begin - existing = Kalibro::Client::ProjectClient.new.project_names + existing = Kalibro::Project.all_names rescue Exception => error errors.add_to_base(error.message) end @@ -79,7 +79,7 @@ client.first_result_after(name, date) def send_project_to_service begin - Kalibro::Client::ProjectClient.save(self) + Kalibro::Project.create(self).save Kalibro::Client::KalibroClient.process_project(name, periodicity_in_days) rescue Exception => error errors.add_to_base(error.message) @@ -87,13 +87,12 @@ client.first_result_after(name, date) end - def remove_project_from_service + def destroy_project_from_service begin - Kalibro::Client::ProjectClient.remove(name) + Kalibro::Project.destroy(name) rescue Exception => error errors.add_to_base(error.message) end - end def module_result_client diff --git a/plugins/mezuro/test/fixtures/project_fixtures.rb b/plugins/mezuro/test/fixtures/project_fixtures.rb index 71ceaf7..d2534b0 100644 --- a/plugins/mezuro/test/fixtures/project_fixtures.rb +++ b/plugins/mezuro/test/fixtures/project_fixtures.rb @@ -2,16 +2,28 @@ require File.dirname(__FILE__) + '/repository_fixtures' class ProjectFixtures - def self.qt_calculator - Kalibro::Project.new qt_calculator_hash + def self.project + Kalibro::Project.new project_hash end - def self.qt_calculator_hash + def self.project_content + content = MezuroPlugin::ProjectContent.new + content.name = 'Qt-Calculator' + content.license = 'GPL' + content.description = 'Calculator for Qt' + content.repository_type = RepositoryFixtures.repository_hash[:type] + content.repository_url = RepositoryFixtures.repository_hash[:address] + content.configuration_name = 'Kalibro for Java' + content.periodicity_in_days = 1 + content + end + + def self.project_hash { :name => 'Qt-Calculator', :license => 'GPL', :description => 'Calculator for Qt', - :repository => RepositoryFixtures.qt_calculator_hash, + :repository => RepositoryFixtures.repository_hash, :configuration_name => 'Kalibro for Java', :state => 'READY' } diff --git a/plugins/mezuro/test/fixtures/project_result_fixtures.rb b/plugins/mezuro/test/fixtures/project_result_fixtures.rb index 39595e8..196bb50 100644 --- a/plugins/mezuro/test/fixtures/project_result_fixtures.rb +++ b/plugins/mezuro/test/fixtures/project_result_fixtures.rb @@ -6,7 +6,7 @@ class ProjectResultFixtures def self.qt_calculator result = Kalibro::Entities::ProjectResult.new - result.project = ProjectFixtures.qt_calculator + result.project = ProjectFixtures.project result.date = ModuleResultFixtures.create.date result.load_time = 14878 result.analysis_time = 1022 @@ -16,7 +16,7 @@ class ProjectResultFixtures end def self.qt_calculator_hash - {:project => ProjectFixtures.qt_calculator_hash, :date => ModuleResultFixtures.create_hash[:date], + {:project => ProjectFixtures.project_hash, :date => ModuleResultFixtures.create_hash[:date], :load_time => 14878, :analysis_time => 1022, :source_tree => ModuleNodeFixtures.qt_calculator_tree_hash, :collect_time => 14878} end diff --git a/plugins/mezuro/test/fixtures/repository_fixtures.rb b/plugins/mezuro/test/fixtures/repository_fixtures.rb index ba1a19f..3131f77 100644 --- a/plugins/mezuro/test/fixtures/repository_fixtures.rb +++ b/plugins/mezuro/test/fixtures/repository_fixtures.rb @@ -1,10 +1,10 @@ class RepositoryFixtures - def self.qt_calculator - Kalibro::Repository.new qt_calculator_hash + def self.repository + Kalibro::Repository.new repository_hash end - def self.qt_calculator_hash + def self.repository_hash {:type => 'SUBVERSION', :address => 'https://qt-calculator.svn.sourceforge.net/svnroot/qt-calculator'} end diff --git a/plugins/mezuro/test/unit/kalibro/project_test.rb b/plugins/mezuro/test/unit/kalibro/project_test.rb index 740f92c..9f5c31d 100644 --- a/plugins/mezuro/test/unit/kalibro/project_test.rb +++ b/plugins/mezuro/test/unit/kalibro/project_test.rb @@ -5,11 +5,18 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/project_fixtures" class ProjectTest < ActiveSupport::TestCase def setup - @hash = ProjectFixtures.qt_calculator_hash - @project = ProjectFixtures.qt_calculator + @hash = ProjectFixtures.project_hash + @project = ProjectFixtures.project + @project_content = ProjectFixtures.project_content end - should 'get project by name' do + should 'get all project names' do + response_hash = {:project_name => [@project.name]} + Kalibro::Project.expects(:request).with(:get_project_names).returns(response_hash) + assert_equal response_hash[:project_name], Kalibro::Project.all_names + end + + should 'find project by name' do request_body = {:project_name => @project.name} response_hash = {:project => @hash} Kalibro::Project.expects(:new).with(@hash).returns(@project) @@ -43,31 +50,24 @@ class ProjectTest < ActiveSupport::TestCase assert_raise Exception do Kalibro::Project.destroy(@project.name) end end -=begin - should 'not try to remove inexistent project from service' do - @port.expects(:request).with(:get_project_names).returns({:project_name => 'Different project'}) - @port.expects(:request).with(:remove_project, {:project_name => @project.name}).never - Kalibro::Client::ProjectClient.remove(@project.name) - end - - private - - def create_project_content_mock - @project_content = mock - @project_content.expects(:name).returns(@project.name) - @project_content.expects(:license).returns(@project.license) - @project_content.expects(:description).returns(@project.description) - @project_content.expects(:repository_type).returns(@project.repository.type) - @project_content.expects(:repository_url).returns(@project.repository.address) - @project_content.expects(:configuration_name).returns(@project.configuration_name) + should 'initialize new project from hash' do + project = Kalibro::Project.new @hash + assert_equal @project.name, project.name + assert_equal @project.repository.type, project.repository.type end - should 'create project from hash' do - assert_equal @project, Kalibro::Project.new(@hash) + should 'create project' do + project = Kalibro::Project.create @project_content + assert_equal @project.name, project.name + assert_equal @project.repository.type, project.repository.type end should 'convert project to hash' do - assert_equal @hash, @project.to_hash + hash = @project.to_hash + assert_equal @hash[:name], hash[:name] + assert_equal @hash[:configuration_name], hash[:configuration_name] + assert_equal @hash[:repository], hash[:repository] + assert_equal @hash[:state], hash[:state] end -=end + end diff --git a/plugins/mezuro/test/unit/kalibro/repository_test.rb b/plugins/mezuro/test/unit/kalibro/repository_test.rb index b54da89..ad87f4d 100644 --- a/plugins/mezuro/test/unit/kalibro/repository_test.rb +++ b/plugins/mezuro/test/unit/kalibro/repository_test.rb @@ -5,8 +5,8 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/repository_fixtures" class RepositoryTest < ActiveSupport::TestCase def setup - @hash = RepositoryFixtures.qt_calculator_hash - @repository = RepositoryFixtures.qt_calculator + @hash = RepositoryFixtures.repository_hash + @repository = RepositoryFixtures.repository end should 'new repository from hash' do 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 caa4984..6b7e707 100644 --- a/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb +++ b/plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb @@ -6,15 +6,8 @@ require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/project_result_fixtures" class ProjectContentTest < ActiveSupport::TestCase def setup - @project = ProjectFixtures.qt_calculator - @content = MezuroPlugin::ProjectContent.new - @content.name = @project.name - @content.license = @project.license - @content.description = @project.description - @content.repository_type = @project.repository.type - @content.repository_url = @project.repository.address - @content.configuration_name = @project.configuration_name - @content.periodicity_in_days = 1 + @project = ProjectFixtures.project + @content = ProjectFixtures.project_content end should 'be an article' do @@ -34,7 +27,7 @@ class ProjectContentTest < ActiveSupport::TestCase end should 'get project from service' do - Kalibro::Client::ProjectClient.expects(:project).with(@content.name).returns(@project) + Kalibro::Project.expects(:find_by_name).with(@content.name).returns(@project) assert_equal @project, @content.project end @@ -100,20 +93,20 @@ returns(module_result) end should 'send correct project to service' do - Kalibro::Client::ProjectClient.expects(:save).with(@content) + project = mock + Kalibro::Project.expects(:create).with(@content).returns(project) + project.expects(:save).returns(true) Kalibro::Client::KalibroClient.expects(:process_project).with(@content.name, @content.periodicity_in_days) @content.send :send_project_to_service end - should 'remove project from service' do - Kalibro::Client::ProjectClient.expects(:remove).with(@content.name) - @content.send :remove_project_from_service + should 'destroy project from service' do + Kalibro::Project.expects(:destroy).with(@content.name) + @content.send :destroy_project_from_service end should 'not save a project with an existing project name in kalibro' do - client = mock - Kalibro::Client::ProjectClient.expects(:new).returns(client) - client.expects(:project_names).returns([@content.name]) + Kalibro::Project.expects(:all_names).returns([@content.name]) @content.send :validate_kalibro_project_name assert_equal "Project name already exists in Kalibro", @content.errors.on_base end @@ -121,7 +114,7 @@ returns(module_result) private def mock_project_client - Kalibro::Client::ProjectClient.expects(:project).with(@content.name).returns(@project) + Kalibro::Project.expects(:find_by_name).with(@content.name).returns(@project) end def mock_project_result_client -- libgit2 0.21.2