การตรวจสอบแอตทริบิวต์ XML โดยใช้ความมั่นใจของ Jayway

ฉันกำลังพยายามตรวจสอบว่า xmlns:ns1="http://thomas-bayer.com/blz/" ใน XML ต่อไปนี้โดยใช้ รองรับ XML

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <ns1:getBankResponse xmlns:ns1="http://thomas-bayer.com/blz/">
      <ns1:details>
        <ns1:bezeichnung>ABK-Kreditbank</ns1:bezeichnung>
        <ns1:bic>ABKBDEB1XXX</ns1:bic>
        <ns1:ort>Berlin</ns1:ort>
        <ns1:plz>10789</ns1:plz>
      </ns1:details>
    </ns1:getBankResponse>
  </soapenv:Body>
</soapenv:Envelope>

ตามที่ผมเข้าใจแล้ว

xmlns:ns1="http://thomas-bayer.com/blz/"

เป็น แอตทริบิวต์ ของ

<ns1:getBankResponse

คำนำหน้า @ ควรส่งคืนแอตทริบิวต์ที่ตรงกับต้นกำเนิดที่ตามหลัง @

สมมติว่า xmlString เท่ากับ XML ตอบกลับ SOAP ข้างต้น ค่าต่อไปนี้ทั้งหมดส่งคืนค่าว่าง:

String nameSpace1 = given(xmlString).getString("Envelope.Body.getBankResponse.@xmlns:ns1");
String nameSpace2 = given(xmlString).getString("Envelope.Body.getBankResponse.@ns1");
String nameSpace3 = given(xmlString).getString("Envelope.Body.getBankResponse.@xmlns");
List<String> nameSpace = given(xmlString).get("Envelope.Body.getBankResponse[0].@xmlns:ns1");
List<String> nameSpace = given(xmlString).get("Envelope.Body.getBankResponse[0].@ns1");
List<String> nameSpace = given(xmlString).get("Envelope.Body.getBankResponse[0].@xmlns");

ความช่วยเหลือใด ๆ ที่จะได้รับการชื่นชมอย่างมาก.

ป.ล. ฉันรู้ว่าเรื่องความมั่นใจนั้นเกี่ยวข้องกับ RESTful API เป็นหลัก แต่สามารถใช้เพื่อทดสอบ SOAP ได้


person MikeJRamsey56    schedule 29.04.2016    source แหล่งที่มา


คำตอบ (2)


ใน REST Assured เวอร์ชันที่กำลังจะมาถึง (อาจเป็น 2.9.1) คุณจะสามารถกำหนดค่า XmlPath ให้ไม่ทราบเนมสเปซได้ ซึ่งหมายความว่าคุณสามารถทำสิ่งนี้ได้:

XmlPath xmlPath = new XmlPath(xmlResponse).using(xmlPathConfig().namespaceAware(false));
assertThat(xmlPath.getString("soapenv:Envelope.soapenv:Body.ns1:getBankResponse.@xmlns:ns1"), equalTo("http://thomas-bayer.com/blz/"));

คุณสามารถทดลองใช้ได้วันนี้โดยขึ้นอยู่กับเวอร์ชัน 2.9.1-SNAPSHOT หลังจากเพิ่มที่เก็บ Maven ต่อไปนี้:

<repositories>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <snapshots />
        </repository>
</repositories>
person Johan    schedule 30.04.2016
comment
ขอบคุณ สิ่งที่ฉันกำลังมองหา - person MikeJRamsey56; 30.04.2016

แก้ไขบางส่วนด้วยวิธีนี้:

 XmlPath xmlPath = new XmlPath(xmlResponse).using(xmlPathConfig().declaredNamespace("ns1", "http://thomas-bayer.com/blz/"));

// Then

 assertThat(xmlPath.getString("Envelope.Body.getBankResponse.ns1:details.ns1:bezeichnung.text()"), equalTo("ABK-Kreditbank"));

ฉันพูดเพียงบางส่วนเพราะถึงแม้ว่ามันจะพิสูจน์ได้ว่ามีการใช้พื้นที่ชื่อ ns1 ดังนั้นจึงต้องถูกกำหนดให้ถูกต้อง แต่ก็ไม่ได้แสดงว่า:

xmlns:ns1="http://thomas-bayer.com/blz/"

เพราะการทดสอบยังคงผ่านหากฉันเปลี่ยน URI

ใครมีคำตอบที่ดีกว่านี้บ้าง?

person MikeJRamsey56    schedule 29.04.2016
comment
ไม่มีตัวเลือกที่ดีกว่าในเวอร์ชันที่วางจำหน่ายในปัจจุบัน (2.9.0) - person Johan; 30.04.2016