From 3b49f0397b62279cdddc05967417ee1490709a1d Mon Sep 17 00:00:00 2001 From: Rashi Agarwal Date: Thu, 14 Jun 2018 22:07:05 +1000 Subject: [PATCH] feat: handling tags ['tag-1', {name: 'tag-2', all: true}] --- lib/pact/pact_broker/fetch_pacts.rb | 29 +++-------------- .../pact_ruby_fetch_pacts_test.rb | 31 +++++++------------ 2 files changed, 16 insertions(+), 44 deletions(-) diff --git a/lib/pact/pact_broker/fetch_pacts.rb b/lib/pact/pact_broker/fetch_pacts.rb index 06bec7d5..d86942de 100644 --- a/lib/pact/pact_broker/fetch_pacts.rb +++ b/lib/pact/pact_broker/fetch_pacts.rb @@ -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) @@ -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 diff --git a/spec/service_providers/pact_ruby_fetch_pacts_test.rb b/spec/service_providers/pact_ruby_fetch_pacts_test.rb index dd317d4d..97425b2a 100644 --- a/spec/service_providers/pact_ruby_fetch_pacts_test.rb +++ b/spec/service_providers/pact_ruby_fetch_pacts_test.rb @@ -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}$} - ) } } } @@ -62,7 +56,6 @@ context 'retrieving latest pacts by provider' do let(:tags) { nil } - let(:all_pacts) { false } before do pact_broker @@ -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 @@ -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', @@ -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)') @@ -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( @@ -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