Class: Pact::Consumer::MessageInteractionBuilder
- Inherits:
-
Object
- Object
- Pact::Consumer::MessageInteractionBuilder
show all
- Defined in:
- lib/pact/consumer/message_interaction_builder.rb
Defined Under Namespace
Classes: CreateInteractionError, InteractionBuilderError, InteractionMismatchesError, PluginInitError
Constant Summary
collapse
"contentType"
- JSON_CONTENT_TYPE =
"application/json"
- PROTO_CONTENT_TYPE =
"application/protobuf"
- PROTOBUF_PLUGIN_NAME =
"protobuf"
- PROTOBUF_PLUGIN_VERSION =
"0.6.5"
- WRITE_PACT_FILE_ERRORS =
{
1 => {reason: :file_not_accessible, status: 1, description: "The pact file was not able to be written"},
2 => {reason: :internal_error, status: 2, description: "The message pact for the given handle was not found"}
}.freeze
- 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
-
#build_interaction_json ⇒ Object
-
#execute(&block) ⇒ Object
-
#given(provider_state, metadata = {}) ⇒ Object
-
#initialize(pact_config, description: nil) ⇒ MessageInteractionBuilder
constructor
A new instance of MessageInteractionBuilder.
-
#reference(group, name, value) ⇒ Object
-
#upon_receiving(description) ⇒ Object
-
#validate! ⇒ Object
-
#with_header(key, value) ⇒ Object
-
#with_headers(headers_hash) ⇒ Object
-
#with_json_contents(contents_hash) ⇒ Object
-
#with_metadata(meta_hash) ⇒ Object
-
#with_pact_protobuf_plugin_version(version) ⇒ Object
-
#with_plugin(plugin_name, plugin_version) ⇒ Object
Any content plugin that needs no transport (e.g. avro), see #with_plugin_contents.
-
#with_plugin_contents(content_type, contents_hash) ⇒ Object
contents_hash is passed to the plugin as-is, e.g.
-
#with_proto_class(proto_path, message_class_name, include_dirs = []) ⇒ Object
-
#with_proto_contents(contents_hash) ⇒ Object
Constructor Details
Returns a new instance of MessageInteractionBuilder.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 49
def initialize(pact_config, description: nil)
@pact_config = pact_config
@description = description
@json_contents = nil
@proto_contents = nil
@proto_path = nil
@proto_message_class = nil
@proto_include_dirs = []
@plugin_name = nil
@plugin_version = nil
@plugin_content_type = nil
@plugin_contents = nil
@meta = {}
@headers = {}
@provider_state_meta = nil
@references = []
end
|
Instance Method Details
#build_interaction_json ⇒ Object
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 191
def build_interaction_json
return JSON.dump({"pact:content-type" => @plugin_content_type}.merge(@plugin_contents.transform_keys(&:to_s))) if custom_plugin_interaction?
return JSON.dump(@json_contents) unless proto_interaction?
contents = {
"pact:proto": @proto_path,
"pact:message-type": @proto_message_class,
"pact:content-type": PROTO_CONTENT_TYPE
}.merge(@proto_contents)
contents["pact:protobuf-config"] = {additionalIncludes: @proto_include_dirs} if @proto_include_dirs.present?
JSON.dump(contents)
end
|
#execute(&block) ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 160
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) if plugin_interaction?
message_pact = PactFfi::MessageConsumer.new_message_interaction(pact_handle, @description)
@references.each do |group, name, value|
next if PactFfi.add_interaction_reference(message_pact, group, name, value)
raise InteractionBuilderError.new("could not add reference #{group}/#{name}")
end
configure_interaction!(message_pact)
payload, metadata = fetch_reified_message(pact_handle)
configure_provider_state(message_pact, metadata)
yield(payload, metadata)
write_pacts!(pact_handle, @pact_config.pact_dir)
ensure
@used = true
PactFfi::MessageConsumer.free_handle(message_pact)
PactFfi::PluginConsumer.cleanup_plugins(pact_handle)
PactFfi.free_pact_handle(pact_handle)
end
|
#given(provider_state, metadata = {}) ⇒ Object
68
69
70
71
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 68
def given(provider_state, metadata = {})
@provider_state_meta = {provider_state => metadata}
self
end
|
#reference(group, name, value) ⇒ Object
#upon_receiving(description) ⇒ Object
73
74
75
76
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 73
def upon_receiving(description)
@description = description
self
end
|
#validate! ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 147
def validate!
if custom_plugin_interaction?
raise InteractionBuilderError.new("plugin is not defined, please set one with #with_plugin") if @plugin_name.blank? || @plugin_version.blank?
raise InteractionBuilderError.new("invalid request format, should be a hash") unless @plugin_contents.is_a?(Hash)
elsif proto_interaction?
raise InteractionBuilderError.new("proto_path / proto_message are not defined, please set ones with #with_proto_message") if @proto_contents.blank? || @proto_message_class.blank?
raise InteractionBuilderError.new("invalid request format, should be a hash") unless @proto_contents.is_a?(Hash)
else
raise InteractionBuilderError.new("invalid request format, should be a hash") unless @json_contents.is_a?(Hash)
end
raise InteractionBuilderError.new("description is required for message interactions, please set one with #upon_receiving") if @description.blank?
end
|
142
143
144
145
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 142
def (key, value)
@headers[key] = value
self
end
|
137
138
139
140
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 137
def ()
@headers = InteractionContents.basic()
self
end
|
#with_json_contents(contents_hash) ⇒ Object
78
79
80
81
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 78
def with_json_contents(contents_hash)
@json_contents = InteractionContents.basic(contents_hash)
self
end
|
132
133
134
135
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 132
def with_metadata(meta_hash)
@meta = InteractionContents.basic(meta_hash)
self
end
|
#with_pact_protobuf_plugin_version(version) ⇒ Object
101
102
103
104
105
106
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 101
def with_pact_protobuf_plugin_version(version)
raise InteractionBuilderError.new("version is required") if version.blank?
@proto_plugin_version = version
self
end
|
#with_plugin(plugin_name, plugin_version) ⇒ Object
Any content plugin that needs no transport (e.g. avro), see #with_plugin_contents
114
115
116
117
118
119
120
121
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 114
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_contents(content_type, contents_hash) ⇒ Object
contents_hash is passed to the plugin as-is, e.g. => path, "pact:record-name" => "Order", ...
124
125
126
127
128
129
130
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 124
def with_plugin_contents(content_type, contents_hash)
raise InteractionBuilderError.new("content_type is required") if content_type.blank?
@plugin_content_type = content_type
@plugin_contents = InteractionContents.plugin(contents_hash)
self
end
|
#with_proto_class(proto_path, message_class_name, include_dirs = []) ⇒ Object
91
92
93
94
95
96
97
98
99
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 91
def with_proto_class(proto_path, message_class_name, include_dirs = [])
absolute_path = File.expand_path(proto_path)
raise InteractionBuilderError.new("proto file #{proto_path} does not exist") unless File.exist?(absolute_path)
@proto_path = absolute_path
@proto_message_class = message_class_name
@proto_include_dirs = include_dirs.map { |dir| File.expand_path(dir) }
self
end
|
#with_proto_contents(contents_hash) ⇒ Object
108
109
110
111
|
# File 'lib/pact/consumer/message_interaction_builder.rb', line 108
def with_proto_contents(contents_hash)
@proto_contents = InteractionContents.plugin(contents_hash)
self
end
|