Class: Pact::V2::Consumer::PluginSyncMessageInteractionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb

Defined Under Namespace

Classes: CreateInteractionError, InteractionBuilderError, InteractionMismatchesError, PluginInitError

Constant Summary collapse

INIT_PLUGIN_ERRORS =
{
  1 => {reason: :internal_error, status: 1, description: "A general panic was caught"},
  2 => {reason: :plugin_load_failed, status: 2, description: "Failed to load the plugin"},
  3 => {reason: :invalid_handle, status: 3, description: "Pact Handle is not valid"}
}.freeze
CREATE_INTERACTION_ERRORS =
{
  1 => {reason: :internal_error, status: 1, description: "A general panic was caught"},
  2 => {reason: :mock_server_already_running, status: 2, description: "The mock server has already been started"},
  3 => {reason: :invalid_handle, status: 3, description: "The interaction handle is invalid"},
  4 => {reason: :invalid_content_type, status: 4, description: "The content type is not valid"},
  5 => {reason: :invalid_contents, status: 5, description: "The contents JSON is not valid JSON"},
  6 => {reason: :plugin_error, status: 6, description: "The plugin returned an error"}
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(pact_config, description: nil) ⇒ PluginSyncMessageInteractionBuilder

Returns a new instance of PluginSyncMessageInteractionBuilder.



37
38
39
40
41
42
43
44
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 37

def initialize(pact_config, description: nil)
  @pact_config = pact_config
  @description = description || ""
  @request = nil
  @response = nil
  @response_meta = nil
  @provider_state_meta = nil
end

Instance Method Details

#execute(&block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 112

def execute(&block)
  raise InteractionBuilderError.new("interaction is designed to be used one-time only") if defined?(@used)

  validate!

  pact_handle = init_pact
  init_plugin!(pact_handle)

  message_pact = PactFfi::SyncMessageConsumer.new_interaction(pact_handle, @description)
  @provider_state_meta&.each_pair do |provider_state, meta|
    if meta.present?
      meta.each_pair { |k, v| PactFfi.given_with_param(message_pact, provider_state, k.to_s, v.to_s) }
    else
      PactFfi.given(message_pact, provider_state)
    end
  end
  result = PactFfi::PluginConsumer.interaction_contents(message_pact, 0, @content_type, interaction_json)
  if CREATE_INTERACTION_ERRORS[result].present?
    error = CREATE_INTERACTION_ERRORS[result]
    raise CreateInteractionError.new("There was an error while trying to add interaction \"#{@description}\"", error[:reason], error[:status])
  end

  mock_server = MockServer.create_for_transport!(pact: pact_handle, transport: @transport, host: @pact_config.mock_host, port: @pact_config.mock_port)

  yield(message_pact, mock_server)

ensure
  if mock_server.matched?
    mock_server.write_pacts!(@pact_config.pact_dir)
  else
    msg = mismatches_error_msg(mock_server)
    raise InteractionMismatchesError.new(msg)
  end
  @used = true
  mock_server&.cleanup
  PactFfi::PluginConsumer.cleanup_plugins(pact_handle)
  PactFfi.free_pact_handle(pact_handle)
end

#given(provider_state, metadata = {}) ⇒ Object



55
56
57
58
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 55

def given(provider_state,  = {})
  @provider_state_meta = {provider_state => }
  self
end

#interaction_jsonObject



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 95

def interaction_json
  result = {
    request: @request
  }
  result.merge!(@plugin_metadata) if @plugin_metadata.is_a?(Hash)

  result[:response] = @response if @response.is_a?(Hash)
  result[:responseMetadata] = @response_meta if @response_meta.is_a?(Hash)

  JSON.dump(result)
end

#upon_receiving(description) ⇒ Object



60
61
62
63
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 60

def upon_receiving(description)
  @description = description
  self
end

#validate!Object



107
108
109
110
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 107

def validate!
  raise InteractionBuilderError.new("invalid request format, should be a hash") unless @request.is_a?(Hash)
  raise InteractionBuilderError.new("invalid response format, should be a hash") unless @response.is_a?(Hash) || @response_meta.is_a?(Hash)
end

#will_respond_with(resp_hash) ⇒ Object



75
76
77
78
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 75

def will_respond_with(resp_hash)
  @response = InteractionContents.plugin(resp_hash)
  self
end

#will_respond_with_meta(meta_hash) ⇒ Object



80
81
82
83
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 80

def will_respond_with_meta(meta_hash)
  @response_meta = InteractionContents.plugin(meta_hash)
  self
end

#with_content_type(content_type) ⇒ Object



70
71
72
73
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 70

def with_content_type(content_type)
  @content_type = content_type
  self
end

#with_plugin(plugin_name, plugin_version) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 46

def with_plugin(plugin_name, plugin_version)
  raise InteractionBuilderError.new("plugin_name is required") if plugin_name.blank?
  raise InteractionBuilderError.new("plugin_version is required") if plugin_version.blank?

  @plugin_name = plugin_name
  @plugin_version = plugin_version
  self
end

#with_plugin_metadata(meta_hash) ⇒ Object



85
86
87
88
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 85

def (meta_hash)
  @plugin_metadata = meta_hash
  self
end

#with_request(req_hash) ⇒ Object



65
66
67
68
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 65

def with_request(req_hash)
  @request = InteractionContents.plugin(req_hash)
  self
end

#with_transport(transport) ⇒ Object



90
91
92
93
# File 'lib/pact/v2/consumer/plugin_sync_message_interaction_builder.rb', line 90

def with_transport(transport)
  @transport = transport
  self
end