diff --git a/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb b/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb index 67e97d219..cc0447bbf 100644 --- a/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb +++ b/lib/pact_broker/api/decorators/verifiable_pact_decorator.rb @@ -1,15 +1,13 @@ require_relative 'base_decorator' require 'pact_broker/api/pact_broker_urls' -require 'delegate' -require 'pact_broker/pacts/verifiable_pact_messages' +require 'pact_broker/pacts/build_verifiable_pact_notices' module PactBroker module Api module Decorators class VerifiablePactDecorator < BaseDecorator - def initialize(verifiable_pact) - super(verifiable_pact) - end + + property :shortDescription, getter: -> (context) { PactBroker::Pacts::VerifiablePactMessages.new(context[:represented], nil).pact_version_short_description } nested :verificationProperties do include PactBroker::Api::PactBrokerUrls @@ -22,32 +20,8 @@ def initialize(verifiable_pact) property :noteToDevelopers, getter: -> (_) { "Please print out the text from the 'notices' rather than using the inclusionReason and the pendingReason fields. These will be removed when this API moves out of beta."} def notices(user_options) - # TODO move this out of the decorator pact_url = pact_version_url(represented, user_options[:base_url]) - messages = PactBroker::Pacts::VerifiablePactMessages.new(represented, pact_url) - - the_notices = [{ - when: 'before_verification', - text: messages.inclusion_reason - }] - - if user_options[:include_pending_status] - append_notice(the_notices, 'before_verification', messages.pending_reason) - append_notice(the_notices, 'after_verification:success_true_published_false', messages.verification_success_true_published_false) - append_notice(the_notices, 'after_verification:success_false_published_false', messages.verification_success_false_published_false) - append_notice(the_notices, 'after_verification:success_true_published_true', messages.verification_success_true_published_true) - append_notice(the_notices, 'after_verification:success_false_published_true', messages.verification_success_false_published_true) - end - the_notices - end - - def append_notice the_notices, the_when, text - if text - the_notices << { - when: the_when, - text: text - } - end + PactBroker::Pacts::BuildVerifiablePactNotices.call(represented, pact_url, include_pending_status: user_options[:include_pending_status]) end end diff --git a/lib/pact_broker/pacts/build_verifiable_pact_notices.rb b/lib/pact_broker/pacts/build_verifiable_pact_notices.rb new file mode 100644 index 000000000..c3b3fec12 --- /dev/null +++ b/lib/pact_broker/pacts/build_verifiable_pact_notices.rb @@ -0,0 +1,35 @@ +require 'pact_broker/pacts/verifiable_pact_messages' + +module PactBroker + module Pacts + class BuildVerifiablePactNotices + + def self.call(verifiable_pact, pact_url, options) + messages = VerifiablePactMessages.new(verifiable_pact, pact_url) + + notices = [{ + when: 'before_verification', + text: messages.inclusion_reason + }] + + if options[:include_pending_status] + append_notice(notices, 'before_verification', messages.pending_reason) + append_notice(notices, 'after_verification:success_true_published_false', messages.verification_success_true_published_false) + append_notice(notices, 'after_verification:success_false_published_false', messages.verification_success_false_published_false) + append_notice(notices, 'after_verification:success_true_published_true', messages.verification_success_true_published_true) + append_notice(notices, 'after_verification:success_false_published_true', messages.verification_success_false_published_true) + end + notices + end + + def self.append_notice notices, the_when, text + if text + notices << { + when: the_when, + text: text + } + end + end + end + end +end diff --git a/lib/pact_broker/pacts/verifiable_pact_messages.rb b/lib/pact_broker/pacts/verifiable_pact_messages.rb index c1dbf94c7..a655b6621 100644 --- a/lib/pact_broker/pacts/verifiable_pact_messages.rb +++ b/lib/pact_broker/pacts/verifiable_pact_messages.rb @@ -64,6 +64,10 @@ def verification_success_false_published_true verification_success_false_published_false end + def pact_version_short_description + short_selector_descriptions + end + private attr_reader :verifiable_pact, :pact_version_url @@ -141,6 +145,27 @@ def selector_description selector end end + def short_selector_descriptions + selectors.collect{ | selector | short_selector_description(selector) }.join(", ") + end + + # this is used by Pact Go to create the test method name, so needs to be concise + def short_selector_description selector + if selector.overall_latest? + "latest" + elsif selector.latest_for_tag? + if selector.fallback_tag? + "latest #{selector.fallback_tag}" + else + "latest #{selector.tag}" + end + elsif selector.tag + "one of #{selector.tag}" + else + selector.to_json + end + end + def selectors verifiable_pact.selectors end diff --git a/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb index f4be29974..4c0bdf634 100644 --- a/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/verifiable_pact_decorator_spec.rb @@ -5,53 +5,41 @@ module Api module Decorators describe VerifiablePactDecorator do before do - allow_any_instance_of(PactBroker::Api::PactBrokerUrls).to receive(:pact_version_url).and_return('/pact-version-url') - allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:inclusion_reason).and_return("the inclusion reason") - allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:pending_reason).and_return(pending_reason) - allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:verification_success_true_published_false).and_return('verification_success_true_published_false') - allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:verification_success_false_published_false).and_return('verification_success_false_published_false') - allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:verification_success_true_published_true).and_return('verification_success_true_published_true') - allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:verification_success_false_published_true).and_return('verification_success_false_published_true') + allow_any_instance_of(PactBroker::Api::PactBrokerUrls).to receive(:pact_version_url).and_return('http://pact') + allow(PactBroker::Pacts::BuildVerifiablePactNotices).to receive(:call).and_return(notices) + allow_any_instance_of(PactBroker::Pacts::VerifiablePactMessages).to receive(:pact_version_short_description).and_return('short desc') end + let(:pending_reason) { "the pending reason" } + let(:notices) do + [ + { + some: "notice" + } + ] + end let(:expected_hash) do { + "shortDescription" => "short desc", "verificationProperties" => { "pending" => true, "notices" => [ { - "when" => "before_verification", - "text" => "the inclusion reason" - },{ - "when" => "before_verification", - "text" => pending_reason - },{ - "when" => "after_verification:success_true_published_false", - "text" => "verification_success_true_published_false" - },{ - "when" => "after_verification:success_false_published_false", - "text" => "verification_success_false_published_false" - },{ - "when" => "after_verification:success_true_published_true", - "text" => "verification_success_true_published_true" - },{ - "when" => "after_verification:success_false_published_true", - "text" => "verification_success_false_published_true" + "some" => "notice" } ] }, "_links" => { "self" => { - "href" => "/pact-version-url", + "href" => "http://pact", "name" => "name" } } } end - let(:decorator) { VerifiablePactDecorator.new(pact) } let(:pact) do - double('pact', + double('PactBroker::Pacts::VerifiablePact', pending: true, wip: wip, name: "name", @@ -61,10 +49,10 @@ module Decorators end let(:pending_provider_tags) { %w[dev] } let(:consumer_tags) { %w[dev] } - let(:json) { decorator.to_json(options) } let(:options) { { user_options: { base_url: 'http://example.org', include_pending_status: include_pending_status } } } let(:include_pending_status) { true } let(:wip){ false } + let(:json) { decorator.to_json(options) } subject { JSON.parse(json) } @@ -77,27 +65,17 @@ module Decorators subject end - it "creates the inclusion message" do - expect(PactBroker::Pacts::VerifiablePactMessages).to receive(:new).with(pact, '/pact-version-url').and_call_original + it "creates the notices" do + allow(PactBroker::Pacts::BuildVerifiablePactNotices).to receive(:call).with(pact, 'http://pact', include_pending_status: include_pending_status) subject end context "when include_pending_status is false" do let(:include_pending_status) { false } - let(:notices) { subject['verificationProperties']['notices'].collect{ | notice | notice['text'] } } it "does not include the pending flag" do expect(subject['verificationProperties']).to_not have_key('pending') end - - it "does not include the pending reason" do - expect(subject['verificationProperties']).to_not have_key('pendingReason') - expect(notices).to_not include(pending_reason) - end - - it "does not include the pending notices" do - expect(subject['verificationProperties']['notices'].size).to eq 1 - end end context "when wip is true" do diff --git a/spec/lib/pact_broker/pacts/build_verifiable_pact_notices_spec.rb b/spec/lib/pact_broker/pacts/build_verifiable_pact_notices_spec.rb new file mode 100644 index 000000000..5cbe338bd --- /dev/null +++ b/spec/lib/pact_broker/pacts/build_verifiable_pact_notices_spec.rb @@ -0,0 +1,76 @@ +require 'pact_broker/pacts/build_verifiable_pact_notices' + +module PactBroker + module Pacts + describe BuildVerifiablePactNotices do + before do + allow(VerifiablePactMessages).to receive(:new).and_return(verifiable_pact_messages) + end + + let(:verifiable_pact_messages) do + instance_double(VerifiablePactMessages, + inclusion_reason: 'the inclusion reason', + pending_reason: 'pending reason', + verification_success_true_published_false: 'verification_success_true_published_false', + verification_success_false_published_false: 'verification_success_false_published_false', + verification_success_true_published_true: 'verification_success_true_published_true', + verification_success_false_published_true: 'verification_success_false_published_true' + ) + end + + let(:options) { {} } + let(:pact_url) { 'http://pact' } + let(:verifiable_pact) { instance_double('PactBroker::Pacts::VerifiablePact') } + + subject { BuildVerifiablePactNotices.call(verifiable_pact, pact_url, options) } + + + context "when include_pending_status is true" do + let(:expected_notices) do + [ + { + :when => "before_verification", + :text => "the inclusion reason" + },{ + :when => "before_verification", + :text => 'pending reason' + },{ + :when => "after_verification:success_true_published_false", + :text => "verification_success_true_published_false" + },{ + :when => "after_verification:success_false_published_false", + :text => "verification_success_false_published_false" + },{ + :when => "after_verification:success_true_published_true", + :text => "verification_success_true_published_true" + },{ + :when => "after_verification:success_false_published_true", + :text => "verification_success_false_published_true" + } + ] + end + + let(:options) { { include_pending_status: true } } + + it "it returns a list of notices with information about the pending status" do + expect(subject).to eq expected_notices + end + end + + context "when include_pending_status is not true" do + let(:expected_notices) do + [ + { + :when => "before_verification", + :text => "the inclusion reason" + } + ] + end + + it "it returns a list of notices without information about the pending status" do + expect(subject).to eq expected_notices + end + end + end + end +end diff --git a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb index 6fdd08dce..38e754b5d 100644 --- a/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb +++ b/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb @@ -9,20 +9,20 @@ module Pacts let(:non_pending_provider_tags) { [] } let(:pending) { false } let(:wip) { false } + let(:selectors) { Selectors.new } + let(:pact_version_url) { "http://pact" } let(:verifiable_pact) do double(VerifiablePact, - consumer_name: "Foo", - consumer_version_number: "123", - provider_name: "Bar", - pending_provider_tags: pending_provider_tags, - non_pending_provider_tags: non_pending_provider_tags, - pending?: pending, - wip?: wip, - selectors: selectors + consumer_name: "Foo", + consumer_version_number: "123", + provider_name: "Bar", + pending_provider_tags: pending_provider_tags, + non_pending_provider_tags: non_pending_provider_tags, + pending?: pending, + wip?: wip, + selectors: selectors ) end - let(:selectors) { [] } - let(:pact_version_url) { "http://pact" } subject { VerifiablePactMessages.new(verifiable_pact, pact_version_url) }