Search in sources :

Example 71 with RestResult

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

the class RestClientTest method testDeleteWithParams.

@Test
public void testDeleteWithParams() {
    RestClient client = new RestClientImpl("/test", 1000, 204);
    Map<String, Object> params = ImmutableMap.of("param1", "value1");
    RestResult restResult = client.delete("path", params);
    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)

Example 72 with RestResult

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

the class RestClientTest method testRequest.

@Test
public void testRequest() {
    MockRestClientImpl client = new MockRestClientImpl("test", 1000);
    WebTarget target = Mockito.mock(WebTarget.class);
    Builder builder = Mockito.mock(Builder.class);
    Mockito.when(target.path("test")).thenReturn(target);
    Mockito.when(target.path("test").path(AbstractRestClient.encode("id"))).thenReturn(target);
    Mockito.when(target.path("test").request()).thenReturn(builder);
    Mockito.when(target.path("test").path(AbstractRestClient.encode("id")).request()).thenReturn(builder);
    Response response = Mockito.mock(Response.class);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(response.getHeaders()).thenReturn(new MultivaluedHashMap<>());
    Mockito.when(response.readEntity(String.class)).thenReturn("content");
    Mockito.when(builder.delete()).thenReturn(response);
    Mockito.when(builder.get()).thenReturn(response);
    Mockito.when(builder.put(Mockito.any())).thenReturn(response);
    Mockito.when(builder.post(Mockito.any())).thenReturn(response);
    Whitebox.setInternalState(client, "target", target);
    RestResult result;
    // Test delete
    client.setAuthContext("token1");
    result = client.delete("test", ImmutableMap.of());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token1");
    client.resetAuthContext();
    client.setAuthContext("token2");
    result = client.delete("test", "id");
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token2");
    client.resetAuthContext();
    // Test get
    client.setAuthContext("token3");
    result = client.get("test");
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token3");
    client.resetAuthContext();
    client.setAuthContext("token4");
    result = client.get("test", ImmutableMap.of());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token4");
    client.resetAuthContext();
    client.setAuthContext("token5");
    result = client.get("test", "id");
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token5");
    client.resetAuthContext();
    // Test put
    client.setAuthContext("token6");
    result = client.post("test", new Object());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token6");
    client.resetAuthContext();
    client.setAuthContext("token7");
    result = client.post("test", new Object(), new MultivaluedHashMap<>());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token7");
    client.resetAuthContext();
    client.setAuthContext("token8");
    result = client.post("test", new Object(), ImmutableMap.of());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token8");
    client.resetAuthContext();
    client.setAuthContext("token9");
    result = client.post("test", new Object(), new MultivaluedHashMap<>(), ImmutableMap.of());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token9");
    client.resetAuthContext();
    // Test post
    client.setAuthContext("token10");
    result = client.post("test", new Object());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token10");
    client.resetAuthContext();
    client.setAuthContext("token11");
    result = client.post("test", new Object(), new MultivaluedHashMap<>());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token11");
    client.resetAuthContext();
    client.setAuthContext("token12");
    result = client.post("test", new Object(), ImmutableMap.of());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token12");
    client.resetAuthContext();
    client.setAuthContext("token13");
    result = client.post("test", new Object(), new MultivaluedHashMap<>(), ImmutableMap.of());
    Assert.assertEquals(200, result.status());
    Mockito.verify(builder).header(HttpHeaders.AUTHORIZATION, "token13");
    client.resetAuthContext();
}
Also used : Response(jakarta.ws.rs.core.Response) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) RestResult(com.baidu.hugegraph.rest.RestResult) Builder(jakarta.ws.rs.client.Invocation.Builder) WebTarget(jakarta.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 73 with RestResult

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

the class RestClientTest method testGetWithParams.

@Test
public void testGetWithParams() {
    RestClient client = new RestClientImpl("/test", 1000, 200);
    Map<String, Object> params = new HashMap<>();
    params.put("key1", ImmutableList.of("value1-1", "value1-2"));
    params.put("key2", "value2");
    RestResult restResult = client.get("path", params);
    Assert.assertEquals(200, restResult.status());
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) HashMap(java.util.HashMap) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) AbstractRestClient(com.baidu.hugegraph.rest.AbstractRestClient) RestClient(com.baidu.hugegraph.rest.RestClient) Test(org.junit.Test)

Example 74 with RestResult

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

the class RestResultTest method testReadGremlinVertices.

@Test
public void testReadGremlinVertices() {
    String json = "{" + "\"requestId\": \"b0fd8ead-333f-43ac-97b0-4d78784726ae\"," + "\"status\": {" + "\"message\": \"\"," + "\"code\": 200," + "\"attributes\": {}" + "}," + "\"result\": {" + "\"data\": [" + "{" + "\"id\": \"person:marko\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Beijing\",\"Wuhan\",\"Beijing\"]," + "\"name\": \"marko\"," + "\"age\": 29" + "}" + "}," + "{" + "\"id\": \"software:lop\"," + "\"label\": \"software\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"price\": 328," + "\"name\": \"lop\"," + "\"lang\": [\"java\",\"python\",\"c++\"]" + "}" + "}," + "{" + "\"id\": \"person:peter\"," + "\"label\": \"person\"," + "\"type\": \"vertex\"," + "\"properties\": {" + "\"city\": [\"Shanghai\"]," + "\"name\": \"peter\"," + "\"age\": 35" + "}" + "}" + "]," + "\"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("b0fd8ead-333f-43ac-97b0-4d78784726ae", response.requestId());
    Assert.assertEquals(200, response.status().code());
    Vertex marko = new Vertex("person");
    marko.id("person:marko");
    marko.property("name", "marko");
    marko.property("city", ImmutableList.of("Beijing", "Wuhan", "Beijing"));
    marko.property("age", 29);
    Vertex lop = new Vertex("software");
    lop.id("software:lop");
    lop.property("name", "lop");
    lop.property("lang", ImmutableList.of("java", "python", "c++"));
    lop.property("price", 328);
    Vertex peter = new Vertex("person");
    peter.id("person:peter");
    peter.property("name", "peter");
    peter.property("city", ImmutableList.of("Shanghai"));
    peter.property("age", 35);
    List<Vertex> vertices = new ArrayList<>(3);
    vertices.add(peter);
    vertices.add(marko);
    vertices.add(lop);
    Iterator<Result> results = response.result().iterator();
    while (results.hasNext()) {
        Result result = results.next();
        Assert.assertEquals(Vertex.class, result.getObject().getClass());
        Vertex vertex = result.getVertex();
        Assert.assertTrue(Utils.contains(vertices, vertex));
    }
}
Also used : Response(com.baidu.hugegraph.structure.gremlin.Response) Vertex(com.baidu.hugegraph.structure.graph.Vertex) RestResult(com.baidu.hugegraph.rest.RestResult) ArrayList(java.util.ArrayList) Result(com.baidu.hugegraph.structure.gremlin.Result) RestResult(com.baidu.hugegraph.rest.RestResult) Test(org.junit.Test)

Example 75 with RestResult

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

the class RestResultTest method testReadVertexLabel.

@Test
public void testReadVertexLabel() {
    String json = "{" + "\"id\": 1," + "\"primary_keys\": [\"name\"]," + "\"index_labels\": []," + "\"name\": \"software\"," + "\"id_strategy\": \"PRIMARY_KEY\"," + "\"properties\": [\"price\", \"name\", \"lang\"]" + "}";
    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());
    VertexLabel vertexLabel = result.readObject(VertexLabel.class);
    Assert.assertEquals("software", vertexLabel.name());
    Assert.assertEquals(IdStrategy.PRIMARY_KEY, vertexLabel.idStrategy());
    Assert.assertEquals(ImmutableList.of("name"), vertexLabel.primaryKeys());
    Assert.assertEquals(ImmutableSet.of("price", "name", "lang"), vertexLabel.properties());
}
Also used : RestResult(com.baidu.hugegraph.rest.RestResult) VertexLabel(com.baidu.hugegraph.structure.schema.VertexLabel) 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