synapse-core 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. data/lib/synapse.rb +351 -0
  2. data/lib/synapse/command/command_bus.rb +45 -0
  3. data/lib/synapse/command/command_callback.rb +18 -0
  4. data/lib/synapse/command/command_filter.rb +17 -0
  5. data/lib/synapse/command/command_handler.rb +13 -0
  6. data/lib/synapse/command/dispatch_interceptor.rb +16 -0
  7. data/lib/synapse/command/duplication.rb +43 -0
  8. data/lib/synapse/command/errors.rb +27 -0
  9. data/lib/synapse/command/filters/validation.rb +32 -0
  10. data/lib/synapse/command/gateway.rb +34 -0
  11. data/lib/synapse/command/interceptor_chain.rb +31 -0
  12. data/lib/synapse/command/interceptors/serialization.rb +35 -0
  13. data/lib/synapse/command/message.rb +19 -0
  14. data/lib/synapse/command/rollback_policy.rb +22 -0
  15. data/lib/synapse/command/simple_command_bus.rb +138 -0
  16. data/lib/synapse/command/wiring.rb +47 -0
  17. data/lib/synapse/domain/aggregate_root.rb +121 -0
  18. data/lib/synapse/domain/event_container.rb +127 -0
  19. data/lib/synapse/domain/message.rb +82 -0
  20. data/lib/synapse/domain/message_builder.rb +34 -0
  21. data/lib/synapse/domain/stream.rb +108 -0
  22. data/lib/synapse/duplication.rb +60 -0
  23. data/lib/synapse/errors.rb +13 -0
  24. data/lib/synapse/event_bus/event_bus.rb +40 -0
  25. data/lib/synapse/event_bus/event_listener.rb +16 -0
  26. data/lib/synapse/event_bus/event_listener_proxy.rb +12 -0
  27. data/lib/synapse/event_bus/simple_event_bus.rb +69 -0
  28. data/lib/synapse/event_bus/wiring.rb +23 -0
  29. data/lib/synapse/event_sourcing/aggregate_factory.rb +69 -0
  30. data/lib/synapse/event_sourcing/aggregate_root.rb +104 -0
  31. data/lib/synapse/event_sourcing/conflict_resolver.rb +80 -0
  32. data/lib/synapse/event_sourcing/entity.rb +64 -0
  33. data/lib/synapse/event_sourcing/member.rb +72 -0
  34. data/lib/synapse/event_sourcing/repository.rb +119 -0
  35. data/lib/synapse/event_sourcing/snapshot/count_stream.rb +86 -0
  36. data/lib/synapse/event_sourcing/snapshot/count_trigger.rb +91 -0
  37. data/lib/synapse/event_sourcing/snapshot/taker.rb +73 -0
  38. data/lib/synapse/event_sourcing/storage_listener.rb +34 -0
  39. data/lib/synapse/event_sourcing/stream_decorator.rb +25 -0
  40. data/lib/synapse/event_store/errors.rb +16 -0
  41. data/lib/synapse/event_store/event_store.rb +43 -0
  42. data/lib/synapse/event_store/in_memory.rb +59 -0
  43. data/lib/synapse/event_store/mongo/cursor_event_stream.rb +63 -0
  44. data/lib/synapse/event_store/mongo/event_store.rb +86 -0
  45. data/lib/synapse/event_store/mongo/per_commit_strategy.rb +253 -0
  46. data/lib/synapse/event_store/mongo/per_event_strategy.rb +143 -0
  47. data/lib/synapse/event_store/mongo/storage_strategy.rb +113 -0
  48. data/lib/synapse/event_store/mongo/template.rb +73 -0
  49. data/lib/synapse/identifier.rb +23 -0
  50. data/lib/synapse/message.rb +101 -0
  51. data/lib/synapse/message_builder.rb +38 -0
  52. data/lib/synapse/process_manager/correlation.rb +32 -0
  53. data/lib/synapse/process_manager/correlation_resolver.rb +14 -0
  54. data/lib/synapse/process_manager/correlation_set.rb +58 -0
  55. data/lib/synapse/process_manager/process.rb +71 -0
  56. data/lib/synapse/repository/errors.rb +26 -0
  57. data/lib/synapse/repository/lock_manager.rb +40 -0
  58. data/lib/synapse/repository/locking.rb +97 -0
  59. data/lib/synapse/repository/pessimistic_lock_manager.rb +61 -0
  60. data/lib/synapse/repository/repository.rb +109 -0
  61. data/lib/synapse/serialization/converter.rb +39 -0
  62. data/lib/synapse/serialization/converter/chain.rb +45 -0
  63. data/lib/synapse/serialization/converter/factory.rb +68 -0
  64. data/lib/synapse/serialization/converter/identity.rb +29 -0
  65. data/lib/synapse/serialization/converter/json.rb +31 -0
  66. data/lib/synapse/serialization/converter/ox.rb +31 -0
  67. data/lib/synapse/serialization/errors.rb +12 -0
  68. data/lib/synapse/serialization/lazy_object.rb +61 -0
  69. data/lib/synapse/serialization/message/data.rb +25 -0
  70. data/lib/synapse/serialization/message/metadata.rb +13 -0
  71. data/lib/synapse/serialization/message/serialization_aware.rb +17 -0
  72. data/lib/synapse/serialization/message/serialization_aware_message.rb +66 -0
  73. data/lib/synapse/serialization/message/serialized_message.rb +201 -0
  74. data/lib/synapse/serialization/message/serialized_message_builder.rb +64 -0
  75. data/lib/synapse/serialization/message/serialized_object_cache.rb +50 -0
  76. data/lib/synapse/serialization/message/serializer.rb +47 -0
  77. data/lib/synapse/serialization/revision_resolver.rb +30 -0
  78. data/lib/synapse/serialization/serialized_object.rb +37 -0
  79. data/lib/synapse/serialization/serialized_type.rb +31 -0
  80. data/lib/synapse/serialization/serializer.rb +98 -0
  81. data/lib/synapse/serialization/serializer/marshal.rb +32 -0
  82. data/lib/synapse/serialization/serializer/oj.rb +34 -0
  83. data/lib/synapse/serialization/serializer/ox.rb +31 -0
  84. data/lib/synapse/uow/factory.rb +28 -0
  85. data/lib/synapse/uow/listener.rb +79 -0
  86. data/lib/synapse/uow/listener_collection.rb +93 -0
  87. data/lib/synapse/uow/nesting.rb +262 -0
  88. data/lib/synapse/uow/provider.rb +71 -0
  89. data/lib/synapse/uow/storage_listener.rb +14 -0
  90. data/lib/synapse/uow/transaction_manager.rb +27 -0
  91. data/lib/synapse/uow/uow.rb +178 -0
  92. data/lib/synapse/upcasting/chain.rb +78 -0
  93. data/lib/synapse/upcasting/context.rb +58 -0
  94. data/lib/synapse/upcasting/data.rb +30 -0
  95. data/lib/synapse/upcasting/single_upcaster.rb +57 -0
  96. data/lib/synapse/upcasting/upcaster.rb +55 -0
  97. data/lib/synapse/version.rb +3 -0
  98. data/lib/synapse/wiring/message_wiring.rb +41 -0
  99. data/lib/synapse/wiring/wire.rb +55 -0
  100. data/lib/synapse/wiring/wire_registry.rb +61 -0
  101. data/test/command/duplication_test.rb +54 -0
  102. data/test/command/gateway_test.rb +25 -0
  103. data/test/command/interceptor_chain_test.rb +26 -0
  104. data/test/command/serialization_test.rb +37 -0
  105. data/test/command/simple_command_bus_test.rb +141 -0
  106. data/test/command/validation_test.rb +42 -0
  107. data/test/command/wiring_test.rb +73 -0
  108. data/test/domain/aggregate_root_test.rb +57 -0
  109. data/test/domain/fixtures.rb +31 -0
  110. data/test/domain/message_test.rb +61 -0
  111. data/test/domain/stream_test.rb +35 -0
  112. data/test/duplication_test.rb +40 -0
  113. data/test/event_bus/wiring_test.rb +46 -0
  114. data/test/event_sourcing/aggregate_factory_test.rb +28 -0
  115. data/test/event_sourcing/aggregate_root_test.rb +76 -0
  116. data/test/event_sourcing/entity_test.rb +34 -0
  117. data/test/event_sourcing/fixtures.rb +85 -0
  118. data/test/event_sourcing/repository_test.rb +102 -0
  119. data/test/event_sourcing/snapshot/aggregate_taker_test.rb +39 -0
  120. data/test/event_sourcing/snapshot/deferred_taker_test.rb +19 -0
  121. data/test/event_sourcing/snapshot/integration_test.rb +65 -0
  122. data/test/event_sourcing/storage_listener_test.rb +77 -0
  123. data/test/event_store/in_memory_test.rb +47 -0
  124. data/test/process_manager/correlation_set_test.rb +49 -0
  125. data/test/process_manager/correlation_test.rb +24 -0
  126. data/test/process_manager/process_test.rb +52 -0
  127. data/test/repository/locking_test.rb +101 -0
  128. data/test/serialization/converter/factory_test.rb +33 -0
  129. data/test/serialization/converter/identity_test.rb +17 -0
  130. data/test/serialization/converter/json_test.rb +31 -0
  131. data/test/serialization/converter/ox_test.rb +40 -0
  132. data/test/serialization/fixtures.rb +17 -0
  133. data/test/serialization/lazy_object_test.rb +32 -0
  134. data/test/serialization/message/metadata_test.rb +19 -0
  135. data/test/serialization/message/serialization_aware_message_test.rb +88 -0
  136. data/test/serialization/message/serialized_message_builder_test.rb +41 -0
  137. data/test/serialization/message/serialized_message_test.rb +140 -0
  138. data/test/serialization/message/serializer_test.rb +50 -0
  139. data/test/serialization/revision_resolver_test.rb +12 -0
  140. data/test/serialization/serialized_object_test.rb +36 -0
  141. data/test/serialization/serialized_type_test.rb +27 -0
  142. data/test/serialization/serializer/marshal_test.rb +22 -0
  143. data/test/serialization/serializer/oj_test.rb +24 -0
  144. data/test/serialization/serializer/ox_test.rb +36 -0
  145. data/test/serialization/serializer_test.rb +20 -0
  146. data/test/test_helper.rb +19 -0
  147. data/test/uow/factory_test.rb +23 -0
  148. data/test/uow/outer_commit_listener_test.rb +50 -0
  149. data/test/uow/provider_test.rb +70 -0
  150. data/test/uow/uow_test.rb +337 -0
  151. data/test/upcasting/chain_test.rb +29 -0
  152. data/test/upcasting/fixtures.rb +66 -0
  153. data/test/wiring/wire_registry_test.rb +60 -0
  154. data/test/wiring/wire_test.rb +51 -0
  155. metadata +263 -0
@@ -0,0 +1,101 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Repository
5
+ class LockingRepositoryTest < Test::Unit::TestCase
6
+ def setup
7
+ @event_bus = Object.new
8
+ @lock_manager = Object.new
9
+ @storage_listener = Object.new
10
+ @unit_provider = UnitOfWork::UnitOfWorkProvider.new
11
+
12
+ @unit = UnitOfWork::UnitOfWork.new @unit_provider
13
+ @unit.start
14
+
15
+ # I herd you like dependencies
16
+ @repository = TestRepository.new @lock_manager
17
+ @repository.event_bus = @event_bus
18
+ @repository.storage_listener = @storage_listener
19
+ @repository.unit_provider = @unit_provider
20
+ end
21
+
22
+ def test_add
23
+ mock(@lock_manager).obtain_lock(123)
24
+ mock(@lock_manager).release_lock(123)
25
+
26
+ aggregate = TestAggregateRoot.new 123, nil
27
+
28
+ mock(@storage_listener).store(aggregate)
29
+
30
+ @repository.add aggregate
31
+ @unit.commit
32
+ end
33
+
34
+ def test_add_incompatible_aggregate
35
+ mock(@lock_manager).obtain_lock(123)
36
+ mock(@lock_manager).release_lock(123)
37
+
38
+ aggregate = OpenStruct.new
39
+ aggregate.id = 123
40
+
41
+ assert_raises ArgumentError do
42
+ @repository.add aggregate
43
+ end
44
+ end
45
+
46
+ def test_add_versioned
47
+ mock(@lock_manager).obtain_lock(123)
48
+ mock(@lock_manager).release_lock(123)
49
+
50
+ aggregate = TestAggregateRoot.new 123, 0
51
+
52
+ assert_raises ArgumentError do
53
+ @repository.add aggregate
54
+ end
55
+ end
56
+
57
+ def test_load_version_ahead
58
+ mock(@lock_manager).obtain_lock(123)
59
+ mock(@lock_manager).release_lock(123)
60
+
61
+ @repository.aggregate = TestAggregateRoot.new 123, 1
62
+
63
+ assert_raises ConflictingAggregateVersionError do
64
+ @repository.load 123, 0
65
+ end
66
+ end
67
+ end
68
+
69
+ class TestAggregateRoot
70
+ include Domain::AggregateRoot
71
+
72
+ def initialize(id, version)
73
+ @id, @version = id, version
74
+ end
75
+ end
76
+
77
+ class TestRepository < LockingRepository
78
+ attr_accessor :aggregate, :storage_listener
79
+
80
+ protected
81
+
82
+ def perform_load(aggregate_id, expected_version)
83
+ unless @aggregate
84
+ raise AggregateNotFoundError
85
+ end
86
+
87
+ assert_version_expected @aggregate, expected_version
88
+
89
+ @aggregate
90
+ end
91
+
92
+ def aggregate_type
93
+ TestAggregateRoot
94
+ end
95
+
96
+ def storage_listener
97
+ @storage_listener
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+
6
+ class ConverterFactoryTest < Test::Unit::TestCase
7
+
8
+ def setup
9
+ @factory = ConverterFactory.new
10
+ end
11
+
12
+ def test_identity
13
+ assert @factory.has_converter?(String, String)
14
+ assert @factory.converter(String, String).is_a?(IdentityConverter)
15
+ end
16
+
17
+ def test_converter
18
+ refute @factory.has_converter?(Object, String)
19
+
20
+ assert_raise ConversionError do
21
+ @factory.converter(Object, String)
22
+ end
23
+
24
+ converter = ObjectToJsonConverter.new
25
+ @factory.register converter
26
+
27
+ assert @factory.has_converter?(Object, String)
28
+ assert_equal converter, @factory.converter(Object, String)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,17 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+ class IdentityConverterTest < Test::Unit::TestCase
6
+ def test_conversion
7
+ converter = IdentityConverter.new Object
8
+ content = Hash.new
9
+
10
+ assert_equal Object, converter.source_type
11
+ assert_equal Object, converter.target_type
12
+ assert content === converter.convert(content)
13
+ assert content === converter.convert_content(content)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+ class JsonToObjectConverterTest < Test::Unit::TestCase
6
+ def test_convert
7
+ converter = JsonToObjectConverter.new
8
+
9
+ assert_equal String, converter.source_type
10
+ assert_equal Object, converter.target_type
11
+
12
+ output = converter.convert_content '{}'
13
+
14
+ assert_equal Hash, output.class
15
+ end
16
+ end
17
+
18
+ class ObjectToJsonConverterTest < Test::Unit::TestCase
19
+ def test_convert
20
+ converter = ObjectToJsonConverter.new
21
+
22
+ assert_equal Object, converter.source_type
23
+ assert_equal String, converter.target_type
24
+
25
+ output = converter.convert_content Hash.new
26
+
27
+ assert_equal String, output.class
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+ class OxDocumentToXmlConverterTest < Test::Unit::TestCase
6
+ def setup
7
+ skip 'Ox not supported on JRuby' if defined? JRUBY_VERSION
8
+ end
9
+
10
+ def test_convert
11
+ converter = OxDocumentToXmlConverter.new
12
+
13
+ assert_equal Ox::Document, converter.source_type
14
+ assert_equal String, converter.target_type
15
+
16
+ input = Ox::Document.new
17
+ output = converter.convert_content input
18
+
19
+ assert_equal String, output.class
20
+ end
21
+ end
22
+
23
+ class XmlToOxDocumentConverterTest < Test::Unit::TestCase
24
+ def setup
25
+ skip 'Ox not supported on JRuby' if defined? JRUBY_VERSION
26
+ end
27
+
28
+ def test_convert
29
+ converter = XmlToOxDocumentConverter.new
30
+
31
+ assert_equal String, converter.source_type
32
+ assert_equal Ox::Document, converter.target_type
33
+
34
+ output = converter.convert_content '<?xml?>'
35
+
36
+ assert_equal Ox::Document, output.class
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ module Synapse
2
+ module Serialization
3
+ class TestEvent
4
+ attr_reader :a, :b
5
+
6
+ def initialize(a, b)
7
+ @a, @b = a, b
8
+ end
9
+
10
+ def ==(other)
11
+ self.class === other and
12
+ other.a == @a and
13
+ other.b == @b
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+ require 'serialization/fixtures'
3
+
4
+ module Synapse
5
+ module Serialization
6
+ class LazyObjectTest < Test::Unit::TestCase
7
+ def test_deserialize_once
8
+ serializer = MarshalSerializer.new
9
+ event = TestEvent.new 'a', 'b'
10
+
11
+ serialized = serializer.serialize event, String
12
+
13
+ lazy = LazyObject.new serialized, serializer
14
+
15
+ assert_equal serialized, lazy.serialized_object
16
+ assert_equal serializer, lazy.serializer
17
+ assert_equal TestEvent, lazy.type
18
+
19
+ refute lazy.deserialized?
20
+
21
+ deserialized_a = lazy.deserialized
22
+ deserialized_b = lazy.deserialized
23
+
24
+ assert deserialized_a === deserialized_b
25
+ assert deserialized_a == event
26
+ assert deserialized_b == event
27
+
28
+ assert lazy.deserialized?
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+ class SerializedMetadataTest < Test::Unit::TestCase
6
+ def test_initialize
7
+ content = 'test-metadata'
8
+ content_type = String
9
+
10
+ metadata = SerializedMetadata.new content, content_type
11
+
12
+ assert_equal content, metadata.content
13
+ assert_equal content_type, metadata.content_type
14
+ assert_equal Hash.to_s, metadata.type.name
15
+ assert_equal nil, metadata.type.revision
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+ class SerializationAwareDomainEventMessageTest < Test::Unit::TestCase
6
+ def test_delegation
7
+ message = Domain::DomainEventMessage.build do |builder|
8
+ builder.payload = Object.new
9
+ builder.aggregate_id = 123
10
+ builder.sequence_number = 0
11
+ end
12
+
13
+ aware = SerializationAwareDomainEventMessage.new message
14
+
15
+ # EventMessage
16
+ assert_same message.id, aware.id
17
+ assert_same message.metadata, aware.metadata
18
+ assert_same message.payload, aware.payload
19
+ assert_same message.timestamp, aware.timestamp
20
+
21
+ # DomainEventMessage
22
+ assert_same message.aggregate_id, aware.aggregate_id
23
+ assert_same message.sequence_number, aware.sequence_number
24
+ end
25
+
26
+ def test_caching
27
+ message = Domain::DomainEventMessage.build do |builder|
28
+ builder.payload = Object.new
29
+ builder.aggregate_id = 123
30
+ builder.sequence_number = 0
31
+ end
32
+
33
+ aware = SerializationAwareDomainEventMessage.new message
34
+
35
+ serializer_a = MarshalSerializer.new
36
+ serializer_b = MarshalSerializer.new
37
+
38
+ [:serialize_metadata, :serialize_payload].each do |method|
39
+ serialized_a = aware.send method, serializer_a, String
40
+ serialized_b = aware.send method, serializer_a, String
41
+ serialized_c = aware.send method, serializer_b, String
42
+
43
+ assert_same serialized_a, serialized_b
44
+ refute_same serialized_a, serialized_c
45
+ end
46
+ end
47
+
48
+ def test_decorate
49
+ message = Domain::DomainEventMessage.build
50
+
51
+ aware = SerializationAwareDomainEventMessage.decorate message
52
+ new_aware = SerializationAwareDomainEventMessage.decorate aware
53
+
54
+ assert_same aware, new_aware
55
+ end
56
+
57
+ def test_and_metadata
58
+ message = Domain::DomainEventMessage.build do |builder|
59
+ builder.metadata = { foo: 0 }
60
+ end
61
+
62
+ aware = SerializationAwareDomainEventMessage.new message
63
+ new_aware = aware.and_metadata Hash.new
64
+
65
+ assert_same new_aware, aware
66
+
67
+ new_aware = aware.and_metadata bar: 1
68
+
69
+ assert new_aware.is_a? SerializationAwareDomainEventMessage
70
+ end
71
+
72
+ def test_with_metadata
73
+ message = Domain::DomainEventMessage.build do |builder|
74
+ builder.metadata = { foo: 0 }
75
+ end
76
+
77
+ aware = SerializationAwareDomainEventMessage.new message
78
+ new_aware = aware.with_metadata foo: 0
79
+
80
+ assert_same new_aware, aware
81
+
82
+ new_aware = aware.with_metadata bar: 1
83
+
84
+ assert new_aware.is_a? SerializationAwareDomainEventMessage
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+
6
+ class SerializedDomainEventMessageBuilderTest < Test::Unit::TestCase
7
+ def test_from_data
8
+ builder = SerializedDomainEventMessageBuilder.new
9
+
10
+ serializer = MarshalSerializer.new
11
+
12
+ data = StubSerializedDomainEventData.new
13
+ data.id = 0
14
+ data.metadata = SerializedObject.new(nil, nil, SerializedType.new(Object.to_s, nil))
15
+ data.payload = SerializedObject.new(nil, nil, SerializedType.new(Object.to_s, nil))
16
+ data.aggregate_id = 1
17
+ data.sequence_number = 2
18
+
19
+ builder.from_data data, serializer
20
+
21
+ built = builder.build
22
+
23
+ assert_equal data.id, built.id
24
+
25
+ assert_equal data.metadata, built.serialized_metadata.serialized_object
26
+ assert_equal serializer, built.serialized_metadata.serializer
27
+ assert_equal data.payload, built.serialized_payload.serialized_object
28
+ assert_equal serializer, built.serialized_payload.serializer
29
+
30
+ assert_equal data.timestamp, built.timestamp
31
+ assert_equal data.aggregate_id, built.aggregate_id
32
+ assert_equal data.sequence_number, built.sequence_number
33
+ end
34
+ end
35
+
36
+ class StubSerializedDomainEventData < SerializedDomainEventData
37
+ attr_accessor :id, :metadata, :payload, :timestamp, :aggregate_id, :sequence_number
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,140 @@
1
+ require 'test_helper'
2
+
3
+ module Synapse
4
+ module Serialization
5
+
6
+ class SerializedMessageTest < Test::Unit::TestCase
7
+ def test_build
8
+ message = SerializedMessage.build do |builder|
9
+ assert builder.is_a? SerializedMessageBuilder
10
+ end
11
+ assert message.is_a? SerializedMessage
12
+ end
13
+ end
14
+
15
+ class SerializedEventMessageTest < Test::Unit::TestCase
16
+ def test_build
17
+ message = SerializedEventMessage.build do |builder|
18
+ assert builder.is_a? SerializedEventMessageBuilder
19
+ end
20
+ assert message.is_a? SerializedEventMessage
21
+ end
22
+ end
23
+
24
+ class SerializedDomainEventMessageTest < Test::Unit::TestCase
25
+ def setup
26
+ @serializer_a = MarshalSerializer.new
27
+ @serializer_b = MarshalSerializer.new
28
+ end
29
+
30
+ def test_serialization
31
+ metadata = { foo: 0 }
32
+ payload = { bar: 1 }
33
+
34
+ metadata_serialized = @serializer_a.serialize metadata, String
35
+ payload_serialized = @serializer_a.serialize payload, String
36
+
37
+ metadata_lazy = LazyObject.new metadata_serialized, @serializer_a
38
+ payload_lazy = LazyObject.new payload_serialized, @serializer_a
39
+
40
+ message = SerializedDomainEventMessage.build do |builder|
41
+ builder.metadata = metadata_lazy
42
+ builder.payload = payload_lazy
43
+ end
44
+
45
+ refute message.serialized_metadata.deserialized?
46
+ assert_equal metadata, message.metadata
47
+
48
+ assert_equal Hash, message.payload_type
49
+ refute message.serialized_payload.deserialized?
50
+
51
+ assert_equal payload, message.payload
52
+ assert message.serialized_payload.deserialized?
53
+
54
+ metadata_serialized_a = message.serialize_metadata @serializer_a, String
55
+ assert_same metadata_serialized, metadata_serialized_a
56
+ payload_serialized_a = message.serialize_payload @serializer_a, String
57
+ assert_same payload_serialized, payload_serialized_a
58
+
59
+ metadata_serialized_b = message.serialize_metadata @serializer_b, String
60
+ assert_equal metadata_serialized, metadata_serialized_b
61
+ payload_serialized_b = message.serialize_payload @serializer_b, String
62
+ assert_equal payload_serialized, payload_serialized_b
63
+ end
64
+
65
+ def test_and_metadata
66
+ metadata = { foo: 0 }
67
+ payload = { bar: 1 }
68
+
69
+ metadata_serialized = @serializer_a.serialize metadata, String
70
+ metadata_lazy = LazyObject.new metadata_serialized, @serializer_a
71
+ payload_serialized = @serializer_a.serialize metadata, String
72
+ payload_lazy = LazyObject.new payload_serialized, @serializer_a
73
+
74
+ message = SerializedDomainEventMessage.build do |builder|
75
+ builder.id = 1
76
+ builder.metadata = metadata_lazy
77
+ builder.payload = payload_lazy
78
+ builder.timestamp = Time.now
79
+ builder.aggregate_id = 2
80
+ builder.sequence_number = 3
81
+ end
82
+
83
+ new_message = message.and_metadata Hash.new
84
+ assert_same message, new_message
85
+
86
+ new_message = message.and_metadata baz: 3
87
+
88
+ merged = {
89
+ foo: 0,
90
+ baz: 3
91
+ }
92
+
93
+ assert_equal merged, new_message.metadata
94
+ assert_message_content_equal message, new_message
95
+ end
96
+
97
+ def test_with_metadata
98
+ metadata = { foo: 0 }
99
+ payload = { bar: 1 }
100
+
101
+ metadata_serialized = @serializer_a.serialize metadata, String
102
+ metadata_lazy = LazyObject.new metadata_serialized, @serializer_a
103
+ payload_serialized = @serializer_a.serialize metadata, String
104
+ payload_lazy = LazyObject.new payload_serialized, @serializer_a
105
+
106
+ message = SerializedDomainEventMessage.build do |builder|
107
+ builder.id = 1
108
+ builder.metadata = metadata_lazy
109
+ builder.payload = payload_lazy
110
+ builder.timestamp = Time.now
111
+ builder.aggregate_id = 2
112
+ builder.sequence_number = 3
113
+ end
114
+
115
+ new_message = message.with_metadata foo: 0
116
+ assert_same message, new_message
117
+
118
+ new_message = message.with_metadata baz: 3
119
+
120
+ replaced = {
121
+ baz: 3
122
+ }
123
+
124
+ assert_equal replaced, new_message.metadata
125
+ assert_message_content_equal message, new_message
126
+ end
127
+
128
+ private
129
+
130
+ def assert_message_content_equal(expected, actual)
131
+ assert_equal expected.id, actual.id
132
+ assert_equal expected.serialized_payload, actual.serialized_payload
133
+ assert_equal expected.timestamp, actual.timestamp
134
+ assert_equal expected.aggregate_id, actual.aggregate_id
135
+ assert_equal expected.sequence_number, actual.sequence_number
136
+ end
137
+ end
138
+
139
+ end
140
+ end