Skip to content

Commit

Permalink
feat(pacts for verification): include a short description of the pact…
Browse files Browse the repository at this point in the history
…'s selectors in the response
  • Loading branch information
bethesque committed Feb 6, 2020
1 parent 4308117 commit 41c6d91
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 80 deletions.
34 changes: 4 additions & 30 deletions lib/pact_broker/api/decorators/verifiable_pact_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
35 changes: 35 additions & 0 deletions lib/pact_broker/pacts/build_verifiable_pact_notices.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions lib/pact_broker/pacts/verifiable_pact_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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) }

Expand All @@ -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
Expand Down
76 changes: 76 additions & 0 deletions spec/lib/pact_broker/pacts/build_verifiable_pact_notices_spec.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down

0 comments on commit 41c6d91

Please sign in to comment.