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));
}
}
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());
}
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);
}
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;
}
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());
}
Aggregations