Search in sources :

Example 16 with JSONArray

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);
}
Also used : JSONArray(net.minidev.json.JSONArray) Test(org.junit.Test)

Example 17 with JSONArray

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);
}
Also used : JSONArray(net.minidev.json.JSONArray) Test(org.junit.Test)

Example 18 with JSONArray

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;
}
Also used : InstanceMetrics(com.microsoft.dhalion.metrics.InstanceMetrics) HashMap(java.util.HashMap) Instant(java.time.Instant) JSONArray(net.minidev.json.JSONArray) DocumentContext(com.jayway.jsonpath.DocumentContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 19 with JSONArray

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"));
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) Test(org.testng.annotations.Test)

Example 20 with JSONArray

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();
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) Test(org.testng.annotations.Test)

Aggregations

JSONArray (net.minidev.json.JSONArray)55 JSONObject (net.minidev.json.JSONObject)41 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 JSONParser (net.minidev.json.parser.JSONParser)5 Test (org.testng.annotations.Test)5 DocumentContext (com.jayway.jsonpath.DocumentContext)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONConverterException (org.btrplace.json.JSONConverterException)3 VM (org.btrplace.model.VM)3 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 ContentType (ddf.catalog.data.ContentType)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)2 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)2 SourceDescriptor (ddf.catalog.source.SourceDescriptor)2 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)2