Search in sources :

Example 56 with RestResult

use of com.baidu.hugegraph.rest.RestResult in project incubator-hugegraph-toolchain by apache.

the class RestResultTest method testReadGremlinEdges.

@Test
public void testReadGremlinEdges() {
    String json = "{" + "\"requestId\": \"cd4cfc17-1ee4-4e9e-af40-cb18b115a8dc\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:peter>created>>software:lop\"," + "\"label\": \"created\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"software\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"software:lop\"," + "\"outV\": \"person:peter\"," + "\"properties\": {" + "\"date\": 1490284800000," + "\"weight\": 0.2" + "}" + "}," + "{" + "\"id\": \"person:peter>knows>>person:marko\"," + "\"label\": \"knows\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"person\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"person:marko\"," + "\"outV\": \"person:peter\"," + "\"properties\": {" + "\"date\": 1452355200000," + "\"weight\": 0.5" + "}" + "}" + "]," + "\"meta\": {}" + "}" + "}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult restResult = new RestResult(this.mockResponse);
    Assert.assertEquals(200, restResult.status());
    Assert.assertNull(restResult.headers());
    Response response = restResult.readObject(Response.class);
    response.graphManager(graph());
    Assert.assertEquals(200, response.status().code());
    Edge created = new Edge("created");
    created.id("person:peter>created>>software:lop");
    created.sourceId("person:peter");
    created.targetId("software:lop");
    created.sourceLabel("person");
    created.targetLabel("software");
    created.property("date", 1490284800000L);
    created.property("weight", 0.2);
    Edge knows = new Edge("knows");
    knows.id("person:peter>knows>>person:marko");
    knows.sourceId("person:peter");
    knows.targetId("person:marko");
    knows.sourceLabel("person");
    knows.targetLabel("person");
    knows.property("date", 1452355200000L);
    knows.property("weight", 0.5);
    List<Edge> edges = new ArrayList<>(2);
    edges.add(created);
    edges.add(knows);
    Iterator<Result> results = response.result().iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Assert.assertEquals(Edge.class, result.getObject().getClass());
        Edge edge = result.getEdge();
        Assert.assertTrue(Utils.contains(edges, edge));
    }
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) RestResult(com.baidu.hugegraph.rest.RestResult) ArrayList(java.util.ArrayList) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 57 with RestResult

use of com.baidu.hugegraph.rest.RestResult in project incubator-hugegraph-toolchain by apache.

the class RestResultTest method testReadVertexLabels.

@Test
public void testReadVertexLabels() {
    String json = "{\"vertexlabels\": [" + "{" + "\"id\": 1," + "\"primary_keys\": [\"name\"]," + "\"index_labels\": []," + "\"name\": \"software\"," + "\"id_strategy\": \"PRIMARY_KEY\"," + "\"properties\": [\"price\", \"name\", \"lang\"]" + "}," + "{" + "\"id\": 2," + "\"primary_keys\": []," + "\"index_labels\": []," + "\"name\": \"person\"," + "\"id_strategy\": \"CUSTOMIZE_STRING\"," + "\"properties\": [\"city\", \"name\", \"age\"]" + "}" + "]}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult result = new RestResult(this.mockResponse);
    Assert.assertEquals(200, result.status());
    Assert.assertNull(result.headers());
    List<VertexLabel> vertexLabels = result.readList("vertexlabels", VertexLabel.class);
    Assert.assertEquals(2, vertexLabels.size());
    VertexLabel vertexLabel1 = vertexLabels.get(0);
    VertexLabel vertexLabel2 = vertexLabels.get(1);
    Assert.assertEquals("software", vertexLabel1.name());
    Assert.assertEquals(IdStrategy.PRIMARY_KEY, vertexLabel1.idStrategy());
    Assert.assertEquals(ImmutableList.of("name"), vertexLabel1.primaryKeys());
    Assert.assertEquals(ImmutableSet.of("price", "name", "lang"), vertexLabel1.properties());
    Assert.assertEquals("person", vertexLabel2.name());
    Assert.assertEquals(IdStrategy.CUSTOMIZE_STRING, vertexLabel2.idStrategy());
    Assert.assertEquals(Collections.emptyList(), vertexLabel2.primaryKeys());
    Assert.assertEquals(ImmutableSet.of("city", "name", "age"), vertexLabel2.properties());
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) Test(org.junit.Test)

Example 58 with RestResult

use of com.baidu.hugegraph.rest.RestResult in project incubator-hugegraph-toolchain by apache.

the class RestResultTest method testReadGremlinEdgeAndNull.

@Test
public void testReadGremlinEdgeAndNull() {
    String json = "{" + "\"requestId\": \"d95ac131-24b5-4140-a3ff-91b0c020764a\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:peter>created>>software:lop\"," + "\"label\": \"created\"," + "\"type\": \"edge\"," + "\"inVLabel\": \"software\"," + "\"outVLabel\": \"person\"," + "\"inV\": \"software:lop\"," + "\"outV\": \"person:peter\"," + "\"properties\": {" + "\"date\": 1490284800000," + "\"weight\": 0.2" + "}" + "}," + "null" + "]," + "\"meta\": {}" + "}" + "}";
    Mockito.when(this.mockResponse.getStatus()).thenReturn(200);
    Mockito.when(this.mockResponse.getHeaders()).thenReturn(null);
    Mockito.when(this.mockResponse.readEntity(String.class)).thenReturn(json);
    RestResult restResult = new RestResult(this.mockResponse);
    Assert.assertEquals(200, restResult.status());
    Assert.assertNull(restResult.headers());
    Response response = restResult.readObject(Response.class);
    response.graphManager(graph());
    Assert.assertEquals(200, response.status().code());
    Iterator<Result> results = response.result().iterator();
    Assert.assertTrue(results.hasNext());
    Result result = results.next();
    Assert.assertEquals(Edge.class, result.getObject().getClass());
    Edge created = new Edge("created");
    created.id("person:peter>created>>software:lop");
    created.sourceId("person:peter");
    created.targetId("software:lop");
    created.sourceLabel("person");
    created.targetLabel("software");
    created.property("date", 1490284800000L);
    created.property("weight", 0.2);
    Assert.assertTrue(Utils.contains(ImmutableList.of(created), result.getEdge()));
    Assert.assertTrue(results.hasNext());
    result = results.next();
    Assert.assertNull(result);
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) RestResult(com.baidu.hugegraph.rest.RestResult) Edge(com.baidu.hugegraph.structure.graph.Edge) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 59 with RestResult

use of com.baidu.hugegraph.rest.RestResult in project incubator-hugegraph-toolchain by apache.

the class ServerException method fromResponse.

public static ServerException fromResponse(Response response) {
    RestResult rs = new RestResult(response);
    ServerException exception = new ServerException(rs.content());
    exception.status(response.getStatus());
    try {
        @SuppressWarnings("unchecked") Map<String, Object> json = rs.readObject(Map.class);
        exception.exception = (String) getByKeys(json, EXCEPTION_KEYS);
        exception.message = (String) getByKeys(json, MESSAGE_KEYS);
        exception.cause = (String) getByKeys(json, CAUSE_KEYS);
        exception.trace = getByKeys(json, TRACE_KEYS);
    } catch (Exception ignored) {
    }
    return exception;
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult)

Example 60 with RestResult

use of com.baidu.hugegraph.rest.RestResult in project hugegraph-common by hugegraph.

the class RestClientTest method testDeleteWithId.

@Test
public void testDeleteWithId() {
    RestClient client = new RestClientImpl("/test", 1000, 204);
    RestResult restResult = client.delete("path", "id1");
    Assert.assertEquals(204, restResult.status());
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) AbstractRestClient(com.baidu.hugegraph.rest.AbstractRestClient) RestClient(com.baidu.hugegraph.rest.RestClient) Test(org.junit.Test)

Aggregations

RestResult (com.baidu.hugegraph.rest.RestResult)132 Test (org.junit.Test)44 LinkedHashMap (java.util.LinkedHashMap)23 AbstractRestClient (com.baidu.hugegraph.rest.AbstractRestClient)19 RestClient (com.baidu.hugegraph.rest.RestClient)19 MultivaluedHashMap (jakarta.ws.rs.core.MultivaluedHashMap)12 Response (com.baidu.hugegraph.structure.gremlin.Response)6 Result (com.baidu.hugegraph.structure.gremlin.Result)6 PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)6 Edge (com.baidu.hugegraph.structure.graph.Edge)5 Vertex (com.baidu.hugegraph.structure.graph.Vertex)5 Map (java.util.Map)5 IndexLabel (com.baidu.hugegraph.structure.schema.IndexLabel)4 InvalidResponseException (com.baidu.hugegraph.exception.InvalidResponseException)3 NotSupportException (com.baidu.hugegraph.exception.NotSupportException)3 EdgeLabel (com.baidu.hugegraph.structure.schema.EdgeLabel)3 VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)3 ArrayList (java.util.ArrayList)3 NotAllCreatedException (com.baidu.hugegraph.exception.NotAllCreatedException)2 Project (com.baidu.hugegraph.structure.auth.Project)2