Class: Messaging::Fixtures::Handler

Inherits:
Object
  • Object
show all
Includes:
Initializer, TestBench::Fixture
Defined in:
lib/messaging/fixtures/handler.rb,
lib/messaging/fixtures/defaults.rb

Defined Under Namespace

Modules: Defaults

Constant Summary collapse

Error =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(handler, input_message, entity = nil, entity_version = nil, clock_time: nil, identifier_uuid: nil, &test_block) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/messaging/fixtures/handler.rb', line 16

def self.build(handler, input_message, entity=nil, entity_version=nil, clock_time: nil, identifier_uuid: nil, &test_block)
  instance = new(handler, input_message, entity, entity_version, clock_time, identifier_uuid, test_block)

  set_store_entity(handler, entity, entity_version)
  set_clock_time(handler, clock_time)
  set_identifier_uuid(handler, identifier_uuid)

  instance
end

.set_clock_time(handler, clock_time) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/messaging/fixtures/handler.rb', line 32

def self.set_clock_time(handler, clock_time)
  if clock_time.nil?
    if handler.respond_to?(:clock)
      handler.clock.now = Defaults.clock_time
    end
  else
    handler.clock.now = clock_time
  end
end

.set_identifier_uuid(handler, identifier_uuid) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/messaging/fixtures/handler.rb', line 42

def self.set_identifier_uuid(handler, identifier_uuid)
  if identifier_uuid.nil?
    if handler.respond_to?(:identifier)
      handler.identifier.set(Defaults.identifier_uuid)
    end
  else
    handler.identifier.set(identifier_uuid)
  end
end

.set_store_entity(handler, entity, entity_version) ⇒ Object



26
27
28
29
30
# File 'lib/messaging/fixtures/handler.rb', line 26

def self.set_store_entity(handler, entity, entity_version)
  return if entity.nil?

  handler.store.add(entity.id, entity, entity_version)
end

Instance Method Details

#assert_input_message(&test_block) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/messaging/fixtures/handler.rb', line 77

def assert_input_message(&test_block)
  context_name = "Input Message"
  if not input_message.nil?
    context_name = "#{context_name}: #{input_message.class.message_type}"
  end

  fixture(Message, input_message, title_context_name: context_name, &test_block)
end

#assert_write(message_class, &test_block) ⇒ Object



95
96
97
98
# File 'lib/messaging/fixtures/handler.rb', line 95

def assert_write(message_class, &test_block)
  fixture = fixture(Writer, handler.write, message_class, &test_block)
  fixture.message
end

#assert_written_message(written_message, &test_block) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/messaging/fixtures/handler.rb', line 86

def assert_written_message(written_message, &test_block)
  context_name = "Written Message"
  if not written_message.nil?
    context_name = "#{context_name}: #{written_message.class.message_type}"
  end

  fixture(Message, written_message, input_message, title_context_name: context_name, &test_block)
end

#callObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/messaging/fixtures/handler.rb', line 52

def call
  context "Handler: #{handler.class.name.split('::').last}" do
    detail "Handler Class: #{handler.class.name}"

    if test_block.nil?
      raise Error, "Handler fixture must be executed with a block"
    end

    detail "Entity Class: #{entity.class.name}"
    detail "Entity Data: #{entity&.attributes.inspect}"

    if not clock_time.nil?
      detail "Clock Time: #{clock_time.inspect}"
    end

    if not identifier_uuid.nil?
      detail "Identifier UUID: #{identifier_uuid}"
    end

    handler.(input_message)

    test_block.call(self)
  end
end

#entity_sequenceObject



9
10
11
12
# File 'lib/messaging/fixtures/handler.rb', line 9

def entity_sequence
  return nil if entity.nil?
  entity.sequence
end

#refute_write(message_class = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/messaging/fixtures/handler.rb', line 100

def refute_write(message_class=nil)
  writer = handler.write

  context_name = "No Write"
  if not message_class.nil?
    write_telemetry_data = Writer.get_data(writer, message_class)
    written = !write_telemetry_data.nil?
    context_name = "#{context_name}: #{message_class.message_type}"
  else
    written = writer.written?
  end

  context context_name do
    detail "Message Class: #{message_class.inspect}"
    test "Not written" do
      refute(written)
    end
  end
end