pact-messages 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.rubocop.yml +34 -0
  5. data/.travis.yml +4 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +185 -0
  10. data/Rakefile +6 -0
  11. data/example/user-app/Gemfile +9 -0
  12. data/example/user-app/Gemfile.lock +81 -0
  13. data/example/user-app/README.md +1 -0
  14. data/example/user-app/Rakefile +7 -0
  15. data/example/user-app/lib/user_app/message_builder.rb +12 -0
  16. data/example/user-app/lib/user_app/message_processor.rb +8 -0
  17. data/example/user-app/lib/user_app/version.rb +4 -0
  18. data/example/user-app/log/pact.log +4 -0
  19. data/example/user-app/spec/pact_helper.rb +24 -0
  20. data/example/user-app/spec/pacts/message_consumer-message_provider.json +55 -0
  21. data/example/user-app/spec/service_consumers/message_processor_spec.rb +16 -0
  22. data/example/user-app/spec/service_providers/message_builder_spec.rb +36 -0
  23. data/example/user-app/spec/spec_helper.rb +5 -0
  24. data/lib/pact/messages.rb +26 -0
  25. data/lib/pact/messages/consumer.rb +6 -0
  26. data/lib/pact/messages/consumer/contract_builder.rb +82 -0
  27. data/lib/pact/messages/consumer/dsl.rb +4 -0
  28. data/lib/pact/messages/consumer/dsl/message_consumer.rb +33 -0
  29. data/lib/pact/messages/consumer/dsl/message_provider.rb +35 -0
  30. data/lib/pact/messages/consumer/dsl/mock_service.rb +51 -0
  31. data/lib/pact/messages/consumer/interaction.rb +17 -0
  32. data/lib/pact/messages/consumer/interaction_builder.rb +27 -0
  33. data/lib/pact/messages/consumer/mock_service_factory.rb +19 -0
  34. data/lib/pact/messages/consumer/world.rb +42 -0
  35. data/lib/pact/messages/contract_repository.rb +45 -0
  36. data/lib/pact/messages/version.rb +5 -0
  37. data/pact-messages.gemspec +26 -0
  38. metadata +168 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a94ee3e9ef31fec6f6b07b85d3e49df838b657a
4
+ data.tar.gz: cac5dcd382939c1efdaeece3c06d178c483eb99e
5
+ SHA512:
6
+ metadata.gz: 43be1eaa883c8c2aa16568615867c816dc377dd59dcf1f7c56a9494b541533867e261ece9a37d0d7dd501dcd976b51d01cd388cc9c2637b95ba77ba83a949071
7
+ data.tar.gz: 6efdea544a69ca2760bad678dc4c9586a9abc684b2ff2986087d8d083ad732beac672d2febaadcd1d9b26c28fea455d6e04d17a4c1885cd7e8d8cbcf7cd8f46d
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .idea
11
+ /log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ LineLength:
2
+ Max: 120
3
+ Metrics/MethodLength:
4
+ Enabled: false
5
+ Documentation:
6
+ Enabled: false
7
+ AndOr:
8
+ Enabled: false
9
+
10
+ # Allow use of empty lines to visually group code into 'paragraphs'
11
+ EmptyLines:
12
+ Enabled: false
13
+ Style/EmptyLinesAroundBlockBody:
14
+ Enabled: false
15
+ Style/EmptyLinesAroundClassBody:
16
+ Enabled: false
17
+ Style/EmptyLinesAroundMethodBody:
18
+ Enabled: false
19
+ Style/EmptyLinesAroundModuleBody:
20
+ Enabled: false
21
+ Style/ModuleFunction:
22
+ Enabled: false
23
+
24
+ TrailingComma:
25
+ Enabled: true
26
+ EnforcedStyleForMultiline: comma
27
+ SupportedStyles:
28
+ - comma
29
+ - no_comma
30
+
31
+ AllCops:
32
+ Exclude:
33
+ - !ruby/regexp /node_modules/
34
+ - !ruby/regexp /db\/schema\.rb/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.1.7"
4
+ - "2.2.3"
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pact-messages.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Alex Malkov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # Pact::Messages
2
+ [![Build Status](https://travis-ci.org/reevoo/pact-messages.svg?branch=master)](https://travis-ci.org/reevoo/pact-messages)
3
+
4
+ Define a pact contract between non-HTTP asynchronous service consumers and providers, enabling "consumer driven contract" testing.
5
+
6
+ This is an extension for [Pact gem](https://github.com/realestate-com-au/pact "Pact") which covers HTTP scenario.
7
+
8
+ This allows testing shape of your JSON messages on both sides of an integration point using fast unit tests.
9
+
10
+ This gem is inspired by the concept of "Consumer driven contracts". See [this article](http://martinfowler.com/articles/consumerDrivenContracts.html) by Ian Robinson for more information.
11
+
12
+
13
+ ## What is it good for?
14
+
15
+ Pact is most valuable for designing and testing integrations where you (or your team/organisation/partner organisation) control the development of both the consumer and the provider, and the requirements of the consumer are going to be used to drive the features of the provider.
16
+
17
+ You can find this solution very useful for systems based on messaging platforms aimed at providing an integration and communication between various business components, e.g. [RabbitMQ messaging system](https://www.rabbitmq.com "RabbitMQ").
18
+
19
+
20
+ ## How does it work?
21
+
22
+ 1. In the specs for the provider facing code in the consumer project, expectations are set up on a mock service provider.
23
+
24
+ 2. When the specs are run, the mock service stores pact contracts in the Contract Repository and writes contract in to a "pact" file.
25
+
26
+ 3. In specs you are able to get pact contract from Contract Repository and verify shape of you message against this contract.
27
+
28
+ 4. You are also able to build a Sample Message from the contract, e.g. build JSON message like {foo: 'bar'} from the contract: {foo: like('bar')}.
29
+
30
+
31
+ ## Installation
32
+
33
+ Add this line to your application's Gemfile:
34
+
35
+ ```ruby
36
+ gem 'pact-messages'
37
+ ```
38
+
39
+ And then execute:
40
+
41
+ $ bundle
42
+
43
+ Or install it yourself as:
44
+
45
+ $ gem install pact-messages
46
+
47
+ ## Usage
48
+
49
+ ### Define Pact contract
50
+
51
+ ```ruby
52
+ # in /spec/service_providers/pact_helper.rb
53
+ # or
54
+ # in /spec/support/pact_helper.rb
55
+
56
+ require 'pact/messages'
57
+
58
+ Pact::Messages.service_consumer 'Message Consumer' do
59
+ has_pact_with 'Message Provider' do
60
+ mock_service 'message_provider_service'
61
+ end
62
+ end
63
+ ```
64
+
65
+
66
+ ### Define Mock Service
67
+
68
+ ```ruby
69
+ Pact::Messages.build_mock_service(:message_provider_service) do |service|
70
+ service.given('User subscribed')
71
+ .provide(
72
+ {
73
+ 'first_name' => like('John'),
74
+ 'last_name' => like('Smith'),
75
+ 'subscribed' => like(true),
76
+ },
77
+ )
78
+
79
+ service.given('User unsubscribed')
80
+ .provide(
81
+ {
82
+ 'first_name' => like('John'),
83
+ 'last_name' => like('Smith'),
84
+ 'subscribed' => like(false),
85
+ },
86
+ )
87
+ end
88
+ ```
89
+
90
+ PS: '.given' is optional, if you have only one state, you don't need to specify '.given'
91
+
92
+ ```ruby
93
+ Pact::Messages.build_mock_service(:message_provider_service) do |service|
94
+ service.provide(
95
+ {
96
+ 'first_name' => like('John'),
97
+ 'last_name' => like('Smith'),
98
+ 'subscribed' => like(true),
99
+ }
100
+ )
101
+ end
102
+ ```
103
+
104
+
105
+ ### Verify contract on the Provider side
106
+
107
+ ```ruby
108
+ module MessageBuilder
109
+ def self.build(subscribed)
110
+ {
111
+ 'first_name' => 'William',
112
+ 'last_name' => 'Taylor',
113
+ 'subscribed' => subscribed,
114
+ }
115
+ end
116
+ end
117
+ end
118
+ ```
119
+
120
+ #### Rspec
121
+
122
+ ```ruby
123
+ describe MessageBuilder, pact: true do
124
+ subject { described_class.build(subscribed_status) }
125
+
126
+ describe ".build" do
127
+ context "subscribed" do
128
+ let(:subscribed_status) { true }
129
+ let(:user_contract) do
130
+ Pact::Messages.get_message_contract('Message Provider', 'Message Consumer', 'User subscribed')
131
+ end
132
+
133
+ it 'matches the contract' do
134
+ diff = Pact::JsonDiffer.call(user_contract, subject)
135
+ puts Pact::Matchers::UnixDiffFormatter.call(diff) if diff.any? # Print a pretty diff if we fail
136
+ expect(diff).to be_empty
137
+ end
138
+ end
139
+
140
+ context "unsubscribed" do
141
+ let(:subscribed_status) { false }
142
+ let(:user_contract) do
143
+ Pact::Messages.get_message_contract('Message Provider', 'Message Consumer', 'User unsubscribed')
144
+ end
145
+
146
+ it 'matches the contract' do
147
+ diff = Pact::JsonDiffer.call(user_contract, subject)
148
+ puts Pact::Matchers::UnixDiffFormatter.call(diff) if diff.any? # Print a pretty diff if we fail
149
+ expect(diff).to be_empty
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ ```
156
+
157
+ ### Using Sample from the Contract on Consumer side
158
+
159
+ ```ruby
160
+ module MessageProcessor
161
+ def self.full_name(message)
162
+ [message.fetch('first_name'), message.fetch('last_name')].join(' ')
163
+ end
164
+ end
165
+ ```
166
+
167
+ #### Rspec
168
+
169
+ ```ruby
170
+ describe MessageProcessor, pact: true do
171
+ let(:message) do
172
+ Pact::Messages.get_message_sample('Message Provider', 'Message Consumer', 'User subscribed')
173
+ end
174
+
175
+ describe '.full_name' do
176
+ it 'joins first name and last name' do
177
+ expect(described_class.full_name(message)).to eq('John Smith')
178
+ end
179
+ end
180
+ end
181
+ ```
182
+
183
+ ## License
184
+
185
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ group :development, :test do
4
+ gem 'rspec'
5
+ gem 'pact-messages', path: '../../'
6
+ gem 'pry'
7
+ end
8
+
9
+ gem 'rake'
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: ../../
3
+ specs:
4
+ pact-messages (0.1.0)
5
+ pact (~> 1.9)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ awesome_print (1.6.1)
11
+ coderay (1.0.9)
12
+ diff-lcs (1.2.4)
13
+ find_a_port (1.0.1)
14
+ json (1.8.3)
15
+ method_source (0.8.2)
16
+ pact (1.9.0)
17
+ awesome_print (~> 1.1)
18
+ find_a_port (~> 1.0.1)
19
+ json
20
+ pact-mock_service (~> 0.7)
21
+ pact-support (~> 0.5)
22
+ rack-test (~> 0.6.2)
23
+ randexp (~> 0.1.7)
24
+ rspec (>= 2.14)
25
+ term-ansicolor (~> 1.0)
26
+ thor
27
+ webrick
28
+ pact-mock_service (0.7.2)
29
+ find_a_port (~> 1.0.1)
30
+ json
31
+ pact-support (~> 0.5.3)
32
+ rack
33
+ rack-test (~> 0.6.2)
34
+ rspec (>= 2.14)
35
+ term-ansicolor (~> 1.0)
36
+ thor
37
+ webrick
38
+ pact-support (0.5.3)
39
+ awesome_print (~> 1.1)
40
+ find_a_port (~> 1.0.1)
41
+ json
42
+ rack-test (~> 0.6.2)
43
+ randexp (~> 0.1.7)
44
+ rspec (>= 2.14)
45
+ term-ansicolor (~> 1.0)
46
+ thor
47
+ pry (0.9.12.2)
48
+ coderay (~> 1.0.5)
49
+ method_source (~> 0.8)
50
+ slop (~> 3.4)
51
+ rack (1.6.4)
52
+ rack-test (0.6.3)
53
+ rack (>= 1.0)
54
+ rake (10.1.0)
55
+ randexp (0.1.7)
56
+ rspec (2.14.1)
57
+ rspec-core (~> 2.14.0)
58
+ rspec-expectations (~> 2.14.0)
59
+ rspec-mocks (~> 2.14.0)
60
+ rspec-core (2.14.5)
61
+ rspec-expectations (2.14.2)
62
+ diff-lcs (>= 1.1.3, < 2.0)
63
+ rspec-mocks (2.14.3)
64
+ slop (3.4.6)
65
+ term-ansicolor (1.3.2)
66
+ tins (~> 1.0)
67
+ thor (0.19.1)
68
+ tins (1.6.0)
69
+ webrick (1.3.1)
70
+
71
+ PLATFORMS
72
+ ruby
73
+
74
+ DEPENDENCIES
75
+ pact-messages!
76
+ pry
77
+ rake
78
+ rspec
79
+
80
+ BUNDLED WITH
81
+ 1.10.6
@@ -0,0 +1 @@
1
+ # User Test App
@@ -0,0 +1,7 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ $LOAD_PATH << './lib'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
@@ -0,0 +1,12 @@
1
+ module UserApp
2
+ # Message builder
3
+ module MessageBuilder
4
+ def self.build(subscribed)
5
+ {
6
+ 'first_name' => 'William',
7
+ 'last_name' => 'Taylor',
8
+ 'subscribed' => subscribed,
9
+ }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module UserApp
2
+ # Message Processor
3
+ module MessageProcessor
4
+ def self.full_name(message)
5
+ [message.fetch('first_name'), message.fetch('last_name')].join(' ')
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ # Sample App
2
+ module UserApp
3
+ VERSION = '1.0.0'
4
+ end
@@ -0,0 +1,4 @@
1
+ # Logfile created on 2015-11-05 15:43:21 +0000 by logger.rb/47272
2
+ I, [2015-11-05T15:43:21.302165 #91013] INFO -- : Writing pact for Message Provider to /Users/alexmalkov/reevoo/pact-messages/example/user-app/spec/pacts/message_consumer-message_provider.json
3
+ I, [2015-11-05T15:43:55.291312 #91081] INFO -- : Writing pact for Message Provider to /Users/alexmalkov/reevoo/pact-messages/example/user-app/spec/pacts/message_consumer-message_provider.json
4
+ I, [2015-11-05T16:21:19.348693 #97073] INFO -- : Writing pact for Message Provider to /Users/alexmalkov/reevoo/pact-messages/example/user-app/spec/pacts/message_consumer-message_provider.json
@@ -0,0 +1,24 @@
1
+ require_relative 'spec_helper'
2
+ require 'pact/messages'
3
+
4
+ Pact::Messages.service_consumer 'Message Consumer' do
5
+ has_pact_with 'Message Provider' do
6
+ mock_service 'message_provider_service'
7
+ end
8
+ end
9
+
10
+ Pact::Messages.build_mock_service(:message_provider_service) do |service|
11
+ service.given('User subscribed')
12
+ .provide(
13
+ 'first_name' => like('John'),
14
+ 'last_name' => like('Smith'),
15
+ 'subscribed' => true,
16
+ )
17
+
18
+ service.given('User unsubscribed')
19
+ .provide(
20
+ 'first_name' => like('John'),
21
+ 'last_name' => like('Smith'),
22
+ 'subscribed' => false,
23
+ )
24
+ end
@@ -0,0 +1,55 @@
1
+ {
2
+ "consumer": {
3
+ "name": "Message Consumer"
4
+ },
5
+ "provider": {
6
+ "name": "Message Provider"
7
+ },
8
+ "interactions": [
9
+ {
10
+ "description": null,
11
+ "provider_state": "User subscribed",
12
+ "request": {
13
+ "method": "MESSAGE",
14
+ "path": ""
15
+ },
16
+ "response": {
17
+ "body": {
18
+ "first_name": {
19
+ "json_class": "Pact::SomethingLike",
20
+ "contents": "John"
21
+ },
22
+ "last_name": {
23
+ "json_class": "Pact::SomethingLike",
24
+ "contents": "Smith"
25
+ },
26
+ "subscribed": true
27
+ }
28
+ }
29
+ },
30
+ {
31
+ "description": null,
32
+ "provider_state": "User unsubscribed",
33
+ "request": {
34
+ "method": "MESSAGE",
35
+ "path": ""
36
+ },
37
+ "response": {
38
+ "body": {
39
+ "first_name": {
40
+ "json_class": "Pact::SomethingLike",
41
+ "contents": "John"
42
+ },
43
+ "last_name": {
44
+ "json_class": "Pact::SomethingLike",
45
+ "contents": "Smith"
46
+ },
47
+ "subscribed": false
48
+ }
49
+ }
50
+ }
51
+ ],
52
+ "metadata": {
53
+ "pactSpecificationVersion": "1.0.0"
54
+ }
55
+ }
@@ -0,0 +1,16 @@
1
+ require_relative '../pact_helper'
2
+ require 'user_app/message_processor'
3
+
4
+ module UserApp
5
+ describe MessageProcessor, pact: true do
6
+ let(:message) do
7
+ Pact::Messages.get_message_sample('Message Provider', 'Message Consumer', 'User subscribed')
8
+ end
9
+
10
+ describe '.full_name' do
11
+ it 'joins first name and last name' do
12
+ expect(described_class.full_name(message)).to eq('John Smith')
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,36 @@
1
+ require_relative '../pact_helper'
2
+ require 'user_app/message_builder'
3
+
4
+ module UserApp
5
+ describe MessageBuilder, pact: true do
6
+ subject { described_class.build(subscribed_status) }
7
+
8
+ describe '.build' do
9
+ context 'subscribed' do
10
+ let(:subscribed_status) { true }
11
+ let(:user_contract) do
12
+ Pact::Messages.get_message_contract('Message Provider', 'Message Consumer', 'User subscribed')
13
+ end
14
+
15
+ it 'matches the contract' do
16
+ diff = Pact::JsonDiffer.call(user_contract, subject)
17
+ puts Pact::Matchers::UnixDiffFormatter.call(diff) if diff.any? # Print a pretty diff if we fail
18
+ expect(diff).to be_empty
19
+ end
20
+ end
21
+
22
+ context 'unsubscribed' do
23
+ let(:subscribed_status) { false }
24
+ let(:user_contract) do
25
+ Pact::Messages.get_message_contract('Message Provider', 'Message Consumer', 'User unsubscribed')
26
+ end
27
+
28
+ it 'matches the contract' do
29
+ diff = Pact::JsonDiffer.call(user_contract, subject)
30
+ puts Pact::Matchers::UnixDiffFormatter.call(diff) if diff.any? # Print a pretty diff if we fail
31
+ expect(diff).to be_empty
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+
3
+ RSpec.configure do | config |
4
+ config.color = true
5
+ end
@@ -0,0 +1,26 @@
1
+ require 'pact'
2
+
3
+ module Pact
4
+ module Messages
5
+ module_function
6
+
7
+ def service_consumer(name, &block)
8
+ Pact::Messages::Consumer::DSL::MessageConsumer.build(name, &block)
9
+ end
10
+
11
+ def build_mock_service(mock_service_name, &block)
12
+ Pact::Messages::Consumer::MockServiceFactory.build(mock_service_name, &block)
13
+ end
14
+
15
+ def get_message_contract(provider_name, consumer_name, provider_state = nil)
16
+ Pact::Messages::ContractRepository.get_message_contract(provider_name, consumer_name, provider_state)
17
+ end
18
+
19
+ def get_message_sample(provider_name, consumer_name, provider_state = nil)
20
+ Pact::Messages::ContractRepository.get_message_sample(provider_name, consumer_name, provider_state)
21
+ end
22
+ end
23
+ end
24
+
25
+ require 'pact/messages/consumer'
26
+ require 'pact/messages/contract_repository'
@@ -0,0 +1,6 @@
1
+ require_relative 'consumer/contract_builder'
2
+ require_relative 'consumer/interaction'
3
+ require_relative 'consumer/interaction_builder'
4
+ require_relative 'consumer/mock_service_factory'
5
+ require_relative 'consumer/world'
6
+ require_relative 'consumer/dsl'
@@ -0,0 +1,82 @@
1
+ require 'uri'
2
+ require 'json/add/regexp'
3
+ require 'pact/logging'
4
+ require 'pact/consumer_contract'
5
+ require 'pact/mock_service/client'
6
+ require 'pact/consumer_contract/consumer_contract_writer'
7
+
8
+ module Pact
9
+ module Messages
10
+ module Consumer
11
+ class ContractBuilder
12
+ include Pact::Logging
13
+
14
+ attr_reader :consumer_name, :provider_name
15
+
16
+ def initialize(attributes)
17
+ @consumer_name = attributes.fetch(:consumer_name)
18
+ @provider_name = attributes.fetch(:provider_name)
19
+ @consumer_contract_details = {
20
+ consumer: { name: @consumer_name },
21
+ provider: { name: @provider_name },
22
+ pactfile_write_mode: attributes[:pactfile_write_mode].to_s,
23
+ pact_dir: attributes.fetch(:pact_dir),
24
+ }
25
+ @interaction_builder = nil
26
+ @interactions = []
27
+ end
28
+
29
+ def given(provider_state)
30
+ interaction_builder.given(provider_state)
31
+ end
32
+
33
+ def provide(message)
34
+ interaction_builder.provide(message)
35
+ end
36
+
37
+ def verify(_example_description)
38
+ true # TODO: find out how the verification works in original pact
39
+ end
40
+
41
+ def log(msg)
42
+ logger.log(msg)
43
+ end
44
+
45
+ def write_pact
46
+ consumer_contract_params = @consumer_contract_details.merge(interactions: @interactions)
47
+ consumer_contract_writer = Pact::ConsumerContractWriter.new(consumer_contract_params, logger)
48
+ consumer_contract_writer.write
49
+ end
50
+
51
+ def consumer_contract
52
+ @consumer_contract ||= Pact::ConsumerContract.new(
53
+ consumer: Pact::ServiceConsumer.new(name: @consumer_name),
54
+ provider: Pact::ServiceProvider.new(name: @provider_name),
55
+ interactions: @interactions,
56
+ )
57
+ end
58
+
59
+ def handle_interaction_fully_defined(interaction)
60
+ @interactions << interaction
61
+ @interaction_builder = nil
62
+ end
63
+
64
+ private
65
+
66
+ attr_reader :mock_service_client
67
+ attr_writer :interaction_builder
68
+
69
+ def interaction_builder
70
+ @interaction_builder ||=
71
+ begin
72
+ interaction_builder = InteractionBuilder.new do |interaction|
73
+ handle_interaction_fully_defined(interaction)
74
+ self
75
+ end
76
+ interaction_builder
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,4 @@
1
+ require 'pact/shared/dsl'
2
+ require_relative 'dsl/message_consumer'
3
+ require_relative 'dsl/message_provider'
4
+ require_relative 'dsl/mock_service'
@@ -0,0 +1,33 @@
1
+ module Pact
2
+ module Messages
3
+ module Consumer
4
+ module DSL
5
+ class MessageConsumer
6
+ extend Pact::DSL
7
+
8
+ attr_accessor :consumer_name
9
+
10
+ def initialize(consumer_name)
11
+ @consumer_name = consumer_name
12
+ end
13
+
14
+ dsl do
15
+ def has_pact_with(provider_name, &block) # rubocop:disable Style/PredicateName
16
+ MessageProvider.build(provider_name, consumer_name, &block)
17
+ end
18
+ end
19
+
20
+ def finalize
21
+ validate
22
+ end
23
+
24
+ private
25
+
26
+ def validate
27
+ fail 'Please provide a consumer name' unless consumer_name && !consumer_name.empty?
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,35 @@
1
+ module Pact
2
+ module Messages
3
+ module Consumer
4
+ module DSL
5
+ class MessageProvider
6
+ extend Pact::DSL
7
+
8
+ attr_accessor :service, :consumer_name, :provider_name
9
+
10
+ def initialize(provider_name, consumer_name)
11
+ @provider_name = provider_name
12
+ @consumer_name = consumer_name
13
+ @service = nil
14
+ end
15
+
16
+ dsl do
17
+ def mock_service(name, &block)
18
+ self.service = MockService.build(name, consumer_name, provider_name, &block || proc {})
19
+ end
20
+ end
21
+
22
+ def finalize
23
+ validate
24
+ end
25
+
26
+ private
27
+
28
+ def validate
29
+ fail "Please configure a service for #{provider_name}" unless service
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,51 @@
1
+ module Pact
2
+ module Messages
3
+ module Consumer
4
+ module DSL
5
+ class MockService
6
+ extend Pact::DSL
7
+
8
+ attr_accessor :verify, :provider_name, :consumer_name
9
+
10
+ def initialize(name, consumer_name, provider_name)
11
+ @name = name
12
+ @consumer_name = consumer_name
13
+ @provider_name = provider_name
14
+ @verify = true
15
+ end
16
+
17
+ dsl do
18
+ def verify(verify)
19
+ self.verify = verify
20
+ end
21
+ end
22
+
23
+ def finalize
24
+ contract_builder = create_consumer_contract_builder
25
+ Pact::Messages.consumer_world.register_mock_service(@name.to_sym, contract_builder)
26
+ setup_verification(contract_builder) if verify
27
+ contract_builder
28
+ end
29
+
30
+ private
31
+
32
+ def create_consumer_contract_builder
33
+ consumer_contract_builder_fields = {
34
+ consumer_name: consumer_name,
35
+ provider_name: provider_name,
36
+ pactfile_write_mode: Pact.configuration.pactfile_write_mode,
37
+ pact_dir: Pact.configuration.pact_dir,
38
+ }
39
+ Pact::Messages::Consumer::ContractBuilder.new consumer_contract_builder_fields
40
+ end
41
+
42
+ def setup_verification(consumer_contract_builder)
43
+ Pact.configuration.add_provider_verification do |example_description|
44
+ consumer_contract_builder.verify example_description
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ require 'pact/consumer_contract/interaction'
2
+
3
+ module Pact
4
+ module Messages
5
+ module Consumer
6
+ class Interaction < Pact::Interaction
7
+ def to_hash
8
+ {
9
+ description: description,
10
+ provider_state: provider_state,
11
+ response: response.to_hash,
12
+ }
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ require 'net/http'
2
+
3
+ module Pact
4
+ module Messages
5
+ module Consumer
6
+ class InteractionBuilder
7
+ attr_reader :interaction
8
+
9
+ def initialize(&block)
10
+ @interaction = Pact::Messages::Consumer::Interaction.new
11
+ @interaction.request = Pact::Request::Expected.from_hash(method: 'MESSAGE', path: '/')
12
+ @callback = block
13
+ end
14
+
15
+ def given(provider_state)
16
+ interaction.provider_state = provider_state.nil? ? nil : provider_state.to_s
17
+ self
18
+ end
19
+
20
+ def provide(response)
21
+ interaction.response = Pact::Response.new(body: response)
22
+ @callback.call interaction
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module Pact
2
+ module Messages
3
+ module Consumer
4
+ module MockServiceFactory
5
+ module Scope
6
+ extend Pact::Helpers
7
+ end
8
+
9
+ module_function
10
+
11
+ def build(mock_service_name, &block)
12
+ contract_builder = Pact::Messages.consumer_world.find_contract_builder(mock_service_name)
13
+ Scope.module_exec(contract_builder, &block)
14
+ contract_builder.write_pact
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,42 @@
1
+ module Pact
2
+ module Messages
3
+ def self.consumer_world
4
+ @consumer_world ||= Pact::Messages::Consumer::World.new
5
+ end
6
+
7
+ # internal api, for testing only
8
+ def self.clear_consumer_world
9
+ @consumer_world = nil
10
+ end
11
+
12
+ def self.logger
13
+ @logger ||= Logger.new(STDOUT)
14
+ end
15
+
16
+ module Consumer
17
+ class World
18
+ attr_reader :mock_services
19
+
20
+ def initialize
21
+ @mock_services = {}
22
+ end
23
+
24
+ def register_mock_service(mock_service_name, contract_builder)
25
+ mock_services[mock_service_name] = contract_builder
26
+ end
27
+
28
+ def find_contract_builder(mock_service_name)
29
+ mock_services[mock_service_name]
30
+ end
31
+
32
+ def find_contract(provider_name, consumer_name)
33
+ contract_builder = mock_services.values.find do |builder|
34
+ builder.provider_name == provider_name && builder.consumer_name == consumer_name
35
+ end
36
+ contract_builder.consumer_contract if contract_builder
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,45 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'pact'
4
+
5
+ module Pact
6
+ module Messages
7
+ module ContractRepository
8
+ module_function
9
+
10
+ def get_message_contract(provider_name, consumer_name, provider_state = nil)
11
+ contract = get_contract(provider_name, consumer_name)
12
+ fail "Contract between #{provider_name} and #{consumer_name} not found" unless contract
13
+ find_response_for_provider_state(contract.interactions, provider_state)
14
+ end
15
+
16
+ def get_message_sample(provider_name, consumer_name, provider_state = nil)
17
+ Pact::Reification.from_term(get_message_contract(provider_name, consumer_name, provider_state))
18
+ end
19
+
20
+ class << self
21
+ private
22
+
23
+ def get_contract(provider_name, consumer_name)
24
+ # search for it locally, if not found look on the pack broker
25
+ Pact::Messages.consumer_world.find_contract(provider_name, consumer_name) ||
26
+ Pact::ConsumerContract.from_uri(pact_broker_url(provider_name, consumer_name))
27
+ end
28
+
29
+ def pact_broker_url(provider, consumer)
30
+ url = URI.encode("/pacts/provider/#{provider}/consumer/#{consumer}/")
31
+ URI(ENV.fetch('PACT_BROKER_URL') + url + 'latest')
32
+ end
33
+
34
+ def find_response_for_provider_state(interactions, state)
35
+ interaction = interactions.find { |i| i.provider_state == state }
36
+ unless interaction
37
+ fail "State #{state} not found among interaction states #{interactions.map(&:provider_state)}"
38
+ end
39
+
40
+ interaction.response[:body]
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Pact
2
+ module Messages
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pact/messages/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pact-messages'
8
+ spec.version = Pact::Messages::VERSION
9
+ spec.authors = ['Alex Malkov', 'David Sevcik']
10
+ spec.email = ['alex.a.malkov@gmail.com', 'david.sevcik@reevoo.com']
11
+
12
+ spec.summary = 'Enables consumer driven contract testing for asynchronous message driven systems.'
13
+ spec.homepage = 'https://github.com/reevoo/pact-messages'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(/^(test|spec|features)\//) }
17
+ spec.require_paths = ['lib']
18
+
19
+ spec.add_runtime_dependency 'pact', '~> 1.9'
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.10'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.3'
24
+ spec.add_development_dependency 'pry', '~> 0.10'
25
+ spec.add_development_dependency 'rubocop', '~> 0.28'
26
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pact-messages
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Malkov
8
+ - David Sevcik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-11-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pact
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.9'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.9'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.10'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.10'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.3'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.3'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.10'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.10'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rubocop
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: '0.28'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: '0.28'
98
+ description:
99
+ email:
100
+ - alex.a.malkov@gmail.com
101
+ - david.sevcik@reevoo.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".rubocop.yml"
109
+ - ".travis.yml"
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - example/user-app/Gemfile
116
+ - example/user-app/Gemfile.lock
117
+ - example/user-app/README.md
118
+ - example/user-app/Rakefile
119
+ - example/user-app/lib/user_app/message_builder.rb
120
+ - example/user-app/lib/user_app/message_processor.rb
121
+ - example/user-app/lib/user_app/version.rb
122
+ - example/user-app/log/pact.log
123
+ - example/user-app/spec/pact_helper.rb
124
+ - example/user-app/spec/pacts/message_consumer-message_provider.json
125
+ - example/user-app/spec/service_consumers/message_processor_spec.rb
126
+ - example/user-app/spec/service_providers/message_builder_spec.rb
127
+ - example/user-app/spec/spec_helper.rb
128
+ - lib/pact/messages.rb
129
+ - lib/pact/messages/consumer.rb
130
+ - lib/pact/messages/consumer/contract_builder.rb
131
+ - lib/pact/messages/consumer/dsl.rb
132
+ - lib/pact/messages/consumer/dsl/message_consumer.rb
133
+ - lib/pact/messages/consumer/dsl/message_provider.rb
134
+ - lib/pact/messages/consumer/dsl/mock_service.rb
135
+ - lib/pact/messages/consumer/interaction.rb
136
+ - lib/pact/messages/consumer/interaction_builder.rb
137
+ - lib/pact/messages/consumer/mock_service_factory.rb
138
+ - lib/pact/messages/consumer/world.rb
139
+ - lib/pact/messages/contract_repository.rb
140
+ - lib/pact/messages/version.rb
141
+ - pact-messages.gemspec
142
+ homepage: https://github.com/reevoo/pact-messages
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.4.5
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: Enables consumer driven contract testing for asynchronous message driven
166
+ systems.
167
+ test_files: []
168
+ has_rdoc: