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"));
}
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))));
}
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);
}
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);
}
}
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);
}
Aggregations