summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-05-06 15:22:43 -0700
committerJames Turnbull <james@lovedthanlost.net>2009-06-06 19:57:58 +1000
commitf059c518411f2f12bbe9108094b12f9bfc13e080 (patch)
tree8c06764c17a6ebe6c8b659201d5b11e3870ea971
parent7f322b315a7890f1b3cee035d023f09814cc6075 (diff)
downloadpuppet-f059c518411f2f12bbe9108094b12f9bfc13e080.tar.gz
puppet-f059c518411f2f12bbe9108094b12f9bfc13e080.tar.xz
puppet-f059c518411f2f12bbe9108094b12f9bfc13e080.zip
Adding JSON support to Puppet::Relationship
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/relationship.rb33
-rwxr-xr-xspec/unit/relationship.rb101
2 files changed, 133 insertions, 1 deletions
diff --git a/lib/puppet/relationship.rb b/lib/puppet/relationship.rb
index 97c84ac0e..4c44adba7 100644
--- a/lib/puppet/relationship.rb
+++ b/lib/puppet/relationship.rb
@@ -14,6 +14,21 @@ class Puppet::Relationship
attr_reader :event
+ def self.from_json(json)
+ source = json["source"]
+ target = json["target"]
+
+ args = {}
+ if event = json["event"]
+ args[:event] = event
+ end
+ if callback = json["callback"]
+ args[:callback] = callback
+ end
+
+ new(source, target, args)
+ end
+
def event=(event)
if event != :NONE and ! callback
raise ArgumentError, "You must pass a callback for non-NONE events"
@@ -55,8 +70,24 @@ class Puppet::Relationship
"%s => %s" % [source, target]
end
+ def to_json(*args)
+ data = {
+ 'source' => source.to_s,
+ 'target' => target.to_s
+ }
+
+ ["event", "callback"].each do |attr|
+ next unless value = send(attr)
+ data[attr] = value
+ end
+
+ {
+ 'json_class' => self.class.to_s,
+ 'data' => data
+ }.to_json(*args)
+ end
+
def to_s
ref
end
end
-
diff --git a/spec/unit/relationship.rb b/spec/unit/relationship.rb
index 4d1b75856..6aa11ad9f 100755
--- a/spec/unit/relationship.rb
+++ b/spec/unit/relationship.rb
@@ -144,3 +144,104 @@ describe Puppet::Relationship, " when matching edges with a non-standard event"
@edge.should be_match(:random)
end
end
+
+describe Puppet::Relationship, "when converting to json" do
+ confine "Missing 'json' library" => Puppet.features.json?
+
+ before do
+ @edge = Puppet::Relationship.new(:a, :b, :event => :random, :callback => :whatever)
+ end
+
+ def json_output_should
+ @edge.class.expects(:json_create).with { |hash| yield hash }
+ end
+
+ # LAK:NOTE For all of these tests, we convert back to the edge so we can
+ # trap the actual data structure then.
+ it "should set the 'json_class' to Puppet::Relationship" do
+ json_output_should { |hash| hash['json_class'] == "Puppet::Relationship" }
+
+ JSON.parse @edge.to_json
+ end
+
+ it "should store the stringified source as the source in the data" do
+ json_output_should { |hash| hash['data']['source'] == "a" }
+
+ JSON.parse @edge.to_json
+ end
+
+ it "should store the stringified target as the target in the data" do
+ json_output_should { |hash| hash['data']['target'] == "b" }
+
+ JSON.parse @edge.to_json
+ end
+
+ it "should store the jsonified event as the event in the data" do
+ @edge.event = :random
+ json_output_should { |hash| hash['data']['event'] == "random" }
+
+ JSON.parse @edge.to_json
+ end
+
+ it "should not store an event when none is set" do
+ @edge.event = nil
+ json_output_should { |hash| hash['data']['event'].nil? }
+
+ JSON.parse @edge.to_json
+ end
+
+ it "should store the jsonified callback as the callback in the data" do
+ @edge.callback = "whatever"
+ json_output_should { |hash| hash['data']['callback'] == "whatever" }
+
+ JSON.parse @edge.to_json
+ end
+
+ it "should not store a callback when none is set in the edge" do
+ @edge.callback = nil
+ json_output_should { |hash| hash['data']['callback'].nil? }
+
+ JSON.parse @edge.to_json
+ end
+end
+
+describe Puppet::Relationship, "when converting from json" do
+ confine "Missing 'json' library" => Puppet.features.json?
+
+ before do
+ @event = "random"
+ @callback = "whatever"
+ @data = {
+ "source" => "mysource",
+ "target" => "mytarget",
+ "event" => @event,
+ "callback" => @callback
+ }
+ @json = {
+ "json_class" => "Puppet::Relationship",
+ "data" => @data
+ }
+ end
+
+ def json_result_should
+ Puppet::Relationship.expects(:new).with { |*args| yield args }
+ end
+
+ # LAK:NOTE For all of these tests, we convert back to the edge so we can
+ # trap the actual data structure then.
+ it "should pass the source in as the first argument" do
+ Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget").source.should == "mysource"
+ end
+
+ it "should pass the target in as the second argument" do
+ Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget").target.should == "mytarget"
+ end
+
+ it "should pass the event as an argument if it's provided" do
+ Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget", "event" => "myevent", "callback" => "eh").event.should == "myevent"
+ end
+
+ it "should pass the callback as an argument if it's provided" do
+ Puppet::Relationship.from_json("source" => "mysource", "target" => "mytarget", "callback" => "mycallback").callback.should == "mycallback"
+ end
+end