Skip to content

Commit

Permalink
feat: handling tags ['tag-1', {name: 'tag-2', all: true}]
Browse files Browse the repository at this point in the history
  • Loading branch information
rashiagarwal committed Jun 22, 2018
1 parent 7313818 commit 3b49f03
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 44 deletions.
29 changes: 5 additions & 24 deletions lib/pact/pact_broker/fetch_pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,32 @@
module Pact
module PactBroker
class FetchPacts
attr_reader :provider, :tags, :broker_base_url, :basic_auth_options, :http_client, :index_entity, :all_pacts
attr_reader :provider, :tags, :broker_base_url, :basic_auth_options, :http_client, :index_entity

ALL_PROVIDER_TAG_RELATION = 'pb:provider-pacts-with-tag'.freeze
LATEST_PROVIDER_TAG_RELATION = 'pb:latest-provider-pacts-with-tag'.freeze
ALL_PROVIDER_RELATION = 'pb:provider-pacts'.freeze
LATEST_PROVIDER_RELATION = 'pb:latest-provider-pacts'.freeze
PACTS = 'pacts'.freeze
HREF = 'href'.freeze

def initialize(provider, tags, broker_base_url, basic_auth_options, all_pacts)
def initialize(provider, tags, broker_base_url, basic_auth_options)
@provider = provider
@tags = tags
@broker_base_url = broker_base_url
@http_client = Pact::Hal::HttpClient.new(basic_auth_options)
@all_pacts = all_pacts
end

def self.call(provider, tags, broker_base_url, basic_auth_options, all_pacts)
new(provider, tags, broker_base_url, basic_auth_options, all_pacts).call
def self.call(provider, tags, broker_base_url, basic_auth_options)
new(provider, tags, broker_base_url, basic_auth_options).call
end

def call
get_index
if tags && tags.any?
get_tagged_pacts_for_provider
else
get_pacts_for_provider
end
tags&.any? ? get_tagged_pacts_for_provider : get_latest_pacts_for_provider
end

private

def get_pacts_for_provider
if all_pacts
get_all_pacts_for_provider
else
get_latest_pacts_for_provider
end
end

def get_tagged_pacts_for_provider
tags.collect do |tag|
link = get_link(tag)
Expand Down Expand Up @@ -78,11 +64,6 @@ def get_latest_pacts_for_provider
get_pact_urls(link.expand(provider: provider).get)
end

def get_all_pacts_for_provider
link = index_entity._link(ALL_PROVIDER_RELATION)
get_pact_urls(link.expand(provider: provider).get)
end

def get_pact_urls(link_by_provider)
link_by_provider.fetch(PACTS).collect { |pact| pact[HREF] }
end
Expand Down
31 changes: 11 additions & 20 deletions spec/service_providers/pact_ruby_fetch_pacts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@
generate: broker_base_url + 'pacts/provider/{provider}/tag/{tag}',
matcher: %r{/pacts/provider/{provider}/tag/{tag}$}
)
},
'pb:provider-pacts' => {
href: Pact.term(
generate: broker_base_url + 'pacts/provider/{provider}',
matcher: %r{/pacts/provider/{provider}$}
)
}
}
}
Expand All @@ -62,7 +56,6 @@

context 'retrieving latest pacts by provider' do
let(:tags) { nil }
let(:all_pacts) { false }

before do
pact_broker
Expand Down Expand Up @@ -91,15 +84,14 @@
end

it 'returns the array of pact urls' do
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options, all_pacts)
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options)

expect(pacts).to eq(%w[http://pact-broker-url-for-consumer-1 http://pact-broker-url-for-consumer-2])
end
end

context 'retrieving latest pacts by provider with the specified tag' do
let(:tags) { %w[tag-1 tag-2] }
let(:all_pacts) { false }
let(:tags) { ['tag-1', { name: 'tag-2', all: false }] }

before do
pact_broker
Expand Down Expand Up @@ -151,21 +143,20 @@
end

it 'returns the array of pact urls' do
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options, all_pacts)
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options)

expect(pacts).to eq(%w[http://pact-broker-url-for-consumer-1-tag-1 http://pact-broker-url-for-consumer-2-tag-1
http://pact-broker-url-for-consumer-1-tag-2 http://pact-broker-url-for-consumer-2-tag-2])
end
end

context 'retrieving all pact versions for the provider with the specified consumer version tag' do
context 'retrieving all pact versions for tag-2 and latest pact versions for tag-1 for the provider with the specified consumer version tag' do
let(:tags) { ['tag-1', { name: 'tag-2', all: true }] }
let(:all_pacts) { true }

before do
pact_broker
.given('consumer-1 and consumer-2 have 2 pacts with provider provider-1 tagged with tag-1')
.upon_receiving('a request to retrieve all pact versions for the provider with the specified consumer version tag (tag-1)')
.upon_receiving('a request to retrieve latest pact versions for the provider with the specified consumer version tag (tag-1)')
.with(
method: :get,
path: '/pacts/provider/provider-1/latest/tag-1',
Expand All @@ -186,6 +177,7 @@
}
}
)

pact_broker
.given('consumer-1 and consumer-2 have 2 pacts with provider provider-1 tagged with tag-2')
.upon_receiving('a request to retrieve all pact versions for the provider with the specified consumer version tag (tag-2)')
Expand All @@ -212,24 +204,23 @@
end

it 'returns the array of pact urls' do
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options, all_pacts)
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options)

expect(pacts).to eq(%w[http://pact-broker-url-for-consumer-1-tag-1 http://pact-broker-url-for-consumer-2-tag-1
http://pact-broker-url-for-consumer-1-tag-2-all http://pact-broker-url-for-consumer-2-tag-2-all])
end
end

context 'retrieving all pact versions for the specified provider' do
context 'retrieving all the latest pact versions for the specified provider' do
let(:tags) { nil }
let(:all_pacts) { true }

before do
pact_broker
.given('consumer-1 and consumer-2 have 2 pacts with provider provider-1')
.upon_receiving('a request to retrieve all pact versions for the specified provider')
.upon_receiving('a request to retrieve latest pacts for the specified provider')
.with(
method: :get,
path: '/pacts/provider/provider-1',
path: '/pacts/provider/provider-1/latest',
headers: get_headers
)
.will_respond_with(
Expand All @@ -250,7 +241,7 @@
end

it 'returns the array of pact urls' do
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options, all_pacts)
pacts = Pact::PactBroker::FetchPacts.call(provider, tags, broker_base_url, basic_auth_options)

expect(pacts).to eq(%w[http://pact-broker-url-for-consumer-1-all http://pact-broker-url-for-consumer-2-all])
end
Expand Down

0 comments on commit 3b49f03

Please sign in to comment.