Skip to content

Commit

Permalink
feat: add hal+json endpoint for listing integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed May 10, 2019
1 parent 634ccd5 commit 40f3e02
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 2 deletions.
20 changes: 20 additions & 0 deletions db/migrations/20190511_create_integrations_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Sequel.migration do
up do
create_view(:integrations,
from(:pact_publications)
.select(
:consumer_id,
Sequel[:c][:name].as(:consumer_name),
:provider_id,
Sequel[:p][:name].as(:provider_name)
).distinct
.join(:pacticipants, {:id => :consumer_id}, {:table_alias => :c, implicit_qualifier: :pact_publications})
.join(:pacticipants, {:id => :provider_id}, {:table_alias => :p, implicit_qualifier: :pact_publications})

)
end

down do
drop_view(:integrations)
end
end
17 changes: 17 additions & 0 deletions lib/pact_broker/api/decorators/integration_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require_relative 'base_decorator'

module PactBroker
module Api
module Decorators
class IntegrationDecorator < BaseDecorator
property :consumer do
property :name
end

property :provider do
property :name
end
end
end
end
end
19 changes: 19 additions & 0 deletions lib/pact_broker/api/decorators/integrations_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require_relative 'base_decorator'
require_relative 'integration_decorator'

module PactBroker
module Api
module Decorators
class IntegrationsDecorator < BaseDecorator
collection :entries, as: :integrations, embedded: true, :extend => PactBroker::Api::Decorators::IntegrationDecorator

link :self do | context |
{
href: context[:resource_url],
title: "All integrations"
}
end
end
end
end
end
12 changes: 10 additions & 2 deletions lib/pact_broker/api/resources/integrations.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
require 'pact_broker/api/resources/base_resource'
require 'pact_broker/api/renderers/integrations_dot_renderer'
require 'pact_broker/api/decorators/integrations_decorator'

module PactBroker
module Api
module Resources
class Integrations < BaseResource
def content_types_provided
[["text/vnd.graphviz", :to_dot]]
[
["text/vnd.graphviz", :to_dot],
["application/hal+json", :to_json]
]
end

def allowed_methods
Expand All @@ -17,8 +21,12 @@ def to_dot
PactBroker::Api::Renderers::IntegrationsDotRenderer.call(integrations)
end

def to_json
PactBroker::Api::Decorators::IntegrationsDecorator.new(integrations).to_json(user_options: decorator_context)
end

def integrations
pact_service.find_latest_pacts
integration_service.find_all
end

def delete_resource
Expand Down
10 changes: 10 additions & 0 deletions lib/pact_broker/integrations/integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'pact_broker/db'

module PactBroker
module Integrations
class Integration < Sequel::Model
associate(:many_to_one, :consumer, :class => "PactBroker::Domain::Pacticipant", :key => :consumer_id, :primary_key => :id)
associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id)
end
end
end
5 changes: 5 additions & 0 deletions lib/pact_broker/integrations/service.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pact_broker/services'
require 'pact_broker/repositories'
require 'pact_broker/logging'
require 'pact_broker/integrations/integration'

module PactBroker
module Integrations
Expand All @@ -9,6 +10,10 @@ class Service
extend PactBroker::Services
include PactBroker::Logging

def self.find_all
PactBroker::Integrations::Integration.eager(:consumer).eager(:provider).all
end

def self.delete(consumer_name, provider_name)
consumer = pacticipant_service.find_pacticipant_by_name(consumer_name)
provider = pacticipant_service.find_pacticipant_by_name(provider_name)
Expand Down
17 changes: 17 additions & 0 deletions spec/features/get_integrations_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe "Get integrations dot file" do
before do
TestDataBuilder.new
.create_pact_with_hierarchy("Foo", "1", "Bar")
end

let(:path) { "/integrations" }
let(:response_body_hash) { JSON.parse(subject.body, symbolize_names: true) }

subject { get path, nil, {'HTTP_ACCEPT' => 'application/hal+json' } }

it { is_expected.to be_a_hal_json_success_response }

it "returns a json body with embedded integrations" do
expect(JSON.parse(subject.body)["_embedded"]["integrations"]).to be_a(Array)
end
end
37 changes: 37 additions & 0 deletions spec/lib/pact_broker/api/decorators/integration_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'pact_broker/api/decorators/integration_decorator'
require 'pact_broker/integrations/integration'

module PactBroker
module Api
module Decorators
describe IntegrationDecorator do
let(:integration) do
instance_double(PactBroker::Integrations::Integration,
consumer: consumer,
provider: provider
)
end
let(:consumer) { double("consumer", name: "the consumer") }
let(:provider) { double("provider", name: "the provider") }

let(:expected_hash) do
{
"consumer" => {
"name" => "the consumer"
},
"provider" => {
"name" => "the provider"
}
}
end

let(:json) { IntegrationDecorator.new(integration).to_json }
subject { JSON.parse(json) }

it "generates a hash" do
expect(subject).to match_pact expected_hash
end
end
end
end
end
29 changes: 29 additions & 0 deletions spec/lib/pact_broker/api/decorators/integrations_decorator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'pact_broker/api/decorators/integrations_decorator'

module PactBroker
module Api
module Decorators
describe IntegrationsDecorator do
before do
allow(IntegrationDecorator).to receive(:new).and_return(integration_decorator)
end
let(:integration_decorator) { instance_double(IntegrationDecorator).as_null_object }
let(:integration) { double('integration') }
let(:integrations_decorator) { IntegrationsDecorator.new([integration]) }
let(:options) { { user_options: { resource_url: 'http://example.org/integrations' } } }

let(:json) { integrations_decorator.to_json(options) }

subject { JSON.parse(json) }

it "includes a list of integrations" do
expect(subject["_embedded"]["integrations"]).to be_an(Array)
end

it "includes a link to itself" do
expect(subject["_links"]["self"]["href"]).to eq "http://example.org/integrations"
end
end
end
end
end

0 comments on commit 40f3e02

Please sign in to comment.