json_parse_agent_spec.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. require 'rails_helper'
  2. describe Agents::JsonParseAgent do
  3. before(:each) do
  4. @checker = Agents::JsonParseAgent.new(:name => "somename", :options => Agents::JsonParseAgent.new.default_options)
  5. @checker.user = users(:jane)
  6. @checker.save!
  7. end
  8. it "event description does not throw an exception" do
  9. expect(@checker.event_description).to include('parsed')
  10. end
  11. describe "validating" do
  12. before do
  13. expect(@checker).to be_valid
  14. end
  15. it "requires data to be present" do
  16. @checker.options['data'] = ''
  17. expect(@checker).not_to be_valid
  18. end
  19. it "requires data_key to be set" do
  20. @checker.options['data_key'] = ''
  21. expect(@checker).not_to be_valid
  22. end
  23. end
  24. context '#working' do
  25. it 'is not working without having received an event' do
  26. expect(@checker).not_to be_working
  27. end
  28. it 'is working after receiving an event without error' do
  29. @checker.last_receive_at = Time.now
  30. expect(@checker).to be_working
  31. end
  32. end
  33. describe "#receive" do
  34. it "parses valid JSON" do
  35. event = Event.new(payload: { data: '{"test": "data"}' } )
  36. expect { @checker.receive([event]) }.to change(Event, :count).by(1)
  37. end
  38. it "writes to the error log when the JSON could not be parsed" do
  39. event = Event.new(payload: { data: '{"test": "data}' } )
  40. expect { @checker.receive([event]) }.to change(AgentLog, :count).by(1)
  41. end
  42. it "support merge mode" do
  43. @checker.options[:mode] = "merge"
  44. event = Event.new(payload: { data: '{"test": "data"}', extra: 'a' } )
  45. expect { @checker.receive([event]) }.to change { Event.count }.by(1)
  46. last_payload = Event.last.payload
  47. expect(last_payload['extra']).to eq('a')
  48. end
  49. end
  50. end