Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Lookups, refactoring, specs

  • Loading branch information...
commit 661b647e2b014c3ba390c00a63586ee39ed733ca 1 parent 237330d
Anton authored
11 lib/wsdl-reader/binding.rb
View
@@ -23,6 +23,13 @@ def all_operations
self.map { |_, binding| binding.operations.values }.flatten
end
+ def operations(binding=nil)
+ bindings = binding.nil? ? self : ({ a: self[binding] })
+ bindings.inject({ }) do |hash, (_, b)|
+ hash.merge b.operations
+ end
+ end
+
def operation?(operation_name)
any? { |_, binding| binding.operation? operation_name }
end
@@ -32,6 +39,7 @@ class Binding
attr_reader :operations
attr_reader :name
attr_reader :type
+ attr_reader :type_nns
attr_reader :style
attr_reader :transport
@@ -39,6 +47,7 @@ def initialize(element)
@operations = Operations.new
@name = element.attributes['name']
@type = element.attributes['type'] # because of Object#type
+ @type_nns = @type.split(':').last rescue @type
@style = nil
@transport = nil
@@ -86,7 +95,7 @@ def camelize_operation(name)
return name_string.camelize :lower if name_string.underscore == name_string
end
end
-
+
def store_operation_name(element, operation)
operation.attributes.each do |name, value|
case name
24 lib/wsdl-reader/error.rb
View
@@ -3,6 +3,30 @@ module Reader
class ParserError < StandardError
end
+ class LookupError < StandardError
+ end
+
+ class ManyOperationsFoundError < LookupError
+ def initialize(type, element_name)
+ @type, @element_name = type, element_name
+ end
+
+
+ def message
+ "More than one operations found for element: [#@element_name]"
+ end
+ end
+
+ class OperationNotFoundError < LookupError
+ def initialize(type, element_name)
+ @type, @element_name = type, element_name
+ end
+
+ def message
+ "No operation matches for element: [#@element_name]"
+ end
+ end
+
class FileOpenError < ParserError
end
end
33 lib/wsdl-reader/message.rb
View
@@ -1,6 +1,32 @@
module WSDL
module Reader
class Messages < Hash
+
+ def lookup_operations_by_element(type, element_name, port_types)
+ messages = lookup_messages_by_element(element_name)
+ messages.map do |message|
+ port_types.lookup_operations_by_message(type, message)
+ end.flatten
+ end
+
+ def lookup_operation_by_element! (type, element_name, port_types)
+ messages = lookup_operations_by_element type, element_name, port_types
+ case messages.size
+ when 1
+ messages.first
+ when 0
+ raise OperationNotFoundError.new type, element_name
+ else
+ raise ManyOperationsFoundError.new type, element_name
+ end
+ end
+
+ def lookup_messages_by_element(element_name)
+ values.select do |message|
+ message.parts.values.find { |part| part[:element].split(':').last == element_name }
+ end
+ end
+
end
class Message
@@ -9,14 +35,13 @@ class Message
def initialize(element)
@parts = Hash.new
- @name = element.attributes['name']
+ @name = element.attributes['name']
process_all_parts element
end
def element
- warn "More than one parts!" if parts.size > 1
- parts.map { |name, hash| hash[:element] }.first
+ parts.map { |_, hash| hash[:element] }.first
end
protected
@@ -41,7 +66,7 @@ def store_part_attributes(part)
current_part[:name] = value
when 'element'
current_part[:element] = value
- current_part[:mode] = :element
+ current_part[:mode] = :element
when 'type'
current_part[:type] = value
current_part[:mode] = :type
16 lib/wsdl-reader/operation.rb
View
@@ -18,6 +18,10 @@ def initialize(element, binding)
parse! element
end
+ def method_name
+ @name.underscore
+ end
+
def message
@name
end
@@ -31,11 +35,11 @@ def lookup_element(port_types, messages)
def parse!(operation_element)
operation_element.find_all { |e| e.class == REXML::Element }.each do |action_element|
- store_action(action_element, operation_element)
+ store_action(action_element)
end
end
- def store_action(action_element, operation_element)
+ def store_action(action_element)
case action_element.name
when "operation" # soap:operation
action_element.attributes.each do |name, value|
@@ -47,17 +51,17 @@ def store_action(action_element, operation_element)
end
end
when "input"
- @input = fill_action(action_element, operation_element)
+ @input = fill_action(action_element)
when "output"
- @output = fill_action(action_element, operation_element)
+ @output = fill_action(action_element)
when "fault"
- @fault = fill_action(action_element, operation_element)
+ @fault = fill_action(action_element)
end
end
- def fill_action(action_element, operation_element)
+ def fill_action(action_element)
filling_action = { }
filling_action[:name] = action_element.attributes['name']
7 lib/wsdl-reader/parser.rb
View
@@ -15,7 +15,8 @@ class Parser
attr_reader :prefixes, :target_namespace
attr_reader :document, :uri
- delegate :get_operations, :operation?, to: :bindings
+ delegate :operations, :operation?, to: :bindings
+ delegate :lookup_operation_by_element!, to: :messages
def initialize(uri)
@uri = uri
@@ -34,10 +35,6 @@ def initialize(uri)
process_content @document.root.children
end
- def operations
- bindings.all_operations
- end
-
private
def get_wsdl_source
44 lib/wsdl-reader/port_type.rb
View
@@ -1,12 +1,18 @@
module WSDL
module Reader
class PortTypes < Hash
- def lookup_operation_message(type, operation, messages)
+ def lookup_operation_message(type, operation, messages) # TODO: lookup_message_by_operation
each do |_, port_type|
message = port_type.lookup_operation_message type, operation, messages
return message if message
end
end
+
+ def lookup_operations_by_message(type, message)
+ inject([]) do |array, (_, port_type)|
+ array + port_type.lookup_operations_by_message(type, message)
+ end
+ end
end
class PortType
@@ -15,13 +21,29 @@ class PortType
def initialize(element)
@operations = Hash.new
- @name = element.attributes['name']
+ @name = element.attributes['name']
process_all_operations(element)
end
def lookup_operation_message(type, operation, messages)
- messages[@operations[operation.name][type][:message].split(':').last]
+ operation_name = operation.is_a?(Operation) ? operation.name : operation.to_s
+ messages[@operations[operation_name][type][:message].split(':').last]
+ end
+
+ def lookup_operations_by_message(type, message)
+ ops = @operations.values.select do |operation|
+ operation_message? type, operation, message
+ end
+ ops.map do |operation_hash|
+ operation_hash[:name]
+ end
+ end
+
+ def operation_message?(type, operation, message)
+ message_name = message.is_a?(Message) ? message.name : message.to_s
+ operation[type][:message] == message_name ||
+ operation[type][:message].split(':').last == message_name
end
protected
@@ -89,17 +111,17 @@ def process_all_operations(element)
operation.find_all { |e| e.class == REXML::Element }.each do |action|
case action.name
- when "input"
- store_input(action, element, operation)
+ when "input"
+ store_input(action, element, operation)
- when "output"
- store_output(action, element, operation)
+ when "output"
+ store_output(action, element, operation)
- when "fault"
- store_fault(action, element, operation)
+ when "fault"
+ store_fault(action, element, operation)
- else
- warn "Ignoring element '#{action.name}' in operation '#{operation.attributes['name']}' for portType '#{element.attributes['name']}'"
+ else
+ warn "Ignoring element '#{action.name}' in operation '#{operation.attributes['name']}' for portType '#{element.attributes['name']}'"
end
end
else
18 spec/binding_spec.rb
View
@@ -12,25 +12,25 @@
it "#get_operations with binding UserServicePortBinding" do
operations = subject.get_operations "UserServicePortBinding"
operations.should have(2).operation
- operations.should eq %w{getFirstName getLastName}
+ operations.should eq %w{getFirstNameOperation getLastNameOperation}
end
it "#get_binding_for_operation_name with binding = UserServicePortBinding and operation = getFirstName" do
- bindings_array = subject.get_binding_for_operation_name "UserServicePortBinding", "getFirstName"
+ bindings_array = subject.get_binding_for_operation_name "UserServicePortBinding", "getFirstNameOperation"
bindings_array.should have(1).binding
bindings_array.first.should be_a WSDL::Reader::Binding
bindings_array.first.name.should eq "UserServicePortBinding"
- bindings_array.first.operations.keys.should include "getFirstName"
+ bindings_array.first.operations.keys.should include "getFirstNameOperation"
end
it "#get_binding_for_operation_name operation = getFirstName but without binding" do
- bindings_array = subject.get_binding_for_operation_name "getFirstName"
+ bindings_array = subject.get_binding_for_operation_name "getFirstNameOperation"
- bindings_array.should have(1).binding
+ bindings_array.should have(2).binding
bindings_array.first.should be_a WSDL::Reader::Binding
bindings_array.first.name.should eq "UserServicePortBinding"
- bindings_array.first.operations.keys.should include "getFirstName"
+ bindings_array.first.operations.keys.should include "getFirstNameOperation"
end
it "#all_operations should eql to #get_operations(nil)" do
@@ -38,7 +38,7 @@
end
it "#operation? should return true if any binding include is operation_name" do
- subject.operation?("getFirstName").should be_true
+ subject.operation?("getFirstNameOperation").should be_true
end
it "#operation? should return false if all bindings dont include is operation_name" do
@@ -49,7 +49,7 @@
context "WSDL::Reader::Binding" do
def operations
- %w{getFirstName getLastName}
+ %w{getFirstNameOperation getLastNameOperation}
end
let(:parser) { WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl') }
@@ -64,7 +64,7 @@ def operations
its(:transport) { should eq "http://schemas.xmlsoap.org/soap/http" }
it "#operation? should find for operation name" do
- subject.operation?("getFirstName").should be_true
+ subject.operation?("getFirstNameOperation").should be_true
end
it "#lookup_port_type should lookup port_type from given port_types" do
35 spec/fixtures/UserService.wsdl
View
@@ -10,31 +10,31 @@
</xs:schema>
</types>
- <message name="getFirstName">
+ <message name="getFirstNameRequest">
<part name="parameters" element="tns:GetFirstName"/>
</message>
- <message name="getLastName">
+ <message name="getLastNameRequest">
<part name="parameters" element="tns:GetLastName"/>
</message>
- <message name="UserNameResponse">
+ <message name="userNameResponse">
<part name="parameters" element="xs:string"/>
</message>
<portType name="UserService">
- <operation name="getFirstName">
- <input message="tns:getFirstName"/>
- <output message="tns:UserNameResponse"/>
+ <operation name="getFirstNameOperation">
+ <input message="tns:getFirstNameRequest"/>
+ <output message="tns:userNameResponse"/>
</operation>
- <operation name="getLastName">
- <input message="tns:getLastName"/>
- <output message="tns:UserNameResponse"/>
+ <operation name="getLastNameOperation">
+ <input message="tns:getLastNameRequest"/>
+ <output message="tns:userNameResponse"/>
</operation>
</portType>
<binding name="UserServicePortBinding" type="tns:UserService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
- <operation name="getFirstName">
+ <operation name="getFirstNameOperation">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
@@ -43,7 +43,20 @@
<soap:body use="literal"/>
</output>
</operation>
- <operation name="getLastName">
+ <operation name="getLastNameOperation">
+ <soap:operation soapAction=""/>
+ <input>
+ <soap:body use="literal"/>
+ </input>
+ <output>
+ <soap:body use="literal"/>
+ </output>
+ </operation>
+ </binding>
+
+ <binding name="UserServicePortBinding2" type="tns:UserService">
+ <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
+ <operation name="getLastNameOperation">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
29 spec/message_spec.rb
View
@@ -3,20 +3,39 @@
describe "WSDL Messages" do
context "WSDL::Reader::Messages" do
- it "child of Hash" do
- WSDL::Reader::Messages.superclass.should be Hash
+ let(:parser) { WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl') }
+ subject { parser.messages }
+
+ it "should be kind of WSDL::Reader::Messages" do
+ subject.should be_kind_of WSDL::Reader::Messages
+ end
+
+ it "should be kind of Hash" do
+ subject.should be_kind_of Hash
+ end
+
+ it "#lookup_messages_by_element should return operation name by given element name" do
+ messages = subject.lookup_messages_by_element("GetFirstName")
+ messages.count.should eq 1
+ messages.first.name.should eq "getFirstNameRequest"
+ end
+
+ it "#lookup_operations_by_element should return operation name by given element name" do
+ operations = subject.lookup_operations_by_element(:input, "GetFirstName", parser.port_types)
+ operations.count.should eq 1
+ operations.first.should eq "getFirstNameOperation"
end
+
end
context "WSDL::Reader::Message" do
subject do
parser = WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl')
- element = parser.document.root.children.find { |e| e.class == REXML::Element && e.name == 'message' }
- WSDL::Reader::Message.new(element)
+ parser.messages.first.last
end
its(:parts) { should eq "parameters" => { name: "parameters", element: "tns:GetFirstName", mode: :element } }
- its(:name) { should eq "getFirstName" }
+ its(:name) { should eq "getFirstNameRequest" }
end
end
12 spec/operation_spec.rb
View
@@ -2,20 +2,24 @@
describe "WSDL Operation" do
let(:parser) { WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl') }
- let(:binding) { parser.bindings.values.first }
+ let(:binding) { parser.bindings['UserServicePortBinding'] }
let(:messages) { parser.messages }
- subject { binding.operations.values.first }
+ subject { binding.operations['getFirstNameOperation'] }
it "should be WSDL::Reader::Operation instance" do
should be_a WSDL::Reader::Operation
end
its :name do
- should eql "getFirstName"
+ should eql "getFirstNameOperation"
+ end
+
+ its :method_name do
+ should eql "get_first_name_operation"
end
its :binding do
- should be binding
+ should be binding
end
its :soap_action do
37 spec/parser_spec.rb
View
@@ -11,51 +11,56 @@
subject { parser }
context "attributes types" do
- it { should be_a WSDL::Reader::Parser }
+ it { should be_kind_of WSDL::Reader::Parser }
- its(:document) { should be_a REXML::Document }
+ its(:document) { should be_kind_of REXML::Document }
- its(:prefixes) { should be_a Hash }
+ its(:prefixes) { should be_kind_of Hash }
- its(:target_namespace) { should be_a String }
+ its(:target_namespace) { should be_kind_of String }
- its(:types) { should be_a SOAP::XSD }
+ its(:types) { should be_kind_of SOAP::XSD }
- its(:messages) { should be_a WSDL::Reader::Messages }
+ its(:messages) { should be_kind_of WSDL::Reader::Messages }
- its(:port_types) { should be_a WSDL::Reader::PortTypes }
+ its(:port_types) { should be_kind_of WSDL::Reader::PortTypes }
- its(:bindings) { should be_a WSDL::Reader::Bindings }
+ its(:bindings) { should be_kind_of WSDL::Reader::Bindings }
- its(:services) { should be_a SOAP::WSDL::Services }
+ its(:services) { should be_kind_of SOAP::WSDL::Services }
end
context "attributes values" do
#its(:document) { clean_xml(subject.document.to_s).should eql clean_xml(File.read('spec/fixtures/UserService.wsdl')) }
its(:prefixes) do
- should eq "soap" => "http://schemas.xmlsoap.org/wsdl/soap/",
- "tns" => "http://example.com/UserService/",
- "xs" => "http://www.w3.org/2001/XMLSchema",
- "__default__" => "http://schemas.xmlsoap.org/wsdl/"
+ should eq "soap" => "http://schemas.xmlsoap.org/wsdl/soap/",
+ "tns" => "http://example.com/UserService/",
+ "xs" => "http://www.w3.org/2001/XMLSchema",
+ "__default__" => "http://schemas.xmlsoap.org/wsdl/"
end
its(:target_namespace) { should eq "http://example.com/UserService/" }
its(:types) { should eq SOAP::XSD.new }
- its('messages.keys') { should eq %w{getFirstName getLastName UserNameResponse} }
+ its('messages.keys') { should eq %w{getFirstNameRequest getLastNameRequest userNameResponse} }
its('port_types.keys') { should eq %w{UserService} }
- its('bindings.keys') { should eq %w{UserServicePortBinding} }
+ its('bindings.keys') { should eq %w{UserServicePortBinding UserServicePortBinding2} }
its('services.keys') { should eq %w{UserService} }
end
context "methods" do
- its(:get_operations) { should eql parser.bindings.get_operations }
+ its(:operations) { should eql parser.bindings.operations }
+
+ it "should delegate lookup_operation_by_element! to messages" do
+ subject.messages.should_receive(:lookup_operation_by_element!).with(1,2,3)
+ subject.lookup_operation_by_element! 1,2,3
+ end
end
end
29 spec/port_type_spec.rb
View
@@ -3,19 +3,19 @@
describe "WSDL PortType" do
def user_service_port_type
- {"getFirstName" => { :name => "getFirstName",
- :input => { :message => "tns:getFirstName" },
- :output => { :message => "tns:UserNameResponse" } },
+ { "getFirstNameOperation" => { :name => "getFirstNameOperation",
+ :input => { :message => "tns:getFirstNameRequest" },
+ :output => { :message => "tns:userNameResponse" } },
- "getLastName" => { :name => "getLastName",
- :input => { :message => "tns:getLastName" },
- :output => { :message => "tns:UserNameResponse" } }
+ "getLastNameOperation" => { :name => "getLastNameOperation",
+ :input => { :message => "tns:getLastNameRequest" },
+ :output => { :message => "tns:userNameResponse" } }
}
end
- let(:parser) { WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl') }
+ let(:parser) { WSDL::Reader::Parser.new('spec/fixtures/UserService.wsdl') }
let(:messages) { parser.messages }
- let(:operation) { parser.bindings.values.first.operations.values.first }
+ let(:operation) { parser.bindings.operations['getFirstNameOperation'] }
context "WSDL::Reader::PortTypes" do
it "child of Hash" do
@@ -25,7 +25,7 @@ def user_service_port_type
it "should lookup operation message in all port types" do
message = parser.port_types.lookup_operation_message :input, operation, messages
message.should be_a WSDL::Reader::Message
- message.name.should eq "getFirstName"
+ message.name.should eq "getFirstNameRequest"
end
end
@@ -38,10 +38,19 @@ def user_service_port_type
its(:operations) { should eq user_service_port_type }
its(:name) { should eq "UserService" }
+ it "#operation_message? should check message in operation" do
+ operation = { input: { message: 'getFirstNameRequest' } }
+ subject.operation_message?(:input, operation, messages.values.first).should be_true
+
+ operation = { input: { message: 'getLastNameRequest' } }
+ subject.operation_message?(:input, operation, messages.values.first).should be_false
+ end
+
it "#lookup_operation_message should lookup message in messages for given type and operation" do
message = subject.lookup_operation_message :input, operation, messages
message.should be_a WSDL::Reader::Message
- message.name.should eq "getFirstName"
+ message.name.should eq "getFirstNameRequest"
end
+
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.