Commit a9432a0d8cd7bfd65433eb1b3aee078c5a7e6eae
Committed by
Paulo Meireles
1 parent
d0564075
Exists in
master
and in
28 other branches
[Mezuro] Refactoring Kalibro Client to Kalibro Model
Showing
9 changed files
with
79 additions
and
108 deletions
Show diff stats
plugins/mezuro/lib/kalibro/client/kalibro_client.rb
| @@ -1,39 +0,0 @@ | @@ -1,39 +0,0 @@ | ||
| 1 | -class Kalibro::Client::KalibroClient | ||
| 2 | - | ||
| 3 | - def self.process_project(project_name) | ||
| 4 | - new.process_project(project_name) | ||
| 5 | - end | ||
| 6 | - | ||
| 7 | - def initialize | ||
| 8 | - @port = Kalibro::Client::Port.new('Kalibro') | ||
| 9 | - end | ||
| 10 | - | ||
| 11 | - def supported_repository_types | ||
| 12 | - @port.request(:get_supported_repository_types)[:repository_type].to_a | ||
| 13 | - end | ||
| 14 | - | ||
| 15 | - def process_project(project_name) | ||
| 16 | - @port.request(:process_project, {:project_name => project_name}) | ||
| 17 | - end | ||
| 18 | - | ||
| 19 | - def self.process_project(project_name, days) | ||
| 20 | - if days.to_i.zero? | ||
| 21 | - new.process_project(project_name) | ||
| 22 | - else | ||
| 23 | - new.process_periodically(project_name, days) | ||
| 24 | - end | ||
| 25 | - end | ||
| 26 | - | ||
| 27 | - def process_periodically(project_name, period_in_days) | ||
| 28 | - @port.request(:process_periodically, {:project_name => project_name, :period_in_days => period_in_days}) | ||
| 29 | - end | ||
| 30 | - | ||
| 31 | - def process_period(project_name) | ||
| 32 | - @port.request(:get_process_period, {:project_name => project_name})[:period] | ||
| 33 | - end | ||
| 34 | - | ||
| 35 | - def cancel_periodic_process(project_name) | ||
| 36 | - @port.request(:cancel_periodic_process, {:project_name => project_name}) | ||
| 37 | - end | ||
| 38 | - | ||
| 39 | -end |
| @@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
| 1 | +class Kalibro::Kalibro < Kalibro::Model | ||
| 2 | + | ||
| 3 | + def self.repository_types | ||
| 4 | + request("Kalibro", :get_supported_repository_types)[:repository_type].to_a | ||
| 5 | + end | ||
| 6 | + | ||
| 7 | + def self.process_project(project_name, days = '0') | ||
| 8 | + if days.to_i.zero? | ||
| 9 | + request("Kalibro", :process_project, {:project_name => project_name}) | ||
| 10 | + else | ||
| 11 | + request("Kalibro", :process_periodically, {:project_name => project_name, :period_in_days => days}) | ||
| 12 | + end | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + def self.process_period(project_name) | ||
| 16 | + request("Kalibro", :get_process_period, {:project_name => project_name})[:period] | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + def self.cancel_periodic_process(project_name) | ||
| 20 | + request("Kalibro", :cancel_periodic_process, {:project_name => project_name}) | ||
| 21 | + end | ||
| 22 | +end |
plugins/mezuro/lib/kalibro/model.rb
| @@ -38,4 +38,14 @@ class Kalibro::Model | @@ -38,4 +38,14 @@ class Kalibro::Model | ||
| 38 | xml_name + "Xml" | 38 | xml_name + "Xml" |
| 39 | end | 39 | end |
| 40 | 40 | ||
| 41 | + def self.client(endpoint) | ||
| 42 | + service_address = YAML.load_file("#{RAILS_ROOT}/plugins/mezuro/service.yaml") | ||
| 43 | + Savon::Client.new("#{service_address}#{endpoint}Endpoint/?wsdl") | ||
| 44 | + end | ||
| 45 | + | ||
| 46 | + def self.request(endpoint, action, request_body = nil) | ||
| 47 | + response = client(endpoint).request(:kalibro, action) { soap.body = request_body } | ||
| 48 | + response.to_hash["#{action}_response".to_sym] | ||
| 49 | + end | ||
| 50 | + | ||
| 41 | end | 51 | end |
plugins/mezuro/lib/kalibro/project.rb
| @@ -3,12 +3,12 @@ class Kalibro::Project < Kalibro::Model | @@ -3,12 +3,12 @@ class Kalibro::Project < Kalibro::Model | ||
| 3 | attr_accessor :name, :license, :description, :repository, :configuration_name, :state, :error | 3 | attr_accessor :name, :license, :description, :repository, :configuration_name, :state, :error |
| 4 | 4 | ||
| 5 | def self.all_names | 5 | def self.all_names |
| 6 | - request(:get_project_names)[:project_name] | 6 | + request("Project", :get_project_names)[:project_name] |
| 7 | end | 7 | end |
| 8 | 8 | ||
| 9 | def self.find_by_name(project_name) | 9 | def self.find_by_name(project_name) |
| 10 | begin | 10 | begin |
| 11 | - attributes = request(:get_project, :project_name => project_name)[:project] | 11 | + attributes = request("Project", :get_project, :project_name => project_name)[:project] |
| 12 | new attributes | 12 | new attributes |
| 13 | rescue Exception => error | 13 | rescue Exception => error |
| 14 | nil | 14 | nil |
| @@ -16,7 +16,7 @@ class Kalibro::Project < Kalibro::Model | @@ -16,7 +16,7 @@ class Kalibro::Project < Kalibro::Model | ||
| 16 | end | 16 | end |
| 17 | 17 | ||
| 18 | def self.destroy(project_name) | 18 | def self.destroy(project_name) |
| 19 | - request(:remove_project, {:project_name => project_name}) | 19 | + request("Project", :remove_project, {:project_name => project_name}) |
| 20 | end | 20 | end |
| 21 | 21 | ||
| 22 | def self.create (content) | 22 | def self.create (content) |
| @@ -34,7 +34,7 @@ class Kalibro::Project < Kalibro::Model | @@ -34,7 +34,7 @@ class Kalibro::Project < Kalibro::Model | ||
| 34 | 34 | ||
| 35 | def save | 35 | def save |
| 36 | begin | 36 | begin |
| 37 | - self.class.request(:save_project, {:project => to_hash}) | 37 | + self.class.request("Project", :save_project, {:project => to_hash}) |
| 38 | true | 38 | true |
| 39 | rescue Exception => error | 39 | rescue Exception => error |
| 40 | false | 40 | false |
| @@ -45,18 +45,5 @@ class Kalibro::Project < Kalibro::Model | @@ -45,18 +45,5 @@ class Kalibro::Project < Kalibro::Model | ||
| 45 | @repository = (value.kind_of?(Hash)) ? Kalibro::Repository.new(value) : value | 45 | @repository = (value.kind_of?(Hash)) ? Kalibro::Repository.new(value) : value |
| 46 | end | 46 | end |
| 47 | 47 | ||
| 48 | - private | ||
| 49 | - | ||
| 50 | - def self.client | ||
| 51 | - endpoint = "Project" | ||
| 52 | - service_address = YAML.load_file("#{RAILS_ROOT}/plugins/mezuro/service.yaml") | ||
| 53 | - Savon::Client.new("#{service_address}#{endpoint}Endpoint/?wsdl") | ||
| 54 | - end | ||
| 55 | - | ||
| 56 | - def self.request(action, request_body = nil) | ||
| 57 | - response = client.request(:kalibro, action) { soap.body = request_body } | ||
| 58 | - response.to_hash["#{action}_response".to_sym] | ||
| 59 | - end | ||
| 60 | - | ||
| 61 | end | 48 | end |
| 62 | 49 |
plugins/mezuro/lib/mezuro_plugin/project_content.rb
| @@ -80,7 +80,7 @@ client.first_result_after(name, date) | @@ -80,7 +80,7 @@ client.first_result_after(name, date) | ||
| 80 | def send_project_to_service | 80 | def send_project_to_service |
| 81 | begin | 81 | begin |
| 82 | Kalibro::Project.create(self).save | 82 | Kalibro::Project.create(self).save |
| 83 | - Kalibro::Client::KalibroClient.process_project(name, periodicity_in_days) | 83 | + Kalibro::Kalibro.process_project(name, periodicity_in_days) |
| 84 | rescue Exception => error | 84 | rescue Exception => error |
| 85 | errors.add_to_base(error.message) | 85 | errors.add_to_base(error.message) |
| 86 | end | 86 | end |
plugins/mezuro/test/unit/kalibro/client/kalibro_client_test.rb
| @@ -1,43 +0,0 @@ | @@ -1,43 +0,0 @@ | ||
| 1 | -require "test_helper" | ||
| 2 | - | ||
| 3 | -class KalibroClientTest < ActiveSupport::TestCase | ||
| 4 | - | ||
| 5 | - def setup | ||
| 6 | - @port = mock | ||
| 7 | - Kalibro::Client::Port.expects(:new).with('Kalibro').returns(@port) | ||
| 8 | - @client = Kalibro::Client::KalibroClient.new | ||
| 9 | - end | ||
| 10 | - | ||
| 11 | - should 'get supported repository types' do | ||
| 12 | - types = ['BAZAAR', 'GIT', 'SUBVERSION'] | ||
| 13 | - @port.expects(:request).with(:get_supported_repository_types).returns({:repository_type => types}) | ||
| 14 | - assert_equal types, @client.supported_repository_types | ||
| 15 | - end | ||
| 16 | - | ||
| 17 | - should 'process project' do | ||
| 18 | - name = 'KalibroClientTest' | ||
| 19 | - @port.expects(:request).with(:process_project, {:project_name => name}) | ||
| 20 | - @client.process_project(name) | ||
| 21 | - end | ||
| 22 | - | ||
| 23 | - should 'instantiate for processing project' do | ||
| 24 | - instance = mock | ||
| 25 | - Kalibro::Client::KalibroClient.expects(:new).returns(instance) | ||
| 26 | - instance.expects(:process_project).with('myproject') | ||
| 27 | - Kalibro::Client::KalibroClient.process_project('myproject', 0) | ||
| 28 | - end | ||
| 29 | - | ||
| 30 | - should 'process project with periodicity' do | ||
| 31 | - name = 'KalibroClientTest' | ||
| 32 | - @port.expects(:request).with(:process_periodically, {:project_name => name, :period_in_days => 30}) | ||
| 33 | - @client.process_periodically(name, 30) | ||
| 34 | - end | ||
| 35 | - | ||
| 36 | - should 'instantiate for processing project periodically' do | ||
| 37 | - instance = mock | ||
| 38 | - Kalibro::Client::KalibroClient.expects(:new).returns(instance) | ||
| 39 | - instance.expects(:process_periodically).with('myproject', 30) | ||
| 40 | - Kalibro::Client::KalibroClient.process_project('myproject', 30) | ||
| 41 | - end | ||
| 42 | - | ||
| 43 | -end |
| @@ -0,0 +1,34 @@ | @@ -0,0 +1,34 @@ | ||
| 1 | +require "test_helper" | ||
| 2 | + | ||
| 3 | +class KalibroClientTest < ActiveSupport::TestCase | ||
| 4 | + | ||
| 5 | + def setup | ||
| 6 | + @name = 'KalibroTest' | ||
| 7 | + end | ||
| 8 | + | ||
| 9 | + should 'get supported repository types' do | ||
| 10 | + types = ['BAZAAR', 'GIT', 'SUBVERSION'] | ||
| 11 | + Kalibro::Kalibro.expects(:request).with('Kalibro', :get_supported_repository_types).returns({:repository_type => types}) | ||
| 12 | + assert_equal types, Kalibro::Kalibro.repository_types | ||
| 13 | + end | ||
| 14 | + | ||
| 15 | + should 'process project without days' do | ||
| 16 | + Kalibro::Kalibro.expects(:request).with('Kalibro', :process_project, {:project_name => @name}) | ||
| 17 | + Kalibro::Kalibro.process_project(@name) | ||
| 18 | + end | ||
| 19 | + | ||
| 20 | + should 'process project with days' do | ||
| 21 | + Kalibro::Kalibro.expects(:request).with('Kalibro', :process_periodically, {:project_name => @name, :period_in_days => "1"}) | ||
| 22 | + Kalibro::Kalibro.process_project(@name, "1") | ||
| 23 | + end | ||
| 24 | + | ||
| 25 | + should 'process period' do | ||
| 26 | + Kalibro::Kalibro.expects(:request).with('Kalibro', :get_process_period, {:project_name => @name}).returns({:period => "1"}) | ||
| 27 | + assert_equal "1", Kalibro::Kalibro.process_period(@name) | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | + should 'cancel periodic process' do | ||
| 31 | + Kalibro::Kalibro.expects(:request).with("Kalibro", :cancel_periodic_process, {:project_name => @name}) | ||
| 32 | + Kalibro::Kalibro.cancel_periodic_process(@name) | ||
| 33 | + end | ||
| 34 | +end |
plugins/mezuro/test/unit/kalibro/project_test.rb
| @@ -12,7 +12,7 @@ class ProjectTest < ActiveSupport::TestCase | @@ -12,7 +12,7 @@ class ProjectTest < ActiveSupport::TestCase | ||
| 12 | 12 | ||
| 13 | should 'get all project names' do | 13 | should 'get all project names' do |
| 14 | response_hash = {:project_name => [@project.name]} | 14 | response_hash = {:project_name => [@project.name]} |
| 15 | - Kalibro::Project.expects(:request).with(:get_project_names).returns(response_hash) | 15 | + Kalibro::Project.expects(:request).with("Project", :get_project_names).returns(response_hash) |
| 16 | assert_equal response_hash[:project_name], Kalibro::Project.all_names | 16 | assert_equal response_hash[:project_name], Kalibro::Project.all_names |
| 17 | end | 17 | end |
| 18 | 18 | ||
| @@ -20,33 +20,33 @@ class ProjectTest < ActiveSupport::TestCase | @@ -20,33 +20,33 @@ class ProjectTest < ActiveSupport::TestCase | ||
| 20 | request_body = {:project_name => @project.name} | 20 | request_body = {:project_name => @project.name} |
| 21 | response_hash = {:project => @hash} | 21 | response_hash = {:project => @hash} |
| 22 | Kalibro::Project.expects(:new).with(@hash).returns(@project) | 22 | Kalibro::Project.expects(:new).with(@hash).returns(@project) |
| 23 | - Kalibro::Project.expects(:request).with(:get_project, request_body).returns(response_hash) | 23 | + Kalibro::Project.expects(:request).with("Project", :get_project, request_body).returns(response_hash) |
| 24 | assert_equal @project, Kalibro::Project.find_by_name(@project.name) | 24 | assert_equal @project, Kalibro::Project.find_by_name(@project.name) |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | should 'raise error when project doesnt exist' do | 27 | should 'raise error when project doesnt exist' do |
| 28 | request_body = {:project_name => @project.name} | 28 | request_body = {:project_name => @project.name} |
| 29 | - Kalibro::Project.expects(:request).with(:get_project, request_body).raises(Exception.new("(S:Server) There is no project named " + @project.name)) | 29 | + Kalibro::Project.expects(:request).with("Project", :get_project, request_body).raises(Exception.new("(S:Server) There is no project named " + @project.name)) |
| 30 | assert_nil Kalibro::Project.find_by_name(@project.name) | 30 | assert_nil Kalibro::Project.find_by_name(@project.name) |
| 31 | end | 31 | end |
| 32 | 32 | ||
| 33 | should 'return true when project is saved successfully' do | 33 | should 'return true when project is saved successfully' do |
| 34 | - Kalibro::Project.expects(:request).with(:save_project, {:project => @project.to_hash}) | 34 | + Kalibro::Project.expects(:request).with("Project", :save_project, {:project => @project.to_hash}) |
| 35 | assert @project.save | 35 | assert @project.save |
| 36 | end | 36 | end |
| 37 | 37 | ||
| 38 | should 'return false when project is not saved successfully' do | 38 | should 'return false when project is not saved successfully' do |
| 39 | - Kalibro::Project.expects(:request).with(:save_project, {:project => @project.to_hash}).raises(Exception.new) | 39 | + Kalibro::Project.expects(:request).with("Project", :save_project, {:project => @project.to_hash}).raises(Exception.new) |
| 40 | assert !(@project.save) | 40 | assert !(@project.save) |
| 41 | end | 41 | end |
| 42 | 42 | ||
| 43 | should 'remove existent project from service' do | 43 | should 'remove existent project from service' do |
| 44 | - Kalibro::Project.expects(:request).with(:remove_project, {:project_name => @project.name}) | 44 | + Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}) |
| 45 | Kalibro::Project.destroy(@project.name) | 45 | Kalibro::Project.destroy(@project.name) |
| 46 | end | 46 | end |
| 47 | 47 | ||
| 48 | should 'raise error when try to remove inexistent project from service' do | 48 | should 'raise error when try to remove inexistent project from service' do |
| 49 | - Kalibro::Project.expects(:request).with(:remove_project, {:project_name => @project.name}).raises(Exception.new) | 49 | + Kalibro::Project.expects(:request).with("Project", :remove_project, {:project_name => @project.name}).raises(Exception.new) |
| 50 | assert_raise Exception do Kalibro::Project.destroy(@project.name) end | 50 | assert_raise Exception do Kalibro::Project.destroy(@project.name) end |
| 51 | end | 51 | end |
| 52 | 52 |
plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb
| @@ -96,7 +96,7 @@ returns(module_result) | @@ -96,7 +96,7 @@ returns(module_result) | ||
| 96 | project = mock | 96 | project = mock |
| 97 | Kalibro::Project.expects(:create).with(@content).returns(project) | 97 | Kalibro::Project.expects(:create).with(@content).returns(project) |
| 98 | project.expects(:save).returns(true) | 98 | project.expects(:save).returns(true) |
| 99 | - Kalibro::Client::KalibroClient.expects(:process_project).with(@content.name, @content.periodicity_in_days) | 99 | + Kalibro::Kalibro.expects(:process_project).with(@content.name, @content.periodicity_in_days) |
| 100 | @content.send :send_project_to_service | 100 | @content.send :send_project_to_service |
| 101 | end | 101 | end |
| 102 | 102 |