Search in sources :

Example 1 with ReadContext

use of com.jayway.jsonpath.ReadContext in project JsonPath by jayway.

the class JsonPathMatchersTest method shouldMatchJsonPathOnReadContext.

@Test
public void shouldMatchJsonPathOnReadContext() {
    String test = "{\"foo\":\"bar\"}";
    ReadContext context = JsonPath.parse(test);
    assertThat(context, hasJsonPath("$.foo"));
    assertThat(context, hasJsonPath("$.foo", equalTo("bar")));
    assertThat(context, hasNoJsonPath("$.zoo"));
}
Also used : ReadContext(com.jayway.jsonpath.ReadContext) Test(org.junit.Test)

Example 2 with ReadContext

use of com.jayway.jsonpath.ReadContext in project JsonPath by jayway.

the class WithJsonPathTest method shouldNotMatchOnInvalidJson.

@Test
public void shouldNotMatchOnInvalidJson() {
    ReadContext invalidJson = JsonPath.parse("invalid-json");
    assertThat(invalidJson, not(withJsonPath("$.expensive", equalTo(10))));
}
Also used : ReadContext(com.jayway.jsonpath.ReadContext) Test(org.junit.Test)

Example 3 with ReadContext

use of com.jayway.jsonpath.ReadContext in project graylog2-server by Graylog2.

the class IndicesTest method testIndexTemplateCanBeOverridden.

@Test
public void testIndexTemplateCanBeOverridden() throws Exception {
    final String customTemplateName = "custom-template";
    final IndicesAdminClient client = this.client.admin().indices();
    // Create custom index template
    final Map<String, Object> customMapping = ImmutableMap.of("_source", ImmutableMap.of("enabled", false), "properties", ImmutableMap.of("message", ImmutableMap.of("type", "string", "index", "not_analyzed")));
    final PutIndexTemplateResponse putIndexTemplateResponse = client.preparePutTemplate(customTemplateName).setTemplate(indexSet.getIndexWildcard()).setOrder(1).addMapping(IndexMapping.TYPE_MESSAGE, customMapping).get();
    assertThat(putIndexTemplateResponse.isAcknowledged()).isTrue();
    // Validate existing index templates
    final GetIndexTemplatesResponse getTemplatesResponse = client.prepareGetTemplates().get();
    final List<IndexTemplateMetaData> indexTemplates = getTemplatesResponse.getIndexTemplates();
    assertThat(indexTemplates).extracting(IndexTemplateMetaData::getName).containsExactly(customTemplateName);
    // Create index with custom template
    final String testIndexName = "graylog_override_template";
    indices.create(testIndexName, indexSet);
    // Check index mapping
    final GetMappingsResponse indexMappingResponse = client.prepareGetMappings(testIndexName).get();
    final String mapping = indexMappingResponse.getMappings().get(testIndexName).get(IndexMapping.TYPE_MESSAGE).source().string();
    final ReadContext ctx = JsonPath.parse(mapping);
    final boolean sourceEnabled = ctx.read("$.message._source.enabled");
    assertThat(sourceEnabled).isFalse();
    final String messageField = ctx.read("$.message.properties.message.index");
    assertThat(messageField).isEqualTo("not_analyzed");
    // Clean up
    final DeleteIndexTemplateResponse deleteResponse = client.prepareDeleteTemplate(customTemplateName).get();
    assertThat(deleteResponse.isAcknowledged()).isTrue();
    indices.delete(testIndexName);
}
Also used : DeleteIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse) GetIndexTemplatesResponse(org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse) IndexTemplateMetaData(org.elasticsearch.cluster.metadata.IndexTemplateMetaData) ReadContext(com.jayway.jsonpath.ReadContext) PutIndexTemplateResponse(org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse) IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) GetMappingsResponse(org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse) Test(org.junit.Test)

Example 4 with ReadContext

use of com.jayway.jsonpath.ReadContext in project JsonPath by jayway.

the class IsJson method describeMismatchSafely.

@Override
protected void describeMismatchSafely(T json, Description mismatchDescription) {
    try {
        ReadContext context = parse(json);
        jsonMatcher.describeMismatch(context, mismatchDescription);
    } catch (JsonPathException e) {
        buildMismatchDescription(json, mismatchDescription, e);
    } catch (IOException e) {
        buildMismatchDescription(json, mismatchDescription, e);
    }
}
Also used : ReadContext(com.jayway.jsonpath.ReadContext) JsonPathException(com.jayway.jsonpath.JsonPathException) IOException(java.io.IOException)

Example 5 with ReadContext

use of com.jayway.jsonpath.ReadContext in project selenium-tests by Wikia.

the class YoutubeVideoProvider method getLatestVideoForQuery.

/**
   * This method returns latest youtube video(added no longer then hour ago) for a specified query.
   * This one is using a YouTube Data API (v3) - see for reference -
   * https://developers.google.com/youtube/v3/
   */
public static YoutubeVideo getLatestVideoForQuery(String searchQuery) {
    HttpClient httpclient = HttpClientBuilder.create().setConnectionBackoffStrategy(new DefaultBackoffStrategy()).disableAutomaticRetries().build();
    List<NameValuePair> nvps = new ArrayList<>();
    nvps.add(new BasicNameValuePair("key", API_KEY));
    nvps.add(new BasicNameValuePair("part", "snippet"));
    nvps.add(new BasicNameValuePair("order", "date"));
    nvps.add(new BasicNameValuePair("maxResults", "10"));
    nvps.add(new BasicNameValuePair("q", searchQuery));
    nvps.add(new BasicNameValuePair("publishedAfter", DateTime.now(DateTimeZone.forID("UTC")).minusMinutes(60).toString()));
    nvps.add(new BasicNameValuePair("type", "video"));
    HttpGet httpPost = new HttpGet("https://www.googleapis.com/youtube/v3/search?" + URLEncodedUtils.format(nvps, "utf-8"));
    String videoTitle = null;
    String videoUrl = null;
    String videoId = null;
    try {
        HttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        ReadContext responseValue = JsonPath.parse(EntityUtils.toString(entity));
        videoTitle = responseValue.read("$.items[0].snippet.title");
        videoId = responseValue.read("$.items[0].id.videoId");
        videoUrl = String.format("https://www.youtube.com/watch?v=%s", videoId);
    } catch (IOException e) {
        PageObjectLogging.log("A problem occurred while receiving a YouTube video", e, false);
    }
    return new YoutubeVideo(videoTitle, videoUrl, videoId);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) DefaultBackoffStrategy(org.apache.http.impl.client.DefaultBackoffStrategy) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpClient(org.apache.http.client.HttpClient) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ReadContext(com.jayway.jsonpath.ReadContext)

Aggregations

ReadContext (com.jayway.jsonpath.ReadContext)5 Test (org.junit.Test)3 IOException (java.io.IOException)2 JsonPathException (com.jayway.jsonpath.JsonPathException)1 ArrayList (java.util.ArrayList)1 HttpEntity (org.apache.http.HttpEntity)1 HttpResponse (org.apache.http.HttpResponse)1 NameValuePair (org.apache.http.NameValuePair)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 DefaultBackoffStrategy (org.apache.http.impl.client.DefaultBackoffStrategy)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)1 DeleteIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateResponse)1 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)1 PutIndexTemplateResponse (org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse)1 IndicesAdminClient (org.elasticsearch.client.IndicesAdminClient)1 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)1