Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit test for traversers api #1489

Merged
merged 11 commits into from
Aug 6, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.InsertionOrderUtil;
import com.baidu.hugegraph.util.Log;
import com.google.common.base.Strings;

public class InMemoryDBTable extends BackendTable<BackendSession,
TextBackendEntry> {
Expand Down Expand Up @@ -206,9 +207,10 @@ private Iterator<BackendEntry> queryByRange(ConditionQuery query) {
"Invalid scan with multi conditions: %s", query);
Condition.Relation scan = query.relations().iterator().next();
Shard shard = (Shard) scan.value();

int start = Long.valueOf(shard.start()).intValue();
int end = Long.valueOf(shard.end()).intValue();
int start = Strings.isNullOrEmpty(shard.start()) ?
0 : Long.valueOf(shard.start()).intValue();
int end = Strings.isNullOrEmpty(shard.end()) ?
0 : Long.valueOf(shard.end()).intValue();

List<BackendEntry> rs = new ArrayList<>(end - start);

Expand Down
86 changes: 2 additions & 84 deletions hugegraph-hbase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,91 +18,9 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-endpoint</artifactId>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hbase-shaded-endpoint</artifactId>
<version>2.0.6</version>
<exclusions>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-hadoop2-compat</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-jobclient</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

import com.baidu.hugegraph.api.traversers.TraversersApiTestSuite;
import com.baidu.hugegraph.dist.RegisterUtil;

@RunWith(Suite.class)
Expand All @@ -38,7 +39,8 @@
MetricsApiTest.class,
UserApiTest.class,
LoginApiTest.class,
ProjectApiTest.class
ProjectApiTest.class,
TraversersApiTestSuite.class
})
public class ApiTestSuite {

Expand Down
113 changes: 108 additions & 5 deletions hugegraph-test/src/main/java/com/baidu/hugegraph/api/BaseApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -35,6 +36,7 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.apache.http.util.TextUtils;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.client.filter.EncodingFilter;
import org.glassfish.jersey.message.GZipEncoder;
Expand All @@ -49,6 +51,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;

public class BaseApiTest {

Expand All @@ -64,6 +67,9 @@ public class BaseApiTest {
private static final String SCHEMA_ILS = "/schema/indexlabels";
private static final String GRAPH_VERTEX = "/graph/vertices";
private static final String GRAPH_EDGE = "/graph/edges";
private static final String BATCH = "/batch";

protected static final String TRAVERSERS_API = URL_PREFIX + "/traversers";

private static RestClient client;

Expand Down Expand Up @@ -93,7 +99,7 @@ public static RestClient newClient() {
return new RestClient(BASE_URL);
}

static class RestClient {
public static class RestClient {

private Client client;
private WebTarget target;
Expand Down Expand Up @@ -132,6 +138,14 @@ public Response get(String path,
return this.target.path(path).request().headers(headers).get();
}

public Response get(String path, Multimap<String, Object> params) {
WebTarget target = this.target.path(path);
for (Map.Entry<String, Object> entries : params.entries()) {
target = target.queryParam(entries.getKey(), entries.getValue());
}
return target.request().get();
}

public Response get(String path, Map<String, Object> params) {
WebTarget target = this.target.path(path);
for (Map.Entry<String, Object> i : params.entrySet()) {
Expand Down Expand Up @@ -296,6 +310,63 @@ protected static void initIndexLabel() {
assertResponseStatus(202, r);
}

protected static void initEdge() {
String path = URL_PREFIX + GRAPH_EDGE + BATCH;
Map<String, String> ret = listAllVertexName2Ids();
String markoId = ret.get("marko");
String peterId = ret.get("peter");
String joshId = ret.get("josh");
String vadasId = ret.get("vadas");
String rippleId = ret.get("ripple");

String body = String.format("[{"
+ "\"label\": \"knows\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"person\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.5}},{"
+ "\"label\": \"knows\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"person\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.4}},{"
+ "\"label\": \"knows\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"person\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.3}},{"
+ "\"label\": \"created\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"software\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.2}"
+ "},{"
+ "\"label\": \"created\","
+ "\"outV\": \"%s\","
+ "\"inV\": \"%s\","
+ "\"outVLabel\": \"person\","
+ "\"inVLabel\": \"software\","
+ "\"properties\": {"
+ " \"date\": \"2021-01-01\","
+ " \"weight\":0.1}}]",
markoId, peterId, peterId, joshId,
joshId, vadasId, markoId, rippleId,
peterId, rippleId);
createAndAssert(path, body);
}

protected static void initVertex() {
String path = URL_PREFIX + GRAPH_VERTEX;

Expand Down Expand Up @@ -361,6 +432,41 @@ protected static Response createAndAssert(String path, String body) {
return r;
}

protected static Map<String, String> listAllVertexName2Ids() {
Response r = client.get(URL_PREFIX + GRAPH_VERTEX);
String content = assertResponseStatus(200, r);

@SuppressWarnings("rawtypes")
List<Map> vertices = readList(content, "vertices", Map.class);

Map<String, String> vertextName2Ids = new HashMap<>();
for (Map vertice : vertices) {
Map properties = (Map) vertice.get("properties");
if (properties == null ||
!properties.containsKey("name") ||
!vertice.containsKey("id")) {
continue;
}
String name = (String) properties.get("name");
if (TextUtils.isEmpty(name)) {
continue;
}

String id = (String) vertice.get("id");
if (TextUtils.isEmpty(id)) {
continue;
}

vertextName2Ids.put(name, id);
}

return vertextName2Ids;
}

protected static String id2Json(String params) {
return String.format("\"%s\"", params);
}

protected static String getVertexId(String label, String key, String value)
throws IOException {
String props = mapper.writeValueAsString(ImmutableMap.of(key, value));
Expand All @@ -369,10 +475,7 @@ protected static String getVertexId(String label, String key, String value)
"properties", URLEncoder.encode(props, "UTF-8")
);
Response r = client.get(URL_PREFIX + GRAPH_VERTEX, params);
String content = r.readEntity(String.class);
if (r.getStatus() != 200) {
throw new HugeException("Failed to get vertex id: %s", content);
}
String content = assertResponseStatus(200, r);

@SuppressWarnings("rawtypes")
List<Map> list = readList(content, "vertices", Map.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.api.traversers;

import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.baidu.hugegraph.api.BaseApiTest;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

public class AllShortestPathsApiTest extends BaseApiTest {

public static String path = TRAVERSERS_API + "/allshortestpaths";

@Before
public void prepareSchema() {
BaseApiTest.initPropertyKey();
BaseApiTest.initVertexLabel();
BaseApiTest.initEdgeLabel();
BaseApiTest.initVertex();
BaseApiTest.initEdge();
}

@Test
public void testGet() {
Map<String, String> name2Ids = listAllVertexName2Ids();
String markoId = name2Ids.get("marko");
String vadasId = name2Ids.get("vadas");
String peterId = name2Ids.get("peter");
String joshId = name2Ids.get("josh");
Map<String, Object> entities = ImmutableMap.of("source",
id2Json(markoId),
"target",
id2Json(vadasId),
"max_depth", 100);
String content = assertResponseStatus(200,
client().get(path, entities));
List paths = assertJsonContains(content, "paths");
Assert.assertEquals(1, paths.size());
List objects = assertMapContains((Map<?, ?>) paths.get(0), "objects");
Assert.assertEquals(ImmutableList.of(markoId, peterId, joshId,
vadasId), objects);
}
}
Loading