Search in sources :

Example 1 with Json

use of mjson.Json in project grakn by graknlabs.

the class GraqlControllerTest method whenRunningMultiIdempotentInsertQuery_JsonResponseIsTheSameAsJavaGraql.

@Test
public void whenRunningMultiIdempotentInsertQuery_JsonResponseIsTheSameAsJavaGraql() {
    String single = "insert $x label movie;";
    String queryString = single + "\n" + single;
    Response resp = sendQuery(queryString, APPLICATION_JSON, true, sampleKB.tx().keyspace().getValue(), true);
    resp.then().statusCode(200);
    Stream<Query<?>> query = sampleKB.tx().graql().parser().parseList(queryString);
    String graqlResult = printer.graqlString(query.map(Query::execute).collect(Collectors.toList()));
    Json expected = Json.read(graqlResult);
    assertEquals(expected, Json.read(resp.body().asString()));
}
Also used : Response(com.jayway.restassured.response.Response) Query(ai.grakn.graql.Query) Json(mjson.Json) Test(org.junit.Test)

Example 2 with Json

use of mjson.Json in project grakn by graknlabs.

the class ConceptControllerTest method whenGettingConceptsByLabel_EnsureConceptsAreReturned.

@Test
public void whenGettingConceptsByLabel_EnsureConceptsAreReturned() {
    assertConceptsReturned(REST.WebPath.KEYSPACE_ROLE, Role[].class, "roles", roleWrapper1, roleWrapper2);
    assertConceptsReturned(REST.WebPath.KEYSPACE_RULE, Rule[].class, "rules", ruleWrapper);
    // Manual Check is necessary due to collection containing mixture of concepts
    String request = REST.resolveTemplate(REST.WebPath.KEYSPACE_TYPE, keyspace.getValue());
    List<Json> response = Json.read(RestAssured.when().get(request).body().asString()).at("types").asJsonList();
    Set<Concept> types = response.stream().map(JsonConceptBuilder::<Concept>build).collect(Collectors.toSet());
    assertTrue(String.format("Type {$s} missing from response", relationshipTypeWrapper), types.contains(relationshipTypeWrapper));
    assertTrue(String.format("Type {$s} missing from response", attributeTypeWrapper), types.contains(attributeTypeWrapper));
    assertTrue(String.format("Type {$s} missing from response", entityTypeWrapper), types.contains(entityTypeWrapper));
}
Also used : Role(ai.grakn.engine.controller.response.Role) Concept(ai.grakn.engine.controller.response.Concept) Rule(ai.grakn.engine.controller.response.Rule) ClassRule(org.junit.ClassRule) Matchers.containsString(org.hamcrest.Matchers.containsString) Json(mjson.Json) Test(org.junit.Test)

Example 3 with Json

use of mjson.Json in project grakn by graknlabs.

the class RequestsTest method extractJsonField_MustExtractNestedFieldCorrectly.

@Test
public void extractJsonField_MustExtractNestedFieldCorrectly() {
    String nestedFieldValue = "nestedFieldValue";
    Json input = Json.object("topLevelField", Json.object("nestedField", nestedFieldValue));
    String extractedNestedField = extractJsonField(input, "topLevelField", "nestedField").asString();
    assertThat(extractedNestedField, equalTo(nestedFieldValue));
}
Also used : Json(mjson.Json) Test(org.junit.Test)

Example 4 with Json

use of mjson.Json in project grakn by graknlabs.

the class JsonPrinter method build.

@Override
public Json build(Concept concept) {
    Json json = Json.object("id", concept.getId().getValue());
    if (concept.isSchemaConcept()) {
        json.set("name", concept.asSchemaConcept().getLabel().getValue());
        SchemaConcept superConcept = concept.asSchemaConcept().sup();
        if (superConcept != null)
            json.set("sub", superConcept.getLabel().getValue());
    } else if (concept.isThing()) {
        json.set("isa", concept.asThing().type().getLabel().getValue());
    } else {
        throw CommonUtil.unreachableStatement("Unrecognised concept " + concept);
    }
    if (concept.isAttribute()) {
        json.set("value", concept.asAttribute().getValue());
    }
    if (concept.isRule()) {
        Pattern when = concept.asRule().getWhen();
        if (when != null) {
            json.set("when", when.toString());
        }
        Pattern then = concept.asRule().getThen();
        if (then != null) {
            json.set("then", then.toString());
        }
    }
    return json;
}
Also used : Pattern(ai.grakn.graql.Pattern) SchemaConcept(ai.grakn.concept.SchemaConcept) Json(mjson.Json)

Example 5 with Json

use of mjson.Json in project grakn by graknlabs.

the class JsonPrinter method build.

@Override
public final Json build(Map<?, ?> map) {
    Json json = Json.object();
    map.forEach((Object key, Object value) -> {
        if (key instanceof Var)
            key = ((Var) key).getValue();
        String keyString = key == null ? "" : key.toString();
        json.set(keyString, build(value));
    });
    return json;
}
Also used : Var(ai.grakn.graql.Var) Json(mjson.Json)

Aggregations

Json (mjson.Json)10 Test (org.junit.Test)8 Concept (ai.grakn.engine.controller.response.Concept)2 Response (com.jayway.restassured.response.Response)2 SchemaConcept (ai.grakn.concept.SchemaConcept)1 Role (ai.grakn.engine.controller.response.Role)1 Rule (ai.grakn.engine.controller.response.Rule)1 GraknServerException (ai.grakn.exception.GraknServerException)1 Pattern (ai.grakn.graql.Pattern)1 Query (ai.grakn.graql.Query)1 Var (ai.grakn.graql.Var)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 ClassRule (org.junit.ClassRule)1