View
@@ -11,6 +11,54 @@ module DDL
@ddl.metadata(:name => "name", :description => "description", :author => "author", :license => "license", :version => "version", :url => "url", :timeout => "timeout")
end
describe "#symbolize_basic_input_arguments" do
before(:each) do
@ddl.action(:test, :description => "rspec")
@ddl.instance_variable_set("@current_entity", :test)
@ddl.input(:test, :prompt => "prompt", :description => "descr",
:type => :string, :optional => true, :validation => "",
:maxlength => 1, :default => "default")
end
it "should warn when there are string and symbol inputs for the same stringified key" do
@ddl.input("test", :prompt => "prompt", :description => "descr",
:type => :string, :optional => true, :validation => "",
:maxlength => 1, :default => "default")
Log.expects(:warn).with("String and Symbol versions of input test found in the DDL for rspec, ensure your DDL keys are unique.")
@ddl.symbolize_basic_input_arguments(@ddl.action_interface(:test)[:input], {:test => 1, "test" => 2})
end
it "should use the symbol given a string argument when there is not also a matching string input" do
result = @ddl.symbolize_basic_input_arguments(@ddl.action_interface(:test)[:input], {"test" => 2})
expect(result).to eq(:test => 2)
end
it "should use the string given a string argument when there is also a matching string input" do
@ddl.input("test", :prompt => "prompt", :description => "descr",
:type => :string, :optional => true, :validation => "",
:maxlength => 1, :default => "default")
Log.stubs(:warn)
result = @ddl.symbolize_basic_input_arguments(@ddl.action_interface(:test)[:input], {"test" => 2})
expect(result).to eq("test" => 2)
end
it "should not change symbols matching symbol inputs" do
result = @ddl.symbolize_basic_input_arguments(@ddl.action_interface(:test)[:input], {:test => 2})
expect(result).to eq(:test => 2)
end
it "should not change strings matching strings inputs" do
@ddl.input("string_test", :prompt => "prompt", :description => "descr",
:type => :string, :optional => true, :validation => "",
:maxlength => 1, :default => "default")
result = @ddl.symbolize_basic_input_arguments(@ddl.action_interface(:test)[:input], {"string_test" => 2})
expect(result).to eq("string_test" => 2)
end
end
describe "#input" do
it "should validate that an :optional property is set" do
expect { @ddl.input(:x, {:y => 1}) }.to raise_error("Input needs a :optional property")
@@ -45,20 +93,29 @@ module DDL
args.should == {:required => "specified"}
end
end
describe "#validate_rpc_request" do
it "should ensure the action is known" do
@ddl.action(:test, :description => "rspec")
it "should detect json primitive key names and consider them present as their symbol equivelants" do
args = {"required" => "specified"}
expect {
@ddl.validate_rpc_request(:fail, {})
}.to raise_error("Attempted to call action fail for rspec but it's not declared in the DDL")
@ddl.set_default_input_arguments(:test, args)
@ddl.validate_rpc_request(:test, {})
args.should == {"required" => "specified"}
end
it "should check all required arguments are present" do
it "should not consider the string key equiv to the symbol one when both symbol and string keys exist" do
@ddl.input("required", :prompt => "prompt", :description => "descr",
:type => :string, :optional => false, :validation => "",
:maxlength => 1, :default => "string default")
args = {"required" => "specified"}
@ddl.set_default_input_arguments(:test, args)
args.should == {"required" => "specified", :required=>"default"}
end
end
describe "#validate_rpc_request" do
before(:each) do
@ddl.action(:test, :description => "rspec")
@ddl.instance_variable_set("@current_entity", :test)
@ddl.input(:optional, :prompt => "prompt", :description => "descr",
@@ -67,7 +124,17 @@ module DDL
@ddl.input(:required, :prompt => "prompt", :description => "descr",
:type => :string, :optional => false, :validation => "",
:maxlength => 1)
end
it "should ensure the action is known" do
expect {
@ddl.validate_rpc_request(:fail, {})
}.to raise_error("Attempted to call action fail for rspec but it's not declared in the DDL")
@ddl.validate_rpc_request(:test, {:required => "f"})
end
it "should check all required arguments are present" do
@ddl.stubs(:validate_input_argument).returns(true)
expect {
@@ -78,20 +145,17 @@ module DDL
end
it "should input validate every supplied key" do
@ddl.action(:test, :description => "rspec")
@ddl.instance_variable_set("@current_entity", :test)
@ddl.input(:optional, :prompt => "prompt", :description => "descr",
:type => :string, :optional => true, :validation => "",
:maxlength => 1)
@ddl.input(:required, :prompt => "prompt", :description => "descr",
:type => :string, :optional => false, :validation => "",
:maxlength => 1)
@ddl.expects(:validate_input_argument).with(@ddl.entities[:test][:input], :required, "f")
@ddl.expects(:validate_input_argument).with(@ddl.entities[:test][:input], :optional, "f")
@ddl.validate_rpc_request(:test, {:required => "f", :optional => "f"}).should == true
end
it "should input validate string keys for symbol inputs correctly" do
@ddl.expects(:validate_input_argument).with(@ddl.entities[:test][:input], :required, "f")
@ddl.expects(:validate_input_argument).with(@ddl.entities[:test][:input], :optional, "f")
@ddl.validate_rpc_request(:test, {:required => "f", :optional => "f"}).should == true
@ddl.validate_rpc_request(:test, {"required" => "f", "optional" => "f"}).should == true
end
end
View
@@ -39,6 +39,65 @@ module RPC
@client.stubs(:ddl).returns(ddl)
end
describe "#rpc_result_from_reply" do
it "should support symbol style replies" do
raw_result = {
:senderid => "rspec.id",
:body => {
:statuscode => 1,
:statusmsg => "rspec status",
:data => {
:one => 1
}
}
}
result = @client.rpc_result_from_reply("rspec", "test", raw_result)
expect(result.agent).to eq("rspec")
expect(result.action).to eq("test")
expect(result[:sender]).to eq("rspec.id")
expect(result[:statuscode]).to eq(1)
expect(result[:statusmsg]).to eq("rspec status")
expect(result[:data]).to eq(:one => 1)
expect(result.results).to eq(
:sender => "rspec.id",
:statuscode => 1,
:statusmsg => "rspec status",
:data => {:one => 1}
)
end
it "should support string style replies" do
raw_result = {
"senderid" => "rspec.id",
"body" => {
"statuscode" => 1,
"statusmsg" => "rspec status",
"data" => {
"one" => 1,
"two" => 2
}
}
}
result = @client.rpc_result_from_reply("rspec", "test", raw_result)
expect(result.agent).to eq("rspec")
expect(result.action).to eq("test")
expect(result[:sender]).to eq("rspec.id")
expect(result[:statuscode]).to eq(1)
expect(result[:statusmsg]).to eq("rspec status")
expect(result[:data]).to eq("one" => 1, "two" => 2)
expect(result.results).to eq(
:sender => "rspec.id",
:statuscode => 1,
:statusmsg => "rspec status",
:data => {"one" => 1, "two" => 2}
)
end
end
describe "#detect_and_set_stdin_discovery" do
before(:each) do
@client = Client.new("foo", {:options => {:filter => Util.empty_filter, :config => "/nonexisting", :stdin => @stdin}})
View
@@ -20,6 +20,28 @@ module RPC
@request = Request.new(@req, @ddl)
end
describe "#compatible_key" do
it "should return the key if its a known key already" do
expect(@request.compatible_key(:foo)).to be(:foo)
end
it "should return the symbol key if the DDL defines both" do
@ddl.action("test", :description => "rspec")
@ddl.instance_variable_set("@current_entity", "test")
@ddl.input(:test, :prompt => "test", :description => "test", :type => :boolean, :optional => true)
@ddl.input("test", :prompt => "test", :description => "test", :type => :boolean, :optional => true)
expect(@request.compatible_key(:test)).to be(:test)
expect(@request.compatible_key("test")).to eq("test")
end
it "should return the stringified key if a interned version of known string data was requested" do
expect(@request.compatible_key(:string)).to eq(:string)
@req[:body][:data]["string"] = "string data"
expect(@request.compatible_key(:string)).to eq("string")
end
end
describe "#validate!" do
it "should validate the request using the supplied DDL" do
@ddl.expects(:validate_rpc_request).with("test", {:foo => "bar", :process_results => true})
@@ -60,6 +82,18 @@ module RPC
@req.delete(:callerid)
Request.new(@req, @ddl).caller.should == "unknown"
end
it "should support JSON pure inputs" do
@req[:body] = {"action" => "test",
"data" => {"foo" => "bar", "process_results" => true},
"agent" => "tester"}
request = Request.new(@req, @ddl)
expect(request.action).to eq("test")
expect(request.agent).to eq("tester")
expect(request.data).to eq("foo" => "bar", "process_results" => true)
end
end
describe "#include?" do
@@ -82,6 +116,8 @@ module RPC
it "should return correct value" do
@req[:body][:data][:process_results] = false
Request.new(@req, @ddl).should_respond?.should == false
@req[:body][:data]["process_results"] = false
Request.new(@req, @ddl).should_respond?.should == false
end
end
View
@@ -27,6 +27,34 @@ module RPC
end
end
describe "#convert_data_based_on_ddl" do
it "should convert string data to symbol data based on the DDL" do
ddl = DDL.new("rspec", :agent, false)
ddl.metadata(:name => "name", :description => "description", :author => "author", :license => "license", :version => "version", :url => "url", :timeout => "timeout")
ddl.action("test", :description => "rspec")
ddl.instance_variable_set("@current_entity", "test")
ddl.output(:one, :description => "rspec one", :display_as => "One")
DDL.expects(:new).with("rspec").returns(ddl)
raw_result = {
"sender" => "rspec.id",
"statuscode" => 1,
"statusmsg" => "rspec status",
"data" => {
"one" => 1,
"two" => 2
}
}
result = Result.new("rspec", "test", raw_result)
expect(result.data).to eq(
:one => 1,
"two" => 2
)
end
end
describe "#[]" do
it "should access the results hash and return correct data" do
@result[:foo].should == "bar"
@@ -71,17 +99,17 @@ module RPC
describe "#<=>" do
it "should implement the Combined Comparison operator based on sender name" do
result_a = Result.new("tester",
"test",
{ :statuscode => 0,
:statusmsg => "OK",
:sender => "a_rspec",
result_a = Result.new("tester",
"test",
{ :statuscode => 0,
:statusmsg => "OK",
:sender => "a_rspec",
:data => {}})
result_b = Result.new("tester",
"test",
{ :statuscode => 0,
:statusmsg => "OK",
:sender => "b_rspec",
result_b = Result.new("tester",
"test",
{ :statuscode => 0,
:statusmsg => "OK",
:sender => "b_rspec",
:data => {}})
(result_a <=> result_b).should == -1