use of net.minidev.json.JSONArray in project JsonPath by json-path.
the class JSONEntityPathFunctionTest method testPredicateWithFunctionCallTwoMatches.
@Test
public void testPredicateWithFunctionCallTwoMatches() {
String path = "$.batches.results[?(@.values.length() >= 3)].values.avg()";
// Its an array because in some use-cases the min size might match more than one batch and thus we'll get
// the average out for each collection
JSONArray values = new JSONArray();
values.add(12.2d);
values.add(17d);
verifyFunction(conf, path, BATCH_JSON, values);
}
use of net.minidev.json.JSONArray in project JsonPath by json-path.
the class JSONEntityPathFunctionTest method testPredicateWithFunctionCallSingleMatch.
/**
* The fictitious use-case/story - is we have a collection of batches with values indicating some quality metric.
* We want to determine the average of the values for only the batch's values where the number of items in the batch
* is greater than the min batch size which is encoded in the JSON document.
*
* We use the length function in the predicate to determine the number of values in each batch and then for those
* batches where the count is greater than min we calculate the average batch value.
*
* Its completely contrived example, however, this test exercises functions within predicates.
*/
@Test
public void testPredicateWithFunctionCallSingleMatch() {
String path = "$.batches.results[?(@.values.length() >= $.batches.minBatchSize)].values.avg()";
// Its an array because in some use-cases the min size might match more than one batch and thus we'll get
// the average out for each collection
JSONArray values = new JSONArray();
values.add(12.2d);
verifyFunction(conf, path, BATCH_JSON, values);
}
use of net.minidev.json.JSONArray in project incubator-heron by apache.
the class TrackerMetricsProvider method parse.
@SuppressWarnings("unchecked")
private Map<String, InstanceMetrics> parse(String response, String component, String metric) {
Map<String, InstanceMetrics> metricsData = new HashMap<>();
if (response == null || response.isEmpty()) {
return metricsData;
}
DocumentContext result = JsonPath.parse(response);
JSONArray jsonArray = result.read("$.." + metric);
if (jsonArray.size() != 1) {
LOG.info(String.format("Did not get any metrics from tracker for %s:%s ", component, metric));
return metricsData;
}
Map<String, Object> metricsMap = (Map<String, Object>) jsonArray.get(0);
if (metricsMap == null || metricsMap.isEmpty()) {
LOG.info(String.format("Did not get any metrics from tracker for %s:%s ", component, metric));
return metricsData;
}
for (String instanceName : metricsMap.keySet()) {
Map<String, String> tmpValues = (Map<String, String>) metricsMap.get(instanceName);
Map<Instant, Double> values = new HashMap<>();
for (String timeStamp : tmpValues.keySet()) {
values.put(Instant.ofEpochSecond(Long.parseLong(timeStamp)), Double.parseDouble(tmpValues.get(timeStamp)));
}
InstanceMetrics instanceMetrics = new InstanceMetrics(instanceName);
instanceMetrics.addMetric(metric, values);
metricsData.put(instanceName, instanceMetrics);
}
return metricsData;
}
use of net.minidev.json.JSONArray in project molgenis by molgenis.
the class RestControllerV2APIIT method testCreateEntities.
// FIXME
@Test(enabled = false)
public void testCreateEntities() {
// @Transactional
// @PostMapping(value = "/{entityName}", produces = APPLICATION_JSON_VALUE)
// @ResponseBody
// public EntityCollectionBatchCreateResponseBodyV2 createEntities(@PathVariable("entityName") String entityName,
// @RequestBody @Valid EntityCollectionBatchRequestV2 request, HttpServletResponse response) throws Exception
JSONObject jsonObject = new JSONObject();
JSONArray entities = new JSONArray();
JSONObject entity1 = new JSONObject();
entity1.put("value", "ref55");
entity1.put("label", "label55");
entities.add(entity1);
JSONObject entity2 = new JSONObject();
entity2.put("value", "ref57");
entity2.put("label", "label57");
entities.add(entity2);
jsonObject.put("entities", entities);
given().log().all().body(jsonObject.toJSONString()).contentType(APPLICATION_JSON).header(X_MOLGENIS_TOKEN, testUserToken).post(API_V2 + "V2_API_TypeTestAPIV2").then().log().all().statusCode(CREATED).body("location", Matchers.equalTo("/api/v2/V2_API_TypeTestv2?q=id=in=(\"55\",\"57\")"), "resources[0].href", Matchers.equalTo("/api/v2/V2_API_TypeTestAPIV2/55"), "resources[1].href", Matchers.equalTo("/api/v2/V2_API_TypeTestAPIV2/57"));
}
use of net.minidev.json.JSONArray in project molgenis by molgenis.
the class RestControllerV2IT method batchDelete.
@Test(dependsOnMethods = { "batchCreate", "batchCreateTypeTest", "batchUpdate" }, priority = 10)
public void batchDelete() {
JSONObject jsonObject = new JSONObject();
JSONArray entityIds = new JSONArray();
entityIds.add("55");
entityIds.add("57");
jsonObject.put("entityIds", entityIds);
given().log().all().header(X_MOLGENIS_TOKEN, testUserToken).contentType(APPLICATION_JSON).body(jsonObject.toJSONString()).when().delete(API_V2 + "it_emx_datatypes_TypeTestv2").then().statusCode(NO_CONTENT).log().all();
}
Aggregations