diff --git a/lib/pact_broker/test/test_data_builder.rb b/lib/pact_broker/test/test_data_builder.rb index bfe1d41d2..c2351f1e5 100644 --- a/lib/pact_broker/test/test_data_builder.rb +++ b/lib/pact_broker/test/test_data_builder.rb @@ -6,6 +6,7 @@ require 'pact_broker/domain/webhook_execution_result' require 'pact_broker/pacts/repository' require 'pact_broker/pacts/service' +require 'pact_broker/pacts/content' require 'pact_broker/pacticipants/repository' require 'pact_broker/pacticipants/service' require 'pact_broker/versions/repository' @@ -52,64 +53,22 @@ def comment *args end def create_pricing_service - @pricing_service_id = pacticipant_repository.create(:name => 'Pricing Service', :repository_url => 'git@git.realestate.com.au:business-systems/pricing-service').save(raise_on_save_failure: true).id + create_provider("Pricing Service", :repository_url => 'git@git.realestate.com.au:business-systems/pricing-service') self end def create_contract_proposal_service - @contract_proposal_service_id = pacticipant_repository.create(:name => 'Contract Proposal Service', :repository_url => 'git@git.realestate.com.au:business-systems/contract-proposal-service').save(raise_on_save_failure: true).id - self - end - - def create_contract_proposal_service_version number - @contract_proposal_service_version_id = version_repository.create(number: number, pacticipant_id: @contract_proposal_service_id).id + create_provider("Contract Proposal Service", :repository_url => 'git@git.realestate.com.au:business-systems/contract-proposal-service') self end def create_contract_email_service - @contract_email_service_id = pacticipant_repository.create(:name => 'Contract Email Service', :repository_url => 'git@git.realestate.com.au:business-systems/contract-email-service').save(raise_on_save_failure: true).id - self - end - - def create_contract_email_service_version number - @contract_email_service_version_id = version_repository.create(number: number, pacticipant_id: @contract_email_service_id).id - self - end - - def create_ces_cps_pact - @pact_id = pact_repository.create( - version_id: @contract_email_service_version_id, - consumer_id: @contract_email_service_id, - provider_id: @contract_proposal_service_id, - json_content: default_json_content, - pact_version_sha: PactBroker.configuration.sha_generator.call(default_json_content) - ).id + create_consumer("Contract Email Service", :repository_url => 'git@git.realestate.com.au:business-systems/contract-email-service') self end def create_condor - @condor_id = pacticipant_repository.create(:name => 'Condor').save(raise_on_save_failure: true).id - self - end - - def create_condor_version number - @condor_version_id = version_repository.create(number: number, pacticipant_id: @condor_id).id - self - end - - def create_pricing_service_version number - @pricing_service_version_id = version_repository.create(number: number, pacticipant_id: @pricing_service_id).id - self - end - - def create_condor_pricing_service_pact - @pact_id = pact_repository.create( - version_id: @condor_version_id, - consumer_id: @condor_id, - provider_id: @pricing_service_id, - json_content: default_json_content, - pact_version_sha: PactBroker.configuration.sha_generator.call(default_json_content) - ).id + create_consumer("Condor") self end @@ -136,13 +95,13 @@ def create_tag_with_hierarchy pacticipant_name, pacticipant_version, tag_name def create_pacticipant pacticipant_name, params = {} params.delete(:comment) - @pacticipant = PactBroker::Domain::Pacticipant.create(:name => pacticipant_name) + @pacticipant = PactBroker::Domain::Pacticipant.create({ :name => pacticipant_name }.merge(params)) self end def create_consumer consumer_name = "Consumer #{model_counter}", params = {} params.delete(:comment) - create_pacticipant consumer_name + create_pacticipant consumer_name, params @consumer = @pacticipant self end @@ -155,7 +114,7 @@ def use_consumer consumer_name, params = {} def create_provider provider_name = "Provider #{model_counter}", params = {} params.delete(:comment) - create_pacticipant provider_name + create_pacticipant provider_name, params @provider = @pacticipant self end @@ -227,13 +186,13 @@ def create_label label_name def create_pact params = {} params.delete(:comment) json_content = params[:json_content] || default_json_content - pact_version_sha = params[:pact_version_sha] || PactBroker.configuration.sha_generator.call(json_content) + pact_version_sha = params[:pact_version_sha] || generate_pact_version_sha(json_content) @pact = PactBroker::Pacts::Repository.new.create( version_id: @consumer_version.id, consumer_id: @consumer.id, provider_id: @provider.id, - json_content: json_content, - pact_version_sha: pact_version_sha + pact_version_sha: pact_version_sha, + json_content: prepare_json_content(json_content), ) set_created_at_if_set params[:created_at], :pact_publications, {id: @pact.id} set_created_at_if_set params[:created_at], :pact_versions, {sha: @pact.pact_version_sha} @@ -250,9 +209,9 @@ def republish_same_pact params = {} def revise_pact json_content = nil json_content = json_content ? json_content : {random: rand}.to_json - pact_version_sha = PactBroker.configuration.sha_generator.call(json_content) + pact_version_sha = generate_pact_version_sha(json_content) @pact = PactBroker::Pacts::Repository.new.update(@pact.id, - json_content: json_content, + json_content: prepare_json_content(json_content), pact_version_sha: pact_version_sha ) self @@ -362,6 +321,15 @@ def and_return instance_variable_name private + # Remember! This must be called before adding the IDs + def generate_pact_version_sha json_content + PactBroker.configuration.sha_generator.call(json_content) + end + + def prepare_json_content(json_content) + PactBroker::Pacts::Content.from_json(json_content).with_ids.to_json + end + def set_created_at_if_set created_at, table_name, selector if created_at Sequel::Model.db[table_name].where(selector.keys.first => selector.values.first).update(created_at: created_at) diff --git a/spec/lib/pact_broker/domain/order_versions_spec.rb b/spec/lib/pact_broker/domain/order_versions_spec.rb index e72d6f9c7..287c8e870 100644 --- a/spec/lib/pact_broker/domain/order_versions_spec.rb +++ b/spec/lib/pact_broker/domain/order_versions_spec.rb @@ -11,10 +11,10 @@ before do TestDataBuilder.new .create_condor - .create_condor_version('1.5.0') - .create_condor_version('1.4.0') - .create_condor_version('1.3.0') - .create_condor_version('1.6.0') + .create_consumer_version('1.5.0') + .create_consumer_version('1.4.0') + .create_consumer_version('1.3.0') + .create_consumer_version('1.6.0') end let(:ordered_versions) { PactBroker::Domain::Version.order(:order).all.collect(&:number) } diff --git a/spec/service_consumers/provider_states_for_pact_broker_client.rb b/spec/service_consumers/provider_states_for_pact_broker_client.rb index bd2b5f088..34691bbb0 100644 --- a/spec/service_consumers/provider_states_for_pact_broker_client.rb +++ b/spec/service_consumers/provider_states_for_pact_broker_client.rb @@ -134,7 +134,7 @@ provider_state "the 'Pricing Service' already exists in the pact-broker" do set_up do - TestDataBuilder.new.create_pricing_service.create_pricing_service_version("1.3.0") + TestDataBuilder.new.create_pricing_service.create_provider_version("1.3.0") end end @@ -149,9 +149,9 @@ set_up do TestDataBuilder.new .create_condor - .create_condor_version('1.3.0') + .create_consumer_version('1.3.0') .create_pricing_service - .create_condor_pricing_service_pact + .create_pact end end @@ -163,9 +163,9 @@ set_up do TestDataBuilder.new .create_condor - .create_condor_version('1.3.0') + .create_consumer_version('1.3.0') .create_pricing_service - .create_condor_pricing_service_pact + .create_pact end end @@ -177,7 +177,7 @@ provider_state "'Condor' exists in the pact-broker" do set_up do - TestDataBuilder.new.create_condor.create_condor_version('1.3.0') + TestDataBuilder.new.create_condor.create_consumer_version('1.3.0') end end