如何在RESTasured中使用XMLpath测试元数据

时间:2016-04-26 06:06:42

标签: xml odata metadata rest-assured

我想知道如何使用RESTassured使用XMLpath测试元数据。我正在尝试测试OData服务并读取元数据中的每个细节。我可以阅读Json的回复。但是,无法找出一种清晰的方式来阅读元数据。另外,我想知道什么是元数据中的节点。

1 个答案:

答案 0 :(得分:1)

以下是用于解析简单OData XML文档的黄瓜确定步骤。

package steps;

import java.util.List;

import com.jayway.restassured.path.xml.XmlPath;
import com.jayway.restassured.path.xml.element.Node;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;

import static com.jayway.restassured.path.xml.XmlPath.*;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

public class ODataSteps {
    String sampleXml = "";

    @Given("^I have a simple service document$")
    public void i_have_a_simple_service_document() {
        sampleXml = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" +
            "<service xml:base=\"http://services.odata.org/OData/OData.svc/\"" +
            "         xmlns:atom=\"http://www.w3.org/2005/Atom\"" +
            "         xmlns:app=\"http://www.w3.org/2007/app\"" +
            "         xmlns=\"http://www.w3.org/2007/app\">" +
            "  <workspace>" +
            "    <atom:title>Default</atom:title>" +
            "    <collection href=\"Products\">" + 
            "      <atom:title>Products</atom:title>" +
            "    </collection>" +
            "    <collection href=\"Categories\">" +
            "      <atom:title>Categories</atom:title>" + 
            "    </collection>" +
            "    <collection href=\"Suppliers\">" +
            "      <atom:title>Suppliers</atom:title>" +
            "    </collection>" +
            "  </workspace>" +
            "</service>";
    }

    @When("^I search for available collections I find \"(\\d+)\"$")
    public void i_search_for_available_collections_i_find(int expectedNumCategories) {
        XmlPath xmlPath = new XmlPath(sampleXml);
        final List<Node> collections = xmlPath.getList("service.workspace.collection", Node.class);
        assertThat(collections.size(), equalTo(expectedNumCategories));

        final List<String> collectionsStr = given(sampleXml).getList("service.workspace.collection.findAll {it.title.text()}", String.class);
        assertThat(collectionsStr, hasItems("Products", "Categories", "Suppliers"));
    }

}

调用上述内容的cucumber-jvm功能文件如下所示:

@odata
Feature: Demonstrate ability to test OData Service Documents
  The Open Data Protocol (OData) enables the creation of HTTP-based data 
  services, which allow resources identified using Uniform Resource 
  Identifiers (URIs) and defined in an abstract data model, to be    published 
  and edited by Web clients using simple HTTP messages.

@api
Scenario: Parse a canned OData Service document
  Given I have a simple service document
  When I search for available collections I find "3"