• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Java JsonPath类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中io.restassured.path.json.JsonPath的典型用法代码示例。如果您正苦于以下问题:Java JsonPath类的具体用法?Java JsonPath怎么用?Java JsonPath使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



JsonPath类属于io.restassured.path.json包,在下文中一共展示了JsonPath类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: valueOf

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Override
public <T> T valueOf (final String name, final String path) {
	if (!StringUtils.isEmpty (name)) {
		this.response.then ()
			.root (name);
	}
	final JsonPath jsonPath = JsonPath.from (this.response.asString ());
	if (StringUtils.isBlank (path)) {
		return null;
	}
	try {
		return jsonPath.get (path);
	}
	catch (final Exception e) {
		final String message = "Response value parsing failed for [%s] expression.";
		fail (RestResponseParsingFailedError.class, format (message, path), e);
	}
	return null;
}
 
开发者ID:WasiqB,项目名称:coteafs-services,代码行数:20,代码来源:RestResponseValueParser.java


示例2: getFieldId

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
private static String getFieldId(String fieldName) {

        JsonPath jsonPath = JiraConfig.getJIRARequestSpec()
                .when()
                .get(JIRA_REST_PATH + "/field")
                .thenReturn().jsonPath();

        return jsonPath.getString(String.format("find {it.name == '%s'}.id", fieldName));
    }
 
开发者ID:Frameworkium,项目名称:frameworkium-core,代码行数:10,代码来源:JiraTest.java


示例3: canGetC3POandParseWithJsonPath

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Test
public void canGetC3POandParseWithJsonPath(){

    // use RestAssured to make an HTML Call
    Response response = RestAssured.get(
                      "http://swapi.co/api/people/2/?format=json").
                       andReturn();

    String json = response.getBody().asString();
    System.out.println(json);

    // Use the JsonPath parsing library of RestAssured to Parse the JSON
    
    JsonPath jsonPath = new JsonPath(json);
    Assert.assertEquals(
            "C-3PO",
            jsonPath.getString("name"));

}
 
开发者ID:eviltester,项目名称:libraryexamples,代码行数:20,代码来源:SwapiAPIUsageTest.java


示例4: canGetC3POandParseWithJsonPathIntoObject

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Test
public void canGetC3POandParseWithJsonPathIntoObject(){

    // use RestAssured to make an HTML Call
    Response response = RestAssured.get(
            "http://swapi.co/api/people/2/?format=json").
            andReturn();

    String json = response.getBody().asString();
    System.out.println(json);

    // Use the JsonPath parsing library of RestAssured to Parse the JSON into an object

   Person c3po = new JsonPath(json).getObject("$", Person.class);
    Assert.assertEquals(
            "C-3PO",
            c3po.name);

}
 
开发者ID:eviltester,项目名称:libraryexamples,代码行数:20,代码来源:SwapiAPIUsageTest.java


示例5: aJsonPathExampleFromResponse

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Test
public void aJsonPathExampleFromResponse(){

    /*
        Use RestAssured to return response from HTTP to demonstrate same as using JsonPath on String
     */
    Response response = RestAssured.
            when().
            get(jsonendpoint).
            andReturn();

    JsonPath jsonPath = new JsonPath(response.body().asString());

    // multiple matches returned in an ArrayList
    List<HashMap<String,String>> ret = jsonPath.get("projects.project");
    Assert.assertEquals(6, ret.size());

    Assert.assertEquals("A New Projectaniheeiadtatd",
                        ret.get(0).get("name"));

    // can index on multiple matches with array indexing notation [1]
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project[1].name"));
    Assert.assertEquals(3,jsonPath.getInt("projects.project[1].id"));

    // filters and finding stuff
    // find the project with id == '3' and return the name
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project.find {it.id == 3}.name"));

    // use `findAll` to find all the items that match a condition
    ArrayList projectsWithIdLessThanSix = jsonPath.get("projects.project.findAll {it.id <= 6}");
    Assert.assertEquals(4,projectsWithIdLessThanSix.size());




}
 
开发者ID:eviltester,项目名称:tracksrestcasestudy,代码行数:37,代码来源:RestAssuredJSONExamplesTest.java


示例6: the

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@When("^I want to get information on the (.+) project$")
public void usersGetInformationOnAProject(String projectName) throws IOException {
    wireMockServer.start();

    configureFor("localhost", 8080);

    stubFor(get(urlEqualTo(GET_JSON_PATH+projectName))
            .willReturn(aResponse().withBody(jsonString).withHeader("content-type", JSON_CONTENT_TYPE).withStatus(200)));

    Response res = RestAssured.get(GET_JSON_PATH+projectName);

    assertEquals(200, res.getStatusCode());
    String json = res.body().asString();
    JsonPath jp = new JsonPath(json);
    assertNotNull(jp);
    assertEquals("cucumber", jp.getString("testing-framework"));
    assertEquals("cucumber.io", jp.getString("website"));
    assertTrue(jp.getString("supported-language").contains("Java"));
    assertTrue(jp.getString("supported-language").contains("C++"));

    /* ANOTHER WAY TO DO THE SAME */
   /* given().contentType(JSON_CONTENT_TYPE).expect()
            .statusCode(200)
            .body(
                    "testing-framework", Matchers.equalTo("cucumber"),
                    "website", Matchers.equalTo("cucumber.io"),
                    "supported-language", Matchers.contains(
                        "Ruby",
                        "Java",
                        "Javascript",
                        "PHP",
                        "Python",
                        "C++"))
            .when()
            .get(GET_JSON_PATH+projectName); */

    wireMockServer.stop();
}
 
开发者ID:mariaklimenko,项目名称:jeta,代码行数:39,代码来源:RestAssuredSteps.java


示例7: theNameOfThePersonIs

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Then("^the name of the person is \"([^\"]*)\"$")
public void theNameOfThePersonIs(String givenName) throws Throwable {
    String json = response.getBody().asString();
    JsonPath jsonPath = new JsonPath(json);
    Assert.assertEquals(
            givenName,
            jsonPath.getString("name"));
}
 
开发者ID:eviltester,项目名称:libraryexamples,代码行数:9,代码来源:SwapiSteps.java


示例8: canAssertWithHamcrest

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Test
public void canAssertWithHamcrest(){

    // using RestAssured JsonPath to parse the Json Data
    JsonPath json = new JsonPath(swapidata);

    String name = json.getString("name");
    int mass = json.getInt("mass");

    // using C-3PO data
    assertThat(name, is(equalTo("C-3PO")));
    assertThat(name, is(not(equalTo("R2-D2"))));
    assertThat(mass, is(greaterThan(74)));
    
}
 
开发者ID:eviltester,项目名称:libraryexamples,代码行数:16,代码来源:HamcrestAssertionOnSwapiDataUsageTest.java


示例9: getRaPathClass

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
public static Class getRaPathClass(MimeTypeEnum type) {
    switch (type) {
        case XML:
            return XmlPath.class;
        case JSON:
            return JsonPath.class;
    }
    return null;
}
 
开发者ID:qameta,项目名称:rarc,代码行数:10,代码来源:BodyRule.java


示例10: aJsonDeserializationExample

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Ignore("need to add Gson to path")
@Test
public void aJsonDeserializationExample(){
    // if I was to deserialize the whole message into an object then I can just use `get("...").as(ProjectJson.class)`
    /*
        If I add gson to the dependencies section in the pom.xml then I can deserialize into an object

        <dependency>
           <groupId>com.google.code.gson</groupId>
           <artifactId>gson</artifactId>
           <version>2.8.1</version>
         </dependency>

     */


    File jsonExample = new File(System.getProperty("user.dir"),
            "src/test/resources/jsonxml/jsonexample.json");

    JsonPath jsonPath = new JsonPath(jsonExample);


    ProjectJson projectFromJson = RestAssured.
            when().
            get(jsonendpoint).
            jsonPath().
            getObject("projects.project[1]", ProjectJson.class);

    Assert.assertEquals(3, projectFromJson.id);



    ProjectFromXmlOrJson aProjectFromJson =
            jsonPath.getObject("projects.project[1]",
                                ProjectFromXmlOrJson.class);
    Assert.assertEquals(3, aProjectFromJson.id);

    ProjectJson theProjectFromJson =
                        jsonPath.getObject(
                                "projects.project[1]",
                                ProjectJson.class);
    Assert.assertEquals(3, theProjectFromJson.id);
}
 
开发者ID:eviltester,项目名称:tracksrestcasestudy,代码行数:44,代码来源:RestAssuredJSONExamplesTest.java


示例11: generate

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
/**
 * Generates classes based on json/jsonschema.
 *
 * @return class name
 * @throws IOException
 */
public String generate() throws IOException {
    String schemaPath = this.config.getJsonSchemaPath();
    if (schemas.containsKey(schemaPath)){
        LOG.info("Schema already exists " + schemaPath);
        return schemas.get(schemaPath);
    }

    JCodeModel codeModel = new JCodeModel();
    URL source = new File(config.getInputPath()).toURI().toURL();
    GenerationConfig generationConfig = new DefaultGenerationConfig() {
        @Override
        public boolean isGenerateBuilders() { // set config option by overriding metho
            return true;
        }

        @Override
        public SourceType getSourceType() {
            if (JsonPath.from(source).get("$schema") != null) {
                return SourceType.JSONSCHEMA;
            }
            return SourceType.JSON;
        }

        @Override
        public boolean isUseLongIntegers() {
            return true;
        }

        @Override
        public boolean isUseCommonsLang3() {
            return true;
        }
    };

    SchemaMapper mapper = new SchemaMapper(
            new RuleFactory(generationConfig, new GsonAnnotator(), new SchemaStore()), new SchemaGenerator());
    mapper.generate(codeModel, getClassName(), config.getPackageName(), source);
    codeModel.build(new File(config.getOutputPath()));

    schemas.put(schemaPath, getClassName());
    return getClassName();
}
 
开发者ID:qameta,项目名称:rarc,代码行数:49,代码来源:JsonCodegen.java


示例12: alex_should_use_reviews_v2_version

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Test
public void alex_should_use_reviews_v2_version() throws IOException {

    // given
    waitUntilRouteIsPopulated();

    // when

    // Using okhttp because I have not find any way of making rest assured working when setting the required cookies
    final Request request = new Request.Builder()
        .url(url.toString() + "api/v1/products/0/reviews")
        .addHeader("Cookie", "user=alex; Domain=" + url.getHost() +"; Path=/")
        .build();

    final OkHttpClient okHttpClient = new OkHttpClient();
    try(Response response = okHttpClient.newCall(request).execute()) {

        // then

        final String content = response.body().string();

        final List<Map<String, Object>> ratings = JsonPath.from(content).getList("reviews.rating");

        final Map<String, Object> expectationStar5 = new HashMap<>();
        expectationStar5.put("color", "black");
        expectationStar5.put("stars", 5);

        final Map<String, Object> expectationStar4 = new HashMap<>();
        expectationStar4.put("color", "black");
        expectationStar4.put("stars", 4);

        assertThat(ratings)
            .containsExactlyInAnyOrder(expectationStar4, expectationStar5);

    }

}
 
开发者ID:arquillian,项目名称:arquillian-cube,代码行数:38,代码来源:ReviewsIT.java


示例13: aSetOfJsonPathExamples

import io.restassured.path.json.JsonPath; //导入依赖的package包/类
@Test
public void aSetOfJsonPathExamples(){

    /*
        REST Assured documentation https://github.com/rest-assured/rest-assured/wiki/Usage
        
        JSON parsing https://github.com/rest-assured/rest-assured/wiki/Usage#json-example
        JsonPath - https://github.com/rest-assured/rest-assured/wiki/Usage#json-using-jsonpath

        JsonPath blog post with useful examples:
        https://blog.jayway.com/2013/04/12/whats-new-in-rest-assured-1-8/

        Deserialisation
        https://github.com/rest-assured/rest-assured/wiki/Usage#deserialization

     */
    File jsonExample = new File(System.getProperty("user.dir"),
            "src/test/resources/jsonxml/jsonexample.json");

    JsonPath jsonPath = new JsonPath(jsonExample);

    // multiple matches returned in an ArrayList
    ArrayList ret = jsonPath.get("projects.project");
    Assert.assertEquals(6, ret.size());

    // can index on multiple matches with array indexing notation [1]
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project[1].name"));
    Assert.assertEquals(3,jsonPath.getInt("projects.project[1].id"));

    // can count backwards so -1 is last, -2 is second to last
    Assert.assertEquals(10,jsonPath.getInt("projects.project[-1].id"));

    // filters and finding stuff
    // find the project with id == '3' and return the name
    Assert.assertEquals("the new name aniheeiaosono",jsonPath.get("projects.project.find {it.id == 3}.name"));

    // use `findAll` to find all the items that match a condition
    // conditions can use Groovy so convert the id to an integer in the filter if it was a string with  `{it.id.toInteger() <= 6}`

    // use `it` in the filter condition to refer to currently matched item
    ArrayList projectsWithIdLessThanSix = jsonPath.get("projects.project.findAll {it.id <= 6}");
    Assert.assertEquals(4,projectsWithIdLessThanSix.size());



    // get an 'object' as a Map from the list
    Map<String, Object> project1map = jsonPath.getMap("projects.project[1]");
    Assert.assertEquals("active", project1map.get("state"));


    // set root to an element in the Json to make accessing it easier
    JsonPath project2 = new JsonPath(jsonExample).setRoot("projects.project[2]");
    Assert.assertEquals(5 ,project2.getInt("id"));
    Assert.assertEquals("active" ,project2.get("state"));




}
 
开发者ID:eviltester,项目名称:tracksrestcasestudy,代码行数:60,代码来源:RestAssuredJSONExamplesTest.java



注:本文中的io.restassured.path.json.JsonPath类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java Pages类代码示例发布时间:2022-05-22
下一篇:
Java FrenchStemmer类代码示例发布时间:2022-05-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap