Exemplo n.º 1
0
  private void _testGetAggregation(String uri) throws Exception {
    Response resp;

    URI aggrUri =
        buildURI(
            "/"
                + dbTmpName
                + "/"
                + collectionTmpName
                + "/"
                + RequestContext._AGGREGATIONS
                + "/"
                + uri);

    resp = adminExecutor.execute(Request.Get(aggrUri));

    HttpResponse httpResp = resp.returnResponse();
    assertNotNull(httpResp);
    HttpEntity entity = httpResp.getEntity();
    assertNotNull(entity);
    StatusLine statusLine = httpResp.getStatusLine();
    assertNotNull(statusLine);

    assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode());
    assertNotNull("content type not null", entity.getContentType());
    assertEquals(
        "check content type",
        Representation.HAL_JSON_MEDIA_TYPE,
        entity.getContentType().getValue());

    String content = EntityUtils.toString(entity);

    assertNotNull("", content);

    JsonObject json = null;

    try {
      json = JsonObject.readFrom(content);
    } catch (Throwable t) {
      fail("parsing received json");
    }

    assertNotNull("check not null json response", json);
    assertNotNull("check not null _embedded", json.get("_embedded"));
    assertTrue("check _embedded", json.get("_embedded").isObject());

    assertNotNull("", json.get("_embedded").asObject().get("rh:result"));

    assertTrue(
        "check _embedded[\"rh:result\"]",
        json.get("_embedded").asObject().get("rh:result").isArray());

    JsonArray results = json.get("_embedded").asObject().get("rh:result").asArray();

    assertTrue("check we have 2 results", results.size() == 2);

    results
        .values()
        .stream()
        .map(
            (v) -> {
              assertNotNull("check not null _id property", v.asObject().get("_id"));
              return v;
            })
        .map(
            (v) -> {
              assertTrue(
                  "check results _id property is string", v.asObject().get("_id").isString());
              return v;
            })
        .map(
            (v) -> {
              assertNotNull("check not null value property", v.asObject().get("value"));
              return v;
            })
        .forEach(
            (v) -> {
              assertTrue(
                  "check results value property is number", v.asObject().get("value").isNumber());
            });
  }
Exemplo n.º 2
0
  @Test
  public void testGetMapReduceWithVariable() throws Exception {
    String uri = "avg_ages";

    String aggregationsMetadata =
        "{\"aggrs\": ["
            + "{"
            + "\"type\":\"mapReduce\""
            + ","
            + "\"uri\": \""
            + uri
            + "\","
            + "\"map\": \"function() { var minage = JSON.parse($vars).minage; if (this.age > minage ) { emit(this.name, this.age); }; }\","
            + "\"reduce\":\"function(key, values) { return Array.avg(values); }\""
            + ","
            + "\"query\":{\"name\":{\"_$var\":\"name\"}}"
            + "}]}";

    createTmpCollection();
    createMetadataAndTestData(aggregationsMetadata);

    Response resp;

    URI aggrUri =
        buildURI(
            "/"
                + dbTmpName
                + "/"
                + collectionTmpName
                + "/"
                + RequestContext._AGGREGATIONS
                + "/"
                + uri,
            new NameValuePair[] {
              new BasicNameValuePair("avars", "{\"name\": \"a\", \"minage\": 20}")
            });

    resp = adminExecutor.execute(Request.Get(aggrUri));

    HttpResponse httpResp = resp.returnResponse();
    assertNotNull(httpResp);
    HttpEntity entity = httpResp.getEntity();
    assertNotNull(entity);
    StatusLine statusLine = httpResp.getStatusLine();
    assertNotNull(statusLine);

    assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode());
    assertNotNull("content type not null", entity.getContentType());
    assertEquals(
        "check content type",
        Representation.HAL_JSON_MEDIA_TYPE,
        entity.getContentType().getValue());

    String content = EntityUtils.toString(entity);

    assertNotNull("", content);

    JsonObject json = null;

    try {
      json = JsonObject.readFrom(content);
    } catch (Throwable t) {
      fail("parsing received json");
    }

    assertNotNull("check not null json response", json);
    assertNotNull("check not null _embedded", json.get("_embedded"));
    assertTrue("check _embedded", json.get("_embedded").isObject());

    assertNotNull("", json.get("_embedded").asObject().get("rh:result"));
    assertTrue(
        "check _embedded[\"rh:results\"]",
        json.get("_embedded").asObject().get("rh:result").isArray());

    JsonArray results = json.get("_embedded").asObject().get("rh:result").asArray();

    assertTrue("check we have 2 results", results.size() == 1);

    results
        .values()
        .stream()
        .map(
            (v) -> {
              assertNotNull("check not null _id property", v.asObject().get("_id"));
              return v;
            })
        .map(
            (v) -> {
              assertTrue(
                  "check results _id property is string", v.asObject().get("_id").isString());
              return v;
            })
        .map(
            (v) -> {
              assertTrue(
                  "check results _id property is a",
                  v.asObject().get("_id").asString().equals("a"));
              return v;
            })
        .map(
            (v) -> {
              assertNotNull("check not null value property", v.asObject().get("value"));
              return v;
            })
        .forEach(
            (v) -> {
              assertTrue(
                  "check results value property is number", v.asObject().get("value").isNumber());
            });
  }