Skip to content

Commit

Permalink
MQTT: Remove method that was only used in tests
Browse files Browse the repository at this point in the history
Make tests use the real method to prevent mistakes
  • Loading branch information
viktorsmari committed Apr 10, 2020
1 parent 0aa5432 commit 3e9202b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
4 changes: 1 addition & 3 deletions app/lib/mqtt_messages_handler.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class MqttMessagesHandler
def self.handle(packet)
topic = packet.topic
message = packet.payload
def self.handle_topic(topic, message)
return if topic.nil?

# handle_inventory is the only one that does NOT need a device
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/mqtt_subscriber.rake
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace :mqtt do
)

client.get do |topic, message|
MqttMessagesHandler.handle_topic topic, message
MqttMessagesHandler.handle_topic(topic, message)
rescue Exception => e
mqtt_log.info e
Raven.capture_exception(e)
Expand Down
21 changes: 9 additions & 12 deletions spec/lib/mqtt_messages_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
payload: '{"random_property":"random_result"}'
)

@inventory_packet_without_topic = MQTT::Packet::Publish.new(
payload: '{"random_property":"random_result2"}'
)

@hardware_info_packet = MQTT::Packet::Publish.new(
topic: "device/sck/#{device.device_token}/info",
payload: '{"id":48,"uuid":"7d45fead-defd-4482-bc6a-a1b711879e2d"}'
Expand Down Expand Up @@ -81,15 +77,15 @@
it 'queues reading data in order to be stored' do
# model/storer.rb is not using Kairos, but Redis -> Telnet
#expect(Kairos).to receive(:http_post_to).with("/datapoints", @data_array)
MqttMessagesHandler.handle(@packet)
MqttMessagesHandler.handle_topic(@packet.topic, @packet.payload)
end
end

context 'invalid packet' do
it 'it notifies Raven' do
allow(Raven).to receive(:capture_exception)
expect(Kairos).not_to receive(:http_post_to)
MqttMessagesHandler.handle(@invalid_packet)
MqttMessagesHandler.handle_topic(@invalid_packet.topic, @invalid_packet.payload)
#expect(Raven).to have_received(:capture_exception).with(RuntimeError)
end
end
Expand All @@ -100,7 +96,7 @@
expect(Redis.current).to receive(:publish).with(
'token-received', { device_id: device.id }.to_json
)
MqttMessagesHandler.handle(@hello_packet)
MqttMessagesHandler.handle_topic(@hello_packet.topic, @hello_packet.payload)
end
end

Expand All @@ -110,30 +106,31 @@
# This creates a new device_inventory item
expect(@inventory_packet.payload).to eq((device_inventory.report.to_json))
expect(DeviceInventory.count).to eq(1)
MqttMessagesHandler.handle(@inventory_packet)
MqttMessagesHandler.handle_topic(@inventory_packet.topic, @inventory_packet.payload)
expect(DeviceInventory.last.report["random_property"]).to eq('random_result')
expect(DeviceInventory.count).to eq(2)
end

it 'does not log inventory without a correct topic' do
it 'does not log inventory with an incorrect / nil topic' do
expect(DeviceInventory.count).to eq(0)
MqttMessagesHandler.handle(@inventory_packet_without_topic)
MqttMessagesHandler.handle_topic('invenxxx','{"random_property":"random_result2"}')
MqttMessagesHandler.handle_topic(nil,'{"random_property":"random_result2"}')
expect(DeviceInventory.count).to eq(0)
end
end

describe '#hardware_info' do
it 'hardware info has been received and id changed from 47 -> 48' do
expect(device.hardware_info["id"]).to eq(47)
MqttMessagesHandler.handle(@hardware_info_packet)
MqttMessagesHandler.handle_topic(@hardware_info_packet.topic, @hardware_info_packet.payload)
device.reload
expect(device.hardware_info["id"]).to eq(48)
expect(@hardware_info_packet.payload).to eq((device.hardware_info.to_json))
end

it 'does not handle bad topic' do
expect(device.hardware_info["id"]).to eq(47)
MqttMessagesHandler.handle(@hardware_info_packet_bad)
MqttMessagesHandler.handle_topic(@hardware_info_packet_bad.topic, @hardware_info_packet_bad.payload)
device.reload
expect(device.hardware_info["id"]).to eq(47)
expect(@hardware_info_packet_bad.payload).to_not eq((device.hardware_info.to_json))
Expand Down

0 comments on commit 3e9202b

Please sign in to comment.