-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkafka_spec.rb
107 lines (95 loc) · 3.21 KB
/
kafka_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# frozen_string_literal: true
require "sbmt/pact/rspec"
RSpec.describe "Sbmt::Pact::Providers::Test::Kafka", :pact do
has_message_pact_between "sbmt-pact-test-app", "sbmt-pact-test-app"
let(:karafka_message) { Struct.new(:payload, keyword_init: true) }
let(:interaction) do
new_interaction
.given("pet exists", pet_id: 1)
.with_headers(
"identity-key" => match_any_string("some-key")
)
.with_metadata(
topic: match_regex(/.+/, "some-topic"),
key: match_any_string("key")
)
end
context "with json message payload" do
let(:consumer) { PetJsonConsumer.consumer_klass }
let(:interaction) do
super()
.upon_receiving("pet message as json")
.with_json_contents(
id: match_any_integer(1),
tags: match_each_regex(/\w+/, %w[tagX tagY]),
colors: match_each_kv(
{
"red" => {
description: match_any_string("description"),
link: match_any_string("http://some-site.ru"),
relatesTo: match_each_regex(/(red|green|blue)/, %w[blue]),
title: match_any_string("title")
}
},
match_regex(/(red|green|blue)/, "red")
)
)
end
it "executes the pact test without errors" do
interaction.execute do |json_payload, meta|
message = karafka_message.new(payload: json_payload)
expect(Rails.logger).to receive(:info).with(/Pet ID: 1/)
expect(meta).to eq(
{
"contentType" => "application/json",
"headers" => {
"identity-key" => "some-key"
},
"key" => "key",
"topic" => "some-topic"
}
)
consumer.new.process_message(message)
end
end
end
context "with proto message payload" do
let(:consumer) { PetProtoConsumer.consumer_klass }
let(:interaction) do
super()
.upon_receiving("pet message as proto")
.with_proto_class("spec/internal/deps/services/pet_store/grpc/pet_store.proto", "Pet")
.with_proto_contents(
id: match_any_integer(1),
name: match_any_string("some pet"),
tags: match_each_regex(/\w+/, "tagX"),
colors: match_each(
{
description: match_any_string("description"),
link: match_any_string("http://some-site.ru"),
relates_to: match_each_regex(/(red|green|blue)/, "blue"),
color: match_regex(/(RED|GREEN|BLUE)/, "RED")
}
)
)
end
it "executes the pact test without errors" do
interaction.execute do |proto_payload, meta|
deserialized = PetStore::Grpc::PetStore::V1::Pet.decode(proto_payload)
message = karafka_message.new(payload: deserialized)
expect(Rails.logger).to receive(:info).with(/Pet ID: 1/)
expect(meta).to eq(
{
"contentType" => "application/protobuf;message=Pet",
"headers" => {
"identity-key" => "some-key"
},
"key" => "key",
"topic" => "some-topic"
}
)
consumer.new.process_message(message)
end
end
end
end