cpi-event-connector 0.0.15 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cpi-event-connector (0.0.14)
4
+ cpi-event-connector (0.0.15)
5
5
  activesupport (~> 3)
6
6
  bunny (~> 0.8.0)
7
+ nokogiri (~> 1.5)
7
8
  uuidtools (~> 2.1.2)
8
9
 
9
10
  GEM
@@ -18,6 +19,7 @@ GEM
18
19
  i18n (0.6.0)
19
20
  justinf-unification_assertion (0.0.2)
20
21
  multi_json (1.3.6)
22
+ nokogiri (1.5.5)
21
23
  rspec (2.8.0)
22
24
  rspec-core (~> 2.8.0)
23
25
  rspec-expectations (~> 2.8.0)
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "cpi_event_connector/version"
3
+ require "cpi/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "cpi-event-connector"
@@ -24,4 +24,5 @@ Gem::Specification.new do |s|
24
24
  s.add_runtime_dependency "bunny", "~> 0.8.0"
25
25
  s.add_runtime_dependency "activesupport", "~> 3"
26
26
  s.add_runtime_dependency "uuidtools", "~> 2.1.2"
27
+ s.add_runtime_dependency "nokogiri", "~> 1.5"
27
28
  end
@@ -22,7 +22,7 @@ module Cpi
22
22
  end
23
23
  end
24
24
 
25
- def connectivity_test
25
+ def test_connectivity
26
26
  @started = false
27
27
  start
28
28
  "Event Connector can connect to AMQP server at #{@params[:host]}:#{@params[:port]}"
data/lib/cpi/event.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'active_support/core_ext/string'
2
+ require 'nokogiri'
3
+
4
+ module Cpi
5
+ class Event
6
+ def initialize(xml)
7
+ @xml_doc = parse_xml(xml)
8
+ end
9
+
10
+ def event_type
11
+ @xml_doc.xpath('//header/event_type').text
12
+ end
13
+
14
+ def source_tenant_uid
15
+ @xml_doc.xpath('//header/source_tenant').text
16
+ end
17
+
18
+ def event_uid
19
+ nil_if_blank(@xml_doc.xpath('//header/event_uid').text)
20
+ end
21
+
22
+ def queue
23
+ nil_if_blank(@xml_doc.xpath('//header/queue').text)
24
+ end
25
+
26
+ private
27
+
28
+ def nil_if_blank(str)
29
+ str.blank? ? nil : str
30
+ end
31
+
32
+ def parse_xml(xml)
33
+ Nokogiri::XML(xml) do |config|
34
+ config.strict
35
+ end
36
+ end
37
+ end
38
+ end
@@ -3,7 +3,7 @@ require 'active_support/core_ext/string'
3
3
  module Cpi
4
4
  THREAD_LOCAL_VARIABLE = :cpi_connector
5
5
 
6
- class Event
6
+ class EventConnector
7
7
  class << self
8
8
  attr_accessor :connection_params
9
9
  attr_accessor :logger
@@ -19,25 +19,43 @@ module Cpi
19
19
  self.connection_params = params
20
20
  end
21
21
 
22
- def self.transmit(event_type, tenant_uid, root_path, options=nil)
22
+ def self.transmit(event_type, tenant_uid, content, options=nil)
23
+ options ||= {}
23
24
  if !@enabled
24
25
  @logger.debug "Aborting transmit because CPI Event Connector not enabled"
25
26
  return
26
27
  end
27
28
  raise ArgumentError, "Tenant UID must be specified" if tenant_uid.blank?
28
29
 
29
- event_json = Cpi::Generator.create_event(event_type, tenant_uid, root_path)
30
+ event_json = Generator.create_event(event_type, tenant_uid, content, options.delete(:uid))
30
31
  @logger.debug "Transmitting message '#{event_json}'"
31
32
  self.connector.transmit(event_json, options)
32
33
  @logger.debug "Transmission complete"
33
34
  end
34
35
 
35
- def self.connectivity_test
36
+ # This method expects a routing header to be present in the xml document
37
+ #
38
+ # <cpi_event>
39
+ # <header>
40
+ # <event_uid>unique_id</event_uid> - optional
41
+ # <event_type>type</event_type>
42
+ # <source_tenant>tenant_uid</source_tenant>
43
+ # <queue>system_queue_name</queue> - optional
44
+ # </header>
45
+ # <content>some content</content>
46
+ # </cpi_event>
47
+ def self.send_event(xml)
48
+ d = Event.new(xml)
49
+
50
+ transmit(d.event_type, d.source_tenant_uid, xml, {:queue => d.queue, :uid => d.event_uid})
51
+ end
52
+
53
+ def self.test_connectivity
36
54
  if !@enabled
37
55
  return 'Event Connector is not enabled.'
38
56
  end
39
57
 
40
- self.connector.connectivity_test
58
+ self.connector.test_connectivity
41
59
  end
42
60
 
43
61
  private
@@ -3,9 +3,8 @@ require 'uuidtools'
3
3
 
4
4
  module Cpi
5
5
  class Generator
6
-
7
- def self.create_event(event_type, tenant_uid, content)
8
- uid = UUIDTools::UUID.timestamp_create().to_s
6
+ def self.create_event(event_type, tenant_uid, content, uid=nil)
7
+ uid ||= UUIDTools::UUID.timestamp_create().to_s
9
8
 
10
9
  hash = {"event_type" => event_type, "tenant" => tenant_uid, "content" => content, "uid" => uid}
11
10
 
@@ -0,0 +1,3 @@
1
+ module Cpi
2
+ VERSION = "0.1.0"
3
+ end
@@ -1,7 +1,5 @@
1
- require "cpi_event_connector/version"
2
- require "cpi_event_connector/generator"
3
- require "cpi_event_connector/connector"
4
- require "cpi_event_connector/event"
5
-
6
- module Cpi
7
- end
1
+ require "cpi/version"
2
+ require "cpi/generator"
3
+ require "cpi/connector"
4
+ require "cpi/event"
5
+ require "cpi/event_connector"
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require_relative '../lib/cpi_event_connector/connector'
2
+ require_relative '../lib/cpi/connector'
3
3
  require 'bunny'
4
4
 
5
5
  describe Cpi::Connector do
@@ -0,0 +1,152 @@
1
+ require 'spec_helper'
2
+ require_relative '../lib/cpi/event_connector'
3
+ require 'active_support'
4
+ require 'unification_assertion'
5
+
6
+ describe Cpi::EventConnector do
7
+ let(:config) { { :enabled => true, :key => "value" } }
8
+ let(:logger) { stub("Logger", debug: nil, info: nil) }
9
+ let(:real_config) { {
10
+ host: "localhost",
11
+ port: "5672",
12
+ username: "guest",
13
+ password: "guest",
14
+ queue: "connector-test",
15
+ enabled: "true"
16
+ } }
17
+
18
+ describe ".configure" do
19
+ let(:connector) { double(Cpi::Connector) }
20
+
21
+ it "accepts a hash of connection information" do
22
+ Cpi::EventConnector.configure(config)
23
+
24
+ Cpi::EventConnector.connection_params.should == { :key => "value" }
25
+ end
26
+
27
+ it "accepts a logger and sets it as a class variable" do
28
+ Cpi::EventConnector.configure({}, logger)
29
+
30
+ Cpi::EventConnector.logger.should == logger
31
+ end
32
+ end
33
+
34
+ describe "acceptance tests" do
35
+ let(:content) { "<test />" }
36
+
37
+ before do
38
+ Cpi::EventConnector.configure(real_config)
39
+ end
40
+
41
+ it "transmit generates and sends an event" do
42
+ Cpi::EventConnector.transmit("shipment_shipped", "stuff", content)
43
+
44
+ h = get_event_from_rabbimq("connector-test")
45
+
46
+ h.should unify({"event_type" => "shipment_shipped", "tenant" => "stuff", "content" => content, "uid" => :_x})
47
+ end
48
+
49
+ it "send_event does not require a uid or a queue in the routing envelope" do
50
+ xml = <<-XML
51
+ <cpi_event>
52
+ <header>
53
+ <event_type>type</event_type>
54
+ <source_tenant>tenant_uid</source_tenant>
55
+ </header>
56
+ <content>some content</content>
57
+ </cpi_event>
58
+ XML
59
+ Cpi::EventConnector.send_event(xml)
60
+
61
+ h = get_event_from_rabbimq("connector-test")
62
+ h.should unify({"event_type" => "type", "tenant" => "tenant_uid", "content" => xml, "uid" => :_x})
63
+ end
64
+
65
+ it "send_event generates and sends an event" do
66
+ xml = <<-XML
67
+ <cpi_event>
68
+ <header>
69
+ <event_uid>unique_id</event_uid>
70
+ <event_type>type</event_type>
71
+ <source_tenant>tenant_uid</source_tenant>
72
+ <queue>system_queue_name</queue>
73
+ </header>
74
+ <content>some content</content>
75
+ </cpi_event>
76
+ XML
77
+ Cpi::EventConnector.send_event(xml)
78
+
79
+ h = get_event_from_rabbimq("system_queue_name")
80
+ h.should unify({"event_type" => "type", "tenant" => "tenant_uid", "content" => xml, "uid" => 'unique_id'})
81
+ end
82
+ end
83
+
84
+ def get_event_from_rabbimq(queue)
85
+ b = Bunny.new
86
+ b.start
87
+ q = b.queue(queue, :durable => true)
88
+ message = q.pop
89
+ return nil if message[:payload] == :queue_empty
90
+ h = ActiveSupport::JSON.decode(message[:payload])
91
+ b.stop
92
+ h
93
+ end
94
+
95
+ describe ".transmit" do
96
+ it "creates a connection for each thread" do
97
+ Cpi::EventConnector.configure(config)
98
+ connector1 = double("connector 1")
99
+ connector2 = double("connector 2")
100
+ Cpi::Connector.stub(:new).and_return(connector1, connector2)
101
+
102
+ connector1.should_receive(:transmit)
103
+ connector2.should_receive(:transmit)
104
+
105
+ Thread.new do
106
+ Cpi::EventConnector.transmit("message", "tenant_uid", "content")
107
+ end.join
108
+
109
+ Thread.new do
110
+ Cpi::EventConnector.transmit("message", "tenant_uid", "content")
111
+ end.join
112
+ end
113
+
114
+ it "does not transmit when it is not enabled" do
115
+ Cpi::EventConnector.configure({enabled: false})
116
+ Cpi::Generator.should_not_receive(:create_event)
117
+ # And there shouldn't be an exception
118
+
119
+ Cpi::EventConnector.transmit("message", "tenant_uid", "content")
120
+ end
121
+
122
+ it "does nothing when called before being configured" do
123
+ Cpi::EventConnector.logger = stub(debug: nil)
124
+ Cpi::Generator.should_not_receive(:create_event)
125
+ Cpi::EventConnector.transmit("message", "tenant_uid", "content")
126
+ end
127
+
128
+ it "raises exception when no tenant_uid is passed in" do
129
+ Cpi::EventConnector.configure(config)
130
+ Cpi::Generator.should_not_receive(:create_event)
131
+ expect {Cpi::EventConnector.transmit("message", nil, "content")}.to raise_error(ArgumentError)
132
+ end
133
+ end
134
+
135
+ describe ".test_connectivity" do
136
+ it "tells you when the connector is not enabled" do
137
+ Cpi::EventConnector.configure({enabled: false})
138
+ Cpi::EventConnector.test_connectivity.should == 'Event Connector is not enabled.'
139
+ end
140
+
141
+ it "tells you when the connection params are correct" do
142
+ Cpi::EventConnector.configure(real_config)
143
+ Cpi::EventConnector.test_connectivity.should == 'Event Connector can connect to AMQP server at localhost:5672'
144
+ end
145
+
146
+ it "returns false when the connection params are incorrect" do
147
+ real_config[:host] = "doesntwork"
148
+ Cpi::EventConnector.configure(real_config)
149
+ Cpi::EventConnector.test_connectivity.should == "getaddrinfo: Name or service not known"
150
+ end
151
+ end
152
+ end
data/spec/event_spec.rb CHANGED
@@ -1,112 +1,48 @@
1
- require 'spec_helper'
2
- require_relative '../lib/cpi_event_connector'
3
- require 'active_support'
4
- require 'unification_assertion'
1
+ require_relative '../lib/cpi/event'
5
2
 
6
3
  describe Cpi::Event do
7
- let(:config) { { :enabled => true, :key => "value" } }
8
- let(:logger) { stub("Logger", debug: nil, info: nil) }
9
- let(:real_config) { {
10
- host: "localhost",
11
- port: "5672",
12
- username: "guest",
13
- password: "guest",
14
- queue: "connector-test",
15
- enabled: "true"
16
- } }
17
4
 
18
- describe ".configure" do
19
- let(:connector) { double(Cpi::Connector) }
20
-
21
- it "accepts a hash of connection information" do
22
- Cpi::Event.configure(config)
23
-
24
- Cpi::Event.connection_params.should == { :key => "value" }
25
- end
26
-
27
- it "accepts a logger and sets it as a class variable" do
28
- Cpi::Event.configure({}, logger)
29
-
30
- Cpi::Event.logger.should == logger
31
- end
5
+ let(:xml) do
6
+ <<-XML
7
+ <cpi_event>
8
+ <header>
9
+ <event_uid>unique_id</event_uid>
10
+ <event_type>type</event_type>
11
+ <source_tenant>tenant_uid</source_tenant>
12
+ <queue>system_queue_name</queue>
13
+ </header>
14
+ <content>some content</content>
15
+ </cpi_event>
16
+ XML
32
17
  end
33
18
 
34
- describe ".transmit" do
35
-
36
- let(:content) { "<test />" }
19
+ let(:doc) { Cpi::Event.new(xml) }
37
20
 
38
- it "generates and sends an event" do
39
- Cpi::Event.configure(real_config)
40
-
41
- Cpi::Event.transmit("shipment_shipped", "stuff", content)
42
-
43
- b = Bunny.new
44
- b.start
45
- q = b.queue("connector-test", :durable => true)
46
- message = q.pop
47
- h = ActiveSupport::JSON.decode(message[:payload])
48
- b.stop
49
-
50
- h.should unify({"event_type" => "shipment_shipped", "tenant" => "stuff", "content" => content, "uid" => :_x})
51
- end
52
-
53
- it "creates a connection for each thread" do
54
- Cpi::Event.configure(config)
55
-
56
- connector1 = double("connector 1")
57
- connector2 = double("connector 2")
58
- Cpi::Connector.stub(:new).and_return(connector1, connector2)
59
-
60
- connector1.should_receive(:transmit)
61
- connector2.should_receive(:transmit)
62
-
63
- Thread.new do
64
- Cpi::Event.transmit("message", "tenant_uid", "content")
65
- end.join
66
-
67
- Thread.new do
68
- Cpi::Event.transmit("message", "tenant_uid", "content")
69
- end.join
70
- end
71
-
72
- it "does not transmit when it is not enabled" do
73
- real_config[:enabled] = false
74
-
75
- Cpi::Event.configure(real_config)
76
- Cpi::Generator.should_not_receive(:create_event)
77
- # And there shouldn't be an exception
78
-
79
- Cpi::Event.transmit("message", "tenant_uid", "content")
21
+ describe "source_tenant_uid" do
22
+ it "get extracted" do
23
+ doc.source_tenant_uid.should == 'tenant_uid'
80
24
  end
25
+ end
81
26
 
82
- it "does nothing when called before being configured" do
83
- Cpi::Event.logger = stub(debug: nil)
84
- Cpi::Generator.should_not_receive(:create_event)
85
- Cpi::Event.transmit("message", "tenant_uid", "content")
27
+ describe "queue" do
28
+ it "get extracted" do
29
+ doc.queue.should == 'system_queue_name'
86
30
  end
87
31
 
88
- it "raises exception when no tenant_uid is passed in" do
89
- Cpi::Event.configure(real_config)
90
- Cpi::Generator.should_not_receive(:create_event)
91
- expect {Cpi::Event.transmit("message", nil, "content")}.to raise_error(ArgumentError)
32
+ it "is nil when not present" do
33
+ doc = Cpi::Event.new('')
34
+ doc.queue.should be_nil
92
35
  end
93
36
  end
94
-
95
- describe ".connectivity_test" do
96
- it "tells you when the connector is not enabled" do
97
- Cpi::Event.configure({enabled: false})
98
- Cpi::Event.connectivity_test.should == 'Event Connector is not enabled.'
99
- end
100
-
101
- it "tells you when the connection params are correct" do
102
- Cpi::Event.configure(real_config)
103
- Cpi::Event.connectivity_test.should == 'Event Connector can connect to AMQP server at localhost:5672'
37
+
38
+ describe "uid" do
39
+ it "get extracted" do
40
+ doc.event_uid.should == 'unique_id'
104
41
  end
105
42
 
106
- it "returns false when the connection params are incorrect" do
107
- real_config[:host] = "doesntwork"
108
- Cpi::Event.configure(real_config)
109
- Cpi::Event.connectivity_test.should == "getaddrinfo: Name or service not known"
43
+ it "is nil when not present" do
44
+ doc = Cpi::Event.new('')
45
+ doc.event_uid.should be_nil
110
46
  end
111
47
  end
112
48
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
  require 'backports'
3
- require_relative '../lib/cpi_event_connector/generator'
3
+ require_relative '../lib/cpi/generator'
4
4
  require 'active_support'
5
5
 
6
6
  describe Cpi::Generator do
@@ -21,10 +21,17 @@ describe Cpi::Generator do
21
21
  result["uid"].should_not be_nil
22
22
  end
23
23
 
24
- it "generates a different uid for each event" do
24
+ it "generates a different default uid for each event" do
25
25
  event1 = ActiveSupport::JSON.decode(Cpi::Generator.create_event(event_type, tenant_uid, content))
26
26
  event2 = ActiveSupport::JSON.decode(Cpi::Generator.create_event(event_type, tenant_uid, content))
27
27
 
28
28
  event1["uid"].should_not == event2["uid"]
29
29
  end
30
+
31
+ it "allows the uid to be specified" do
32
+ event = Cpi::Generator.create_event(event_type, tenant_uid, content, 'specified')
33
+
34
+ result = ActiveSupport::JSON.decode(event)
35
+ result["uid"].should == 'specified'
36
+ end
30
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cpi-event-connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.15
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-23 00:00:00.000000000 Z
12
+ date: 2012-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -107,6 +107,22 @@ dependencies:
107
107
  - - ~>
108
108
  - !ruby/object:Gem::Version
109
109
  version: 2.1.2
110
+ - !ruby/object:Gem::Dependency
111
+ name: nokogiri
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '1.5'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: '1.5'
110
126
  description: see summary
111
127
  email:
112
128
  - engineering@nulogy.com
@@ -119,12 +135,14 @@ files:
119
135
  - Gemfile.lock
120
136
  - Rakefile
121
137
  - cpi-event-connector.gemspec
138
+ - lib/cpi/connector.rb
139
+ - lib/cpi/event.rb
140
+ - lib/cpi/event_connector.rb
141
+ - lib/cpi/generator.rb
142
+ - lib/cpi/version.rb
122
143
  - lib/cpi_event_connector.rb
123
- - lib/cpi_event_connector/connector.rb
124
- - lib/cpi_event_connector/event.rb
125
- - lib/cpi_event_connector/generator.rb
126
- - lib/cpi_event_connector/version.rb
127
144
  - spec/connector_spec.rb
145
+ - spec/event_connector_spec.rb
128
146
  - spec/event_spec.rb
129
147
  - spec/generator_spec.rb
130
148
  - spec/integration_spec.rb
@@ -1,3 +0,0 @@
1
- module Cpi
2
- VERSION = "0.0.15"
3
- end