use xml_name when parsing an entity
This commit is contained in:
parent
7f731e9af0
commit
42e994e01e
11 changed files with 89 additions and 34 deletions
|
|
@ -91,7 +91,7 @@ module DiasporaFederation
|
||||||
type = prop_def[:type]
|
type = prop_def[:type]
|
||||||
|
|
||||||
if type == String
|
if type == String
|
||||||
data[name] = parse_string_from_node(name, node)
|
data[name] = parse_string_from_node(prop_def[:xml_name], node)
|
||||||
elsif type.instance_of?(Array)
|
elsif type.instance_of?(Array)
|
||||||
data[name] = parse_array_from_node(type, node)
|
data[name] = parse_array_from_node(type, node)
|
||||||
elsif type.ancestors.include?(Entity)
|
elsif type.ancestors.include?(Entity)
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,6 @@ module DiasporaFederation
|
||||||
last_name: person.last_name
|
last_name: person.last_name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let(:klass) { Discovery::HCard }
|
|
||||||
|
|
||||||
let(:html) {
|
let(:html) {
|
||||||
<<-HTML
|
<<-HTML
|
||||||
|
|
@ -109,9 +108,7 @@ module DiasporaFederation
|
||||||
HTML
|
HTML
|
||||||
}
|
}
|
||||||
|
|
||||||
it_behaves_like "an Entity subclass" do
|
it_behaves_like "an Entity subclass"
|
||||||
let(:xml) { nil } # disable to_xml test
|
|
||||||
end
|
|
||||||
|
|
||||||
context "generation" do
|
context "generation" do
|
||||||
it "creates an instance from a data hash" do
|
it "creates an instance from a data hash" do
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@ module DiasporaFederation
|
||||||
public_key: person.serialized_public_key
|
public_key: person.serialized_public_key
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let(:klass) { Discovery::WebFinger }
|
|
||||||
|
|
||||||
let(:xml) {
|
let(:xml) {
|
||||||
<<-XML
|
<<-XML
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,11 @@ module DiasporaFederation
|
||||||
<lat>#{data[:lat]}</lat>
|
<lat>#{data[:lat]}</lat>
|
||||||
<lng>#{data[:lng]}</lng>
|
<lng>#{data[:lng]}</lng>
|
||||||
</location>
|
</location>
|
||||||
XML
|
XML
|
||||||
}
|
}
|
||||||
|
|
||||||
it_behaves_like "an Entity subclass" do
|
it_behaves_like "an Entity subclass"
|
||||||
let(:klass) { Entities::Location }
|
|
||||||
end
|
it_behaves_like "an XML Entity"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
module DiasporaFederation
|
module DiasporaFederation
|
||||||
describe Entities::Person do
|
describe Entities::Person do
|
||||||
let(:data) { FactoryGirl.attributes_for(:person_entity) }
|
let(:data) { FactoryGirl.attributes_for(:person_entity) }
|
||||||
let(:klass) { Entities::Person }
|
|
||||||
|
|
||||||
let(:xml) {
|
let(:xml) {
|
||||||
<<-XML
|
<<-XML
|
||||||
|
|
@ -30,5 +29,7 @@ XML
|
||||||
}
|
}
|
||||||
|
|
||||||
it_behaves_like "an Entity subclass"
|
it_behaves_like "an Entity subclass"
|
||||||
|
|
||||||
|
it_behaves_like "an XML Entity"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,11 @@ module DiasporaFederation
|
||||||
<height>#{data[:height]}</height>
|
<height>#{data[:height]}</height>
|
||||||
<width>#{data[:width]}</width>
|
<width>#{data[:width]}</width>
|
||||||
</photo>
|
</photo>
|
||||||
XML
|
XML
|
||||||
}
|
}
|
||||||
|
|
||||||
it_behaves_like "an Entity subclass" do
|
it_behaves_like "an Entity subclass"
|
||||||
let(:klass) { Entities::Photo }
|
|
||||||
end
|
it_behaves_like "an XML Entity"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
module DiasporaFederation
|
module DiasporaFederation
|
||||||
describe Entities::Profile do
|
describe Entities::Profile do
|
||||||
let(:data) { FactoryGirl.attributes_for(:profile_entity) }
|
let(:data) { FactoryGirl.attributes_for(:profile_entity) }
|
||||||
let(:klass) { Entities::Profile }
|
|
||||||
|
|
||||||
let(:xml) {
|
let(:xml) {
|
||||||
<<-XML
|
<<-XML
|
||||||
|
|
@ -24,5 +23,7 @@ XML
|
||||||
}
|
}
|
||||||
|
|
||||||
it_behaves_like "an Entity subclass"
|
it_behaves_like "an Entity subclass"
|
||||||
|
|
||||||
|
it_behaves_like "an XML Entity"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -11,11 +11,11 @@ module DiasporaFederation
|
||||||
<sender_handle>alice@somepod.org</sender_handle>
|
<sender_handle>alice@somepod.org</sender_handle>
|
||||||
<recipient_handle>bob@otherpod.net</recipient_handle>
|
<recipient_handle>bob@otherpod.net</recipient_handle>
|
||||||
</request>
|
</request>
|
||||||
XML
|
XML
|
||||||
}
|
}
|
||||||
|
|
||||||
it_behaves_like "an Entity subclass" do
|
it_behaves_like "an Entity subclass"
|
||||||
let(:klass) { Entities::Request }
|
|
||||||
end
|
it_behaves_like "an XML Entity"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -58,8 +58,8 @@ module DiasporaFederation
|
||||||
XML
|
XML
|
||||||
}
|
}
|
||||||
|
|
||||||
it_behaves_like "an Entity subclass" do
|
it_behaves_like "an Entity subclass"
|
||||||
let(:klass) { Entities::StatusMessage }
|
|
||||||
end
|
it_behaves_like "an XML Entity"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ module DiasporaFederation
|
||||||
end
|
end
|
||||||
|
|
||||||
it "produces the expected XML" do
|
it "produces the expected XML" do
|
||||||
xml_str = <<-XML.strip
|
xml = <<-XML.strip
|
||||||
<XML>
|
<XML>
|
||||||
<post>
|
<post>
|
||||||
<test_entity>
|
<test_entity>
|
||||||
|
|
@ -44,7 +44,7 @@ module DiasporaFederation
|
||||||
</post>
|
</post>
|
||||||
</XML>
|
</XML>
|
||||||
XML
|
XML
|
||||||
expect(subject.to_xml).to eq(xml_str)
|
expect(subject.to_xml).to eq(xml)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -103,6 +103,24 @@ XML
|
||||||
expect(subject).to be_an_instance_of Entities::TestEntity
|
expect(subject).to be_an_instance_of Entities::TestEntity
|
||||||
expect(subject.test).to eq("asdf")
|
expect(subject.test).to eq("asdf")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "uses xml_name for parsing" do
|
||||||
|
xml = <<-XML.strip
|
||||||
|
<XML>
|
||||||
|
<post>
|
||||||
|
<test_entity_with_xml_name>
|
||||||
|
<test>asdf</test>
|
||||||
|
<asdf>qwer</asdf>
|
||||||
|
</test_entity>
|
||||||
|
</post>
|
||||||
|
</XML>
|
||||||
|
XML
|
||||||
|
entity = Salmon::XmlPayload.unpack(Nokogiri::XML::Document.parse(xml).root)
|
||||||
|
|
||||||
|
expect(entity).to be_an_instance_of Entities::TestEntityWithXmlName
|
||||||
|
expect(entity.test).to eq("asdf")
|
||||||
|
expect(entity.qwer).to eq("qwer")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "nested entities" do
|
context "nested entities" do
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,22 @@
|
||||||
shared_examples "an Entity subclass" do
|
shared_examples "an Entity subclass" do
|
||||||
it "should be an Entity" do
|
it "should be an Entity" do
|
||||||
expect(klass).to be < DiasporaFederation::Entity
|
expect(described_class).to be < DiasporaFederation::Entity
|
||||||
end
|
end
|
||||||
|
|
||||||
it "has its properties set" do
|
it "has its properties set" do
|
||||||
expect(klass.class_prop_names).to include(*data.keys)
|
expect(described_class.class_prop_names).to include(*data.keys)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "behaviour" do
|
context "behaviour" do
|
||||||
let(:instance) { klass.new(data) }
|
let(:instance) { described_class.new(data) }
|
||||||
|
|
||||||
describe "initialize" do
|
describe "initialize" do
|
||||||
it "must not create blank instances" do
|
it "must not create blank instances" do
|
||||||
expect { klass.new({}) }.to raise_error ArgumentError
|
expect { described_class.new({}) }.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if nil was given" do
|
it "fails if nil was given" do
|
||||||
expect { klass.new(nil) }.to raise_error ArgumentError, "expected a Hash"
|
expect { described_class.new(nil) }.to raise_error ArgumentError, "expected a Hash"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should be frozen" do
|
it "should be frozen" do
|
||||||
|
|
@ -29,11 +29,50 @@ shared_examples "an Entity subclass" do
|
||||||
expect(instance.to_h).to eq(data)
|
expect(instance.to_h).to eq(data)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "#to_xml" do
|
shared_examples "an XML Entity" do
|
||||||
it "produces correct XML" do
|
let(:instance) { described_class.new(data) }
|
||||||
expect(instance.to_xml.to_s.strip).to eq(xml.strip) unless xml.nil?
|
|
||||||
end
|
describe "#to_xml" do
|
||||||
|
it "produces correct XML" do
|
||||||
|
expect(instance.to_xml.to_s.strip).to eq(xml.strip)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "parsing" do
|
||||||
|
it "reads its own output" do
|
||||||
|
packed_xml = DiasporaFederation::Salmon::XmlPayload.pack(instance).to_s
|
||||||
|
xml_root = Nokogiri::XML::Document.parse(packed_xml).root
|
||||||
|
parsed_instance = DiasporaFederation::Salmon::XmlPayload.unpack(xml_root)
|
||||||
|
|
||||||
|
check_entity(instance, parsed_instance)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_entity(entity, parsed_entity)
|
||||||
|
entity.class.class_props.each do |prop_def|
|
||||||
|
name = prop_def[:name]
|
||||||
|
type = prop_def[:type]
|
||||||
|
|
||||||
|
value = entity.send(name)
|
||||||
|
parsed_value = parsed_entity.send(name)
|
||||||
|
|
||||||
|
validate_values(parsed_value, type, value)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def validate_values(parsed_value, type, value)
|
||||||
|
if value.nil?
|
||||||
|
expect(parsed_value).to be_nil
|
||||||
|
elsif type == String
|
||||||
|
expect(parsed_value).to eq(value.to_s)
|
||||||
|
elsif type.instance_of?(Array)
|
||||||
|
parsed_entities = parsed_value
|
||||||
|
value.each_with_index { |entity, index| check_entity(entity, parsed_entities[index]) }
|
||||||
|
elsif type.ancestors.include?(DiasporaFederation::Entity)
|
||||||
|
check_entity(value, parsed_value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue