Skip to content

Commit

Permalink
feat: add applicationInstance to deployed version resource as a repla…
Browse files Browse the repository at this point in the history
…cement for target
  • Loading branch information
bethesque committed Oct 14, 2021
1 parent 956227f commit 9bfafc8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 15 deletions.
3 changes: 2 additions & 1 deletion lib/pact_broker/api/decorators/deployed_version_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ module Decorators
class DeployedVersionDecorator < BaseDecorator
property :uuid
property :currently_deployed, camelize: true
property :target, camelize: true
property :target, camelize: true # deprecated
property :applicationInstance, getter: lambda { |_| target }
include Timestamps
property :undeployedAt, getter: lambda { |_| undeployed_at ? FormatDateTime.call(undeployed_at) : nil }, writeable: false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def query_params
query = Rack::Utils.parse_query(request.env["QUERY_STRING"])
q = {}
q[:pacticipant_name] = request.query["pacticipant"] if query["pacticipant"]
if query["target"]
if query["applicationInstance"]
q[:target] = query["applicationInstance"].blank? ? nil : query["applicationInstance"]
elsif query["target"]
q[:target] = query["target"].blank? ? nil : query["target"]
end
q
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def create_path
end

def from_json
@deployed_version = deployed_version_service.find_or_create(deployed_version_uuid, version, environment, target)
@deployed_version = deployed_version_service.find_or_create(deployed_version_uuid, version, environment, application_instance)
response.headers["Location"] = deployed_version_url(deployed_version, base_url)
response.body = decorator_class(:deployed_version_decorator).new(deployed_version).to_json(decorator_options)
end
Expand Down Expand Up @@ -72,8 +72,9 @@ def deployed_version_uuid
end

# TODO disallow an empty string because that is used as a NULL indicator in the database
def target
params(default: {})[:target]&.to_s
def application_instance
parameters = params(default: {})
(parameters[:applicationInstance] || parameters[:target])&.to_s
end

def title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,48 @@
end

context "with query params" do

context "with a pacticipant name and target" do
let(:query_params) { { pacticipant: "Bar", target: "customer-1" } }
context "with a pacticipant name and applicationInstance" do
let(:query_params) { { pacticipant: "Bar", applicationInstance: "customer-1" } }

it "returns a list of matching deployed versions" do
expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1
expect(response_body_hash[:_embedded][:deployedVersions].first[:_embedded][:version][:number]).to eq "4"
end
end

context "with pacticipant name and a blank target" do
let(:query_params) { { pacticipant: "Bar", target: "" } }
context "with pacticipant name and a blank applicationInstance" do
let(:query_params) { { pacticipant: "Bar", applicationInstance: "" } }

it "returns a list of matching deployed versions" do
expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1
expect(response_body_hash[:_embedded][:deployedVersions].first[:_embedded][:version][:number]).to eq "5"
end
end

context "with pacticipant name and no target" do
context "with pacticipant name and no target or applicationInstance" do
let(:query_params) { { pacticipant: "Bar" } }

it "returns a list of matching deployed versions" do
expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 2
end
end

context "with a pacticipant name and target (deprecated)" do
let(:query_params) { { pacticipant: "Bar", target: "customer-1" } }

it "returns a list of matching deployed versions" do
expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1
expect(response_body_hash[:_embedded][:deployedVersions].first[:_embedded][:version][:number]).to eq "4"
end
end

context "with pacticipant name and a blank target (deprecated)" do
let(:query_params) { { pacticipant: "Bar", target: "" } }

it "returns a list of matching deployed versions" do
expect(response_body_hash[:_embedded][:deployedVersions].size).to eq 1
expect(response_body_hash[:_embedded][:deployedVersions].first[:_embedded][:version][:number]).to eq "5"
end
end
end
end
21 changes: 17 additions & 4 deletions spec/features/record_deployment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
let(:response_body) { JSON.parse(subject.body, symbolize_names: true) }
let(:version_path) { "/pacticipants/Foo/versions/2" }
let(:version_response) { get(version_path, nil, { "HTTP_ACCEPT" => "application/hal+json" } ) }
let(:target) { nil }
let(:application_instance) { nil }
let(:path) do
JSON.parse(version_response.body)["_links"]["pb:record-deployment"]
.find{ |relation| relation["name"] == "test" }
.fetch("href")
end
let(:request_body) { { target: target }.to_json }
let(:request_body) { { applicationInstance: application_instance }.to_json }

subject { post(path, request_body, headers) }

Expand All @@ -33,6 +33,7 @@
it "returns the newly created deployment" do
expect(response_body[:currentlyDeployed]).to be true
expect(response_body).to_not have_key(:target)
expect(response_body).to_not have_key(:application_instance)
end

it "creates a new deployed version" do
Expand Down Expand Up @@ -79,8 +80,20 @@
it { is_expected.to be_a_hal_json_created_response }
end

context "when the deployment is to a different target" do
let(:target) { "foo" }
context "when the deployment is to a different application instance" do
let(:application_instance) { "foo" }

it "creates a new deployed version" do
expect { subject }.to change { PactBroker::Deployments::DeployedVersion.currently_deployed.count }.by(1)
end

it "sets the applicationInstance" do
expect(response_body).to have_key(:applicationInstance)
end
end

context "when the deployment is to a different target (deprecated)" do
let(:request_body) { { target: "foo" }.to_json }

it "creates a new deployed version" do
expect { subject }.to change { PactBroker::Deployments::DeployedVersion.currently_deployed.count }.by(1)
Expand Down

0 comments on commit 9bfafc8

Please sign in to comment.