-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add content type to return pact with extra metadata (eg tags)
- Loading branch information
Showing
10 changed files
with
147 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'pact_broker/api/decorators/pact_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
class ExtendedPactDecorator < PactDecorator | ||
class TagDecorator < BaseDecorator | ||
property :name | ||
property :latest, getter: ->(_) { true } | ||
|
||
link "pb:latest-pact" do | opts | | ||
{ | ||
name: "The latest pact with the tag #{represented.name}", | ||
href: latest_tagged_pact_url(represented.pact, represented.name, opts[:base_url]) | ||
} | ||
end | ||
end | ||
|
||
property :content_hash, as: :contract | ||
collection :head_tags, exec_context: :decorator, as: :tags, embedded: true, extend: TagDecorator | ||
|
||
# TODO rather than remove the contract keys that we added in the super class, | ||
# it would be better to inherit from a shared super class | ||
def to_hash(options = {}) | ||
keys_to_remove = represented.content_hash.keys | ||
super.each_with_object({}) do | (key, value), new_hash | | ||
new_hash[key] = value unless keys_to_remove.include?(key) | ||
end | ||
end | ||
|
||
def head_tags | ||
represented.head_tag_names.collect do | tag_name | | ||
OpenStruct.new(name: tag_name, pact: represented) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
spec/lib/pact_broker/api/decorators/extended_pact_decorator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'pact_broker/api/decorators/extended_pact_decorator' | ||
|
||
module PactBroker | ||
module Api | ||
module Decorators | ||
describe ExtendedPactDecorator do | ||
before do | ||
allow(decorator).to receive(:templated_diff_url).and_return('templated-diff-url') | ||
allow(decorator).to receive(:verification_publication_url).and_return('verification-publication-url') | ||
end | ||
let(:content_hash) { | ||
{ | ||
'consumer' => {'name' => 'Consumer'}, | ||
'provider' => {'name' => 'Provider'}, | ||
'interactions' => [], | ||
'metadata' => {} | ||
} | ||
} | ||
|
||
let(:base_url) { 'http://example.org' } | ||
let(:created_at) { Time.new(2014, 3, 4) } | ||
let(:pact) { double('pact', | ||
content_hash: content_hash, | ||
created_at: created_at, | ||
consumer: consumer, | ||
provider: provider, | ||
consumer_version: consumer_version, | ||
consumer_version_number: '1234', | ||
pact_version_sha: '9999', | ||
revision_number: 2, | ||
name: 'A Pact', | ||
head_tag_names: head_tag_names | ||
)} | ||
let(:head_tag_names) { ['prod'] } | ||
let(:consumer) { instance_double(PactBroker::Domain::Pacticipant, name: 'A Consumer')} | ||
let(:provider) { instance_double(PactBroker::Domain::Pacticipant, name: 'A Provider')} | ||
let(:consumer_version) { instance_double(PactBroker::Domain::Version, number: '1234', pacticipant: consumer)} | ||
let(:metadata) { "abcd" } | ||
let(:decorator) { ExtendedPactDecorator.new(pact) } | ||
let(:json) { decorator.to_json(user_options: { base_url: base_url, metadata: metadata }) } | ||
subject { JSON.parse(json, symbolize_names: true) } | ||
|
||
it "includes an array of tags" do | ||
expect(subject[:_embedded][:tags].first).to include name: 'prod', latest: true | ||
# Can't seem to stub the verification_publication_url method on the TagDecorator | ||
expect(subject[:_embedded][:tags].first[:_links][:'pb:latest-pact'][:href]).to eq "http://example.org/pacts/provider/A%20Provider/consumer/A%20Consumer/latest/prod" | ||
end | ||
|
||
it "includes the pact contents under the contract key" do | ||
expect(subject[:contract]).to eq JSON.parse(content_hash.to_json, symbolize_names: true) | ||
end | ||
|
||
it "does not include the contract contents in the root" do | ||
expect(subject).to_not have_key(:interactions) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters